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:
Elliot Winkler 2015-12-22 02:01:21 -05:00
parent 2c26cc3ebb
commit 1b0a36a3ab
1 changed files with 7 additions and 4 deletions

View File

@ -34,8 +34,15 @@ module UnitTests
def define_active_model_class(class_name, options = {}, &block)
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
include ActiveModel::Validations
include attributes_module
def initialize(attributes = {})
attributes.each do |name, value|
@ -43,10 +50,6 @@ module UnitTests
end
end
accessors.each do |column|
attr_accessor column.to_sym
end
if block_given?
class_eval(&block)
end