Sha256: 31502fb8079035593da46a7f690267541ae7fa3ab590f3cbbfc8366fd89499d5

Contents?: true

Size: 1.53 KB

Versions: 29

Compression:

Stored size: 1.53 KB

Contents

require 'active_support'

module NinjaModel
  module SpawnMethods

    def merge(r)
      merged_relation = clone
      return merged_relation unless r
      return to_a & r if r.is_a?(Array)

      order_value = r.ordering
      if order_value.present?
        merged_relation.ordering = merged_relation.ordering + order_value
      end

      merged_predicates = @predicates + r.predicates

      unless @predicates.empty?
        seen = []
        merged_predicates = merged_predicates.reverse.reject { |w|
          nuke = false
          if w.respond_to?(:operator) && w.operator == :==
            attribute = w.attribute
            nuke = seen[attribute]
            seen[attribute] = true
          end
          nuke
        }.reverse
      end
      merged_relation.predicates = merged_predicates

      Relation::SINGLE_VALUE_ATTRS.each do |method|
        value = r.send(:"#{method}_value")
        merged_relation.send(:"#{method}_value=", value) unless value.nil?
      end

      merged_relation

    end

    VALID_FIND_OPTIONS = [:conditions, :limit, :offset, :order]

    def apply_finder_options(options)
      relation = clone
      return relation unless options

      options.assert_valid_keys(VALID_FIND_OPTIONS)
      finders = options.dup
      finders.delete_if { |key, value| value.nil? }

      ([:order, :limit, :offset] & finders.keys).each do |finder|
        relation = relation.send(finder, finders[finder])
      end

      relation = relation.where(finders[:conditions]) if options.key?(:conditions)
      relation
    end
  end
end

Version data entries

29 entries across 29 versions & 1 rubygems

Version Path
ninja-model-1.0.5 lib/ninja_model/relation/spawn_methods.rb
ninja-model-1.0.4 lib/ninja_model/relation/spawn_methods.rb
ninja-model-1.0.3 lib/ninja_model/relation/spawn_methods.rb
ninja-model-1.0.2 lib/ninja_model/relation/spawn_methods.rb
ninja-model-1.0.1 lib/ninja_model/relation/spawn_methods.rb
ninja-model-1.0.0 lib/ninja_model/relation/spawn_methods.rb
ninja-model-0.9.6 lib/ninja_model/relation/spawn_methods.rb
ninja-model-0.9.5 lib/ninja_model/relation/spawn_methods.rb
ninja-model-0.9.4 lib/ninja_model/relation/spawn_methods.rb
ninja-model-0.9.3 lib/ninja_model/relation/spawn_methods.rb
ninja-model-0.9.2 lib/ninja_model/relation/spawn_methods.rb
ninja-model-0.9.1 lib/ninja_model/relation/spawn_methods.rb
ninja-model-0.9.0 lib/ninja_model/relation/spawn_methods.rb
ninja-model-0.8.1 lib/ninja_model/relation/spawn_methods.rb
ninja-model-0.8.0 lib/ninja_model/relation/spawn_methods.rb
ninja-model-0.7.3 lib/ninja_model/relation/spawn_methods.rb
ninja-model-0.7.2 lib/ninja_model/relation/spawn_methods.rb
ninja-model-0.7.1 lib/ninja_model/relation/spawn_methods.rb
ninja-model-0.7.0 lib/ninja_model/relation/spawn_methods.rb
ninja-model-0.6.2 lib/ninja_model/relation/spawn_methods.rb