Module: ActiveRecord::Chemistry::Extendable::Relation::ClassMethods

Defined in:
lib/activerecord/chemistry/extendable/relation.rb

Instance Method Summary collapse

Instance Method Details

#extendable_by(name, scope = nil, options = {}) ⇒ Object



10
11
12
13
# File 'lib/activerecord/chemistry/extendable/relation.rb', line 10

def extendable_by(name, scope = nil, options = {})
  reflections = belongs_to(name, scope, options.merge(dependent: :destroy, optional: true))
  cattr_reader(:extended_by_reflection) { reflections.stringify_keys[name.to_s] }
end

#extends(name, scope = nil, options = {}) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/activerecord/chemistry/extendable/relation.rb', line 15

def extends(name, scope = nil, options = {})
  if Hash === scope
    options = scope
    scope = nil
  end
  options[:dependent] ||= :nullify
  reflections = has_one(name, scope, options)
  default_scope -> { includes(name) }
  validate :extendable_must_be_valid

  cattr_reader(:extendable_reflection) { reflections.stringify_keys[name.to_s] }
  cattr_reader(:extendable_model) { (options[:class_name] || name.to_s.camelize).constantize }
  class_eval "def #{name}; super || build_#{name}(extendable_model.extended_by_reflection.name => self); end"
  alias_method :extendable, name
  alias_method :extendable=, "#{name}=".to_sym
  include Extendable::InstanceMethods
end