Permalink
Browse files

Bugfix: Load definitions for transforms

This change also makes possible to use transforms with Turnip
  • Loading branch information...
greis committed Feb 10, 2012
1 parent 47189eb commit b0027ea9c5c56a8ee8e7c8f0de78cb96ae65dc32
@@ -3,11 +3,12 @@ class Fabrication::Transform
class << self
def apply(attributes_hash)
+ Fabrication::Support.find_definitions if transforms.empty?
attributes_hash.inject({}) {|h,(k,v)| h.update(k => transforms[k].call(v)) }
end
def clear_all
- @@transforms = nil
+ @transforms = nil
end
def define(attribute, transform)
@@ -17,7 +18,7 @@ def define(attribute, transform)
private
def transforms
- @@transforms ||= Hash.new(lambda {|value| value})
+ @transforms ||= Hash.new(lambda {|value| value})
end
end
@@ -3,10 +3,28 @@
describe Fabrication::Transform do
before do
+ Fabrication.clear_definitions
Fabrication::Transform.clear_all
end
describe '.apply' do
+ context 'find definitions' do
+ context 'transforms are empty' do
+ it 'loads the definitions' do
+ Fabrication::Support.should_receive(:find_definitions)
+ Fabrication::Transform.apply(:name => 'Shay')
+ end
+ end
+
+ context 'transforms are not empty' do
+ it 'does not load the definitions' do
+ Fabrication::Transform.apply(:name => 'Shay')
+ Fabrication::Support.should_not_receive(:find_definitions)
+ Fabrication::Transform.apply(:name => 'Gabriel')
+ end
+ end
+ end
+
context 'attributes include a key with transform defined' do
before do
Fabrication::Transform.define(:name, lambda {|value| value.reverse})
File renamed without changes.
@@ -22,6 +22,23 @@ Feature: Active Record Objects
And I should see the following division in the database:
| name | Yellow Squadron |
+ Scenario: a single object with transform to apply
+ Given the following company:
+ | name | Widgets Inc |
+ Given the following division:
+ | name | Southwest |
+ | company | Widgets Inc |
+ Then that division should reference that company
+
+ Scenario: multiple objects with transform to apply
+ Given the following company:
+ | name | Widgets Inc |
+ Given the following divisions:
+ | name | company |
+ | Southwest | Widgets Inc |
+ | North | Widgets Inc |
+ Then they should reference that company
+
Scenario: a parented single generic object
Given 1 company
And that company has 1 division

0 comments on commit b0027ea

Please sign in to comment.