Module: ActiveRecord::Duplicate::ClassMethods

Defined in:
lib/activerecord-duplicate/duplicate.rb

Instance Method Summary collapse

Instance Method Details

#after_duplication(*args, &block) ⇒ Object



92
93
94
95
96
97
98
99
100
101
# File 'lib/activerecord-duplicate/duplicate.rb', line 92

def after_duplication(*args, &block)
  options = args.extract_options!
  
  options[:prepend] = true
  options[:if] = Array.wrap(options[:if])
  options[:if] << "!halted"
  options[:if].unshift("self.duplication_context == :#{options[:on]}") if options[:on]
  
  set_callback(:duplication, :after, *(args << options), &block)
end

#attr_duplicatable(*attributes) ⇒ Object



69
70
71
72
73
# File 'lib/activerecord-duplicate/duplicate.rb', line 69

def attr_duplicatable(*attributes)
  self._duplicatable_attributes ||= []
  self._duplicatable_attributes = _duplicatable_attributes | attributes.map(&:to_sym) if attributes.present?
  self._duplicatable_attributes
end

#attr_duplicatable?(attribute) ⇒ Boolean

Returns:

  • (Boolean)


75
76
77
78
# File 'lib/activerecord-duplicate/duplicate.rb', line 75

def attr_duplicatable?(attribute)
  attribute = attribute.to_sym
  attr_duplicatable.present? ? attr_duplicatable.include?(attribute) : primary_key.to_sym != attribute
end

#before_duplication(*args, &block) ⇒ Object

Duplicated from activemodel/lib/active_model/validations/callbacks.rb



81
82
83
84
85
86
87
88
89
90
# File 'lib/activerecord-duplicate/duplicate.rb', line 81

def before_duplication(*args, &block)
  options = args.last
  
  if options.is_a?(Hash) && options[:on]
    options[:if] = Array.wrap(options[:if])
    options[:if].unshift("self.duplication_context == :#{options[:on]}")
  end
  
  set_callback(:duplication, :before, *args, &block)
end