README.rdoc 5.2 KB
Newer Older
M
Mislav Marohnić 已提交
1 2 3 4 5 6 7 8 9 10
= Active Model -- model interfaces for Rails

Active Model provides a known set of interfaces for usage in model classes.
They allow for Action Pack helpers to interact with non-ActiveRecord models,
for example. Active Model also helps building custom ORMs for use outside of
the Rails framework.

Prior to Rails 3.0, if a plugin or gem developer wanted to have an object
interact with Action Pack helpers, it was required to either copy chunks of
code from Rails, or monkey patch entire helpers to make them handle objects
R
R.T. Lechow 已提交
11
that did not exactly conform to the Active Record interface. This would result
M
Mislav Marohnić 已提交
12 13 14 15 16 17 18
in code duplication and fragile applications that broke on upgrades.

Active Model solves this. You can include functionality from the following
modules:

* Add attribute magic to objects

19 20
    class Person
      include ActiveModel::AttributeMethods
21

22 23
      attribute_method_prefix 'clear_'
      define_attribute_methods [:name, :age]
24

25
      attr_accessor :name, :age
26

27 28 29 30
      def clear_attribute(attr)
        send("#{attr}=", nil)
      end
    end
31

M
Mislav Marohnić 已提交
32 33
    person.clear_name
    person.clear_age
34

35
  {Learn more}[link:classes/ActiveModel/AttributeMethods.html]
36

M
Mislav Marohnić 已提交
37
* Callbacks for certain operations
M
Mikel Lindsaar 已提交
38

39
    class Person
M
Mikel Lindsaar 已提交
40 41
      extend ActiveModel::Callbacks
      define_model_callbacks :create
42

M
Mikel Lindsaar 已提交
43
      def create
44
        run_callbacks :create do
M
Mikel Lindsaar 已提交
45 46 47 48
          # Your create action methods here
        end
      end
    end
49

M
Mislav Marohnić 已提交
50 51
  This generates +before_create+, +around_create+ and +after_create+
  class methods that wrap your create method.
52

53
  {Learn more}[link:classes/ActiveModel/CallBacks.html]
54

M
Mislav Marohnić 已提交
55
* Tracking value changes
56

M
Mislav Marohnić 已提交
57
  The ActiveModel::Dirty module allows for tracking attribute changes:
M
Mikel Lindsaar 已提交
58

59 60
    person = Person.new
    person.name # => nil
M
Mikel Lindsaar 已提交
61 62 63 64
    person.changed? # => false
    person.name = 'bob'
    person.changed? # => true
    person.changed # => ['name']
65
    person.changes # => { 'name' => [nil, 'bob'] }
M
Mikel Lindsaar 已提交
66 67 68
    person.name = 'robert'
    person.save
    person.previous_changes # => {'name' => ['bob, 'robert']}
69

70 71
  {Learn more}[link:classes/ActiveModel/Dirty.html]

M
Mislav Marohnić 已提交
72
* Adding +errors+ interface to objects
73

M
Mislav Marohnić 已提交
74 75
  Exposing error messages allows objects to interact with Action Pack
  helpers seamlessly.
76

77
    class Person
78

79 80 81
      def initialize
        @errors = ActiveModel::Errors.new(self)
      end
82

83 84
      attr_accessor :name
      attr_reader   :errors
85

86
      def validate!
V
Vijay Dev 已提交
87
        errors.add(:name, "can not be nil") if name.nil?
88
      end
89

90 91 92
      def ErrorsPerson.human_attribute_name(attr, options = {})
        "Name"
      end
93

94
    end
95

96
    person.errors.full_messages
V
Vijay Dev 已提交
97
    # => ["Name can not be nil"]
98

99
    person.errors.full_messages
V
Vijay Dev 已提交
100
    # => ["Name can not be nil"]
101 102

  {Learn more}[link:classes/ActiveModel/Errors.html]
103

M
Mislav Marohnić 已提交
104
* Model name introspection
105

106 107 108
    class NamedPerson
      extend ActiveModel::Naming
    end
109

P
Paco Guzman 已提交
110 111
    NamedPerson.model_name        # => "NamedPerson"
    NamedPerson.model_name.human  # => "Named person"
112 113 114

  {Learn more}[link:classes/ActiveModel/Naming.html]

M
Mislav Marohnić 已提交
115
* Observer support
116

M
Mislav Marohnić 已提交
117 118 119
  ActiveModel::Observers allows your object to implement the Observer
  pattern in a Rails App and take advantage of all the standard observer
  functions.
120

121
  {Learn more}[link:classes/ActiveModel/Observer.html]
122

M
Mislav Marohnić 已提交
123
* Making objects serializable
124

M
Mislav Marohnić 已提交
125 126
  ActiveModel::Serialization provides a standard interface for your object
  to provide +to_json+ or +to_xml+ serialization.
127

128 129 130 131
    s = SerialPerson.new
    s.serializable_hash   # => {"name"=>nil}
    s.to_json             # => "{\"name\":null}"
    s.to_xml              # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<serial-person...
132

133
  {Learn more}[link:classes/ActiveModel/Serialization.html]
M
Mikel Lindsaar 已提交
134

M
Mislav Marohnić 已提交
135
* Internationalization (i18n) support
136 137 138 139

    class Person
      extend ActiveModel::Translation
    end
140

M
Mislav Marohnić 已提交
141
    Person.human_attribute_name('my_attribute')
P
Paco Guzman 已提交
142
    # => "My attribute"
143

144
  {Learn more}[link:classes/ActiveModel/Translation.html]
145

M
Mislav Marohnić 已提交
146
* Validation support
147 148 149 150 151 152 153 154 155 156 157

   class Person
     include ActiveModel::Validations

     attr_accessor :first_name, :last_name

     validates_each :first_name, :last_name do |record, attr, value|
       record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
     end
   end

158 159
   person = Person.new
   person.first_name = 'zoolander'
P
Paco Guzman 已提交
160
   person.valid?  # => false
161 162

  {Learn more}[link:classes/ActiveModel/Validations.html]
163

M
Mislav Marohnić 已提交
164
* Custom validators
165 166 167 168 169 170

   class Person
     include ActiveModel::Validations
     validates_with HasNameValidator
     attr_accessor :name
   end
171

172 173 174 175 176
   class HasNameValidator < ActiveModel::Validator
     def validate(record)
      record.errors[:name] = "must exist" if record.name.blank?
     end
   end
177

178
   p = ValidatorPerson.new
P
Paco Guzman 已提交
179 180
   p.valid?                  # =>  false
   p.errors.full_messages    # => ["Name must exist"]
181
   p.name = "Bob"
P
Paco Guzman 已提交
182
   p.valid?                  # =>  true
183 184

  {Learn more}[link:classes/ActiveModel/Validator.html]
185 186 187 188 189 190 191 192 193 194
  

== Download and installation

The latest version of Active Model can be installed with Rubygems:

  % [sudo] gem install activemodel

Source code can be downloaded as part of the Rails project on GitHub

195
* https://github.com/rails/rails/tree/master/activemodel
196 197 198 199


== License

V
Vishnu Atrai 已提交
200
Active Model is released under the MIT license.
201 202 203 204 205 206 207 208 209 210 211


== Support

API documentation is at

* http://api.rubyonrails.org

Bug reports and feature requests can be filed with the rest for the Ruby on Rails project here:

* https://github.com/rails/rails/issues