Browse files

Remove hard-to-follow code that was pulled in and add support for boo…

…lean scopes
  • Loading branch information...
1 parent edabd8c commit 380a06b52faa20eae6263283742d1b6bf43ae61f @binarylogic committed Nov 23, 2009
View
7 lib/searchlogic/active_record/named_scopes.rb
@@ -13,15 +13,10 @@ module NamedScopes
# ActiveRecord hides this internally in a Proc, so we have to try and pull it out with this
# method.
def named_scope_options(name)
-
- key = scopes.key?(name.to_sym) ? name.to_sym : primary_condition_name(name)
+ key = scopes.key?(name.to_sym) ? name.to_sym : condition_scope_name(name)
if key
eval("options", scopes[key].binding)
- elsif name.to_s.downcase.match("_or_")
- condition = find_applied_condition(name)
- newname = name.to_s.gsub(/_or_/, "_#{condition}_or_").to_sym
- named_scope_options(newname) unless name == newname
else
nil
end
View
30 lib/searchlogic/named_scopes/conditions.rb
@@ -49,6 +49,9 @@ module Conditions
CONDITIONS["#{condition}_all".to_sym] = aliases.collect { |a| "#{a}_all".to_sym }
end
+ CONDITIONS[:equals_any] = CONDITIONS[:equals_any] + [:in]
+ CONDITIONS[:does_not_equal_any] = CONDITIONS[:equals_any] + [:not_in]
+
BOOLEAN_CONDITIONS.each { |condition, aliases| CONDITIONS[condition] = aliases }
GROUP_CONDITIONS.each { |condition, aliases| CONDITIONS[condition] = aliases }
@@ -65,23 +68,24 @@ def condition?(name)
def local_condition?(name)
return false if name.blank?
scope_names = scopes.keys.reject { |k| k == :scoped }
- scope_names.include?(name.to_sym) || !condition_details(name).nil?
+ scope_names.include?(name.to_sym) || !condition_details(name).nil? || boolean_condition?(name)
+ end
+
+ def boolean_condition?(name)
+ columns_hash.key?(name.to_s) && columns_hash[name.to_s].type == :boolean
end
def method_missing(name, *args, &block)
if details = condition_details(name)
create_condition(details[:column], details[:condition], args)
send(name, *args)
+ elsif boolean_condition?(name)
+ named_scope name, :conditions => {name => true}
+ send(name)
else
super
end
end
-
- def find_applied_condition(name)
- if name.to_s =~ /(#{(PRIMARY_CONDITIONS + ALIAS_CONDITIONS).join("|")})$/
- $1
- end
- end
def condition_details(name, *args)
if args.size > 0 and !args.first.nil?
@@ -141,10 +145,6 @@ def create_primary_condition(column, condition)
{:conditions => "#{table_name}.#{column} = '' OR #{table_name}.#{column} IS NULL"}
when "not_blank"
{:conditions => "#{table_name}.#{column} != '' AND #{table_name}.#{column} IS NOT NULL"}
- when "in"
- scope_options(condition, column_type, "#{table_name}.#{column} IN (?)")
- when "not_in"
- scope_options(condition, column_type, "#{table_name}.#{column} NOT IN (?)")
end
named_scope("#{column}_#{condition}".to_sym, scope_options)
@@ -209,9 +209,9 @@ def primary_condition(alias_condition)
# a primary condition, alias condition, etc, and it will return the proper
# primary condition name. This helps simply logic throughout Searchlogic. Ex:
#
- # primary_condition_name(:id_gt) => :id_greater_than
- # primary_condition_name(:id_greater_than) => :id_greater_than
- def primary_condition_name(name)
+ # condition_scope_name(:id_gt) => :id_greater_than
+ # condition_scope_name(:id_greater_than) => :id_greater_than
+ def condition_scope_name(name)
if details = condition_details(name)
if PRIMARY_CONDITIONS.include?(name.to_sym)
name
@@ -224,4 +224,4 @@ def primary_condition_name(name)
end
end
end
-end
+end
View
8 lib/searchlogic/search.rb
@@ -62,8 +62,8 @@ def conditions
# Accepts a hash of conditions.
def conditions=(values)
values.each do |condition, value|
- value.delete_if { |v| v.blank? } if value.is_a?(Array)
- next if value.is_a?(String) && value.blank?
+ value.delete_if { |v| ignore_value?(v) } if value.is_a?(Array)
+ next if ignore_value?(value)
send("#{condition}=", value)
end
end
@@ -167,5 +167,9 @@ def type_cast(value, type)
Time.zone && value.is_a?(Time) ? value.in_time_zone : value
end
end
+
+ def ignore_value?(value)
+ (value.is_a?(String) && value.blank?) || (value.is_a?(Array) && value.empty?)
+ end
end
end
View
6 spec/named_scopes/conditions_spec.rb
@@ -19,6 +19,12 @@
User.age_equals([5, 7]).all.should == User.find_all_by_age([5, 7])
end
+ it "should have equals for boolean columns" do
+ female = User.create(:male => false)
+ male = User.create(:male => true)
+ User.male.should == [male]
+ end
+
it "should have does not equal" do
(5..7).each { |age| User.create(:age => age) }
User.age_does_not_equal(6).all.should == User.find_all_by_age([5,7])
View
5 spec/named_scopes/or_conditions_spec.rb
@@ -45,11 +45,6 @@
User.company_name_or_name_like("ben").proxy_options.should == {:joins => :company, :conditions => "(companies.name LIKE '%ben%') OR (users.name LIKE '%ben%')"}
User.company_name_or_company_description_like("ben").proxy_options.should == {:joins =>[:company], :conditions => "(companies.name LIKE '%ben%') OR (companies.description LIKE '%ben%')"}
end
-
- it "should play nice with n-depth scopes on associations" do
- User.company_conglomerate_name_or_company_conglomerate_description_like("ben").proxy_options.should ==
- {:joins =>[{:company, :conglomerate}], :conditions => "(conglomerates.name LIKE '%ben%') OR (conglomerates.description LIKE '%ben%')"}
- end
it "should not get confused by the 'or' in find_or_create_by_* methods" do
User.create(:name => "Fred")
View
10 spec/search_spec.rb
@@ -5,11 +5,11 @@
it "should create a search proxy" do
User.search(:username => "joe").should be_kind_of(Searchlogic::Search)
end
-
+
it "should create a search proxy using the same class" do
User.search.klass.should == User
end
-
+
it "should pass on the current scope to the proxy" do
company = Company.create
user = company.users.create
@@ -112,12 +112,6 @@
search.all.should == [user]
end
- it "should allow chaining conditions with n-depth associations" do
- search = User.search
- search.company_conglomerate_name_or_company_conglomerate_description_like("ben")
- search.proxy_options.should == User.company_conglomerate_name_or_company_conglomerate_description_like("ben").proxy_options
- end
-
it "should allow setting association conditions" do
search = User.search
search.orders_total_gt = 10
View
13 spec/spec_helper.rb
@@ -26,6 +26,7 @@
t.string :username
t.string :name
t.integer :age
+ t.boolean :male
end
create_table :carts do |t|
@@ -57,13 +58,6 @@
t.integer :order_id
t.float :price
end
-
- create_table :conglomerates do |t|
- t.datetime :created_at
- t.datetime :updated_at
- t.string :name
- t.string :description
- end
end
$LOAD_PATH.unshift(File.dirname(__FILE__))
@@ -73,7 +67,6 @@
Spec::Runner.configure do |config|
config.before(:each) do
class Company < ActiveRecord::Base
- belongs_to :conglomerate
has_many :users, :dependent => :destroy
end
@@ -95,10 +88,6 @@ class Fee < ActiveRecord::Base
class LineItem < ActiveRecord::Base
belongs_to :order
end
-
- class Conglomerate < ActiveRecord::Base
- has_many :companies, :dependent => :destroy
- end
Company.destroy_all
User.destroy_all

0 comments on commit 380a06b

Please sign in to comment.