Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 185 lines (128 sloc) 4.839 kb
3e196db @mislav improve Active Model README
mislav authored
1 = Active Model -- model interfaces for Rails
2
3 Active Model provides a known set of interfaces for usage in model classes.
4 They allow for Action Pack helpers to interact with non-ActiveRecord models,
5 for example. Active Model also helps building custom ORMs for use outside of
6 the Rails framework.
7
8 Prior to Rails 3.0, if a plugin or gem developer wanted to have an object
9 interact with Action Pack helpers, it was required to either copy chunks of
10 code from Rails, or monkey patch entire helpers to make them handle objects
86c15d8 @rtlechow Active Model typos.
rtlechow authored
11 that did not exactly conform to the Active Record interface. This would result
3e196db @mislav improve Active Model README
mislav authored
12 in code duplication and fragile applications that broke on upgrades.
13
14 Active Model solves this. You can include functionality from the following
15 modules:
16
17 * Add attribute magic to objects
18
8834b26 @mikel Adding ActiveModel::AttributeMethods documentation
mikel authored
19 class Person
20 include ActiveModel::AttributeMethods
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
21
8834b26 @mikel Adding ActiveModel::AttributeMethods documentation
mikel authored
22 attribute_method_prefix 'clear_'
23 define_attribute_methods [:name, :age]
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
24
8834b26 @mikel Adding ActiveModel::AttributeMethods documentation
mikel authored
25 attr_accessor :name, :age
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
26
8834b26 @mikel Adding ActiveModel::AttributeMethods documentation
mikel authored
27 def clear_attribute(attr)
28 send("#{attr}=", nil)
29 end
30 end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
31
3e196db @mislav improve Active Model README
mislav authored
32 person.clear_name
33 person.clear_age
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
34
8834b26 @mikel Adding ActiveModel::AttributeMethods documentation
mikel authored
35 {Learn more}[link:classes/ActiveModel/AttributeMethods.html]
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
36
3e196db @mislav improve Active Model README
mislav authored
37 * Callbacks for certain operations
e1c15d9 @mikel fixed column alignment
mikel authored
38
8834b26 @mikel Adding ActiveModel::AttributeMethods documentation
mikel authored
39 class Person
e1c15d9 @mikel fixed column alignment
mikel authored
40 extend ActiveModel::Callbacks
41 define_model_callbacks :create
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
42
e1c15d9 @mikel fixed column alignment
mikel authored
43 def create
57bc25c @jfirebaugh Use run_callbacks; the generated _run_<name>_callbacks method is not …
jfirebaugh authored
44 run_callbacks :create do
e1c15d9 @mikel fixed column alignment
mikel authored
45 # Your create action methods here
46 end
47 end
48 end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
49
3e196db @mislav improve Active Model README
mislav authored
50 This generates +before_create+, +around_create+ and +after_create+
51 class methods that wrap your create method.
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
52
582228e @mikel ActiveModel::Callbacks documentation
mikel authored
53 {Learn more}[link:classes/ActiveModel/CallBacks.html]
5dc3f91 @technoweenie initial experimental commit of active_model
technoweenie authored
54
3e196db @mislav improve Active Model README
mislav authored
55 * Tracking value changes
23e434f @mikel ActiveModel::Conversion documentation
mikel authored
56
3e196db @mislav improve Active Model README
mislav authored
57 The ActiveModel::Dirty module allows for tracking attribute changes:
e1c15d9 @mikel fixed column alignment
mikel authored
58
db274a0 @mikel Fixed readme for ActiveModel::Dirty
mikel authored
59 person = Person.new
60 person.name # => nil
d1bedd1 @mikel Updated README
mikel authored
61 person.changed? # => false
62 person.name = 'bob'
63 person.changed? # => true
64 person.changed # => ['name']
db274a0 @mikel Fixed readme for ActiveModel::Dirty
mikel authored
65 person.changes # => { 'name' => [nil, 'bob'] }
d1bedd1 @mikel Updated README
mikel authored
66 person.name = 'robert'
67 person.save
68 person.previous_changes # => {'name' => ['bob, 'robert']}
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
69
4a2d2ef @mikel Adding RDoc for active_model dirty
mikel authored
70 {Learn more}[link:classes/ActiveModel/Dirty.html]
71
3e196db @mislav improve Active Model README
mislav authored
72 * Adding +errors+ interface to objects
4d4bdb0 @mikel Added ActiveModel::Errors documentation
mikel authored
73
3e196db @mislav improve Active Model README
mislav authored
74 Exposing error messages allows objects to interact with Action Pack
75 helpers seamlessly.
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
76
4d4bdb0 @mikel Added ActiveModel::Errors documentation
mikel authored
77 class Person
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
78
4d4bdb0 @mikel Added ActiveModel::Errors documentation
mikel authored
79 def initialize
80 @errors = ActiveModel::Errors.new(self)
81 end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
82
4d4bdb0 @mikel Added ActiveModel::Errors documentation
mikel authored
83 attr_accessor :name
84 attr_reader :errors
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
85
4d4bdb0 @mikel Added ActiveModel::Errors documentation
mikel authored
86 def validate!
4038a6b @vijaydev minor fixesin READMEs
vijaydev authored
87 errors.add(:name, "can not be nil") if name.nil?
4d4bdb0 @mikel Added ActiveModel::Errors documentation
mikel authored
88 end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
89
4d4bdb0 @mikel Added ActiveModel::Errors documentation
mikel authored
90 def ErrorsPerson.human_attribute_name(attr, options = {})
91 "Name"
92 end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
93
4d4bdb0 @mikel Added ActiveModel::Errors documentation
mikel authored
94 end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
95
4d4bdb0 @mikel Added ActiveModel::Errors documentation
mikel authored
96 person.errors.full_messages
4038a6b @vijaydev minor fixesin READMEs
vijaydev authored
97 # => ["Name can not be nil"]
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
98
4d4bdb0 @mikel Added ActiveModel::Errors documentation
mikel authored
99 person.errors.full_messages
4038a6b @vijaydev minor fixesin READMEs
vijaydev authored
100 # => ["Name can not be nil"]
4d4bdb0 @mikel Added ActiveModel::Errors documentation
mikel authored
101
102 {Learn more}[link:classes/ActiveModel/Errors.html]
cf55e4e @mikel Added documentation for ActiveModel::Lint
mikel authored
103
3e196db @mislav improve Active Model README
mislav authored
104 * Model name introspection
cf55e4e @mikel Added documentation for ActiveModel::Lint
mikel authored
105
fbc7c2b @mikel Adding ActiveModel::Naming documentation
mikel authored
106 class NamedPerson
107 extend ActiveModel::Naming
108 end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
109
8a2b69b @pacoguzman applied guidelines to "# =>"
pacoguzman authored
110 NamedPerson.model_name # => "NamedPerson"
111 NamedPerson.model_name.human # => "Named person"
fbc7c2b @mikel Adding ActiveModel::Naming documentation
mikel authored
112
113 {Learn more}[link:classes/ActiveModel/Naming.html]
114
3e196db @mislav improve Active Model README
mislav authored
115 * Observer support
e606328 @mikel Fixing documentation for ActiveModel::Observer
mikel authored
116
3e196db @mislav improve Active Model README
mislav authored
117 ActiveModel::Observers allows your object to implement the Observer
118 pattern in a Rails App and take advantage of all the standard observer
119 functions.
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
120
e606328 @mikel Fixing documentation for ActiveModel::Observer
mikel authored
121 {Learn more}[link:classes/ActiveModel/Observer.html]
19814df @mikel Adding documentation for ActiveModel::Serialization
mikel authored
122
3e196db @mislav improve Active Model README
mislav authored
123 * Making objects serializable
19814df @mikel Adding documentation for ActiveModel::Serialization
mikel authored
124
3e196db @mislav improve Active Model README
mislav authored
125 ActiveModel::Serialization provides a standard interface for your object
126 to provide +to_json+ or +to_xml+ serialization.
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
127
19814df @mikel Adding documentation for ActiveModel::Serialization
mikel authored
128 s = SerialPerson.new
129 s.serializable_hash # => {"name"=>nil}
130 s.to_json # => "{\"name\":null}"
131 s.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<serial-person...
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
132
19814df @mikel Adding documentation for ActiveModel::Serialization
mikel authored
133 {Learn more}[link:classes/ActiveModel/Serialization.html]
71328a9 @mikel Fixing up state machine docs
mikel authored
134
3e196db @mislav improve Active Model README
mislav authored
135 * Internationalization (i18n) support
91c3840 @mikel Adding documentation for ActiveModel::Translation
mikel authored
136
137 class Person
138 extend ActiveModel::Translation
139 end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
140
3e196db @mislav improve Active Model README
mislav authored
141 Person.human_attribute_name('my_attribute')
8a2b69b @pacoguzman applied guidelines to "# =>"
pacoguzman authored
142 # => "My attribute"
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
143
91c3840 @mikel Adding documentation for ActiveModel::Translation
mikel authored
144 {Learn more}[link:classes/ActiveModel/Translation.html]
9aee365 @mikel Adding ActiveModel::Validations documentation
mikel authored
145
3e196db @mislav improve Active Model README
mislav authored
146 * Validation support
9aee365 @mikel Adding ActiveModel::Validations documentation
mikel authored
147
148 class Person
149 include ActiveModel::Validations
150
151 attr_accessor :first_name, :last_name
152
153 validates_each :first_name, :last_name do |record, attr, value|
154 record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
155 end
156 end
157
ed50730 @dolzenko Fix minor mistake in activemodel/README
dolzenko authored
158 person = Person.new
159 person.first_name = 'zoolander'
8a2b69b @pacoguzman applied guidelines to "# =>"
pacoguzman authored
160 person.valid? # => false
9aee365 @mikel Adding ActiveModel::Validations documentation
mikel authored
161
162 {Learn more}[link:classes/ActiveModel/Validations.html]
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
163
3e196db @mislav improve Active Model README
mislav authored
164 * Custom validators
3cb0283 @mikel Documentation cleanup and linkage for validator
mikel authored
165
166 class Person
167 include ActiveModel::Validations
168 validates_with HasNameValidator
169 attr_accessor :name
170 end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
171
3cb0283 @mikel Documentation cleanup and linkage for validator
mikel authored
172 class HasNameValidator < ActiveModel::Validator
173 def validate(record)
174 record.errors[:name] = "must exist" if record.name.blank?
175 end
176 end
b451de0 @spastorino Deletes trailing whitespaces (over text files only find * -type f -ex…
spastorino authored
177
3cb0283 @mikel Documentation cleanup and linkage for validator
mikel authored
178 p = ValidatorPerson.new
8a2b69b @pacoguzman applied guidelines to "# =>"
pacoguzman authored
179 p.valid? # => false
180 p.errors.full_messages # => ["Name must exist"]
3cb0283 @mikel Documentation cleanup and linkage for validator
mikel authored
181 p.name = "Bob"
8a2b69b @pacoguzman applied guidelines to "# =>"
pacoguzman authored
182 p.valid? # => true
3cb0283 @mikel Documentation cleanup and linkage for validator
mikel authored
183
184 {Learn more}[link:classes/ActiveModel/Validator.html]
Something went wrong with that request. Please try again.