Module: ActiveType::ChangeAssociation::ClassMethods

Defined in:
lib/active_type/change_association.rb

Instance Method Summary collapse

Instance Method Details

#change_association(association_name, new_scope, new_options = {}) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/active_type/change_association.rb', line 9

def change_association(association_name, new_scope, new_options = {})
  if (existing_association = self.reflect_on_association(association_name))
    if new_scope.is_a?(Hash)
      new_options = new_scope
      new_scope = nil
    end
    original_options = existing_association.options
    if ActiveRecord::VERSION::MAJOR > 3
      new_scope ||= existing_association.scope
      public_send(existing_association.macro, association_name, new_scope, **original_options.merge(new_options))
    else
      public_send(existing_association.macro, association_name, **original_options.merge(new_options))
    end
  else
    raise ArgumentError, "unrecognized association `#{association_name}`"
  end
end