README.rdoc 4.8 KB
Newer Older
M
Mislav Marohnić 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
= 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
that did not exacly conform to the Active Record interface. This would result
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 21 22 23 24 25
    class Person
      include ActiveModel::AttributeMethods
      
      attribute_method_prefix 'clear_'
      define_attribute_methods [:name, :age]
      
      attr_accessor :name, :age
M
Mislav Marohnić 已提交
26
      
27 28 29 30 31
      def clear_attribute(attr)
        send("#{attr}=", nil)
      end
    end
    
M
Mislav Marohnić 已提交
32 33
    person.clear_name
    person.clear_age
34 35 36
  
  {Learn more}[link:classes/ActiveModel/AttributeMethods.html]
  
M
Mislav Marohnić 已提交
37
* Callbacks for certain operations
M
Mikel Lindsaar 已提交
38

39
    class Person
M
Mikel Lindsaar 已提交
40 41 42 43 44 45 46 47 48
      extend ActiveModel::Callbacks
      define_model_callbacks :create
    
      def create
        _run_create_callbacks do
          # Your create action methods here
        end
      end
    end
M
Mislav Marohnić 已提交
49 50 51 52
  
  This generates +before_create+, +around_create+ and +after_create+
  class methods that wrap your create method.
  
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']}
M
Mislav Marohnić 已提交
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 76
  Exposing error messages allows objects to interact with Action Pack
  helpers seamlessly.
  
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 87 88
      def validate!
        errors.add(:name, "can not be nil") if name == nil
      end
89
    
90 91 92
      def ErrorsPerson.human_attribute_name(attr, options = {})
        "Name"
      end
93
    
94 95 96 97
    end
    
    person.errors.full_messages
    # => ["Name Can not be nil"]
M
Mislav Marohnić 已提交
98
    
99 100 101 102
    person.errors.full_messages
    # => ["Name Can not be nil"]

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

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

106 107 108 109 110 111 112 113 114
    class NamedPerson
      extend ActiveModel::Naming
    end
    
    NamedPerson.model_name        #=> "NamedPerson"
    NamedPerson.model_name.human  #=> "Named person"

  {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 127
  ActiveModel::Serialization provides a standard interface for your object
  to provide +to_json+ or +to_xml+ serialization.
  
128 129 130 131 132 133
    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...
  
  {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
M
Mislav Marohnić 已提交
140 141 142
    
    Person.human_attribute_name('my_attribute')
    #=> "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'
M
Mislav Marohnić 已提交
160
   person.valid?  #=> false
161 162 163

  {Learn more}[link:classes/ActiveModel/Validations.html]
  
M
Mislav Marohnić 已提交
164
* Custom validators
165 166 167 168 169 170 171 172 173 174 175 176

   class Person
     include ActiveModel::Validations
     validates_with HasNameValidator
     attr_accessor :name
   end
   
   class HasNameValidator < ActiveModel::Validator
     def validate(record)
      record.errors[:name] = "must exist" if record.name.blank?
     end
   end
M
Mislav Marohnić 已提交
177
   
178 179 180 181 182 183 184
   p = ValidatorPerson.new
   p.valid?                  #=>  false
   p.errors.full_messages    #=> ["Name must exist"]
   p.name = "Bob"
   p.valid?                  #=>  true

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