From 1b0a36a3ab529999da53120b8104b01e9c8311a5 Mon Sep 17 00:00:00 2001 From: Elliot Winkler Date: Tue, 22 Dec 2015 02:01:21 -0500 Subject: [PATCH] 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 --- spec/support/unit/helpers/model_builder.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/spec/support/unit/helpers/model_builder.rb b/spec/support/unit/helpers/model_builder.rb index 955ef5d7..930ed604 100644 --- a/spec/support/unit/helpers/model_builder.rb +++ b/spec/support/unit/helpers/model_builder.rb @@ -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