From ac287b2aa04c8376bf7d1f95c99047796442406e Mon Sep 17 00:00:00 2001 From: Arun Agrawal Date: Fri, 5 Aug 2011 12:16:53 +0530 Subject: Adding Basic file for ActiveModel. @vatrai and @sukeerthiadiga is going to take care other detailed stuff. --- railties/guides/source/active_model_basics.textile | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 railties/guides/source/active_model_basics.textile (limited to 'railties/guides/source/active_model_basics.textile') diff --git a/railties/guides/source/active_model_basics.textile b/railties/guides/source/active_model_basics.textile new file mode 100644 index 0000000000..5e41d39447 --- /dev/null +++ b/railties/guides/source/active_model_basics.textile @@ -0,0 +1,16 @@ +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. + + +h3. Changelog + +* August 5, 2011: Initial version by "Arun Agrawal":http://github.com/arunagw \ No newline at end of file -- cgit v1.2.3 From 4bde1b0041d73accf75525697461ac6b9fad5404 Mon Sep 17 00:00:00 2001 From: Vishnu Atrai Date: Fri, 5 Aug 2011 13:13:22 +0530 Subject: ActiveModel::AttributeMethods basic guide --- railties/guides/source/active_model_basics.textile | 27 +++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) (limited to 'railties/guides/source/active_model_basics.textile') diff --git a/railties/guides/source/active_model_basics.textile b/railties/guides/source/active_model_basics.textile index 5e41d39447..c76469f62c 100644 --- a/railties/guides/source/active_model_basics.textile +++ b/railties/guides/source/active_model_basics.textile @@ -8,8 +8,33 @@ WARNING. This Guide is based on Rails 3.0. Some of the code shown here will not 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. +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. + + +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) + attribute > 100 ? true : false + end + +end + h3. Changelog -- cgit v1.2.3 From 9eb3e637fb7ffa7a35847b5dd577c3a2736e5101 Mon Sep 17 00:00:00 2001 From: Vishnu Atrai Date: Fri, 5 Aug 2011 13:34:28 +0530 Subject: AttributeMethods refector suffix method added some usages --- railties/guides/source/active_model_basics.textile | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'railties/guides/source/active_model_basics.textile') diff --git a/railties/guides/source/active_model_basics.textile b/railties/guides/source/active_model_basics.textile index c76469f62c..87a9658a94 100644 --- a/railties/guides/source/active_model_basics.textile +++ b/railties/guides/source/active_model_basics.textile @@ -30,12 +30,23 @@ private end def attribute_highest?(attribute) - attribute > 100 ? true : false + 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 + +h4. Callbacks + + + h3. Changelog * August 5, 2011: Initial version by "Arun Agrawal":http://github.com/arunagw \ No newline at end of file -- cgit v1.2.3 From 33d7a6bc55a983ea690961d3a434096fe80d0fca Mon Sep 17 00:00:00 2001 From: Vishnu Atrai Date: Fri, 5 Aug 2011 14:35:04 +0530 Subject: ActiveModel::Callbacks basic guide --- railties/guides/source/active_model_basics.textile | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'railties/guides/source/active_model_basics.textile') diff --git a/railties/guides/source/active_model_basics.textile b/railties/guides/source/active_model_basics.textile index 87a9658a94..e4c84365d3 100644 --- a/railties/guides/source/active_model_basics.textile +++ b/railties/guides/source/active_model_basics.textile @@ -45,7 +45,31 @@ person.age_highest? # false h4. Callbacks +Callbacks gives Active Record style callbacks. This provides the ability to define the callbacks and those will run at appropriate time. After defining a callbacks you can wrap with before, after and around custom methods. + +class Person + extend ActiveModel::Callbacks + + define_model_callbacks :update + + before_update :reset_me + + def update + _run_update_callbacks do + puts 'saving...' + end + end + + def reset_me + puts 'before saving...' + end +end + +person = Person.new +person.update # before saving... + # saving... + h3. Changelog -- cgit v1.2.3 From d5adaf2d38f81429e21d9670b6a852668edb3757 Mon Sep 17 00:00:00 2001 From: Vishnu Atrai Date: Fri, 5 Aug 2011 14:48:00 +0530 Subject: ActiveModel::Callbacks basic guide --- railties/guides/source/active_model_basics.textile | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) (limited to 'railties/guides/source/active_model_basics.textile') diff --git a/railties/guides/source/active_model_basics.textile b/railties/guides/source/active_model_basics.textile index e4c84365d3..92fa5bc8f4 100644 --- a/railties/guides/source/active_model_basics.textile +++ b/railties/guides/source/active_model_basics.textile @@ -57,18 +57,14 @@ class Person def update _run_update_callbacks do - puts 'saving...' + # This will call when we are trying to call update on object. end end def reset_me - puts 'before saving...' + # This method will call when you are calling update on object as a before_update callback as defined. end end - -person = Person.new -person.update # before saving... - # saving... h3. Changelog -- cgit v1.2.3 From a3cf68291df3e9f5b23f56a90929c601ffc26ebd Mon Sep 17 00:00:00 2001 From: Vishnu Atrai Date: Fri, 5 Aug 2011 15:28:01 +0530 Subject: ActiveModel::Conversion basic guide --- railties/guides/source/active_model_basics.textile | 23 ++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'railties/guides/source/active_model_basics.textile') diff --git a/railties/guides/source/active_model_basics.textile b/railties/guides/source/active_model_basics.textile index 92fa5bc8f4..daf41d2296 100644 --- a/railties/guides/source/active_model_basics.textile +++ b/railties/guides/source/active_model_basics.textile @@ -67,6 +67,29 @@ class Person end +h4. Conversion + +If a class defines persisted? and id methods then you can include Conversion module in that class and you can able to call Rails conversion methods to objects of that class. + + +class Person + include ActiveModel::Conversion + + def persisted? + false + end + + def id + nil + end +end + +person = Person.new +person.to_model == person #=> true +person.to_key #=> nil +person.to_param #=> nil + + h3. Changelog * August 5, 2011: Initial version by "Arun Agrawal":http://github.com/arunagw \ No newline at end of file -- cgit v1.2.3 From 86ae14df4733cac1748513994caec2f9775ae224 Mon Sep 17 00:00:00 2001 From: Sukeerthi Adiga G Date: Fri, 5 Aug 2011 15:21:12 +0530 Subject: Dirty object methods added to active model basics --- railties/guides/source/active_model_basics.textile | 88 +++++++++++++++++++++- 1 file changed, 87 insertions(+), 1 deletion(-) (limited to 'railties/guides/source/active_model_basics.textile') diff --git a/railties/guides/source/active_model_basics.textile b/railties/guides/source/active_model_basics.textile index daf41d2296..404cc71c50 100644 --- a/railties/guides/source/active_model_basics.textile +++ b/railties/guides/source/active_model_basics.textile @@ -90,6 +90,92 @@ person.to_key #=> nil person.to_param #=> nil +h4. Dirty + +An object becomes dirty when an object is gone through one or more changes to its attributes and not yet saved. This gives the ability to check whether an object has been changed or not. It also has attribute based accessor methods. Lets consider a Person class with attributes first_name and last_name + + +require 'rubygems' +require 'active_model' + +class Person + include ActiveModel::Dirty + define_attribute_methods [:first_name, :last_name] + + def first_name + @first_name + end + + def first_name=(value) + first_name_will_change! + @first_name = value + end + + def last_name + @last_name + end + + def last_name=(value) + last_name_will_change! + @last_name = value + end + + def save + @previously_changed = changes + end + +end + + +h5. Querying object directly for its list of all changed attributes. + + +person = Person.new +person.first_name = "First Name" + +person.first_name #=> "First Name" +person.first_name = "First Name Changed" + +person.changed? #=> true + +#returns an list of fields arry which all has been changed before saved. +person.changed #=> ["first_name"] + +#returns a hash of the fields that have changed with their original values. +person.changed_attributes #=> {"first_name" => "First Name Changed"} + +#returns a hash of changes, with the attribute names as the keys, and the values will be an array of the old and new value for that field. +person.changes #=> {"first_name" => ["First Name","First Name Changed"]} + + +h5. Attribute based accessor methods + +Track whether the particular attribute has been changed or not. + + +#attr_name_changed? +person.first_name #=> "First Name" + +#assign some other value to first_name attribute +person.first_name = "First Name 1" + +person.first_name_changed? #=> true + + +Track what was the previous value of the attribute. + +#attr_name_was accessor +person.first_name_was #=> "First Name" + + + +Track both previous and current value of the changed attribute. Returns an array if changed else returns nil + +#attr_name_change +person.first_name_change #=> ["First Name", "First Name 1"] +person.last_name_change #=> nil + + h3. Changelog -* August 5, 2011: Initial version by "Arun Agrawal":http://github.com/arunagw \ No newline at end of file +* August 5, 2011: Initial version by "Arun Agrawal":http://github.com/arunagw -- cgit v1.2.3 From 54cd73e20d5fe2c4168e04f9525b8793e9e3c64c Mon Sep 17 00:00:00 2001 From: Vishnu Atrai Date: Fri, 5 Aug 2011 20:15:48 +0530 Subject: ActiveModel::Validations basic guide --- railties/guides/source/active_model_basics.textile | 24 ++++++++++++++++++++++ 1 file changed, 24 insertions(+) (limited to 'railties/guides/source/active_model_basics.textile') diff --git a/railties/guides/source/active_model_basics.textile b/railties/guides/source/active_model_basics.textile index 404cc71c50..f3a2b2edbc 100644 --- a/railties/guides/source/active_model_basics.textile +++ b/railties/guides/source/active_model_basics.textile @@ -176,6 +176,30 @@ person.first_name_change #=> ["First Name", "First Name 1"] person.last_name_change #=> nil +h4. Validations + +Validations module adds the ability to class objects to validate them in Active Record style. + + +class Person + include ActiveModel::Validations + + attr_accessor :name, :email + + validates :name, :presence => true + validates_format_of :email, :with => /^([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})$/i + +end + +person = Person.new +person.valid? #=> false +person.name = 'vishnu' +person.email = 'me' +person.valid? #=> false +person.email = 'me@vishnuatrai.com' +person.valid? #=> true + + h3. Changelog * August 5, 2011: Initial version by "Arun Agrawal":http://github.com/arunagw -- cgit v1.2.3 From 6783ce0de4511e2568765f2e4919758d70690391 Mon Sep 17 00:00:00 2001 From: Jeff Dutil Date: Tue, 16 Aug 2011 23:35:40 -0400 Subject: Updates to remove extra whitespaces and notably fix a whitespace issue with ajax_on_rails causing a code block not to render the entire block properly. --- railties/guides/source/active_model_basics.textile | 1 - 1 file changed, 1 deletion(-) (limited to 'railties/guides/source/active_model_basics.textile') diff --git a/railties/guides/source/active_model_basics.textile b/railties/guides/source/active_model_basics.textile index f3a2b2edbc..3c19fb5177 100644 --- a/railties/guides/source/active_model_basics.textile +++ b/railties/guides/source/active_model_basics.textile @@ -168,7 +168,6 @@ Track what was the previous value of the attribute. person.first_name_was #=> "First Name" - Track both previous and current value of the changed attribute. Returns an array if changed else returns nil #attr_name_change -- cgit v1.2.3 From a5e925c1d35ff10a22a98841f6a02925d4263947 Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Wed, 24 Aug 2011 10:25:29 +0300 Subject: Add strict validation example to guides --- railties/guides/source/active_model_basics.textile | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'railties/guides/source/active_model_basics.textile') diff --git a/railties/guides/source/active_model_basics.textile b/railties/guides/source/active_model_basics.textile index f3a2b2edbc..7d435456bd 100644 --- a/railties/guides/source/active_model_basics.textile +++ b/railties/guides/source/active_model_basics.textile @@ -184,20 +184,23 @@ Validations module adds the ability to class objects to validate them in Active class Person include ActiveModel::Validations - attr_accessor :name, :email + attr_accessor :name, :email, :token validates :name, :presence => true validates_format_of :email, :with => /^([^\s]+)((?:[-a-z0-9]\.)[a-z]{2,})$/i + validates! :token, :presence => true end -person = Person.new +person = Person.new(:token => "2b1f325") person.valid? #=> false person.name = 'vishnu' person.email = 'me' person.valid? #=> false person.email = 'me@vishnuatrai.com' person.valid? #=> true +person.token = nil +person.valid? #=> raises ActiveModel::StrictValidationFailed h3. Changelog -- cgit v1.2.3 From f2f05da06e48b5b4996380af33b59733048032f2 Mon Sep 17 00:00:00 2001 From: Bogdan Gusiev Date: Thu, 25 Aug 2011 19:37:11 +0300 Subject: Moved strict validation changelog entry to right place --- railties/guides/source/active_model_basics.textile | 1 + 1 file changed, 1 insertion(+) (limited to 'railties/guides/source/active_model_basics.textile') diff --git a/railties/guides/source/active_model_basics.textile b/railties/guides/source/active_model_basics.textile index ec27a071c9..0672669dc5 100644 --- a/railties/guides/source/active_model_basics.textile +++ b/railties/guides/source/active_model_basics.textile @@ -204,4 +204,5 @@ person.valid? #=> raises ActiveModel::StrictValidationFai h3. Changelog +* August 24, 2011: Add strict validation usage example. "Bogdan Gusiev":http://gusiev.com * August 5, 2011: Initial version by "Arun Agrawal":http://github.com/arunagw -- cgit v1.2.3 From f83f169b85eea4f580ae95609506fbb3cc5b8ccb Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Sat, 3 Sep 2011 01:40:00 +0530 Subject: some of the changes for validation earlier reverted from d20281a --- railties/guides/source/active_model_basics.textile | 2 ++ 1 file changed, 2 insertions(+) (limited to 'railties/guides/source/active_model_basics.textile') diff --git a/railties/guides/source/active_model_basics.textile b/railties/guides/source/active_model_basics.textile index 0672669dc5..73df567579 100644 --- a/railties/guides/source/active_model_basics.textile +++ b/railties/guides/source/active_model_basics.textile @@ -163,12 +163,14 @@ person.first_name_changed? #=> true Track what was the previous value of the attribute. + #attr_name_was accessor person.first_name_was #=> "First Name" Track both previous and current value of the changed attribute. Returns an array if changed else returns nil + #attr_name_change person.first_name_change #=> ["First Name", "First Name 1"] -- cgit v1.2.3 From 9980f46ca2d250d1d3e2fac84c5dc9ca6cbab1ea Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Thu, 15 Sep 2011 00:13:29 +0530 Subject: No more changelogs inside guides --- railties/guides/source/active_model_basics.textile | 5 ----- 1 file changed, 5 deletions(-) (limited to 'railties/guides/source/active_model_basics.textile') diff --git a/railties/guides/source/active_model_basics.textile b/railties/guides/source/active_model_basics.textile index 73df567579..9c8ad24cee 100644 --- a/railties/guides/source/active_model_basics.textile +++ b/railties/guides/source/active_model_basics.textile @@ -203,8 +203,3 @@ person.valid? #=> true person.token = nil person.valid? #=> raises ActiveModel::StrictValidationFailed - -h3. Changelog - -* August 24, 2011: Add strict validation usage example. "Bogdan Gusiev":http://gusiev.com -* August 5, 2011: Initial version by "Arun Agrawal":http://github.com/arunagw -- cgit v1.2.3