1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/railties/guides/source/active_model_basics.textile

52 lines
1.4 KiB
Text
Raw Normal View History

h2. Active Model Basics
This guide should provide you with all you need to get started using model classes. Active Model allow for Action Pack helpers to interact with non-ActiveRecord models. Active Model also helps building custom ORMs for use outside of the Rails framework.
endprologue.
WARNING. This Guide is based on Rails 3.0. Some of the code shown here will not work in earlier versions of Rails.
h3. Introduction
Active Model is a library containing various modules used in developing frameworks that need to interact with the Rails Action Pack library. Active Model provides a known set of interfaces for usage in classes. Some of modules are explained below -
h4. AttributeMethods
AttributeMethods module can add custom prefixes and suffixes on methods of a class. It is used by defining the prefixes and suffixes, which methods on the object will use them.
<ruby>
class Person
include ActiveModel::AttributeMethods
attribute_method_prefix 'reset_'
attribute_method_suffix '_highest?'
define_attribute_methods ['age']
attr_accessor :age
private
def reset_attribute(attribute)
send("#{attribute}=", 0)
end
def attribute_highest?(attribute)
send(attribute) > 100 ? true : false
end
end
person = Person.new
person.age = 110
person.age_highest? # true
person.reset_age # 0
person.age_highest? # false
</ruby>
h4. Callbacks
h3. Changelog
* August 5, 2011: Initial version by "Arun Agrawal":http://github.com/arunagw