From 77acfefedf80a2c30f8a0f71b5b6d33d1b9fd144 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Thu, 19 Mar 2009 23:45:08 +0000 Subject: Make Active Resource use ActiveModel::Errors --- activeresource/lib/active_resource.rb | 7 + activeresource/lib/active_resource/validations.rb | 196 +--------------------- activeresource/test/base_errors_test.rb | 2 +- 3 files changed, 9 insertions(+), 196 deletions(-) (limited to 'activeresource') diff --git a/activeresource/lib/active_resource.rb b/activeresource/lib/active_resource.rb index db9007060f..f0d30b1624 100644 --- a/activeresource/lib/active_resource.rb +++ b/activeresource/lib/active_resource.rb @@ -31,6 +31,13 @@ rescue LoadError end end +begin + require 'active_model' +rescue LoadError + $:.unshift "#{File.dirname(__FILE__)}/../../activemodel/lib" + require 'active_model' +end + require 'active_resource/formats' require 'active_resource/base' require 'active_resource/validations' diff --git a/activeresource/lib/active_resource/validations.rb b/activeresource/lib/active_resource/validations.rb index de3339935f..ca01ed3c75 100644 --- a/activeresource/lib/active_resource/validations.rb +++ b/activeresource/lib/active_resource/validations.rb @@ -4,201 +4,7 @@ module ActiveResource # Active Resource validation is reported to and from this object, which is used by Base#save # to determine whether the object in a valid state to be saved. See usage example in Validations. - class Errors - include Enumerable - attr_reader :errors - - delegate :empty?, :to => :errors - - def initialize(base) # :nodoc: - @base, @errors = base, {} - end - - # Add an error to the base Active Resource object rather than an attribute. - # - # ==== Examples - # my_folder = Folder.find(1) - # my_folder.errors.add_to_base("You can't edit an existing folder") - # my_folder.errors.on_base - # # => "You can't edit an existing folder" - # - # my_folder.errors.add_to_base("This folder has been tagged as frozen") - # my_folder.valid? - # # => false - # my_folder.errors.on_base - # # => ["You can't edit an existing folder", "This folder has been tagged as frozen"] - # - def add_to_base(msg) - add(:base, msg) - end - - # Adds an error to an Active Resource object's attribute (named for the +attribute+ parameter) - # with the error message in +msg+. - # - # ==== Examples - # my_resource = Node.find(1) - # my_resource.errors.add('name', 'can not be "base"') if my_resource.name == 'base' - # my_resource.errors.on('name') - # # => 'can not be "base"!' - # - # my_resource.errors.add('desc', 'can not be blank') if my_resource.desc == '' - # my_resource.valid? - # # => false - # my_resource.errors.on('desc') - # # => 'can not be blank!' - # - def add(attribute, msg) - @errors[attribute.to_s] = [] if @errors[attribute.to_s].nil? - @errors[attribute.to_s] << msg - end - - # Returns true if the specified +attribute+ has errors associated with it. - # - # ==== Examples - # my_resource = Disk.find(1) - # my_resource.errors.add('location', 'must be Main') unless my_resource.location == 'Main' - # my_resource.errors.on('location') - # # => 'must be Main!' - # - # my_resource.errors.invalid?('location') - # # => true - # my_resource.errors.invalid?('name') - # # => false - def invalid?(attribute) - !@errors[attribute.to_s].nil? - end - - # A method to return the errors associated with +attribute+, which returns nil, if no errors are - # associated with the specified +attribute+, the error message if one error is associated with the specified +attribute+, - # or an array of error messages if more than one error is associated with the specified +attribute+. - # - # ==== Examples - # my_person = Person.new(params[:person]) - # my_person.errors.on('login') - # # => nil - # - # my_person.errors.add('login', 'can not be empty') if my_person.login == '' - # my_person.errors.on('login') - # # => 'can not be empty' - # - # my_person.errors.add('login', 'can not be longer than 10 characters') if my_person.login.length > 10 - # my_person.errors.on('login') - # # => ['can not be empty', 'can not be longer than 10 characters'] - def on(attribute) - errors = @errors[attribute.to_s] - return nil if errors.nil? - errors.size == 1 ? errors.first : errors - end - - alias :[] :on - - # A method to return errors assigned to +base+ object through add_to_base, which returns nil, if no errors are - # associated with the specified +attribute+, the error message if one error is associated with the specified +attribute+, - # or an array of error messages if more than one error is associated with the specified +attribute+. - # - # ==== Examples - # my_account = Account.find(1) - # my_account.errors.on_base - # # => nil - # - # my_account.errors.add_to_base("This account is frozen") - # my_account.errors.on_base - # # => "This account is frozen" - # - # my_account.errors.add_to_base("This account has been closed") - # my_account.errors.on_base - # # => ["This account is frozen", "This account has been closed"] - # - def on_base - on(:base) - end - - # Yields each attribute and associated message per error added. - # - # ==== Examples - # my_person = Person.new(params[:person]) - # - # my_person.errors.add('login', 'can not be empty') if my_person.login == '' - # my_person.errors.add('password', 'can not be empty') if my_person.password == '' - # messages = '' - # my_person.errors.each {|attr, msg| messages += attr.humanize + " " + msg + "
"} - # messages - # # => "Login can not be empty
Password can not be empty
" - # - def each - @errors.each_key { |attr| @errors[attr].each { |msg| yield attr, msg } } - end - - # Yields each full error message added. So Person.errors.add("first_name", "can't be empty") will be returned - # through iteration as "First name can't be empty". - # - # ==== Examples - # my_person = Person.new(params[:person]) - # - # my_person.errors.add('login', 'can not be empty') if my_person.login == '' - # my_person.errors.add('password', 'can not be empty') if my_person.password == '' - # messages = '' - # my_person.errors.each_full {|msg| messages += msg + "
"} - # messages - # # => "Login can not be empty
Password can not be empty
" - # - def each_full - full_messages.each { |msg| yield msg } - end - - # Returns all the full error messages in an array. - # - # ==== Examples - # my_person = Person.new(params[:person]) - # - # my_person.errors.add('login', 'can not be empty') if my_person.login == '' - # my_person.errors.add('password', 'can not be empty') if my_person.password == '' - # messages = '' - # my_person.errors.full_messages.each {|msg| messages += msg + "
"} - # messages - # # => "Login can not be empty
Password can not be empty
" - # - def full_messages - full_messages = [] - - @errors.each_key do |attr| - @errors[attr].each do |msg| - next if msg.nil? - - if attr == "base" - full_messages << msg - else - full_messages << [attr.humanize, msg].join(' ') - end - end - end - full_messages - end - - def clear - @errors = {} - end - - # Returns the total number of errors added. Two errors added to the same attribute will be counted as such - # with this as well. - # - # ==== Examples - # my_person = Person.new(params[:person]) - # my_person.errors.size - # # => 0 - # - # my_person.errors.add('login', 'can not be empty') if my_person.login == '' - # my_person.errors.add('password', 'can not be empty') if my_person.password == '' - # my_person.error.size - # # => 2 - # - def size - @errors.values.inject(0) { |error_count, attribute| error_count + attribute.size } - end - - alias_method :count, :size - alias_method :length, :size - + class Errors < ::ActiveModel::Errors # Grabs errors from the XML response. def from_xml(xml) clear diff --git a/activeresource/test/base_errors_test.rb b/activeresource/test/base_errors_test.rb index 7ae92c7d98..517babd16b 100644 --- a/activeresource/test/base_errors_test.rb +++ b/activeresource/test/base_errors_test.rb @@ -28,7 +28,7 @@ class BaseErrorsTest < Test::Unit::TestCase def test_should_iterate_over_errors errors = [] - @person.errors.each { |attribute, message| errors << [attribute, message] } + @person.errors.each { |attribute, message| errors << [attribute.to_s, message] } assert errors.include?(["name", "can't be blank"]) end -- cgit v1.2.3 From 09afbfd47f8180bb6d5f907abdeab6badeda879e Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Fri, 20 Mar 2009 18:51:01 +0000 Subject: Fix ActiveResource::Errors deprecation messages --- activeresource/lib/active_resource/validations.rb | 4 ++-- activeresource/test/base_errors_test.rb | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) (limited to 'activeresource') diff --git a/activeresource/lib/active_resource/validations.rb b/activeresource/lib/active_resource/validations.rb index ca01ed3c75..7fe3162c02 100644 --- a/activeresource/lib/active_resource/validations.rb +++ b/activeresource/lib/active_resource/validations.rb @@ -4,7 +4,7 @@ module ActiveResource # Active Resource validation is reported to and from this object, which is used by Base#save # to determine whether the object in a valid state to be saved. See usage example in Validations. - class Errors < ::ActiveModel::Errors + class Errors < ActiveModel::Errors # Grabs errors from the XML response. def from_xml(xml) clear @@ -38,7 +38,7 @@ module ActiveResource # person.errors.empty? # => false # person.errors.count # => 1 # person.errors.full_messages # => ["Last name can't be empty"] - # person.errors.on(:last_name) # => "can't be empty" + # person.errors[:last_name] # => ["can't be empty"] # person.last_name = "Halpert" # person.save # => true (and person is now saved to the remote service) # diff --git a/activeresource/test/base_errors_test.rb b/activeresource/test/base_errors_test.rb index 517babd16b..8c177c8006 100644 --- a/activeresource/test/base_errors_test.rb +++ b/activeresource/test/base_errors_test.rb @@ -21,9 +21,9 @@ class BaseErrorsTest < Test::Unit::TestCase def test_should_parse_errors_to_individual_attributes assert @person.errors.invalid?(:name) - assert_equal "can't be blank", @person.errors.on(:age) + assert_equal ["can't be blank"], @person.errors[:age] assert_equal ["can't be blank", "must start with a letter"], @person.errors[:name] - assert_equal "Person quota full for today.", @person.errors.on_base + assert_equal ["Person quota full for today."], @person.errors[:base] end def test_should_iterate_over_errors -- cgit v1.2.3 From 320933205e16164ff55245aef1e95fb06e609d06 Mon Sep 17 00:00:00 2001 From: Pratik Naik Date: Sat, 21 Mar 2009 18:29:15 +0000 Subject: Deprecate Errors#on_base/add_to_base/invalid?/each_full --- activeresource/lib/active_resource/validations.rb | 2 +- activeresource/test/abstract_unit.rb | 3 +++ activeresource/test/base_errors_test.rb | 4 ++-- 3 files changed, 6 insertions(+), 3 deletions(-) (limited to 'activeresource') diff --git a/activeresource/lib/active_resource/validations.rb b/activeresource/lib/active_resource/validations.rb index 7fe3162c02..9d3d45b010 100644 --- a/activeresource/lib/active_resource/validations.rb +++ b/activeresource/lib/active_resource/validations.rb @@ -17,7 +17,7 @@ module ActiveResource end end - add_to_base message if attr_message.nil? + self[:base] << message if attr_message.nil? end end end diff --git a/activeresource/test/abstract_unit.rb b/activeresource/test/abstract_unit.rb index 0f11ea482a..ce9371d050 100644 --- a/activeresource/test/abstract_unit.rb +++ b/activeresource/test/abstract_unit.rb @@ -14,6 +14,9 @@ require 'setter_trap' ActiveResource::Base.logger = Logger.new("#{File.dirname(__FILE__)}/debug.log") +# Show backtraces for deprecated behavior for quicker cleanup. +ActiveSupport::Deprecation.debug = true + def uses_gem(gem_name, test_name, version = '> 0') gem gem_name.to_s, version require gem_name.to_s diff --git a/activeresource/test/base_errors_test.rb b/activeresource/test/base_errors_test.rb index 8c177c8006..28813821df 100644 --- a/activeresource/test/base_errors_test.rb +++ b/activeresource/test/base_errors_test.rb @@ -20,7 +20,7 @@ class BaseErrorsTest < Test::Unit::TestCase end def test_should_parse_errors_to_individual_attributes - assert @person.errors.invalid?(:name) + assert @person.errors[:name].any? assert_equal ["can't be blank"], @person.errors[:age] assert_equal ["can't be blank", "must start with a letter"], @person.errors[:name] assert_equal ["Person quota full for today."], @person.errors[:base] @@ -34,7 +34,7 @@ class BaseErrorsTest < Test::Unit::TestCase def test_should_iterate_over_full_errors errors = [] - @person.errors.each_full { |message| errors << message } + @person.errors.to_a.each { |message| errors << message } assert errors.include?("Name can't be blank") end -- cgit v1.2.3 From 3962be5b8c20da54421ae472f06c741f0472fd46 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 29 May 2009 21:47:19 -0500 Subject: Use URI and Inflector --- activeresource/lib/active_resource/base.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'activeresource') diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index dc24e713ff..847cc600d5 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -1,12 +1,14 @@ require 'active_support' require 'active_support/core_ext/class/attribute_accessors' require 'active_support/core_ext/class/inheritable_attributes' +require 'active_support/core_ext/kernel/reporting' require 'active_support/core_ext/module/attr_accessor_with_default' require 'active_support/core_ext/module/delegation' require 'active_support/core_ext/module/aliasing' require 'active_support/core_ext/object/blank' require 'active_support/core_ext/object/misc' require 'set' +require 'uri' module ActiveResource autoload :Formats, 'active_resource/formats' @@ -346,9 +348,9 @@ module ActiveResource # Do not include any modules in the default element name. This makes it easier to seclude ARes objects # in a separate namespace without having to set element_name repeatedly. - attr_accessor_with_default(:element_name) { to_s.split("::").last.underscore } #:nodoc: + attr_accessor_with_default(:element_name) { ActiveSupport::Inflector.underscore(to_s.split("::").last) } #:nodoc: - attr_accessor_with_default(:collection_name) { element_name.pluralize } #:nodoc: + attr_accessor_with_default(:collection_name) { ActiveSupport::Inflector.pluralize(element_name) } #:nodoc: attr_accessor_with_default(:primary_key, 'id') #:nodoc: # Gets the \prefix for a resource's nested URL (e.g., prefix/collectionname/1.xml) -- cgit v1.2.3 From 85eb3af873e3393416ff2c764c5590abc0538a64 Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Fri, 29 May 2009 21:47:53 -0500 Subject: Tolerate missing logger --- activeresource/lib/active_resource/base.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'activeresource') diff --git a/activeresource/lib/active_resource/base.rb b/activeresource/lib/active_resource/base.rb index 847cc600d5..11a7bbba3e 100644 --- a/activeresource/lib/active_resource/base.rb +++ b/activeresource/lib/active_resource/base.rb @@ -386,7 +386,7 @@ module ActiveResource end_code silence_warnings { instance_eval code, __FILE__, __LINE__ } rescue - logger.error "Couldn't set prefix: #{$!}\n #{code}" + logger.error "Couldn't set prefix: #{$!}\n #{code}" if logger raise end -- cgit v1.2.3