From 102ff9331165480fae988949250c199e375dd361 Mon Sep 17 00:00:00 2001 From: Gabe Berke-Williams Date: Mon, 23 Apr 2012 18:07:49 -0400 Subject: [PATCH] WIP: continue converting to options hash. --- .../allow_mass_assignment_of_matcher.rb | 20 +++++++++------ .../active_record/serialize_matcher.rb | 25 ++++++++++--------- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb b/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb index 3aab75f0..c642c178 100644 --- a/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb +++ b/lib/shoulda/matchers/active_model/allow_mass_assignment_of_matcher.rb @@ -20,19 +20,19 @@ module Shoulda # :nodoc: def initialize(attribute) @attribute = attribute.to_s + @options = {} end def as(role) - unless at_least_rails_3_1? + if less_than_active_model_3_1? raise "You can specify role only in Rails 3.1 or greater" end - @role = role + @options[:role] = role self end def matches?(subject) @subject = subject - @role ||= :default if attr_mass_assignable? if whitelisting? @negative_failure_message = "#{@attribute} was made accessible" @@ -62,6 +62,10 @@ module Shoulda # :nodoc: private + def role + @options[:role] || :default + end + def protected_attributes @protected_attributes ||= (@subject.class.protected_attributes || []) end @@ -79,10 +83,10 @@ module Shoulda # :nodoc: end def authorizer - if at_least_rails_3_1? - @subject.class.active_authorizer[@role] - else + if less_than_active_model_3_1? @subject.class.active_authorizer + else + @subject.class.active_authorizer[role] end end @@ -90,8 +94,8 @@ module Shoulda # :nodoc: @subject.class.name end - def at_least_rails_3_1? - ::ActiveModel::VERSION::MAJOR == 3 && ::ActiveModel::VERSION::MINOR >= 1 + def less_than_active_model_3_1? + ! (::ActiveModel::VERSION::MAJOR == 3 && ::ActiveModel::VERSION::MINOR >= 1) end end end diff --git a/lib/shoulda/matchers/active_record/serialize_matcher.rb b/lib/shoulda/matchers/active_record/serialize_matcher.rb index 4936dc03..02ee2bd0 100644 --- a/lib/shoulda/matchers/active_record/serialize_matcher.rb +++ b/lib/shoulda/matchers/active_record/serialize_matcher.rb @@ -18,15 +18,16 @@ module Shoulda # :nodoc: class SerializeMatcher # :nodoc: def initialize(name) @name = name.to_s + @options = {} end def as(type) - @type = type + @options[:type] = type self end def as_instance_of(type) - @instance_type = type + @options[:instance_type] = type self end @@ -45,7 +46,7 @@ module Shoulda # :nodoc: def description description = "serialize :#{@name}" - description += " class_name => #{@type}" if @type + description += " class_name => #{@options[:type]}" if @options.key?(:type) description end @@ -61,15 +62,15 @@ module Shoulda # :nodoc: end def class_valid? - if @type + if @options[:type] klass = model_class.serialized_attributes[@name] - if klass == @type + if klass == @options[:type] true else - if klass.respond_to?(:object_class) && klass.object_class == @type + if klass.respond_to?(:object_class) && klass.object_class == @options[:type] true else - @missing = ":#{@name} should be a type of #{@type}" + @missing = ":#{@name} should be a type of #{@options[:type]}" false end end @@ -83,11 +84,11 @@ module Shoulda # :nodoc: end def instance_class_valid? - if @instance_type - if model_class.serialized_attributes[@name].class == @instance_type + if @options.key?(:instance_type) + if model_class.serialized_attributes[@name].class == @options[:instance_type] true else - @missing = ":#{@name} should be an instance of #{@type}" + @missing = ":#{@name} should be an instance of #{@options[:type]}" false end else @@ -101,8 +102,8 @@ module Shoulda # :nodoc: def expectation expectation = "#{model_class.name} to serialize the attribute called :#{@name}" - expectation += " with a type of #{@type}" if @type - expectation += " with an instance of #{@instance_type}" if @instance_type + expectation += " with a type of #{@options[:type]}" if @options[:type] + expectation += " with an instance of #{@options[:instance_type]}" if @options[:instance_type] expectation end end