From 4831a895c445f650c9daf0b445136ceb653f781b Mon Sep 17 00:00:00 2001 From: Francesco Rodriguez Date: Mon, 7 May 2012 22:47:35 -0500 Subject: added docs to alias_attribute method --- activemodel/lib/active_model/attribute_methods.rb | 31 +++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index 97a83e58af..59977b6142 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -180,6 +180,37 @@ module ActiveModel undefine_attribute_methods end + + # Allows you to make aliases for attributes. + # + # For example: + # + # class Person + # + # include ActiveModel::AttributeMethods + # attr_accessor :name + # attribute_method_prefix 'clear_' + # + # define_attribute_methods [:name] + # + # private + # + # def clear_attribute(attr) + # send("#{attr}=", nil) + # end + # end + # + # class Person + # attr_accessor :nickname + # + # alias_attribute :nickname, :name + # end + # + # person = Person.new + # person.nickname = "Bob" + # person.nickname # => "Bob" + # person.clear_nickname + # person.nickname # => nil def alias_attribute(new_name, old_name) attribute_method_matchers.each do |matcher| matcher_new = matcher.method_name(new_name).to_s -- cgit v1.2.3 From 7e26f7f0f7e3c230c333e1b265727a9b8cf7c91f Mon Sep 17 00:00:00 2001 From: Vijay Dev Date: Tue, 8 May 2012 23:54:47 +0530 Subject: simplify the alias_attribute example [ci skip] --- activemodel/lib/active_model/attribute_methods.rb | 21 +-------------------- 1 file changed, 1 insertion(+), 20 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/attribute_methods.rb b/activemodel/lib/active_model/attribute_methods.rb index 59977b6142..25d5d84ce6 100644 --- a/activemodel/lib/active_model/attribute_methods.rb +++ b/activemodel/lib/active_model/attribute_methods.rb @@ -183,34 +183,15 @@ module ActiveModel # Allows you to make aliases for attributes. # - # For example: - # # class Person - # - # include ActiveModel::AttributeMethods # attr_accessor :name - # attribute_method_prefix 'clear_' - # - # define_attribute_methods [:name] - # - # private - # - # def clear_attribute(attr) - # send("#{attr}=", nil) - # end - # end - # - # class Person - # attr_accessor :nickname - # # alias_attribute :nickname, :name # end # # person = Person.new # person.nickname = "Bob" # person.nickname # => "Bob" - # person.clear_nickname - # person.nickname # => nil + # person.name # => "Bob" def alias_attribute(new_name, old_name) attribute_method_matchers.each do |matcher| matcher_new = matcher.method_name(new_name).to_s -- cgit v1.2.3 From a8637cf4938d2decd17e702c399ca9c0cf1a6052 Mon Sep 17 00:00:00 2001 From: Jon Leighton Date: Fri, 11 May 2012 17:17:29 +0100 Subject: Use respond_to?(:to_ary) rather than is_a?(Enumerable) to detect collection-thing. --- activemodel/lib/active_model/serialization.rb | 4 ++-- activemodel/lib/active_model/serializers/xml.rb | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/serialization.rb b/activemodel/lib/active_model/serialization.rb index 4403ef060b..00cd05b7ef 100644 --- a/activemodel/lib/active_model/serialization.rb +++ b/activemodel/lib/active_model/serialization.rb @@ -87,8 +87,8 @@ module ActiveModel Array(options[:methods]).each { |m| hash[m.to_s] = send(m) if respond_to?(m) } serializable_add_includes(options) do |association, records, opts| - hash[association.to_s] = if records.is_a?(Enumerable) - records.map { |a| a.serializable_hash(opts) } + hash[association.to_s] = if records.respond_to?(:to_ary) + records.to_ary.map { |a| a.serializable_hash(opts) } else records.serializable_hash(opts) end diff --git a/activemodel/lib/active_model/serializers/xml.rb b/activemodel/lib/active_model/serializers/xml.rb index 5084298210..b78f1ff3f3 100644 --- a/activemodel/lib/active_model/serializers/xml.rb +++ b/activemodel/lib/active_model/serializers/xml.rb @@ -115,7 +115,9 @@ module ActiveModel merged_options = opts.merge(options.slice(:builder, :indent)) merged_options[:skip_instruct] = true - if records.is_a?(Enumerable) + if records.respond_to?(:to_ary) + records = records.to_ary + tag = ActiveSupport::XmlMini.rename_key(association.to_s, options) type = options[:skip_types] ? { } : {:type => "array"} association_name = association.to_s.singularize -- cgit v1.2.3 From a00228c1a35578c4bf4d462eec977a50120288da Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Thu, 10 May 2012 20:48:23 -0300 Subject: Lazy load I18n --- activemodel/lib/active_model.rb | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb index 2586147a20..7503fb7929 100644 --- a/activemodel/lib/active_model.rb +++ b/activemodel/lib/active_model.rb @@ -59,5 +59,6 @@ module ActiveModel end end -require 'active_support/i18n' -I18n.load_path << File.dirname(__FILE__) + '/active_model/locale/en.yml' +ActiveSupport.on_load(:i18n) do + I18n.load_path << File.dirname(__FILE__) + '/active_model/locale/en.yml' +end -- cgit v1.2.3 From 36dd1857dc097b6fbc65396bfabaa152da9c899f Mon Sep 17 00:00:00 2001 From: Santiago Pastorino Date: Fri, 11 May 2012 13:56:05 -0300 Subject: Remove useless load path modifications --- activemodel/lib/active_model.rb | 2 -- 1 file changed, 2 deletions(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model.rb b/activemodel/lib/active_model.rb index 7503fb7929..ded1b752df 100644 --- a/activemodel/lib/active_model.rb +++ b/activemodel/lib/active_model.rb @@ -21,8 +21,6 @@ # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #++ -activesupport_path = File.expand_path('../../../activesupport/lib', __FILE__) -$:.unshift(activesupport_path) if File.directory?(activesupport_path) && !$:.include?(activesupport_path) require 'active_support' require 'active_model/version' -- cgit v1.2.3 From 7a95d079a31a4468d9ecbe5b4bbcd834f49ad92e Mon Sep 17 00:00:00 2001 From: Alexey Vakhov Date: Mon, 14 May 2012 09:13:59 +0400 Subject: Missed colon --- activemodel/lib/active_model/serialization.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activemodel/lib') diff --git a/activemodel/lib/active_model/serialization.rb b/activemodel/lib/active_model/serialization.rb index 00cd05b7ef..3751e4a741 100644 --- a/activemodel/lib/active_model/serialization.rb +++ b/activemodel/lib/active_model/serialization.rb @@ -64,7 +64,7 @@ module ActiveModel # person.to_json # => "{\"name\":\"Bob\"}" # person.to_xml # => "\n:only, :except, :methods and include. + # Valid options are :only, :except, :methods and :include. # The following are all valid examples: # # person.serializable_hash(:only => 'name') -- cgit v1.2.3