module ScopedAssociations::ActiveRecord4::HasOne

Public Class Methods

new(model, name, scope, options) click to toggle source
Calls superclass method
# File lib/scoped_associations/activerecord4/has_one.rb, line 15
def initialize(model, name, scope, options)
  super
  if scoped?
    search_scope = foreign_scope(model)
    proc_scope = proc { where(search_scope => name) }
    if @scope.nil?
      @scope = proc { instance_eval(&proc_scope) }
      else
        old_scope = @scope
        @scope = proc { instance_eval(&proc_scope)
                         .instance_eval(&old_scope) }
    end
  end
end

Public Instance Methods

build(*args) click to toggle source
Calls superclass method
# File lib/scoped_associations/activerecord4/has_one.rb, line 9
def build(*args)
  reflection = super
  extend_reflection(reflection)
  reflection
end
foreign_scope(model) click to toggle source
# File lib/scoped_associations/activerecord4/has_one.rb, line 30
def foreign_scope(model)
  if options[:as]
    "#{options[:as]}_scope"
  else
    model.name.demodulize.underscore + "_scope"
  end
end
valid_options() click to toggle source
Calls superclass method
# File lib/scoped_associations/activerecord4/has_one.rb, line 5
def valid_options
  super + [:scoped]
end

Private Instance Methods

extend_reflection(reflection) click to toggle source
# File lib/scoped_associations/activerecord4/has_one.rb, line 40
def extend_reflection(reflection)
  if scoped?
    reflection.extend ReflectionExtension
  end
end
scoped?() click to toggle source
# File lib/scoped_associations/activerecord4/has_one.rb, line 46
def scoped?
  options[:scoped]
end