Module: ConcernsOnRails::Models::Aliasable::ClassMethods

Defined in:
lib/concerns_on_rails/models/aliasable.rb

Instance Method Summary collapse

Instance Method Details

#alias_association(new_name, source_name, only: nil, except: nil, deprecated: nil, alias_foreign_key: false) ⇒ Object

Register ‘new_name` as a full alias of the existing association `source_name`. Argument order mirrors `alias_method new, old`. Callable many times; aliases of aliases collapse to the terminal source; re-declaring an existing alias WITH THE SAME SOURCE (the STI subclass path) refreshes its reflection in place instead of raising, while repointing it at a different source raises — that is almost always an accident, and the generated methods/reflection would silently change meaning. Options:

only:/except: — narrow the generated method map by group
  (:reader, :writer, :build, :reload, :ids); inapplicable groups
  are ignored, unknown ones raise.
deprecated: — true or a String hint; delegators warn through
  ConcernsOnRails.deprecator before delegating.
alias_foreign_key: — belongs_to only; alias_attribute the FK
  (<alias>_id, plus <alias>_type when polymorphic).


127
128
129
130
131
132
133
134
135
136
137
# File 'lib/concerns_on_rails/models/aliasable.rb', line 127

def alias_association(new_name, source_name, only: nil, except: nil, deprecated: nil, alias_foreign_key: false)
  new_name = new_name.to_sym
  source = aliasable_aliases[source_name.to_sym] || source_name.to_sym # collapse alias-of-alias
  aliasable_guard_repoint!(new_name, source)
  reflection = aliasable_validate!(new_name, source)
  aliasable_validate_foreign_key!(new_name, reflection) if alias_foreign_key
  aliasable_install(new_name, source, reflection,
                    groups: aliasable_method_groups(only, except),
                    deprecated: deprecated, alias_foreign_key: alias_foreign_key)
  new_name
end