Sha256: aaee03bae7e8a747b397e54d74771047f6ebf105d44cf32ad3f83ef44e26ad9c

Contents?: true

Size: 1.77 KB

Versions: 20

Compression:

Stored size: 1.77 KB

Contents

module Factory
  # Build actions for the model
  def self.build(model, &block)
    name = model.to_s.underscore
    
    define_method("#{name}_attributes", block)
    define_method("valid_#{name}_attributes") {|*args| valid_attributes_for(model, *args)}
    define_method("new_#{name}")              {|*args| new_record(model, *args)}
    define_method("create_#{name}")           {|*args| create_record(model, *args)}
  end
  
  # Get valid attributes for the model
  def valid_attributes_for(model, attributes = {})
    name = model.to_s.underscore
    send("#{name}_attributes", attributes)
    attributes
  end
  
  # Build an unsaved record
  def new_record(model, *args)
    model.new(valid_attributes_for(model, *args))
  end
  
  # Build and save/reload a record
  def create_record(model, *args)
    record = new_record(model, *args)
    record.save!
    record.reload
    record
  end
  
  build AutoShop do |attributes|
    attributes.reverse_merge!(
      :name => "Joe's Auto Body",
      :num_customers => 0
    )
  end
  
  build Car do |attributes|
    valid_vehicle_attributes(attributes)
  end
  
  build Highway do |attributes|
    attributes.reverse_merge!(
      :name => 'Route 66'
    )
  end
  
  build Motorcycle do |attributes|
    valid_car_attributes(attributes)
  end
  
  build Switch do |attributes|
    attributes.reverse_merge!(
      :state => 'off'
    )
  end
  
  build ToggleSwitch do |attributes|
    attributes.reverse_merge!(
      :state => 'off'
    )
  end
  
  build Vehicle do |attributes|
    attributes[:highway] = create_highway unless attributes.include?(:highway)
    attributes[:auto_shop] = create_auto_shop unless attributes.include?(:auto_shop)
    attributes.reverse_merge!(
      :seatbelt_on => false,
      :insurance_premium => 50
    )
  end
end

Version data entries

20 entries across 20 versions & 3 rubygems

Version Path
spree-enriquez-0.9.4 vendor/plugins/state_machine/test/factory.rb
spree-0.9.4 vendor/plugins/state_machine/test/factory.rb
spree-0.9.3 vendor/plugins/state_machine/test/factory.rb
spree-0.9.2 vendor/plugins/state_machine/test/factory.rb
spree-0.9.1 vendor/plugins/state_machine/test/factory.rb
spree-0.9.0 vendor/plugins/state_machine/test/factory.rb
spree-0.8.4 vendor/plugins/state_machine/test/factory.rb
spree-0.8.5 vendor/plugins/state_machine/test/factory.rb
spree-0.4.0 vendor/plugins/state_machine/test/factory.rb
spree-0.4.1 vendor/plugins/state_machine/test/factory.rb
spree-0.5.0 vendor/plugins/state_machine/test/factory.rb
spree-0.6.0 vendor/plugins/state_machine/test/factory.rb
spree-0.5.1 vendor/plugins/state_machine/test/factory.rb
spree-0.7.1 vendor/plugins/state_machine/test/factory.rb
spree-0.7.0 vendor/plugins/state_machine/test/factory.rb
spree-0.8.0 vendor/plugins/state_machine/test/factory.rb
spree-0.8.1 vendor/plugins/state_machine/test/factory.rb
spree-0.8.2 vendor/plugins/state_machine/test/factory.rb
spree-0.8.3 vendor/plugins/state_machine/test/factory.rb
state_machine-0.3.0 test/factory.rb