challah 0.5.2 → 0.5.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (152) hide show
  1. data/CHANGELOG.md +9 -2
  2. data/README.md +18 -18
  3. data/lib/challah/test.rb +22 -5
  4. data/lib/challah/version.rb +1 -1
  5. data/test/audit_test.rb +32 -32
  6. data/test/controller_test.rb +9 -9
  7. data/test/cookie_store_test.rb +25 -25
  8. data/test/factories.rb +7 -7
  9. data/test/helper.rb +14 -12
  10. data/test/permission_test.rb +14 -14
  11. data/test/restrictions_controller_test.rb +29 -30
  12. data/test/role_test.rb +26 -26
  13. data/test/session_test.rb +65 -65
  14. data/test/sessions_controller_test.rb +13 -13
  15. data/test/simple_cookie_store_test.rb +25 -25
  16. data/test/user_test.rb +79 -79
  17. data/vendor/bundle/cache/factory_girl-3.0.0.gem +0 -0
  18. data/vendor/bundle/gems/factory_girl-3.0.0/Appraisals +11 -0
  19. data/vendor/bundle/gems/factory_girl-3.0.0/CONTRIBUTION_GUIDELINES.md +10 -0
  20. data/vendor/bundle/gems/factory_girl-3.0.0/Changelog +168 -0
  21. data/vendor/bundle/gems/factory_girl-3.0.0/GETTING_STARTED.md +735 -0
  22. data/vendor/bundle/gems/factory_girl-3.0.0/Gemfile +5 -0
  23. data/vendor/bundle/gems/factory_girl-3.0.0/Gemfile.lock +95 -0
  24. data/vendor/bundle/gems/factory_girl-3.0.0/LICENSE +19 -0
  25. data/vendor/bundle/gems/factory_girl-3.0.0/README.md +70 -0
  26. data/vendor/bundle/gems/factory_girl-3.0.0/Rakefile +38 -0
  27. data/vendor/bundle/gems/factory_girl-3.0.0/cucumber.yml +1 -0
  28. data/vendor/bundle/gems/factory_girl-3.0.0/factory_girl.gemspec +38 -0
  29. data/vendor/bundle/gems/factory_girl-3.0.0/features/factory_girl_steps.feature +237 -0
  30. data/vendor/bundle/gems/factory_girl-3.0.0/features/find_definitions.feature +75 -0
  31. data/vendor/bundle/gems/factory_girl-3.0.0/features/step_definitions/database_steps.rb +42 -0
  32. data/vendor/bundle/gems/factory_girl-3.0.0/features/step_definitions/factory_girl_steps.rb +43 -0
  33. data/vendor/bundle/gems/factory_girl-3.0.0/features/support/env.rb +11 -0
  34. data/vendor/bundle/gems/factory_girl-3.0.0/features/support/factories.rb +119 -0
  35. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/2.3.gemfile +7 -0
  36. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.0.gemfile +7 -0
  37. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.0.gemfile.lock +86 -0
  38. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.1.gemfile +7 -0
  39. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.1.gemfile.lock +95 -0
  40. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.2.gemfile +7 -0
  41. data/vendor/bundle/gems/factory_girl-3.0.0/gemfiles/3.2.gemfile.lock +93 -0
  42. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl.rb +69 -0
  43. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/aliases.rb +19 -0
  44. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute.rb +40 -0
  45. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/association.rb +23 -0
  46. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/dynamic.rb +20 -0
  47. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/sequence.rb +17 -0
  48. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute/static.rb +15 -0
  49. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_assigner.rb +73 -0
  50. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/attribute_list.rb +54 -0
  51. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/callback.rb +33 -0
  52. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration.rb +22 -0
  53. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/association.rb +25 -0
  54. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/dynamic.rb +25 -0
  55. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/implicit.rb +32 -0
  56. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration/static.rb +25 -0
  57. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/declaration_list.rb +48 -0
  58. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/definition.rb +76 -0
  59. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/definition_proxy.rb +159 -0
  60. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/errors.rb +19 -0
  61. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/evaluator.rb +76 -0
  62. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/evaluator_class_definer.rb +34 -0
  63. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/factory.rb +145 -0
  64. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/factory_runner.rb +24 -0
  65. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/find_definitions.rb +25 -0
  66. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/null_factory.rb +15 -0
  67. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/null_object.rb +7 -0
  68. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/registry.rb +46 -0
  69. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/reload.rb +8 -0
  70. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/sequence.rb +24 -0
  71. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/step_definitions.rb +130 -0
  72. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy.rb +32 -0
  73. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/attributes_for.rb +12 -0
  74. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/build.rb +15 -0
  75. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/create.rb +17 -0
  76. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy/stub.rb +61 -0
  77. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/strategy_calculator.rb +29 -0
  78. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax.rb +16 -0
  79. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/blueprint.rb +42 -0
  80. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/default.rb +56 -0
  81. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/generate.rb +70 -0
  82. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/make.rb +45 -0
  83. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/methods.rb +136 -0
  84. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/sham.rb +45 -0
  85. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/syntax/vintage.rb +130 -0
  86. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/trait.rb +29 -0
  87. data/vendor/bundle/gems/factory_girl-3.0.0/lib/factory_girl/version.rb +4 -0
  88. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/aliases_spec.rb +19 -0
  89. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attribute_aliases_spec.rb +45 -0
  90. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attribute_existing_on_object_spec.rb +68 -0
  91. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attributes_for_spec.rb +89 -0
  92. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attributes_from_instance_spec.rb +53 -0
  93. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/attributes_ordered_spec.rb +51 -0
  94. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/build_list_spec.rb +41 -0
  95. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/build_spec.rb +89 -0
  96. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/build_stubbed_spec.rb +104 -0
  97. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/callbacks_spec.rb +47 -0
  98. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/create_list_spec.rb +82 -0
  99. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/create_spec.rb +117 -0
  100. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/define_child_before_parent_spec.rb +21 -0
  101. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/definition_spec.rb +26 -0
  102. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/definition_without_block_spec.rb +15 -0
  103. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/initialize_with_spec.rb +147 -0
  104. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/modify_factories_spec.rb +184 -0
  105. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/modify_inherited_spec.rb +52 -0
  106. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/nested_attributes_spec.rb +32 -0
  107. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/overrides_spec.rb +61 -0
  108. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/parent_spec.rb +90 -0
  109. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/sequence_spec.rb +33 -0
  110. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/stub_spec.rb +62 -0
  111. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/blueprint_spec.rb +34 -0
  112. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/generate_spec.rb +59 -0
  113. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/make_spec.rb +52 -0
  114. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/sham_spec.rb +43 -0
  115. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/syntax/vintage_spec.rb +217 -0
  116. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/traits_spec.rb +421 -0
  117. data/vendor/bundle/gems/factory_girl-3.0.0/spec/acceptance/transient_attributes_spec.rb +124 -0
  118. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/aliases_spec.rb +31 -0
  119. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/association_spec.rb +28 -0
  120. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/dynamic_spec.rb +52 -0
  121. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/sequence_spec.rb +16 -0
  122. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute/static_spec.rb +19 -0
  123. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute_list_spec.rb +78 -0
  124. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/attribute_spec.rb +16 -0
  125. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/callback_spec.rb +41 -0
  126. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/declaration/implicit_spec.rb +25 -0
  127. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/declaration_list_spec.rb +71 -0
  128. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/definition_proxy_spec.rb +197 -0
  129. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/definition_spec.rb +104 -0
  130. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/evaluator_class_definer_spec.rb +54 -0
  131. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/factory_spec.rb +279 -0
  132. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/find_definitions_spec.rb +110 -0
  133. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/null_factory_spec.rb +13 -0
  134. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/null_object_spec.rb +8 -0
  135. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/registry_spec.rb +81 -0
  136. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/sequence_spec.rb +47 -0
  137. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/attributes_for_spec.rb +18 -0
  138. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/build_spec.rb +7 -0
  139. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/create_spec.rb +13 -0
  140. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy/stub_spec.rb +40 -0
  141. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy_calculator_spec.rb +33 -0
  142. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl/strategy_spec.rb +21 -0
  143. data/vendor/bundle/gems/factory_girl-3.0.0/spec/factory_girl_spec.rb +22 -0
  144. data/vendor/bundle/gems/factory_girl-3.0.0/spec/spec_helper.rb +26 -0
  145. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/macros/define_constant.rb +86 -0
  146. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/callback.rb +9 -0
  147. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/declaration.rb +71 -0
  148. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/delegate.rb +44 -0
  149. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/matchers/trait.rb +9 -0
  150. data/vendor/bundle/gems/factory_girl-3.0.0/spec/support/shared_examples/strategy.rb +104 -0
  151. data/vendor/bundle/specifications/factory_girl-3.0.0.gemspec +62 -0
  152. metadata +147 -12
@@ -0,0 +1,19 @@
1
+ module FactoryGirl
2
+ # Raised when a factory is defined that attempts to instantiate itself.
3
+ class AssociationDefinitionError < RuntimeError; end
4
+
5
+ # Raised when a callback is defined that has an invalid name
6
+ class InvalidCallbackNameError < RuntimeError; end
7
+
8
+ # Raised when a factory is defined with the same name as a previously-defined factory.
9
+ class DuplicateDefinitionError < RuntimeError; end
10
+
11
+ # Raised when attempting to register a sequence from a dynamic attribute block
12
+ class SequenceAbuseError < RuntimeError; end
13
+
14
+ # Raised when defining an invalid attribute:
15
+ # * Defining an attribute which has a name ending in "="
16
+ # * Defining an attribute with both a static and lazy value
17
+ # * Defining an attribute twice in the same factory
18
+ class AttributeDefinitionError < RuntimeError; end
19
+ end
@@ -0,0 +1,76 @@
1
+ require "active_support/core_ext/hash/except"
2
+ require "active_support/core_ext/class/attribute"
3
+
4
+ module FactoryGirl
5
+ class Evaluator
6
+ class_attribute :callbacks, :attribute_lists
7
+
8
+ def self.attribute_list
9
+ AttributeList.new.tap do |list|
10
+ attribute_lists.each do |attribute_list|
11
+ list.apply_attributes attribute_list.to_a
12
+ end
13
+ end
14
+ end
15
+
16
+ private_instance_methods.each do |method|
17
+ undef_method(method) unless method =~ /^__|initialize/
18
+ end
19
+
20
+ def initialize(build_strategy, overrides = {})
21
+ @build_strategy = build_strategy
22
+ @overrides = overrides
23
+ @cached_attributes = overrides
24
+
25
+ singleton = class << self; self end
26
+ @overrides.each do |name, value|
27
+ singleton.send :define_method, name, lambda { value }
28
+ end
29
+
30
+ @build_strategy.add_observer(CallbackRunner.new(self.class.callbacks, self))
31
+ end
32
+
33
+ def association(factory_name, overrides = {})
34
+ build_strategy = if overrides.has_key?(:strategy)
35
+ overrides[:strategy]
36
+ else
37
+ Strategy::Create
38
+ end
39
+
40
+ build_strategy = StrategyCalculator.new(build_strategy).strategy
41
+ runner = FactoryRunner.new(factory_name, build_strategy, [overrides.except(:strategy)])
42
+ @build_strategy.association(runner)
43
+ end
44
+
45
+ def instance=(object_instance)
46
+ @instance = object_instance
47
+ end
48
+
49
+ def method_missing(method_name, *args, &block)
50
+ if @cached_attributes.key?(method_name)
51
+ @cached_attributes[method_name]
52
+ else
53
+ @instance.send(method_name, *args, &block)
54
+ end
55
+ end
56
+
57
+ def __overrides
58
+ @overrides
59
+ end
60
+
61
+ private
62
+
63
+ class CallbackRunner
64
+ def initialize(callbacks, evaluator)
65
+ @callbacks = callbacks
66
+ @evaluator = evaluator
67
+ end
68
+
69
+ def update(name, result_instance)
70
+ @callbacks.select {|callback| callback.name == name }.each do |callback|
71
+ callback.run(result_instance, @evaluator)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
@@ -0,0 +1,34 @@
1
+ module FactoryGirl
2
+ class EvaluatorClassDefiner
3
+ def initialize(attributes, callbacks, parent_class)
4
+ @parent_class = parent_class
5
+ @callbacks = callbacks
6
+ @attributes = attributes
7
+
8
+ attributes.each do |attribute|
9
+ define_attribute(attribute.name, attribute.to_proc)
10
+ end
11
+ end
12
+
13
+ def evaluator_class
14
+ @evaluator_class ||= Class.new(@parent_class).tap do |klass|
15
+ klass.callbacks ||= []
16
+ klass.callbacks += @callbacks
17
+ klass.attribute_lists ||= []
18
+ klass.attribute_lists += [@attributes]
19
+ end
20
+ end
21
+
22
+ private
23
+
24
+ def define_attribute(attribute_name, attribute_proc)
25
+ evaluator_class.send(:define_method, attribute_name) do
26
+ if @cached_attributes.key?(attribute_name)
27
+ @cached_attributes[attribute_name]
28
+ else
29
+ @cached_attributes[attribute_name] = instance_exec(&attribute_proc)
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,145 @@
1
+ require "active_support/core_ext/hash/keys"
2
+ require "active_support/inflector"
3
+
4
+ module FactoryGirl
5
+ class Factory
6
+ attr_reader :name, :definition #:nodoc:
7
+
8
+ def initialize(name, options = {}) #:nodoc:
9
+ assert_valid_options(options)
10
+ @name = name.is_a?(Symbol) ? name : name.to_s.underscore.to_sym
11
+ @parent = options[:parent]
12
+ @aliases = options[:aliases] || []
13
+ @class_name = options[:class]
14
+ @definition = Definition.new(@name, options[:traits] || [])
15
+ @compiled = false
16
+ end
17
+
18
+ delegate :add_callback, :declare_attribute, :to_create, :define_trait,
19
+ :defined_traits, :inherit_traits, :processing_order, to: :@definition
20
+
21
+ def build_class #:nodoc:
22
+ @build_class ||= if class_name.is_a? Class
23
+ class_name
24
+ else
25
+ class_name.to_s.camelize.constantize
26
+ end
27
+ end
28
+
29
+ def run(strategy_class, overrides, &block) #:nodoc:
30
+ block ||= lambda {|result| result }
31
+ compile
32
+
33
+ strategy = strategy_class.new
34
+
35
+ evaluator = evaluator_class.new(strategy, overrides.symbolize_keys)
36
+ attribute_assigner = AttributeAssigner.new(evaluator, &instance_builder)
37
+
38
+ strategy.result(attribute_assigner, to_create).tap(&block)
39
+ end
40
+
41
+ def human_names
42
+ names.map {|name| name.to_s.humanize.downcase }
43
+ end
44
+
45
+ def associations
46
+ evaluator_class.attribute_list.associations
47
+ end
48
+
49
+ # Names for this factory, including aliases.
50
+ #
51
+ # Example:
52
+ #
53
+ # factory :user, aliases: [:author] do
54
+ # # ...
55
+ # end
56
+ #
57
+ # FactoryGirl.create(:author).class
58
+ # # => User
59
+ #
60
+ # Because an attribute defined without a value or block will build an
61
+ # association with the same name, this allows associations to be defined
62
+ # without factories, such as:
63
+ #
64
+ # factory :user, aliases: [:author] do
65
+ # # ...
66
+ # end
67
+ #
68
+ # factory :post do
69
+ # author
70
+ # end
71
+ #
72
+ # FactoryGirl.create(:post).author.class
73
+ # # => User
74
+ def names
75
+ [name] + @aliases
76
+ end
77
+
78
+ def compile
79
+ unless @compiled
80
+ parent.compile
81
+ parent.defined_traits.each {|trait| define_trait(trait) }
82
+ @definition.compile
83
+ @compiled = true
84
+ end
85
+ end
86
+
87
+ def with_traits(traits)
88
+ self.clone.tap do |factory_with_traits|
89
+ factory_with_traits.inherit_traits traits
90
+ end
91
+ end
92
+
93
+ protected
94
+
95
+ def class_name #:nodoc:
96
+ @class_name || parent.class_name || name
97
+ end
98
+
99
+ def evaluator_class
100
+ @evaluator_class ||= EvaluatorClassDefiner.new(attributes, callbacks, parent.evaluator_class).evaluator_class
101
+ end
102
+
103
+ def attributes
104
+ compile
105
+ AttributeList.new(@name).tap do |list|
106
+ processing_order.each do |factory|
107
+ list.apply_attributes factory.attributes
108
+ end
109
+ end
110
+ end
111
+
112
+ def callbacks
113
+ processing_order.map {|factory| factory.callbacks }.flatten
114
+ end
115
+
116
+ def constructor
117
+ @constructor ||= @definition.constructor || parent.constructor
118
+ end
119
+
120
+ private
121
+
122
+ def assert_valid_options(options)
123
+ options.assert_valid_keys(:class, :parent, :aliases, :traits)
124
+ end
125
+
126
+ def parent
127
+ if @parent
128
+ FactoryGirl.factory_by_name(@parent)
129
+ else
130
+ NullFactory.new
131
+ end
132
+ end
133
+
134
+ def instance_builder
135
+ build_class = self.build_class
136
+ constructor || lambda { build_class.new }
137
+ end
138
+
139
+ def initialize_copy(source)
140
+ super
141
+ @definition = @definition.clone
142
+ @evaluator_class = nil
143
+ end
144
+ end
145
+ end
@@ -0,0 +1,24 @@
1
+ module FactoryGirl
2
+ class FactoryRunner
3
+ def initialize(name, strategy, traits_and_overrides)
4
+ @name = name
5
+ @strategy = strategy
6
+
7
+ @overrides = traits_and_overrides.extract_options!
8
+ @traits = traits_and_overrides
9
+ end
10
+
11
+ def run(strategy_override = nil, &block)
12
+ strategy_override ||= @strategy
13
+ factory = FactoryGirl.factory_by_name(@name)
14
+
15
+ factory.compile
16
+
17
+ if @traits.any?
18
+ factory = factory.with_traits(@traits)
19
+ end
20
+
21
+ factory.run(strategy_override, @overrides, &block)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,25 @@
1
+ module FactoryGirl
2
+
3
+ class << self
4
+ # An Array of strings specifying locations that should be searched for
5
+ # factory definitions. By default, factory_girl will attempt to require
6
+ # "factories," "test/factories," and "spec/factories." Only the first
7
+ # existing file will be loaded.
8
+ attr_accessor :definition_file_paths
9
+ end
10
+ self.definition_file_paths = %w(factories test/factories spec/factories)
11
+
12
+ def self.find_definitions #:nodoc:
13
+ absolute_definition_file_paths = definition_file_paths.map {|path| File.expand_path(path) }
14
+
15
+ absolute_definition_file_paths.uniq.each do |path|
16
+ load("#{path}.rb") if File.exists?("#{path}.rb")
17
+
18
+ if File.directory? path
19
+ Dir[File.join(path, '**', '*.rb')].sort.each do |file|
20
+ load file
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,15 @@
1
+ module FactoryGirl
2
+ class NullFactory
3
+ attr_reader :definition
4
+
5
+ def initialize
6
+ @definition = Definition.new
7
+ end
8
+
9
+ delegate :defined_traits, :callbacks, :attributes, :constructor, to: :definition
10
+
11
+ def compile; end
12
+ def class_name; end
13
+ def evaluator_class; FactoryGirl::Evaluator; end
14
+ end
15
+ end
@@ -0,0 +1,7 @@
1
+ module FactoryGirl
2
+ class NullObject < ::BasicObject
3
+ def method_missing(*args)
4
+ nil
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,46 @@
1
+ module FactoryGirl
2
+ class Registry
3
+ include Enumerable
4
+
5
+ def initialize(name)
6
+ @name = name
7
+ @items = {}
8
+ end
9
+
10
+ def add(item)
11
+ item.names.each { |name| add_as(name, item) }
12
+ item
13
+ end
14
+
15
+ def find(name)
16
+ @items[name.to_sym] or raise ArgumentError.new("#{@name} not registered: #{name.to_s}")
17
+ end
18
+
19
+ def each(&block)
20
+ @items.values.uniq.each(&block)
21
+ end
22
+
23
+ def [](name)
24
+ find(name)
25
+ end
26
+
27
+ def registered?(name)
28
+ @items.key?(name.to_sym)
29
+ end
30
+
31
+ def clear
32
+ @items.clear
33
+ end
34
+
35
+ private
36
+
37
+ def add_as(name, item)
38
+ if registered?(name)
39
+ raise DuplicateDefinitionError, "#{@name} already registered: #{name}"
40
+ else
41
+ @items[name.to_sym] = item
42
+ end
43
+ end
44
+ end
45
+ end
46
+
@@ -0,0 +1,8 @@
1
+ module FactoryGirl
2
+ def self.reload
3
+ self.factories.clear
4
+ self.sequences.clear
5
+ self.traits.clear
6
+ self.find_definitions
7
+ end
8
+ end
@@ -0,0 +1,24 @@
1
+ module FactoryGirl
2
+
3
+ # Sequences are defined using sequence within a FactoryGirl.define block.
4
+ # Sequence values are generated using next.
5
+ class Sequence
6
+ attr_reader :name
7
+
8
+ def initialize(name, value = 1, &proc) #:nodoc:
9
+ @name = name
10
+ @proc = proc
11
+ @value = value
12
+ end
13
+
14
+ def next
15
+ @proc ? @proc.call(@value) : @value
16
+ ensure
17
+ @value = @value.next
18
+ end
19
+
20
+ def names
21
+ [@name]
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,130 @@
1
+ module FactoryGirlStepHelpers
2
+ def convert_human_hash_to_attribute_hash(human_hash, associations = [])
3
+ HumanHashToAttributeHash.new(human_hash, associations).attributes
4
+ end
5
+
6
+ class HumanHashToAttributeHash
7
+ attr_reader :associations
8
+
9
+ def initialize(human_hash, associations)
10
+ @human_hash = human_hash
11
+ @associations = associations
12
+ end
13
+
14
+ def attributes(strategy = CreateAttributes)
15
+ @human_hash.inject({}) do |attribute_hash, (human_key, value)|
16
+ attributes = strategy.new(self, *process_key_value(human_key, value))
17
+ attribute_hash.merge({ attributes.key => attributes.value })
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def process_key_value(key, value)
24
+ value = value.strip if value.is_a?(String)
25
+ [key.downcase.gsub(' ', '_').to_sym, value]
26
+ end
27
+
28
+ class AssociationManager
29
+ def initialize(human_hash_to_attributes_hash, key, value)
30
+ @human_hash_to_attributes_hash = human_hash_to_attributes_hash
31
+ @key = key
32
+ @value = value
33
+ end
34
+
35
+ def association
36
+ @human_hash_to_attributes_hash.associations.detect {|association| association.name == @key }
37
+ end
38
+
39
+ def association_instance
40
+ return unless association
41
+
42
+ if attributes_hash = nested_attribute_hash
43
+ factory.build_class.first(conditions: attributes_hash.attributes(FindAttributes)) or
44
+ FactoryGirl.create(association.factory, attributes_hash.attributes)
45
+ end
46
+ end
47
+
48
+ private
49
+
50
+ def factory
51
+ FactoryGirl.factory_by_name(association.factory)
52
+ end
53
+
54
+ def nested_attribute_hash
55
+ attribute, value = @value.split(':', 2)
56
+ return if value.blank?
57
+
58
+ HumanHashToAttributeHash.new({ attribute => value }, factory.associations)
59
+ end
60
+ end
61
+
62
+ class AttributeStrategy
63
+ attr_reader :key, :value, :association_manager
64
+
65
+ def initialize(human_hash_to_attributes_hash, key, value)
66
+ @association_manager = AssociationManager.new(human_hash_to_attributes_hash, key, value)
67
+ @key = key
68
+ @value = value
69
+ end
70
+ end
71
+
72
+ class FindAttributes < AttributeStrategy
73
+ def initialize(human_hash_to_attributes_hash, key, value)
74
+ super
75
+
76
+ if association_manager.association
77
+ @key = "#{@key}_id"
78
+ @value = association_manager.association_instance.try(:id)
79
+ end
80
+ end
81
+ end
82
+
83
+ class CreateAttributes < AttributeStrategy
84
+ def initialize(human_hash_to_attributes_hash, key, value)
85
+ super
86
+
87
+ if association_manager.association
88
+ @value = association_manager.association_instance
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
94
+
95
+ World(FactoryGirlStepHelpers)
96
+
97
+ FactoryGirl.factories.each do |factory|
98
+ factory.compile
99
+ factory.human_names.each do |human_name|
100
+ Given /^the following (?:#{human_name}|#{human_name.pluralize}) exists?:?$/i do |table|
101
+ table.hashes.each do |human_hash|
102
+ attributes = convert_human_hash_to_attribute_hash(human_hash, factory.associations)
103
+ FactoryGirl.create(factory.name, attributes)
104
+ end
105
+ end
106
+
107
+ Given /^an? #{human_name} exists$/i do
108
+ FactoryGirl.create(factory.name)
109
+ end
110
+
111
+ Given /^(\d+) #{human_name.pluralize} exist$/i do |count|
112
+ FactoryGirl.create_list(factory.name, count.to_i)
113
+ end
114
+
115
+ if factory.build_class.respond_to?(:columns)
116
+ factory.build_class.columns.each do |column|
117
+ name = column.respond_to?(:name) ? column.name : column.to_s
118
+ human_column_name = name.downcase.gsub('_', ' ')
119
+ Given /^an? #{human_name} exists with an? #{human_column_name} of "([^"]*)"$/i do |value|
120
+ FactoryGirl.create(factory.name, name => value)
121
+ end
122
+
123
+ Given /^(\d+) #{human_name.pluralize} exist with an? #{human_column_name} of "([^"]*)"$/i do |count, value|
124
+ FactoryGirl.create_list(factory.name, count.to_i, name => value)
125
+ end
126
+ end
127
+ end
128
+ end
129
+ end
130
+