Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
  def model_factory_helper(calling_spec, model_class, options)
    raise "factory_method requires default_attributes" unless options.has_key?(:default_attributes)
    raise "factory_method default_attributes must be a Hash" unless options[:default_attributes].is_a?(Hash)

    proper_class_name = model_class.name
    lowered_class_name = model_class.name.downcase

    calling_spec.instance_eval <<-"end;"
      module #{proper_class_name}HelperFactory
        
        def #{lowered_class_name}_default_attributes(options = {})
          #{options[:default_attributes].inspect}.merge(options)
        end
        
        def #{lowered_class_name}_with(options = {})
          options = {} if options == :all
          #{proper_class_name}.new(#{lowered_class_name}_default_attributes(options))
        end
        
        def #{lowered_class_name}_without(field)
          #{proper_class_name}.new(#{lowered_class_name}_default_attributes(field => nil))
        end
        
        def it_should_require_fields(*fields)
          fields.each do |field|
            it "should require \#{field}" do
              #{lowered_class_name}_without(field).should_not be_valid
            end
          end
        end        
      end
    
      include #{proper_class_name}HelperFactory
    end;
  end