searchlogic 2.3.6 → 2.3.7

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
@@ -1,5 +1,5 @@
1
1
  ---
2
2
  :minor: 3
3
- :patch: 6
3
+ :patch: 7
4
4
  :major: 2
5
5
  :build:
@@ -13,15 +13,10 @@ module Searchlogic
13
13
  # ActiveRecord hides this internally in a Proc, so we have to try and pull it out with this
14
14
  # method.
15
15
  def named_scope_options(name)
16
-
17
- key = scopes.key?(name.to_sym) ? name.to_sym : primary_condition_name(name)
16
+ key = scopes.key?(name.to_sym) ? name.to_sym : condition_scope_name(name)
18
17
 
19
18
  if key
20
19
  eval("options", scopes[key].binding)
21
- elsif name.to_s.downcase.match("_or_")
22
- condition = find_applied_condition(name)
23
- newname = name.to_s.gsub(/_or_/, "_#{condition}_or_").to_sym
24
- named_scope_options(newname) unless name == newname
25
20
  else
26
21
  nil
27
22
  end
@@ -49,6 +49,9 @@ module Searchlogic
49
49
  CONDITIONS["#{condition}_all".to_sym] = aliases.collect { |a| "#{a}_all".to_sym }
50
50
  end
51
51
 
52
+ CONDITIONS[:equals_any] = CONDITIONS[:equals_any] + [:in]
53
+ CONDITIONS[:does_not_equal_any] = CONDITIONS[:equals_any] + [:not_in]
54
+
52
55
  BOOLEAN_CONDITIONS.each { |condition, aliases| CONDITIONS[condition] = aliases }
53
56
 
54
57
  GROUP_CONDITIONS.each { |condition, aliases| CONDITIONS[condition] = aliases }
@@ -65,23 +68,24 @@ module Searchlogic
65
68
  def local_condition?(name)
66
69
  return false if name.blank?
67
70
  scope_names = scopes.keys.reject { |k| k == :scoped }
68
- scope_names.include?(name.to_sym) || !condition_details(name).nil?
71
+ scope_names.include?(name.to_sym) || !condition_details(name).nil? || boolean_condition?(name)
72
+ end
73
+
74
+ def boolean_condition?(name)
75
+ columns_hash.key?(name.to_s) && columns_hash[name.to_s].type == :boolean
69
76
  end
70
77
 
71
78
  def method_missing(name, *args, &block)
72
79
  if details = condition_details(name)
73
80
  create_condition(details[:column], details[:condition], args)
74
81
  send(name, *args)
82
+ elsif boolean_condition?(name)
83
+ named_scope name, :conditions => {name => true}
84
+ send(name)
75
85
  else
76
86
  super
77
87
  end
78
88
  end
