Class: Pickle::Adapter::Dupe

Inherits:
Pickle::Adapter show all
Defined in:
lib/pickle_dupe/adapter.rb

Overview

dupe adapter

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model) ⇒ Dupe

Returns a new instance of Dupe.



19
20
21
22
23
24
25
26
27
28
# File 'lib/pickle_dupe/adapter.rb', line 19

def initialize(model)
  # Not all dupe models has an associated model class
  # so we dynamically create them here
  unless @klass = (model.name.to_s.classify.constantize rescue nil)
    class_handler = Object.const_set(model.name.to_s.classify, Class.new(ActiveResource::Base))
    class_handler.site = 'http://test.com'
    puts "WARNING: #{model.name} model is defined dynamically because there was a dupe definition but no model was defined"
  end
  @name = model.name.to_s
end

Class Method Details

.factoriesObject



15
16
17
# File 'lib/pickle_dupe/adapter.rb', line 15

def self.factories
  (::Dupe.models.values rescue []).map {|model| new(model)}
end

Instance Method Details

#create(attrs = {}) ⇒ Object



30
31
32
33
34
35
# File 'lib/pickle_dupe/adapter.rb', line 30

def create(attrs = {})
  duped_object = ::Dupe.create(@name, attrs)
  assign_missing_associations(duped_object, attrs) unless attrs.blank?

  return @klass.send(:find, duped_object.id)
end