fabrication 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -44,6 +44,32 @@ They are really easy to configure! Just add this to your `config/application.rb`
44
44
  g.fixture_replacement :fabrication, :dir => "spec/fabricators"
45
45
  end
46
46
 
47
+ ### Cucumber Steps ###
48
+
49
+ Packaged with the gem is a generator which will load some handy cucumber table steps into your step_definitions folder. You can get them by running `rails g fabrication:cucumber_steps`.
50
+
51
+ To generating a single "widget" object (expecting a Fabricator(:widget) to be defined):
52
+
53
+ Given the following widget:
54
+ | name | widget_1 |
55
+ | color | red |
56
+
57
+ To generate multiple "widget" objects:
58
+
59
+ Given the following widgets:
60
+ | name | color |
61
+ | widget_1 | red |
62
+ | widget_2 | blue |
63
+ ...
64
+
65
+ To generate "wockets" nested within "widget" objects:
66
+
67
+ And that widget has the following wocket:
68
+ | title | Amazing |
69
+ | category | fancy |
70
+
71
+ That will use the most recently defined "widget" and pass it into the Fabricator. That obviously requires your "wocket" to have a setter for a "widget".
72
+
47
73
  ### Usage ###
48
74
 
49
75
  Define your fabricators.
@@ -1,3 +1,3 @@
1
1
  module Fabrication
2
- VERSION = '0.9.3'
2
+ VERSION = '0.9.4'
3
3
  end
@@ -0,0 +1,15 @@
1
+ require 'rails/generators/base'
2
+
3
+ module Fabrication::Generators
4
+ class CucumberStepsGenerator < Rails::Generators::Base
5
+
6
+ def generate
7
+ template 'fabrication_steps.rb', "features/step_definitions/fabrication_steps.rb"
8
+ end
9
+
10
+ def self.source_root
11
+ @_fabrication_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
12
+ end
13
+
14
+ end
15
+ end
@@ -0,0 +1,54 @@
1
+ module FabricationMethods
2
+ def create_from_table(model_name, table, extra = {})
3
+ fabricator_name = model_name.gsub(/\W+/, '_').downcase.singularize.to_sym
4
+ is_singular = model_name.to_s.singularize == model_name.to_s
5
+ hashes = if is_singular
6
+ [table.rows_hash]
7
+ else
8
+ table.hashes
9
+ end
10
+ @they = hashes.map do |hash|
11
+ hash = hash.merge(extra).inject({}) {|h,(k,v)| h.update(k.gsub(/\W+/,'_').to_sym => v)}
12
+ object = Fabricate.build(fabricator_name, hash)
13
+ yield object if block_given?
14
+ object.save!
15
+ object
16
+ end
17
+ if is_singular
18
+ @it = @they.last
19
+ instance_variable_set("@#{fabricator_name}", @it)
20
+ end
21
+ end
22
+ end
23
+
24
+ World(FabricationMethods)
25
+
26
+ Given %r{^the following (.+):$} do |model_name, table|
27
+ create_from_table(model_name, table)
28
+ end
29
+
30
+ Given %r{^that (.+) has the following (.+):$} do |parent, child, table|
31
+ child= child.gsub(/\W+/,'_')
32
+ is_child_plural = child.pluralize == child
33
+ parent = parent.gsub(/\W+/,'_').downcase.sub(/^_/, '')
34
+ parent_instance = instance_variable_get("@#{parent}")
35
+ parent_class = parent_instance.class
36
+ if assoc = parent_class.reflect_on_association(child.to_sym) || parent_class.reflect_on_association(child.pluralize.to_sym)
37
+ parent = (assoc.options[:as] || parent).to_s
38
+ child = (assoc.options[:class_name] || assoc.options[:source] || child).to_s
39
+ # source will always be singular, repluralize based on original argument
40
+ child = child.pluralize if is_child_plural
41
+ end
42
+ if child.classify.constantize.method_defined?(parent.pluralize)
43
+ create_from_table(child, table, parent.pluralize => [parent_instance])
44
+ elsif child.classify.constantize.method_defined?(parent)
45
+ create_from_table(child, table, parent => parent_instance)
46
+ else
47
+ create_from_table(child, table)
48
+ if assoc.macro == :has_many
49
+ parent_instance.send("#{assoc.name}=", @they)
50
+ else
51
+ parent_instance.send("#{assoc.name}=", @they.first)
52
+ end
53
+ end
54
+ end
@@ -1,15 +1,17 @@
1
- require 'rails/generators/fabrication_generator'
1
+ require 'rails/generators/named_base'
2
2
 
3
- module Fabrication
4
- module Generators
5
- class ModelGenerator < Base
6
- argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
7
- class_option :dir, :type => :string, :default => "spec/fabricators", :desc => "The directory where the fabricators should go"
8
- class_option :extension, :type => :string, :default => "rb", :desc => "file extension name"
3
+ module Fabrication::Generators
4
+ class ModelGenerator < Rails::Generators::NamedBase
5
+ argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
6
+ class_option :dir, :type => :string, :default => "spec/fabricators", :desc => "The directory where the fabricators should go"
7
+ class_option :extension, :type => :string, :default => "rb", :desc => "file extension name"
9
8
 
10
- def create_fabrication_file
11
- template 'fabricator.rb', File.join(options[:dir], "#{singular_table_name}_fabricator.#{options[:extension].to_s}")
12
- end
9
+ def create_fabrication_file
10
+ template 'fabricator.rb', File.join(options[:dir], "#{singular_table_name}_fabricator.#{options[:extension].to_s}")
11
+ end
12
+
13
+ def self.source_root
14
+ @_fabrication_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'templates'))
13
15
  end
14
16
  end
15
17
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 9
8
- - 3
9
- version: 0.9.3
8
+ - 4
9
+ version: 0.9.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Paul Elliott
@@ -14,7 +14,7 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-12-01 00:00:00 -05:00
17
+ date: 2010-12-02 00:00:00 -05:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -27,9 +27,9 @@ dependencies:
27
27
  - !ruby/object:Gem::Version
28
28
  segments:
29
29
  - 2
30
- - 1
30
+ - 2
31
31
  - 0
32
- version: 2.1.0
32
+ version: 2.2.0
33
33
  type: :development
34
34
  version_requirements: *id001
35
35
  - !ruby/object:Gem::Dependency
@@ -58,8 +58,8 @@ dependencies:
58
58
  segments:
59
59
  - 3
60
60
  - 0
61
- - 1
62
- version: 3.0.1
61
+ - 3
62
+ version: 3.0.3
63
63
  type: :development
64
64
  version_requirements: *id003
65
65
  - !ruby/object:Gem::Dependency
@@ -68,7 +68,7 @@ dependencies:
68
68
  requirement: &id004 !ruby/object:Gem::Requirement
69
69
  none: false
70
70
  requirements:
71
- - - ">="
71
+ - - "="
72
72
  - !ruby/object:Gem::Version
73
73
  segments:
74
74
  - 1
@@ -88,8 +88,8 @@ dependencies:
88
88
  segments:
89
89
  - 1
90
90
  - 1
91
- - 2
92
- version: 1.1.2
91
+ - 4
92
+ version: 1.1.4
93
93
  type: :development
94
94
  version_requirements: *id005
95
95
  - !ruby/object:Gem::Dependency
@@ -148,9 +148,10 @@ files:
148
148
  - lib/fabrication/support.rb
149
149
  - lib/fabrication/version.rb
150
150
  - lib/fabrication.rb
151
+ - lib/rails/generators/fabrication/cucumber_steps/cucumber_steps_generator.rb
152
+ - lib/rails/generators/fabrication/cucumber_steps/templates/fabrication_steps.rb
151
153
  - lib/rails/generators/fabrication/model/model_generator.rb
152
154
  - lib/rails/generators/fabrication/model/templates/fabricator.rb
153
- - lib/rails/generators/fabrication_generator.rb
154
155
  - spec/fabrication/attribute_spec.rb
155
156
  - spec/fabrication/fabricator_spec.rb
156
157
  - spec/fabrication/generator/active_record_spec.rb
@@ -1,11 +0,0 @@
1
- require 'rails/generators/named_base'
2
-
3
- module Fabrication
4
- module Generators
5
- class Base < Rails::Generators::NamedBase #:nodoc:
6
- def self.source_root
7
- @_fabrication_source_root ||= File.expand_path(File.join(File.dirname(__FILE__), 'fabrication', generator_name, 'templates'))
8
- end
9
- end
10
- end
11
- end