79
-
80
- def find_applied_condition(name)
81
- if name.to_s =~ /(#{(PRIMARY_CONDITIONS + ALIAS_CONDITIONS).join("|")})$/
82
- $1
83
- end
84
- end
85
89
 
86
90
  def condition_details(name, *args)
87
91
  if args.size > 0 and !args.first.nil?
@@ -141,10 +145,6 @@ module Searchlogic
141
145
  {:conditions => "#{table_name}.#{column} = '' OR #{table_name}.#{column} IS NULL"}
142
146
  when "not_blank"
143
147
  {:conditions => "#{table_name}.#{column} != '' AND #{table_name}.#{column} IS NOT NULL"}
144
- when "in"
145
- scope_options(condition, column_type, "#{table_name}.#{column} IN (?)")
146
- when "not_in"
147
- scope_options(condition, column_type, "#{table_name}.#{column} NOT IN (?)")
148
148
  end
149
149
 
150
150
  named_scope("#{column}_#{condition}".to_sym, scope_options)
@@ -209,9 +209,9 @@ module Searchlogic
209
209
  # a primary condition, alias condition, etc, and it will return the proper
210
210
  # primary condition name. This helps simply logic throughout Searchlogic. Ex:
211
211
  #
212
- # primary_condition_name(:id_gt) => :id_greater_than
213
- # primary_condition_name(:id_greater_than) => :id_greater_than
214
- def primary_condition_name(name)
212
+ # condition_scope_name(:id_gt) => :id_greater_than
213
+ # condition_scope_name(:id_greater_than) => :id_greater_than
214
+ def condition_scope_name(name)
215
215
  if details = condition_details(name)
216
216
  if PRIMARY_CONDITIONS.include?(name.to_sym)
217
217
  name
@@ -224,4 +224,4 @@ module Searchlogic
224
224
  end
225
225
  end
226
226
  end
227
- end
227
+ end
@@ -62,8 +62,8 @@ module Searchlogic
62
62
  # Accepts a hash of conditions.
63
63
  def conditions=(values)
64
64
  values.each do |condition, value|
65
- value.delete_if { |v| v.blank? } if value.is_a?(Array)
66
- next if value.blank?
65
+ value.delete_if { |v| ignore_value?(v) } if value.is_a?(Array)
66
+ next if ignore_value?(value)
67
67
  send("#{condition}=", value)
68
68
  end
69
69
  end
@@ -167,5 +167,9 @@ module Searchlogic
167
167
  Time.zone && value.is_a?(Time) ? value.in_time_zone : value
168
168
  end
169
169
  end
170
+
171
+ def ignore_value?(value)
172
+ (value.is_a?(String) && value.blank?) || (value.is_a?(Array) && value.empty?)
173
+ end
170
174
  end
171
175
  end
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.3.6"
8
+ s.version = "2.3.7"
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{2009-11-05}
12
+ s.date = %q{2009-11-23}
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 = [
@@ -19,6 +19,12 @@ describe "Conditions" do
19
19
  User.age_equals([5, 7]).all.should == User.find_all_by_age([5, 7])
20
20
  end
21
21
 
22
+ it "should have equals for boolean columns" do
23
+ female = User.create(:male => false)
24
+ male = User.create(:male => true)
25
+ User.male.should == [male]
26
+ end
27
+
22
28
  it "should have does not equal" do
23
29
  (5..7).each { |age| User.create(:age => age) }
24
30
  User.age_does_not_equal(6).all.should == User.find_all_by_age([5,7])
@@ -45,9 +45,15 @@ describe "Or conditions" do
45
45
  User.company_name_or_name_like("ben").proxy_options.should == {:joins => :company, :conditions => "(companies.name LIKE '%ben%') OR (users.name LIKE '%ben%')"}
46
46
  User.company_name_or_company_description_like("ben").proxy_options.should == {:joins =>[:company], :conditions => "(companies.name LIKE '%ben%') OR (companies.description LIKE '%ben%')"}
47
47
  end
48
+
49
+ it "should not get confused by the 'or' in find_or_create_by_* methods" do
50
+ User.create(:name => "Fred")
51
+ User.find_or_create_by_name("Fred").should be_a_kind_of User
52
+ end
48
53
 
49
- it "should play nice with n-depth scopes on associations" do
50
- User.company_conglomerate_name_or_company_conglomerate_description_like("ben").proxy_options.should ==
51
- {:joins =>[{:company, :conglomerate}], :conditions => "(conglomerates.name LIKE '%ben%') OR (conglomerates.description LIKE '%ben%')"}
54
+ it "should not get confused by the 'or' in compound find_or_create_by_* methods" do
55
+ User.create(:name => "Fred", :username => "fredb")
56
+ User.find_or_create_by_name_and_username("Fred", "fredb").should be_a_kind_of User
52
57
  end
53
- end
58
+
59
+ end
data/spec/search_spec.rb CHANGED
@@ -5,11 +5,11 @@ describe "Search" do
5
5
  it "should create a search proxy" do
6
6
  User.search(:username => "joe").should be_kind_of(Searchlogic::Search)
7
7
  end
8
-
8
+
9
9
  it "should create a search proxy using the same class" do
10
10
  User.search.klass.should == User
11
11
  end
12
-
12
+
13
13
  it "should pass on the current scope to the proxy" do
14
14
  company = Company.create
15
15
  user = company.users.create
@@ -112,12 +112,6 @@ describe "Search" do
112
112
  search.all.should == [user]
113
113
  end
114
114
 
115
- it "should allow chaining conditions with n-depth associations" do
116
- search = User.search
117
- search.company_conglomerate_name_or_company_conglomerate_description_like("ben")
118
- search.proxy_options.should == User.company_conglomerate_name_or_company_conglomerate_description_like("ben").proxy_options
119
- end
120
-
121
115
  it "should allow setting association conditions" do
122
116
  search = User.search
123
117
  search.orders_total_gt = 10
@@ -338,6 +332,10 @@ describe "Search" do
338
332
  User.search(:username_nil => false).proxy_options.should == {}
339
333
  end
340
334
 
335
+ it "should not ignore conditions with a value of false where the named scope does not have an arity of 0" do
336
+ User.search(:username_is => false).proxy_options.should == User.username_is(false).proxy_options
337
+ end
338
+
341
339
  it "should recognize the order condition" do
342
340
  User.search(:order => "ascend_by_username").proxy_options.should == User.ascend_by_username.proxy_options
343
341
  end
data/spec/spec_helper.rb CHANGED
@@ -26,6 +26,7 @@ ActiveRecord::Schema.define(:version => 1) do
26
26
  t.string :username
27
27
  t.string :name
28
28
  t.integer :age
29
+ t.boolean :male
29
30
  end
30
31
 
31
32
  create_table :carts do |t|
@@ -57,13 +58,6 @@ ActiveRecord::Schema.define(:version => 1) do
57
58
  t.integer :order_id
58
59
  t.float :price
59
60
  end
60
-
61
- create_table :conglomerates do |t|
62
- t.datetime :created_at
63
- t.datetime :updated_at
64
- t.string :name
65
- t.string :description
66
- end
67
61
  end
68
62
 
69
63
  $LOAD_PATH.unshift(File.dirname(__FILE__))
@@ -73,7 +67,6 @@ require 'searchlogic'
73
67
  Spec::Runner.configure do |config|
74
68
  config.before(:each) do
75
69
  class Company < ActiveRecord::Base
76
- belongs_to :conglomerate
77
70
  has_many :users, :dependent => :destroy
78
71
  end
79
72
 
@@ -95,10 +88,6 @@ Spec::Runner.configure do |config|
95
88
  class LineItem < ActiveRecord::Base
96
89
  belongs_to :order
97
90
  end
98
-
99
- class Conglomerate < ActiveRecord::Base
100
- has_many :companies, :dependent => :destroy
101
- end
102
91
 
103
92
  Company.destroy_all
104
93
  User.destroy_all
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.3.6
4
+ version: 2.3.7
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: 2009-11-05 00:00:00 -05:00
12
+ date: 2009-11-23 00:00:00 -05:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency