Sha256: 345fd2a2e390f0c792b7ac9e7e9ef5f1581a5c1fc42e3a5c947061ea6f743ff9

Contents?: true

Size: 1.91 KB

Versions: 2

Compression:

Stored size: 1.91 KB

Contents

# frozen_string_literal: true

module Clowne
  module Adapters # :nodoc: all
    class ActiveRecord
      module Associations
        class Base
          # Params:
          # +reflection+:: Association eflection object
          # +source+:: Instance of cloned object (ex: User.new(posts: posts))
          # +declaration+:: = Relation description
          #                   (ex: Clowne::Declarations::IncludeAssociation.new(:posts))
          # +params+:: = Instance of Clowne::Params
          def initialize(reflection, source, declaration, params)
            @source = source
            @scope = declaration.scope
            @clone_with = declaration.clone_with
            @params = params
            @association_name = declaration.name.to_s
            @reflection = reflection
            @cloner_options = params
            @cloner_options.merge!(traits: declaration.traits) if declaration.traits
          end

          def call(_record)
            raise NotImplementedError
          end

          def association
            @_association ||= source.__send__(association_name)
          end

          def clone_one(child)
            cloner = cloner_for(child)
            cloner ? cloner.call(child, cloner_options) : child.dup
          end

          def with_scope
            base_scope = association
            if scope.is_a?(Symbol)
              base_scope.__send__(scope)
            elsif scope.is_a?(Proc)
              base_scope.instance_exec(params, &scope) || base_scope
            else
              base_scope
            end
          end

          private

          def cloner_for(child)
            return clone_with if clone_with

            return child.class.cloner_class if child.class.respond_to?(:cloner_class)
          end

          attr_reader :source, :scope, :clone_with, :params, :association_name,
                      :reflection, :cloner_options
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
clowne-0.1.0.beta1 lib/clowne/adapters/active_record/associations/base.rb
clowne-0.1.0.pre1 lib/clowne/adapters/active_record/associations/base.rb