Class: Alchemy::Custom::Model::Cloner

Inherits:
ActiveType::Object
  • Object
show all
Defined in:
app/models/alchemy/custom/model/cloner.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cloner_of(name, scope = nil, options = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
# File 'app/models/alchemy/custom/model/cloner.rb', line 38

def cloner_of(name,scope = nil, options={})
  class_attribute :to_cloner_name
  name = name.to_sym
  if options[:foreign_key]
    attribute options[:foreign_key]
  else
    attribute :"#{name}_id"
  end
  belongs_to name, scope, options
  self.to_cloner_name = name
end

Instance Method Details

#applyObject

ritorna true se il clone è andato a buon fine false altrimenti



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/models/alchemy/custom/model/cloner.rb', line 17

def apply
  if valid?
    cloned = self.send(self.to_cloner_name).clone_to_other_lang(get_attributes_for_clone.with_indifferent_access)
    if cloned.valid?
      true
    else
      cloned.errors.full_messages.each do | error|
        self.errors.add(:base, error)
      end
      false
    end
  else
    false
  end
end