lib/left_joins.rb in left_joins-1.0.1 vs lib/left_joins.rb in left_joins-1.0.2

- old
+ new

@@ -1,33 +1,47 @@ require "left_joins/version" require 'active_record' require 'active_record/relation' -module ActiveRecord::QueryMethods +module LeftJoins IS_RAILS3_FLAG = Gem::Version.new(ActiveRecord::VERSION::STRING) < Gem::Version.new('4.0.0') - if IS_RAILS3_FLAG +end + +module ActiveRecord::QueryMethods + + # ---------------------------------------------------------------- + # ● Implement check_if_method_has_arguments! method for Rails 3 + # ---------------------------------------------------------------- + if LeftJoins::IS_RAILS3_FLAG def check_if_method_has_arguments!(method_name, args) if args.blank? raise ArgumentError, "The method .#{method_name}() must contain arguments." end end end if not method_defined?(:left_outer_joins!) + # ---------------------------------------------------------------- + # ● Storing left joins values into @left_outer_joins_values + # ---------------------------------------------------------------- + attr_accessor :left_outer_joins_values def left_outer_joins(*args) check_if_method_has_arguments!(:left_outer_joins, args) args.compact! args.flatten! - return (IS_RAILS3_FLAG ? clone : spawn).left_outer_joins!(*args) + return (LeftJoins::IS_RAILS3_FLAG ? clone : spawn).left_outer_joins!(*args) end def left_outer_joins!(*args) (@left_outer_joins_values ||= []) << args self end + # ---------------------------------------------------------------- + # ● Implement left joins when building arel + # ---------------------------------------------------------------- alias_method :left_joins, :left_outer_joins alias_method :build_arel_without_outer_joins, :build_arel def build_arel(*args) arel = build_arel_without_outer_joins(*args) build_left_outer_joins(arel, @left_outer_joins_values.flatten) if @left_outer_joins_values @@ -53,11 +67,11 @@ end alias_method :left_outer_joins, :left_joins end class ::ActiveRecord::Associations::JoinDependency - if IS_RAILS3_FLAG + if LeftJoins::IS_RAILS3_FLAG alias_method :build_without_hooking_join_type, :build def build(associations, parent = nil, join_type = Arel::Nodes::InnerJoin) join_type = Thread.current.thread_variable_get :left_joins_join_type || join_type return build_without_hooking_join_type(associations, parent, join_type) end @@ -74,11 +88,11 @@ def perform_calculation(operation, column_name, options = {}) operation = operation.to_s.downcase # If #count is used with #distinct (i.e. `relation.distinct.count`) it is # considered distinct. - distinct = IS_RAILS3_FLAG ? options[:distinct] || self.uniq_value : self.distinct_value + distinct = LeftJoins::IS_RAILS3_FLAG ? options[:distinct] || self.uniq_value : self.distinct_value if operation == "count" column_name ||= select_for_count column_name = primary_key if column_name == :all && distinct distinct = nil if column_name =~ /\s*DISTINCT[\s(]+/i @@ -92,5 +106,35 @@ end end end end +# ---------------------------------------------------------------- +# ● Implement left joins when merging relations +# ---------------------------------------------------------------- +if not LeftJoins::IS_RAILS3_FLAG + require 'active_record/relation/merger' + class ActiveRecord::Relation + class Merger + alias_method :merge_without_left_joins, :merge + def merge + values = other.left_outer_joins_values + relation.left_outer_joins!(*values) if values.present? + return merge_without_left_joins + end + end + end + + module ActiveRecord + module SpawnMethods + + private + + alias_method :relation_with_without_left_joins, :relation_with + def relation_with(values) # :nodoc: + result = relation_with_without_left_joins(values) + result.left_outer_joins_values = self.left_outer_joins_values + return result + end + end + end +end