From 81c9f2c3dd81b2d709cb08d23673eec81b521c94 Mon Sep 17 00:00:00 2001 From: Chris Griego Date: Sun, 11 Mar 2012 23:27:14 -0500 Subject: [PATCH] Make step definitions more ORM agnostic by preferring .attribute_names Support for .attribute_names * ActiveRecord starting in v3.1.0 * ActiveAttr starting in v0.5.0 --- features/factory_girl_steps.feature | 4 ++++ features/support/factories.rb | 20 ++++++++++++++++++++ lib/factory_girl/step_definitions.rb | 27 +++++++++++++++++---------- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/features/factory_girl_steps.feature b/features/factory_girl_steps.feature index 610962e73..218087c98 100644 --- a/features/factory_girl_steps.feature +++ b/features/factory_girl_steps.feature @@ -235,3 +235,7 @@ Feature: Use step definitions generated by factories Scenario: step definitions work correctly with ORMs that have simple `columns` Given a simple column exists Then there should be 1 SimpleColumn + + Scenario: step definitions work correctly with model classes that simply have attribute names + Given a named attribute model exists with a title of "a fun title" + Then there should be 1 NamedAttributeModel diff --git a/features/support/factories.rb b/features/support/factories.rb index b858cdd2a..1c70d142e 100644 --- a/features/support/factories.rb +++ b/features/support/factories.rb @@ -73,6 +73,23 @@ def self.count end end +class NamedAttributeModel + def self.attribute_names + %w(title) + end + + attr_accessor :title + + def save! + @@count ||= 0 + @@count += 1 + end + + def self.count + @@count + end +end + FactoryGirl.define do # To make sure the step defs work with an email sequence :email do |n| @@ -113,6 +130,9 @@ def self.count # This is here to make FG work with ORMs that have `columns => [:name, :admin, :etc]` on the class (Neo4j) factory :simple_column do end + + factory :named_attribute_model do + end end require 'factory_girl/step_definitions' diff --git a/lib/factory_girl/step_definitions.rb b/lib/factory_girl/step_definitions.rb index 2709cb71c..d3aeaf5aa 100644 --- a/lib/factory_girl/step_definitions.rb +++ b/lib/factory_girl/step_definitions.rb @@ -112,17 +112,24 @@ def initialize(human_hash_to_attributes_hash, key, value) FactoryGirl.create_list(factory.name, count.to_i) end - if factory.build_class.respond_to?(:columns) - factory.build_class.columns.each do |column| - name = column.respond_to?(:name) ? column.name : column.to_s - human_column_name = name.downcase.gsub('_', ' ') - Given /^an? #{human_name} exists with an? #{human_column_name} of "([^"]*)"$/i do |value| - FactoryGirl.create(factory.name, name => value) - end + attribute_names = if factory.build_class.respond_to?(:attribute_names) + factory.build_class.attribute_names + elsif factory.build_class.respond_to?(:columns) + factory.build_class.columns.map do |column| + column.respond_to?(:name) ? column.name : column.to_s + end + else + [] + end - Given /^(\d+) #{human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value| - FactoryGirl.create_list(factory.name, count.to_i, name => value) - end + attribute_names.each do |attribute_name| + human_column_name = attribute_name.downcase.gsub('_', ' ') + Given /^an? #{human_name} exists with an? #{human_column_name} of "([^"]*)"$/i do |value| + FactoryGirl.create(factory.name, attribute_name => value) + end + + Given /^(\d+) #{human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value| + FactoryGirl.create_list(factory.name, count.to_i, attribute_name => value) end end end