searchlogic 2.4.7 → 2.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/VERSION.yml CHANGED
@@ -2,4 +2,4 @@
2
2
  :major: 2
3
3
  :minor: 4
4
4
  :build:
5
- :patch: 7
5
+ :patch: 8
@@ -5,7 +5,9 @@ module Searchlogic
5
5
  module Consistency
6
6
  def self.included(klass)
7
7
  klass.class_eval do
8
- alias_method_chain :merge_joins, :searchlogic
8
+ alias_method_chain :merge_joins, :singularity
9
+ alias_method_chain :merge_joins, :consistent_conditions
10
+ alias_method_chain :merge_joins, :merged_duplicates
9
11
  end
10
12
  end
11
13
 
@@ -13,11 +15,19 @@ module Searchlogic
13
15
  # are not. The merge_joins method in AR should account for this, but it doesn't.
14
16
  # This fixes that problem. This way there is one join per string, which allows
15
17
  # the merge_joins method to delete duplicates.
16
- def merge_joins_with_searchlogic(*args)
17
- joins = merge_joins_without_searchlogic(*args)
18
- joins = joins.collect { |j| j.is_a?(String) ? j.split(" ") : j }.flatten.uniq
19
- joins = joins.collect do |j|
20
- if j.is_a?(String) && !j =~ / (AND|OR) /i
18
+ def merge_joins_with_singularity(*args)
19
+ joins = merge_joins_without_singularity(*args)
20
+ joins.collect { |j| j.is_a?(String) ? j.split(" ") : j }.flatten.uniq
21
+ end
22
+
23
+ # This method ensures that the order of the conditions in the joins are the same.
24
+ # The strings of the joins MUST be exactly the same for AR to remove the duplicates.
25
+ # AR is not consistent in this approach, resulting in duplicate joins errors when
26
+ # combining scopes.
27
+ def merge_joins_with_consistent_conditions(*args)
28
+ joins = merge_joins_without_consistent_conditions(*args)
29
+ joins.collect do |j|
30
+ if j.is_a?(String) && (j =~ / (AND|OR) /i).nil?
21
31
  j.gsub(/(.*) ON (.*) = (.*)/) do |m|
22
32
  sorted = [$2,$3].sort
23
33
  "#{$1} ON #{sorted[0]} = #{sorted[1]}"
@@ -25,7 +35,13 @@ module Searchlogic
25
35
  else
26
36
  j
27
37
  end
28
- end
38
+ end.uniq
39
+ end
40
+
41
+
42
+ def merge_joins_with_merged_duplicates(*args)
43
+ args << ""
44
+ joins = merge_joins_without_merged_duplicates(*args)
29
45
  end
30
46
  end
31
47
  end
@@ -65,7 +65,10 @@ module Searchlogic
65
65
  if !arity || arity == 0
66
66
  # The underlying condition doesn't require any parameters, so let's just create a simple
67
67
  # named scope that is based on a hash.
68
- options = scope.scope(:find)
68
+ options = {}
69
+ with_exclusive_scope do
70
+ options = scope.scope(:find)
71
+ end
69
72
  prepare_named_scope_options(options, association, poly_class)
70
73
  options
71
74
  else
@@ -74,8 +77,11 @@ module Searchlogic
74
77
 
75
78
  eval <<-"end_eval"
