Sha256: 2087d6a06bd787fb1c84c75abe247e69b507df460f9b4586bb704c28f6103bc2

Contents?: true

Size: 1.79 KB

Versions: 1

Compression:

Stored size: 1.79 KB

Contents

module Searchlogic
  module Shared
    module Utilities # :nodoc:
      private
        def merge_conditions(*conditions)
          options = conditions.extract_options!
          conditions.delete_if { |condition| condition.blank? }
          return if conditions.blank?
          return conditions.first if conditions.size == 1
          options[:scope] = true unless options.key?(:scope)
        
          conditions_strs = []
          conditions_subs = []
        
          conditions.each do |condition|
            next if condition.blank?
            arr_condition = condition.is_a?(Array) ? condition : [condition]
            conditions_strs << arr_condition.first
            conditions_subs += arr_condition[1..-1]
          end
        
          return if conditions_strs.blank?
        
          join = options[:any] ? " OR " : " AND "
          conditions_str = options[:scope] ? "(#{conditions_strs.join(")#{join}(")})" : conditions_strs.join(join)
        
          return conditions_str if conditions_subs.blank?
        
          [conditions_str, *conditions_subs]
        end
        
        def scope_condition(condition)
          arr_condition = condition.is_a?(Array) ? condition : [condition]
          arr_condition[0] = "(#{arr_condition[0]})"
          arr_condition.size == 1 ? arr_condition.first : arr_condition
        end
        
        def merge_joins(*joins)
          joins.delete_if { |join| join.blank? }
          return if joins.blank?
          return joins.first if joins.size == 1
          
          new_joins = []
          joins.each do |join|
            case join
            when Array
              new_joins += join
            else
              new_joins << join
            end
          end
          
          new_joins.compact.uniq
        end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
searchlogic-1.5.6 lib/searchlogic/shared/utilities.rb