README.rdoc 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. = Active Model -- model interfaces for Rails
  2. Active Model provides a known set of interfaces for usage in model classes.
  3. They allow for Action Pack helpers to interact with non-ActiveRecord models,
  4. for example. Active Model also helps building custom ORMs for use outside of
  5. the Rails framework.
  6. Prior to Rails 3.0, if a plugin or gem developer wanted to have an object
  7. interact with Action Pack helpers, it was required to either copy chunks of
  8. code from Rails, or monkey patch entire helpers to make them handle objects
  9. that did not exactly conform to the Active Record interface. This would result
  10. in code duplication and fragile applications that broke on upgrades.
  11. Active Model solves this. You can include functionality from the following
  12. modules:
  13. * Add attribute magic to objects
  14. class Person
  15. include ActiveModel::AttributeMethods
  16. attribute_method_prefix 'clear_'
  17. define_attribute_methods [:name, :age]
  18. attr_accessor :name, :age
  19. def clear_attribute(attr)
  20. send("#{attr}=", nil)
  21. end
  22. end
  23. person.clear_name
  24. person.clear_age
  25. {Learn more}[link:classes/ActiveModel/AttributeMethods.html]
  26. * Callbacks for certain operations
  27. class Person
  28. extend ActiveModel::Callbacks
  29. define_model_callbacks :create
  30. def create
  31. run_callbacks :create do
  32. # Your create action methods here
  33. end
  34. end
  35. end
  36. This generates +before_create+, +around_create+ and +after_create+
  37. class methods that wrap your create method.
  38. {Learn more}[link:classes/ActiveModel/Callbacks.html]
  39. * Tracking value changes
  40. The ActiveModel::Dirty module allows for tracking attribute changes:
  41. person = Person.new
  42. person.name # => nil
  43. person.changed? # => false
  44. person.name = 'bob'
  45. person.changed? # => true
  46. person.changed # => ['name']
  47. person.changes # => { 'name' => [nil, 'bob'] }
  48. person.name = 'robert'
  49. person.save
  50. person.previous_changes # => {'name' => ['bob, 'robert']}
  51. {Learn more}[link:classes/ActiveModel/Dirty.html]
  52. * Adding +errors+ interface to objects
  53. Exposing error messages allows objects to interact with Action Pack
  54. helpers seamlessly.
  55. class Person
  56. def initialize
  57. @errors = ActiveModel::Errors.new(self)
  58. end
  59. attr_accessor :name
  60. attr_reader :errors
  61. def validate!
  62. errors.add(:name, "can not be nil") if name.nil?
  63. end
  64. def ErrorsPerson.human_attribute_name(attr, options = {})
  65. "Name"
  66. end
  67. end
  68. person.errors.full_messages
  69. # => ["Name can not be nil"]
  70. person.errors.full_messages
  71. # => ["Name can not be nil"]
  72. {Learn more}[link:classes/ActiveModel/Errors.html]
  73. * Model name introspection
  74. class NamedPerson
  75. extend ActiveModel::Naming
  76. end
  77. NamedPerson.model_name # => "NamedPerson"
  78. NamedPerson.model_name.human # => "Named person"
  79. {Learn more}[link:classes/ActiveModel/Naming.html]
  80. * Observer support
  81. ActiveModel::Observers allows your object to implement the Observer
  82. pattern in a Rails App and take advantage of all the standard observer
  83. functions.
  84. {Learn more}[link:classes/ActiveModel/Observer.html]
  85. * Making objects serializable
  86. ActiveModel::Serialization provides a standard interface for your object
  87. to provide +to_json+ or +to_xml+ serialization.
  88. s = SerialPerson.new
  89. s.serializable_hash # => {"name"=>nil}
  90. s.to_json # => "{\"name\":null}"
  91. s.to_xml # => "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<serial-person...
  92. {Learn more}[link:classes/ActiveModel/Serialization.html]
  93. * Internationalization (i18n) support
  94. class Person
  95. extend ActiveModel::Translation
  96. end
  97. Person.human_attribute_name('my_attribute')
  98. # => "My attribute"
  99. {Learn more}[link:classes/ActiveModel/Translation.html]
  100. * Validation support
  101. class Person
  102. include ActiveModel::Validations
  103. attr_accessor :first_name, :last_name
  104. validates_each :first_name, :last_name do |record, attr, value|
  105. record.errors.add attr, 'starts with z.' if value.to_s[0] == ?z
  106. end
  107. end
  108. person = Person.new
  109. person.first_name = 'zoolander'
  110. person.valid? # => false
  111. {Learn more}[link:classes/ActiveModel/Validations.html]
  112. * Custom validators
  113. class Person
  114. include ActiveModel::Validations
  115. validates_with HasNameValidator
  116. attr_accessor :name
  117. end
  118. class HasNameValidator < ActiveModel::Validator
  119. def validate(record)
  120. record.errors[:name] = "must exist" if record.name.blank?
  121. end
  122. end
  123. p = ValidatorPerson.new
  124. p.valid? # => false
  125. p.errors.full_messages # => ["Name must exist"]
  126. p.name = "Bob"
  127. p.valid? # => true
  128. {Learn more}[link:classes/ActiveModel/Validator.html]
  129. == Download and installation
  130. The latest version of Active Model can be installed with RubyGems:
  131. % [sudo] gem install activemodel
  132. Source code can be downloaded as part of the Rails project on GitHub
  133. * https://github.com/rails/rails/tree/3-2-stable/activemodel
  134. == License
  135. Active Model is released under the MIT license.
  136. == Support
  137. API documentation is at
  138. * http://api.rubyonrails.org
  139. Bug reports and feature requests can be filed with the rest for the Ruby on Rails project here:
  140. * https://github.com/rails/rails/issues