76
79
  searchlogic_lambda(:#{arg_type}) { |#{proc_args.join(",")}|
77
- scope = klass.send(association_condition, #{proc_args.join(",")})
78
- options = scope ? scope.scope(:find) : {}
80
+ options = {}
81
+ with_exclusive_scope do
82
+ scope = klass.send(association_condition, #{proc_args.join(",")})
83
+ options = scope ? scope.scope(:find) : {}
84
+ end
79
85
  prepare_named_scope_options(options, association, poly_class)
80
86
  options
81
87
  }
data/searchlogic.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{searchlogic}
8
- s.version = "2.4.7"
8
+ s.version = "2.4.8"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ben Johnson of Binary Logic"]
12
- s.date = %q{2010-02-02}
12
+ s.date = %q{2010-02-06}
13
13
  s.description = %q{Searchlogic makes using ActiveRecord named scopes easier and less repetitive.}
14
14
  s.email = %q{bjohnson@binarylogic.com}
15
15
  s.extra_rdoc_files = [
@@ -39,6 +39,7 @@ Gem::Specification.new do |s|
39
39
  "lib/searchlogic/search.rb",
40
40
  "rails/init.rb",
41
41
  "searchlogic.gemspec",
42
+ "spec/active_record/consistency_spec.rb",
42
43
  "spec/core_ext/object_spec.rb",
43
44
  "spec/core_ext/proc_spec.rb",
44
45
  "spec/named_scopes/alias_scope_spec.rb",
@@ -57,7 +58,8 @@ Gem::Specification.new do |s|
57
58
  s.rubygems_version = %q{1.3.5}
58
59
  s.summary = %q{Searchlogic makes using ActiveRecord named scopes easier and less repetitive.}
59
60
  s.test_files = [
60
- "spec/core_ext/object_spec.rb",
61
+ "spec/active_record/consistency_spec.rb",
62
+ "spec/core_ext/object_spec.rb",
61
63
  "spec/core_ext/proc_spec.rb",
62
64
  "spec/named_scopes/alias_scope_spec.rb",
63
65
  "spec/named_scopes/association_conditions_spec.rb",
@@ -0,0 +1,22 @@
1
+ require File.expand_path(File.dirname(__FILE__) + "/../spec_helper")
2
+
3
+ describe "Consistency" do
4
+ it "should merege joins with consistent conditions" do
5
+ user_group = UserGroup.create
6
+ user_group.users.user_groups_name_like("name").user_groups_id_gt(10).scope(:find)[:joins].should == [
7
+ "INNER JOIN \"user_groups_users\" ON \"user_groups_users\".user_id = \"users\".id",
8
+ "INNER JOIN \"user_groups\" ON \"user_groups\".id = \"user_groups_users\".user_group_id"
9
+ ]
10
+ end
11
+
12
+ it "should limit join iterations to each scope and merge the joins if duplicates exist" do
13
+ pending
14
+ #Company.named_scope :name_like, lambda { |a| {:conditions => ["companies.name = ?", a], :joins => {:users => :company}} }
15
+ #Company.named_scope :name1_like, lambda { |a| {:conditions => ["companies.description = ?", a], :joins => {:users => :company}} }
16
+ #Company.named_scope :name2_like, lambda { |a| User.company_id_equals(2).proxy_options }
17
+
18
+ #Company.name_like("a").name1_like("b").name2_like("c").count
19
+
20
+ Company.users_company_name_like("name").users_company_description_like("description").users_company_created_at_after(Time.now).scope(:find).should == {}
21
+ end
22
+ end
@@ -84,19 +84,28 @@ describe "Association Conditions" do
84
84
  Company.ascend_by_users_orders_total.all.should == Company.all
85
85
  end
86
86
 
87
+ it "should implement exclusive scoping" do
88
+ scope = Company.users_company_name_like("name").users_company_description_like("description")
89
+ scope.scope(:find)[:joins].should == [{:users => :company}]
90
+ lambda { scope.all }.should_not raise_error
91
+ end
92
+
87
93
  it "should not create the same join twice" do
88
- company = Company.create
89
- user = company.users.create
90
- order = user.orders.create(:total => 20, :taxes => 3)
91
- Company.users_orders_total_gt(10).users_orders_taxes_lt(5).ascend_by_users_orders_total.all.should == Company.all
94
+ scope = Company.users_orders_total_gt(10).users_orders_taxes_lt(5).ascend_by_users_orders_total
95
+ scope.scope(:find)[:joins].should == [{:users => :orders}]
96
+ lambda { scope.count }.should_not raise_error
92
97
  end
93
98
 
94
99
  it "should not create the same join twice when traveling through the duplicate join" do
95
- Company.users_username_like("bjohnson").users_orders_total_gt(100).all.should == Company.all
100
+ scope = Company.users_username_like("bjohnson").users_orders_total_gt(100)
101
+ scope.scope(:find)[:joins].should == [{:users => :orders}, :users]
102
+ lambda { scope.count }.should_not raise_error
96
103
  end
97
104
 
98
- it "should not create the same join twice when traveling through the duplicate join 2" do
99
- Company.users_orders_total_gt(100).users_orders_line_items_price_gt(20).all.should == Company.all
105
+ it "should not create the same join twice when traveling through the deep duplicate join" do
106
+ scope = Company.users_orders_total_gt(100).users_orders_line_items_price_gt(20)
107
+ scope.scope(:find)[:joins].should == [{:users => {:orders => :line_items}}, {:users => :orders}]
108
+ lambda { scope.all }.should_not raise_error
100
109
  end
101
110
 
102
111
  it "should allow the use of :include when a join was created" do
data/spec/spec_helper.rb CHANGED
@@ -24,6 +24,15 @@ ActiveRecord::Schema.define(:version => 1) do
24
24
  t.integer :users_count, :default => 0
25
25
  end
26
26
 
27
+ create_table :user_groups do |t|
28
+ t.string :name
29
+ end
30
+
31
+ create_table :user_groups_users, :id => false do |t|
32
+ t.integer :user_group_id, :null => false
33
+ t.integer :user_id, :null => false
34
+ end
35
+
27
36
  create_table :users do |t|
28
37
  t.datetime :created_at
29
38
  t.datetime :updated_at
@@ -76,13 +85,19 @@ Spec::Runner.configure do |config|
76
85
  end
77
86
 
78
87
  class Company < ActiveRecord::Base
88
+ has_many :orders, :through => :users
79
89
  has_many :users, :dependent => :destroy
80
90
  end
81
91
 
92
+ class UserGroup < ActiveRecord::Base
93
+ has_and_belongs_to_many :users
94
+ end
95
+
82
96
  class User < ActiveRecord::Base
83
97
  belongs_to :company, :counter_cache => true
84
98
  has_many :orders, :dependent => :destroy
85
99
  has_many :orders_big, :class_name => 'Order', :conditions => 'total > 100'
100
+ has_and_belongs_to_many :user_groups
86
101
  end
87
102
 
88
103
  class Order < ActiveRecord::Base
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: searchlogic
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.4.7
4
+ version: 2.4.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ben Johnson of Binary Logic
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2010-02-02 00:00:00 -05:00
12
+ date: 2010-02-06 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -54,6 +54,7 @@ files:
54
54
  - lib/searchlogic/search.rb
55
55
  - rails/init.rb
56
56
  - searchlogic.gemspec
57
+ - spec/active_record/consistency_spec.rb
57
58
  - spec/core_ext/object_spec.rb
58
59
  - spec/core_ext/proc_spec.rb
59
60
  - spec/named_scopes/alias_scope_spec.rb
@@ -93,6 +94,7 @@ signing_key:
93
94
  specification_version: 3
94
95
  summary: Searchlogic makes using ActiveRecord named scopes easier and less repetitive.
95
96
  test_files:
97
+ - spec/active_record/consistency_spec.rb
96
98
  - spec/core_ext/object_spec.rb
97
99
  - spec/core_ext/proc_spec.rb
98
100
  - spec/named_scopes/alias_scope_spec.rb