Skip to content

Commit

Permalink
Make step definitions more ORM agnostic by preferring .attribute_names
Browse files Browse the repository at this point in the history
Support for .attribute_names
* ActiveRecord starting in v3.1.0
* ActiveAttr starting in v0.5.0
  • Loading branch information
cgriego authored and joshuaclayton committed Mar 30, 2012
1 parent 4f5b775 commit 81c9f2c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
4 changes: 4 additions & 0 deletions features/factory_girl_steps.feature
Expand Up @@ -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
20 changes: 20 additions & 0 deletions features/support/factories.rb
Expand Up @@ -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|
Expand Down Expand Up @@ -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'
Expand Down
27 changes: 17 additions & 10 deletions lib/factory_girl/step_definitions.rb
Expand Up @@ -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
Expand Down

0 comments on commit 81c9f2c

Please sign in to comment.