Define attrs in a module when building AM models in tests
When a developer builds an ActiveModel model and wants to override an attribute, she may do so using `super` to call the original method. For instance: def foo=(value) super(value.next) end as opposed to: def foo=(value) @foo = value.next end
This commit is contained in:
parent
2c26cc3ebb
commit
1b0a36a3ab
|
@ -34,8 +34,15 @@ module UnitTests
|
||||||
def define_active_model_class(class_name, options = {}, &block)
|
def define_active_model_class(class_name, options = {}, &block)
|
||||||
accessors = options.fetch(:accessors, [])
|
accessors = options.fetch(:accessors, [])
|
||||||
|
|
||||||
|
attributes_module = Module.new do
|
||||||
|
accessors.each do |column|
|
||||||
|
attr_accessor column.to_sym
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
define_class(class_name) do
|
define_class(class_name) do
|
||||||
include ActiveModel::Validations
|
include ActiveModel::Validations
|
||||||
|
include attributes_module
|
||||||
|
|
||||||
def initialize(attributes = {})
|
def initialize(attributes = {})
|
||||||
attributes.each do |name, value|
|
attributes.each do |name, value|
|
||||||
|
@ -43,10 +50,6 @@ module UnitTests
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
accessors.each do |column|
|
|
||||||
attr_accessor column.to_sym
|
|
||||||
end
|
|
||||||
|
|
||||||
if block_given?
|
if block_given?
|
||||||
class_eval(&block)
|
class_eval(&block)
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue