WIP: continue converting to options hash.
This commit is contained in:
parent
2b9591052f
commit
102ff93311
|
@ -20,19 +20,19 @@ module Shoulda # :nodoc:
|
||||||
|
|
||||||
def initialize(attribute)
|
def initialize(attribute)
|
||||||
@attribute = attribute.to_s
|
@attribute = attribute.to_s
|
||||||
|
@options = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def as(role)
|
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"
|
raise "You can specify role only in Rails 3.1 or greater"
|
||||||
end
|
end
|
||||||
@role = role
|
@options[:role] = role
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
def matches?(subject)
|
def matches?(subject)
|
||||||
@subject = subject
|
@subject = subject
|
||||||
@role ||= :default
|
|
||||||
if attr_mass_assignable?
|
if attr_mass_assignable?
|
||||||
if whitelisting?
|
if whitelisting?
|
||||||
@negative_failure_message = "#{@attribute} was made accessible"
|
@negative_failure_message = "#{@attribute} was made accessible"
|
||||||
|
@ -62,6 +62,10 @@ module Shoulda # :nodoc:
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def role
|
||||||
|
@options[:role] || :default
|
||||||
|
end
|
||||||
|
|
||||||
def protected_attributes
|
def protected_attributes
|
||||||
@protected_attributes ||= (@subject.class.protected_attributes || [])
|
@protected_attributes ||= (@subject.class.protected_attributes || [])
|
||||||
end
|
end
|
||||||
|
@ -79,10 +83,10 @@ module Shoulda # :nodoc:
|
||||||
end
|
end
|
||||||
|
|
||||||
def authorizer
|
def authorizer
|
||||||
if at_least_rails_3_1?
|
if less_than_active_model_3_1?
|
||||||
@subject.class.active_authorizer[@role]
|
|
||||||
else
|
|
||||||
@subject.class.active_authorizer
|
@subject.class.active_authorizer
|
||||||
|
else
|
||||||
|
@subject.class.active_authorizer[role]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -90,8 +94,8 @@ module Shoulda # :nodoc:
|
||||||
@subject.class.name
|
@subject.class.name
|
||||||
end
|
end
|
||||||
|
|
||||||
def at_least_rails_3_1?
|
def less_than_active_model_3_1?
|
||||||
::ActiveModel::VERSION::MAJOR == 3 && ::ActiveModel::VERSION::MINOR >= 1
|
! (::ActiveModel::VERSION::MAJOR == 3 && ::ActiveModel::VERSION::MINOR >= 1)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -18,15 +18,16 @@ module Shoulda # :nodoc:
|
||||||
class SerializeMatcher # :nodoc:
|
class SerializeMatcher # :nodoc:
|
||||||
def initialize(name)
|
def initialize(name)
|
||||||
@name = name.to_s
|
@name = name.to_s
|
||||||
|
@options = {}
|
||||||
end
|
end
|
||||||
|
|
||||||
def as(type)
|
def as(type)
|
||||||
@type = type
|
@options[:type] = type
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
def as_instance_of(type)
|
def as_instance_of(type)
|
||||||
@instance_type = type
|
@options[:instance_type] = type
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -45,7 +46,7 @@ module Shoulda # :nodoc:
|
||||||
|
|
||||||
def description
|
def description
|
||||||
description = "serialize :#{@name}"
|
description = "serialize :#{@name}"
|
||||||
description += " class_name => #{@type}" if @type
|
description += " class_name => #{@options[:type]}" if @options.key?(:type)
|
||||||
description
|
description
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -61,15 +62,15 @@ module Shoulda # :nodoc:
|
||||||
end
|
end
|
||||||
|
|
||||||
def class_valid?
|
def class_valid?
|
||||||
if @type
|
if @options[:type]
|
||||||
klass = model_class.serialized_attributes[@name]
|
klass = model_class.serialized_attributes[@name]
|
||||||
if klass == @type
|
if klass == @options[:type]
|
||||||
true
|
true
|
||||||
else
|
else
|
||||||
if klass.respond_to?(:object_class) && klass.object_class == @type
|
if klass.respond_to?(:object_class) && klass.object_class == @options[:type]
|
||||||
true
|
true
|
||||||
else
|
else
|
||||||
@missing = ":#{@name} should be a type of #{@type}"
|
@missing = ":#{@name} should be a type of #{@options[:type]}"
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -83,11 +84,11 @@ module Shoulda # :nodoc:
|
||||||
end
|
end
|
||||||
|
|
||||||
def instance_class_valid?
|
def instance_class_valid?
|
||||||
if @instance_type
|
if @options.key?(:instance_type)
|
||||||
if model_class.serialized_attributes[@name].class == @instance_type
|
if model_class.serialized_attributes[@name].class == @options[:instance_type]
|
||||||
true
|
true
|
||||||
else
|
else
|
||||||
@missing = ":#{@name} should be an instance of #{@type}"
|
@missing = ":#{@name} should be an instance of #{@options[:type]}"
|
||||||
false
|
false
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
|
@ -101,8 +102,8 @@ module Shoulda # :nodoc:
|
||||||
|
|
||||||
def expectation
|
def expectation
|
||||||
expectation = "#{model_class.name} to serialize the attribute called :#{@name}"
|
expectation = "#{model_class.name} to serialize the attribute called :#{@name}"
|
||||||
expectation += " with a type of #{@type}" if @type
|
expectation += " with a type of #{@options[:type]}" if @options[:type]
|
||||||
expectation += " with an instance of #{@instance_type}" if @instance_type
|
expectation += " with an instance of #{@options[:instance_type]}" if @options[:instance_type]
|
||||||
expectation
|
expectation
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue