Class: Factory

Inherits:
Object
  • Object
show all
Defined in:
lib/active_resource_test_helper/factory.rb

Overview

Adds a new type of factory to factory_girl.

Class Method Summary collapse

Class Method Details

.define_active_resource_factory(name, options = {}, &definition) ⇒ Object

Define a new factory to use inside of ActiveRecord tests. Uses the same syntax of factory_girl.



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
# File 'lib/active_resource_test_helper/factory.rb', line 7

def self.define_active_resource_factory name, options = {}, &definition
  raise "no block given" unless block_given?

  Factory.define name, options.dup, &definition

  return if options.include? :parent
  return if options.include?(:class) and const_defined?(options[:class].to_s.camelcase)

  if options.include?(:class) and !const_defined?(options[:class].to_s.camelcase)
    model_class_name = options[:class].to_s
  else
    model_class_name = name.to_s.capitalize
  end

  Object.module_eval <<-EOE, __FILE__, __LINE__
    class #{model_class_name} < Ohm::Model
      index :id
      alias :save! :save
    end
  EOE

  const_get(model_class_name).class_eval do
    Factory.factories[name].attributes.each do |attr|
      attribute attr.name.to_sym
      index attr.name.to_sym
    end
  end
end