Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 98 lines (94 sloc) 2.743 kB
3b822e9 @guilleiguaran Add ActiveModel::Model, a mixin to make Ruby objects to work with AP …
guilleiguaran authored
1 module ActiveModel
cb9d03f @carlosantoniodasilva Add docs with usage examples for ActiveModel::Model
carlosantoniodasilva authored
2
3 # == Active Model Basic Model
4 #
cca9a21 update ActiveModel::Model documentation [ci skip]
Francesco Rodriguez authored
5 # Includes the required interface for an object to interact with
6 # <tt>ActionPack</tt>, using different <tt>ActiveModel</tt> modules.
7 # It includes model name introspections, conversions, translations and
8 # validations. Besides that, it allows you to initialize the object with a
9 # hash of attributes, pretty much like <tt>ActiveRecord</tt> does.
cb9d03f @carlosantoniodasilva Add docs with usage examples for ActiveModel::Model
carlosantoniodasilva authored
10 #
11 # A minimal implementation could be:
12 #
13 # class Person
14 # include ActiveModel::Model
15 # attr_accessor :name, :age
16 # end
17 #
cca9a21 update ActiveModel::Model documentation [ci skip]
Francesco Rodriguez authored
18 # person = Person.new(name: 'bob', age: '18')
cb9d03f @carlosantoniodasilva Add docs with usage examples for ActiveModel::Model
carlosantoniodasilva authored
19 # person.name # => 'bob'
cca9a21 update ActiveModel::Model documentation [ci skip]
Francesco Rodriguez authored
20 # person.age # => 18
cb9d03f @carlosantoniodasilva Add docs with usage examples for ActiveModel::Model
carlosantoniodasilva authored
21 #
cca9a21 update ActiveModel::Model documentation [ci skip]
Francesco Rodriguez authored
22 # Note that, by default, <tt>ActiveModel::Model</tt> implements <tt>persisted?</tt>
23 # to return +false+, which is the most common case. You may want to override
24 # it in your class to simulate a different scenario:
cb9d03f @carlosantoniodasilva Add docs with usage examples for ActiveModel::Model
carlosantoniodasilva authored
25 #
26 # class Person
27 # include ActiveModel::Model
28 # attr_accessor :id, :name
29 #
30 # def persisted?
31 # self.id == 1
32 # end
33 # end
34 #
cca9a21 update ActiveModel::Model documentation [ci skip]
Francesco Rodriguez authored
35 # person = Person.new(id: 1, name: 'bob')
cb9d03f @carlosantoniodasilva Add docs with usage examples for ActiveModel::Model
carlosantoniodasilva authored
36 # person.persisted? # => true
37 #
cca9a21 update ActiveModel::Model documentation [ci skip]
Francesco Rodriguez authored
38 # Also, if for some reason you need to run code on <tt>initialize</tt>, make
39 # sure you call +super+ if you want the attributes hash initialization to
40 # happen.
cb9d03f @carlosantoniodasilva Add docs with usage examples for ActiveModel::Model
carlosantoniodasilva authored
41 #
42 # class Person
43 # include ActiveModel::Model
44 # attr_accessor :id, :name, :omg
45 #
92a6cf0 @oscardelben Improve example in initializer
oscardelben authored
46 # def initialize(attributes={})
cb9d03f @carlosantoniodasilva Add docs with usage examples for ActiveModel::Model
carlosantoniodasilva authored
47 # super
48 # @omg ||= true
49 # end
50 # end
51 #
cca9a21 update ActiveModel::Model documentation [ci skip]
Francesco Rodriguez authored
52 # person = Person.new(id: 1, name: 'bob')
cb9d03f @carlosantoniodasilva Add docs with usage examples for ActiveModel::Model
carlosantoniodasilva authored
53 # person.omg # => true
54 #
cca9a21 update ActiveModel::Model documentation [ci skip]
Francesco Rodriguez authored
55 # For more detailed information on other functionalities available, please
56 # refer to the specific modules included in <tt>ActiveModel::Model</tt>
57 # (see below).
3b822e9 @guilleiguaran Add ActiveModel::Model, a mixin to make Ruby objects to work with AP …
guilleiguaran authored
58 module Model
bc7ac33 add :nodoc: to internal implementations [ci skip]
Francesco Rodriguez authored
59 def self.included(base) #:nodoc:
3b822e9 @guilleiguaran Add ActiveModel::Model, a mixin to make Ruby objects to work with AP …
guilleiguaran authored
60 base.class_eval do
61 extend ActiveModel::Naming
62 extend ActiveModel::Translation
63 include ActiveModel::Validations
64 include ActiveModel::Conversion
65 end
66 end
67
cca9a21 update ActiveModel::Model documentation [ci skip]
Francesco Rodriguez authored
68 # Initializes a new model with the given +params+.
69 #
70 # class Person
71 # include ActiveModel::Model
72 # attr_accessor :name, :age
73 # end
74 #
75 # person = Person.new(name: 'bob', age: '18')
76 # person.name # => "bob"
77 # person.age # => 18
3b822e9 @guilleiguaran Add ActiveModel::Model, a mixin to make Ruby objects to work with AP …
guilleiguaran authored
78 def initialize(params={})
79 params.each do |attr, value|
247706e @guilleiguaran Use public_send instead of send in ActiveModel::Model initialize
guilleiguaran authored
80 self.public_send("#{attr}=", value)
3b822e9 @guilleiguaran Add ActiveModel::Model, a mixin to make Ruby objects to work with AP …
guilleiguaran authored
81 end if params
82 end
83
cca9a21 update ActiveModel::Model documentation [ci skip]
Francesco Rodriguez authored
84 # Indicates if the model is persisted. Default is +false+.
85 #
86 # class Person
87 # include ActiveModel::Model
88 # attr_accessor :id, :name
89 # end
90 #
91 # person = Person.new(id: 1, name: 'bob')
92 # person.persisted? # => false
3b822e9 @guilleiguaran Add ActiveModel::Model, a mixin to make Ruby objects to work with AP …
guilleiguaran authored
93 def persisted?
94 false
95 end
96 end
97 end
Something went wrong with that request. Please try again.