Module: ScopedAssociations::ActiveRecord5::HasOne::ReflectionExtension

Defined in:
lib/scoped_associations/activerecord5/has_one.rb

Instance Method Summary collapse

Instance Method Details

#association_classObject



49
50
51
# File 'lib/scoped_associations/activerecord5/has_one.rb', line 49

def association_class
  ScopedHasOneAssociation
end

#foreign_scopeObject



29
30
31
32
33
34
35
36
# File 'lib/scoped_associations/activerecord5/has_one.rb', line 29

def foreign_scope
  if options[:as]
    "#{options[:as]}_scope"
  else
    name = active_record.name
    name.demodulize.underscore + "_scope"
  end
end

#scopeObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/scoped_associations/activerecord5/has_one.rb', line 38

def scope
  old_scope = super
  scope_conditions = { foreign_scope => name }
  proc_scope = proc { where(scope_conditions) }
  if old_scope.nil?
    proc { instance_eval(&proc_scope) }
  else
    proc { instance_eval(&proc_scope).instance_eval(&old_scope) }
  end
end