Sha256: 0cd8d8f2c6b94b237bbb066f5f32c688bfd3da3e9525049f0f5e14715dd8c326

Contents?: true

Size: 1.01 KB

Versions: 13

Compression:

Stored size: 1.01 KB

Contents

module NinjaModel
  module Associations
    class AssociationScope

      attr_reader :association

      delegate :klass, :owner, :reflection, :to => :association
      delegate :chain, :conditions, :options, :ninja_model, :active_record, :to => :reflection

      def initialize(association)
        @association = association
      end

      def scope
        scope = klass.unscoped
        scope = scope.extending(*Array.wrap(options[:extend]))
        scope = scope.apply_finder_options(
          options.slice(
            :order, :limit, :joins, :group, :having, :offset
          )
        )

        add_constraints(scope)
      end

      def add_constraints(scope)
        if reflection.source_macro == :belongs_to
          key = reflection.association_primary_key
          foreign_key = reflection.foreign_key
        else
          key = reflection.foreign_key
          foreign_key = reflection.active_record_primary_key
        end

        scope = scope.where(key => owner[foreign_key])
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
ninja-model-1.0.5 lib/ninja_model/associations/association_scope.rb
ninja-model-1.0.4 lib/ninja_model/associations/association_scope.rb
ninja-model-1.0.3 lib/ninja_model/associations/association_scope.rb
ninja-model-1.0.2 lib/ninja_model/associations/association_scope.rb
ninja-model-1.0.1 lib/ninja_model/associations/association_scope.rb
ninja-model-1.0.0 lib/ninja_model/associations/association_scope.rb
ninja-model-0.9.6 lib/ninja_model/associations/association_scope.rb
ninja-model-0.9.5 lib/ninja_model/associations/association_scope.rb
ninja-model-0.9.4 lib/ninja_model/associations/association_scope.rb
ninja-model-0.9.3 lib/ninja_model/associations/association_scope.rb
ninja-model-0.9.2 lib/ninja_model/associations/association_scope.rb
ninja-model-0.9.1 lib/ninja_model/associations/association_scope.rb
ninja-model-0.9.0 lib/ninja_model/associations/association_scope.rb