module ActiveRecord::Chemistry::Actable::Relation

Public Instance Methods

actable(name, scope = nil, options = {}) click to toggle source
# File lib/activerecord/chemistry/actable/relation.rb, line 31
def actable(name, scope = nil, options = {})
  reflections = has_one(name, scope, options.merge(dependent: :destroy))
  cattr_reader(:actable_reflection) { reflections.stringify_keys[name.to_s] }
end
acts_as(name, scope = nil, options = {}) click to toggle source
# File lib/activerecord/chemistry/actable/relation.rb, line 13
def acts_as(name, scope = nil, options = {})
  if Hash === scope
    options = scope
    scope = nil
  end
  options[:dependent] ||= :destroy
  reflections = belongs_to(name, scope, options)
  default_scope -> { includes(name) }
  validate :actable_must_be_valid

  cattr_reader(:acting_as_reflection) { reflections.stringify_keys[name.to_s] }
  cattr_reader(:acting_as_model) { (options[:class_name] || name.to_s.camelize).constantize }
  class_eval "def #{name}; super || build_#{name}(acting_as_model.actable_reflection.name => self); end"
  alias_method :acting_as, name
  alias_method :acting_as=, "#{name}=".to_sym
  include Actable::InstanceMethods
end