schof-searchlogic 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.rdoc +302 -0
- data/MIT-LICENSE +20 -0
- data/Manifest +157 -0
- data/README.rdoc +461 -0
- data/Rakefile +13 -0
- data/TODO.rdoc +4 -0
- data/init.rb +1 -0
- data/lib/searchlogic.rb +100 -0
- data/lib/searchlogic/active_record/associations.rb +52 -0
- data/lib/searchlogic/active_record/base.rb +224 -0
- data/lib/searchlogic/active_record/connection_adapters/mysql_adapter.rb +176 -0
- data/lib/searchlogic/active_record/connection_adapters/postgresql_adapter.rb +172 -0
- data/lib/searchlogic/active_record/connection_adapters/sqlite_adapter.rb +80 -0
- data/lib/searchlogic/condition/base.rb +165 -0
- data/lib/searchlogic/condition/begins_with.rb +17 -0
- data/lib/searchlogic/condition/blank.rb +21 -0
- data/lib/searchlogic/condition/child_of.rb +11 -0
- data/lib/searchlogic/condition/descendant_of.rb +11 -0
- data/lib/searchlogic/condition/ends_with.rb +17 -0
- data/lib/searchlogic/condition/equals.rb +33 -0
- data/lib/searchlogic/condition/greater_than.rb +15 -0
- data/lib/searchlogic/condition/greater_than_or_equal_to.rb +15 -0
- data/lib/searchlogic/condition/inclusive_descendant_of.rb +10 -0
- data/lib/searchlogic/condition/keywords.rb +47 -0
- data/lib/searchlogic/condition/less_than.rb +15 -0
- data/lib/searchlogic/condition/less_than_or_equal_to.rb +15 -0
- data/lib/searchlogic/condition/like.rb +15 -0
- data/lib/searchlogic/condition/nested_set.rb +17 -0
- data/lib/searchlogic/condition/nil.rb +21 -0
- data/lib/searchlogic/condition/not_begin_with.rb +20 -0
- data/lib/searchlogic/condition/not_blank.rb +19 -0
- data/lib/searchlogic/condition/not_end_with.rb +20 -0
- data/lib/searchlogic/condition/not_equal.rb +27 -0
- data/lib/searchlogic/condition/not_have_keywords.rb +20 -0
- data/lib/searchlogic/condition/not_like.rb +20 -0
- data/lib/searchlogic/condition/not_nil.rb +19 -0
- data/lib/searchlogic/condition/sibling_of.rb +14 -0
- data/lib/searchlogic/conditions/any_or_all.rb +42 -0
- data/lib/searchlogic/conditions/base.rb +244 -0
- data/lib/searchlogic/conditions/groups.rb +74 -0
- data/lib/searchlogic/conditions/magic_methods.rb +286 -0
- data/lib/searchlogic/conditions/multiparameter_attributes.rb +105 -0
- data/lib/searchlogic/conditions/protection.rb +36 -0
- data/lib/searchlogic/config.rb +31 -0
- data/lib/searchlogic/config/helpers.rb +338 -0
- data/lib/searchlogic/config/search.rb +53 -0
- data/lib/searchlogic/core_ext/hash.rb +75 -0
- data/lib/searchlogic/core_ext/object.rb +19 -0
- data/lib/searchlogic/helpers/control_types/link.rb +310 -0
- data/lib/searchlogic/helpers/control_types/links.rb +242 -0
- data/lib/searchlogic/helpers/control_types/remote_link.rb +87 -0
- data/lib/searchlogic/helpers/control_types/remote_links.rb +72 -0
- data/lib/searchlogic/helpers/control_types/remote_select.rb +36 -0
- data/lib/searchlogic/helpers/control_types/select.rb +82 -0
- data/lib/searchlogic/helpers/form.rb +208 -0
- data/lib/searchlogic/helpers/utilities.rb +197 -0
- data/lib/searchlogic/modifiers/absolute.rb +15 -0
- data/lib/searchlogic/modifiers/acos.rb +11 -0
- data/lib/searchlogic/modifiers/asin.rb +11 -0
- data/lib/searchlogic/modifiers/atan.rb +11 -0
- data/lib/searchlogic/modifiers/avg.rb +15 -0
- data/lib/searchlogic/modifiers/base.rb +27 -0
- data/lib/searchlogic/modifiers/ceil.rb +15 -0
- data/lib/searchlogic/modifiers/char_length.rb +15 -0
- data/lib/searchlogic/modifiers/cos.rb +15 -0
- data/lib/searchlogic/modifiers/cot.rb +15 -0
- data/lib/searchlogic/modifiers/count.rb +11 -0
- data/lib/searchlogic/modifiers/day_of_month.rb +15 -0
- data/lib/searchlogic/modifiers/day_of_week.rb +15 -0
- data/lib/searchlogic/modifiers/day_of_year.rb +15 -0
- data/lib/searchlogic/modifiers/degrees.rb +11 -0
- data/lib/searchlogic/modifiers/exp.rb +15 -0
- data/lib/searchlogic/modifiers/floor.rb +15 -0
- data/lib/searchlogic/modifiers/hex.rb +11 -0
- data/lib/searchlogic/modifiers/hour.rb +11 -0
- data/lib/searchlogic/modifiers/log.rb +15 -0
- data/lib/searchlogic/modifiers/log10.rb +11 -0
- data/lib/searchlogic/modifiers/log2.rb +11 -0
- data/lib/searchlogic/modifiers/lower.rb +15 -0
- data/lib/searchlogic/modifiers/ltrim.rb +15 -0
- data/lib/searchlogic/modifiers/md5.rb +11 -0
- data/lib/searchlogic/modifiers/microseconds.rb +11 -0
- data/lib/searchlogic/modifiers/milliseconds.rb +11 -0
- data/lib/searchlogic/modifiers/minute.rb +15 -0
- data/lib/searchlogic/modifiers/month.rb +15 -0
- data/lib/searchlogic/modifiers/octal.rb +15 -0
- data/lib/searchlogic/modifiers/radians.rb +11 -0
- data/lib/searchlogic/modifiers/round.rb +11 -0
- data/lib/searchlogic/modifiers/rtrim.rb +15 -0
- data/lib/searchlogic/modifiers/second.rb +15 -0
- data/lib/searchlogic/modifiers/sign.rb +11 -0
- data/lib/searchlogic/modifiers/sin.rb +11 -0
- data/lib/searchlogic/modifiers/square_root.rb +15 -0
- data/lib/searchlogic/modifiers/sum.rb +11 -0
- data/lib/searchlogic/modifiers/tan.rb +15 -0
- data/lib/searchlogic/modifiers/trim.rb +15 -0
- data/lib/searchlogic/modifiers/upper.rb +15 -0
- data/lib/searchlogic/modifiers/week.rb +11 -0
- data/lib/searchlogic/modifiers/year.rb +11 -0
- data/lib/searchlogic/search/base.rb +148 -0
- data/lib/searchlogic/search/conditions.rb +53 -0
- data/lib/searchlogic/search/ordering.rb +244 -0
- data/lib/searchlogic/search/pagination.rb +121 -0
- data/lib/searchlogic/search/protection.rb +89 -0
- data/lib/searchlogic/search/searching.rb +32 -0
- data/lib/searchlogic/shared/utilities.rb +56 -0
- data/lib/searchlogic/shared/virtual_classes.rb +39 -0
- data/lib/searchlogic/version.rb +79 -0
- data/searchlogic.gemspec +41 -0
- data/test/active_record_tests/associations_test.rb +94 -0
- data/test/active_record_tests/base_test.rb +115 -0
- data/test/condition_tests/base_test.rb +54 -0
- data/test/condition_tests/begins_with_test.rb +11 -0
- data/test/condition_tests/blank_test.rb +31 -0
- data/test/condition_tests/child_of_test.rb +17 -0
- data/test/condition_tests/descendant_of_test.rb +12 -0
- data/test/condition_tests/ends_with_test.rb +11 -0
- data/test/condition_tests/equals_test.rb +28 -0
- data/test/condition_tests/greater_than_or_equal_to_test.rb +11 -0
- data/test/condition_tests/greater_than_test.rb +11 -0
- data/test/condition_tests/inclusive_descendant_of_test.rb +12 -0
- data/test/condition_tests/keywords_test.rb +23 -0
- data/test/condition_tests/less_than_or_equal_to_test.rb +11 -0
- data/test/condition_tests/less_than_test.rb +11 -0
- data/test/condition_tests/like_test.rb +11 -0
- data/test/condition_tests/nil_test.rb +31 -0
- data/test/condition_tests/not_begin_with_test.rb +8 -0
- data/test/condition_tests/not_blank_test.rb +8 -0
- data/test/condition_tests/not_end_with_test.rb +8 -0
- data/test/condition_tests/not_equal_test.rb +19 -0
- data/test/condition_tests/not_have_keywords_test.rb +8 -0
- data/test/condition_tests/not_like_test.rb +8 -0
- data/test/condition_tests/not_nil_test.rb +13 -0
- data/test/condition_tests/sibling_of_test.rb +15 -0
- data/test/conditions_tests/any_or_all_test.rb +23 -0
- data/test/conditions_tests/base_test.rb +185 -0
- data/test/conditions_tests/groups_test.rb +68 -0
- data/test/conditions_tests/magic_methods_test.rb +36 -0
- data/test/conditions_tests/multiparameter_attributes_test.rb +15 -0
- data/test/conditions_tests/protection_test.rb +18 -0
- data/test/config_test.rb +23 -0
- data/test/fixtures/accounts.yml +12 -0
- data/test/fixtures/animals.yml +7 -0
- data/test/fixtures/orders.yml +12 -0
- data/test/fixtures/user_groups.yml +5 -0
- data/test/fixtures/users.yml +45 -0
- data/test/libs/awesome_nested_set.rb +545 -0
- data/test/libs/awesome_nested_set/compatability.rb +29 -0
- data/test/libs/awesome_nested_set/helper.rb +40 -0
- data/test/libs/awesome_nested_set/named_scope.rb +140 -0
- data/test/libs/rexml_fix.rb +14 -0
- data/test/modifier_tests/day_of_month_test.rb +16 -0
- data/test/search_tests/base_test.rb +241 -0
- data/test/search_tests/conditions_test.rb +21 -0
- data/test/search_tests/ordering_test.rb +167 -0
- data/test/search_tests/pagination_test.rb +74 -0
- data/test/search_tests/protection_test.rb +26 -0
- data/test/test_helper.rb +116 -0
- metadata +385 -0
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require File.dirname(__FILE__) << "/lib/searchlogic/version"
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
Echoe.new 'searchlogic' do |p|
|
6
|
+
p.version = Searchlogic::Version::STRING
|
7
|
+
p.author = "Ben Johnson of Binary Logic"
|
8
|
+
p.email = 'bjohnson@binarylogic.com'
|
9
|
+
p.project = 'searchlogic'
|
10
|
+
p.summary = "Object based ActiveRecord searching, ordering, pagination, and more!"
|
11
|
+
p.url = "http://github.com/binarylogic/searchlogic"
|
12
|
+
p.dependencies = %w(activerecord activesupport echoe)
|
13
|
+
end
|
data/TODO.rdoc
ADDED
@@ -0,0 +1,4 @@
|
|
1
|
+
= To Do
|
2
|
+
|
3
|
+
1. Perform "more efficient" checks: year_of_created_at = 2008 and month_of_created_at = 8. Should result in a "BETWEEN" statement utilizing the column indexes. Thanks Georg for letting me know about this.
|
4
|
+
2. Solve conflicts between scope joins and joins in the search (for old versions of AR). Also solve conflicts between joins and includes.
|
data/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require File.dirname(__FILE__) << "/lib/searchlogic"
|
data/lib/searchlogic.rb
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__)) unless $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__)))
|
2
|
+
|
3
|
+
require "active_support"
|
4
|
+
require "active_record"
|
5
|
+
require "active_record/version"
|
6
|
+
|
7
|
+
["mysql", "postgresql", "sqlite"].each do |adapter_name|
|
8
|
+
begin
|
9
|
+
require "active_record/connection_adapters/#{adapter_name}_adapter"
|
10
|
+
require "searchlogic/active_record/connection_adapters/#{adapter_name}_adapter"
|
11
|
+
rescue Exception
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# Core Ext
|
16
|
+
require "searchlogic/core_ext/hash"
|
17
|
+
require "searchlogic/core_ext/object"
|
18
|
+
|
19
|
+
# Shared
|
20
|
+
require "searchlogic/shared/utilities"
|
21
|
+
require "searchlogic/shared/virtual_classes"
|
22
|
+
|
23
|
+
# Base classes
|
24
|
+
require "searchlogic/version"
|
25
|
+
require "searchlogic/config/helpers"
|
26
|
+
require "searchlogic/config/search"
|
27
|
+
require "searchlogic/config"
|
28
|
+
|
29
|
+
# ActiveRecord
|
30
|
+
require "searchlogic/active_record/base"
|
31
|
+
require "searchlogic/active_record/associations"
|
32
|
+
|
33
|
+
# Search
|
34
|
+
require "searchlogic/search/ordering"
|
35
|
+
require "searchlogic/search/pagination"
|
36
|
+
require "searchlogic/search/conditions"
|
37
|
+
require "searchlogic/search/searching"
|
38
|
+
require "searchlogic/search/base"
|
39
|
+
require "searchlogic/search/protection"
|
40
|
+
|
41
|
+
# Conditions
|
42
|
+
require "searchlogic/conditions/any_or_all"
|
43
|
+
require "searchlogic/conditions/groups"
|
44
|
+
require "searchlogic/conditions/magic_methods"
|
45
|
+
require "searchlogic/conditions/multiparameter_attributes"
|
46
|
+
require "searchlogic/conditions/protection"
|
47
|
+
require "searchlogic/conditions/base"
|
48
|
+
|
49
|
+
# Condition
|
50
|
+
require "searchlogic/condition/base"
|
51
|
+
require "searchlogic/condition/nested_set"
|
52
|
+
SEARCHLOGIC_CONDITIONS = [:begins_with, :blank, :child_of, :descendant_of, :ends_with, :equals, :greater_than, :greater_than_or_equal_to, :inclusive_descendant_of, :like, :nil, :not_begin_with,
|
53
|
+
:not_blank, :not_end_with, :not_equal, :not_have_keywords, :not_like, :not_nil, :keywords, :less_than, :less_than_or_equal_to, :sibling_of]
|
54
|
+
SEARCHLOGIC_CONDITIONS.each { |condition| require "searchlogic/condition/#{condition}" }
|
55
|
+
|
56
|
+
# Modifiers
|
57
|
+
require "searchlogic/modifiers/base"
|
58
|
+
SEARCHLOGIC_MODIFIERS = [:absolute, :acos, :asin, :atan, :avg, :ceil, :char_length, :cos, :cot, :count, :day_of_month, :day_of_week, :day_of_year, :degrees, :exp, :floor, :hex, :hour, :log, :log10,
|
59
|
+
:log2, :lower, :ltrim, :md5, :microseconds, :milliseconds, :minute, :month, :octal, :radians, :round, :rtrim, :second, :sign, :sin, :square_root, :sum, :tan, :trim, :upper, :week, :year]
|
60
|
+
SEARCHLOGIC_MODIFIERS.each { |modifier| require "searchlogic/modifiers/#{modifier}" }
|
61
|
+
|
62
|
+
# Helpers
|
63
|
+
require "searchlogic/helpers/utilities"
|
64
|
+
require "searchlogic/helpers/form"
|
65
|
+
require "searchlogic/helpers/control_types/link"
|
66
|
+
require "searchlogic/helpers/control_types/links"
|
67
|
+
require "searchlogic/helpers/control_types/select"
|
68
|
+
require "searchlogic/helpers/control_types/remote_link"
|
69
|
+
require "searchlogic/helpers/control_types/remote_links"
|
70
|
+
require "searchlogic/helpers/control_types/remote_select"
|
71
|
+
|
72
|
+
# Lets do it!
|
73
|
+
module Searchlogic
|
74
|
+
module Search
|
75
|
+
class Base
|
76
|
+
include Conditions
|
77
|
+
include Ordering
|
78
|
+
include Protection
|
79
|
+
include Pagination
|
80
|
+
include Searching
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
module Conditions
|
85
|
+
class Base
|
86
|
+
include AnyOrAll
|
87
|
+
include Groups
|
88
|
+
include MagicMethods
|
89
|
+
include MultiparameterAttributes
|
90
|
+
include Protection
|
91
|
+
end
|
92
|
+
|
93
|
+
SEARCHLOGIC_CONDITIONS.each { |condition| Base.register_condition("Searchlogic::Condition::#{condition.to_s.camelize}".constantize) }
|
94
|
+
SEARCHLOGIC_MODIFIERS.each { |modifier| Base.register_modifier("Searchlogic::Modifiers::#{modifier.to_s.camelize}".constantize) }
|
95
|
+
end
|
96
|
+
|
97
|
+
# The namespace I put all cached search classes.
|
98
|
+
module Cache
|
99
|
+
end
|
100
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Searchlogic
|
2
|
+
module ActiveRecord
|
3
|
+
# = Searchlogic ActiveRecord Associations
|
4
|
+
#
|
5
|
+
# These methods hook into ActiveRecords association methods and add in searchlogic functionality.
|
6
|
+
module Associations
|
7
|
+
module AssociationCollection
|
8
|
+
# This needs to be implemented because AR doesn't leverage scopes with this method like it probably should
|
9
|
+
def find_with_searchlogic(*args)
|
10
|
+
options = args.extract_options!
|
11
|
+
args << filter_options_with_searchlogic(options)
|
12
|
+
find_without_searchlogic(*args)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module HasManyAssociation
|
17
|
+
def count_with_searchlogic(*args)
|
18
|
+
options = args.extract_options!
|
19
|
+
args << filter_options_with_searchlogic(options)
|
20
|
+
count_without_searchlogic(*args)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
ActiveRecord::Associations::AssociationCollection.class_eval do
|
28
|
+
if respond_to?(:find)
|
29
|
+
include Searchlogic::ActiveRecord::Associations::AssociationCollection
|
30
|
+
alias_method_chain :find, :searchlogic
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
ActiveRecord::Associations::HasManyAssociation.class_eval do
|
35
|
+
include Searchlogic::ActiveRecord::Associations::HasManyAssociation
|
36
|
+
alias_method_chain :count, :searchlogic
|
37
|
+
|
38
|
+
# Older versions of AR have find in here, not in AssociationCollection
|
39
|
+
include Searchlogic::ActiveRecord::Associations::AssociationCollection
|
40
|
+
alias_method_chain :find, :searchlogic
|
41
|
+
end
|
42
|
+
|
43
|
+
ActiveRecord::Associations::ClassMethods::InnerJoinDependency::InnerJoinAssociation.class_eval do
|
44
|
+
private
|
45
|
+
# Inner joins impose limitations on queries. They can be quicker but you can't do OR conditions when conditions
|
46
|
+
# overlap from the base model to any of its associations. Also, inner joins won't allow you to order by an association
|
47
|
+
# attribute. What if the association is optional? All of those records are ommitted. It just doesn't make sense to default
|
48
|
+
# to inner joins when providing this as a "convenience" when searching. So let's change it.
|
49
|
+
def join_type
|
50
|
+
"LEFT OUTER JOIN"
|
51
|
+
end
|
52
|
+
end
|
@@ -0,0 +1,224 @@
|
|
1
|
+
module Searchlogic
|
2
|
+
# == Searchlogic ActiveRecord
|
3
|
+
#
|
4
|
+
# Hooks into ActiveRecord to add all of the searchlogic functionality into your models. Only uses what is publically available, doesn't dig into internals, and
|
5
|
+
# searchlogic only gets involved when needed.
|
6
|
+
module ActiveRecord
|
7
|
+
# = Searchlogic ActiveRecord Base
|
8
|
+
# Adds in base level functionality to ActiveRecord
|
9
|
+
module Base
|
10
|
+
# This is an alias method chain. It hook into ActiveRecord's "calculate" method and checks to see if Searchlogic should get involved.
|
11
|
+
def calculate_with_searchlogic(*args)
|
12
|
+
options = args.extract_options!
|
13
|
+
options = filter_options_with_searchlogic(options, false)
|
14
|
+
args << options
|
15
|
+
calculate_without_searchlogic(*args)
|
16
|
+
end
|
17
|
+
|
18
|
+
# This is an alias method chain. It hooks into ActiveRecord's "find" method and checks to see if Searchlogic should get involved.
|
19
|
+
def find_with_searchlogic(*args)
|
20
|
+
options = args.extract_options!
|
21
|
+
options = filter_options_with_searchlogic(options)
|
22
|
+
args << options
|
23
|
+
find_without_searchlogic(*args)
|
24
|
+
end
|
25
|
+
|
26
|
+
# This is an alias method chain. It hooks into ActiveRecord's scopes and checks to see if Searchlogic should get involved. Allowing you to use all of Searchlogics conditions and tools
|
27
|
+
# in scopes as well.
|
28
|
+
#
|
29
|
+
# === Examples
|
30
|
+
#
|
31
|
+
# Named scopes:
|
32
|
+
#
|
33
|
+
# named_scope :top_expensive, :conditions => {:total_gt => 1_000_000}, :per_page => 10
|
34
|
+
# named_scope :top_expensive_ordered, :conditions => {:total_gt => 1_000_000}, :per_page => 10, :order_by => {:user => :first_name}
|
35
|
+
#
|
36
|
+
# Good ole' regular scopes:
|
37
|
+
#
|
38
|
+
# with_scope(:find => {:conditions => {:total_gt => 1_000_000}, :per_page => 10}) do
|
39
|
+
# find(:all)
|
40
|
+
# end
|
41
|
+
#
|
42
|
+
# with_scope(:find => {:conditions => {:total_gt => 1_000_000}, :per_page => 10}) do
|
43
|
+
# build_search
|
44
|
+
# end
|
45
|
+
def with_scope_with_searchlogic(method_scoping = {}, action = :merge, &block)
|
46
|
+
method_scoping[:find] = filter_options_with_searchlogic(method_scoping[:find]) if method_scoping[:find]
|
47
|
+
with_scope_without_searchlogic(method_scoping, action, &block)
|
48
|
+
end
|
49
|
+
|
50
|
+
# This is a special method that Searchlogic adds in. It returns a new search object on the model. So you can search via an object.
|
51
|
+
#
|
52
|
+
# <b>This method is "protected". Meaning it checks the passed options for SQL injections. So trying to write raw SQL in *any* of the option will result in a raised exception. It's safe to pass a params object when instantiating.</b>
|
53
|
+
#
|
54
|
+
# This method has an alias "new_search"
|
55
|
+
#
|
56
|
+
# === Examples
|
57
|
+
#
|
58
|
+
# search = User.new_search
|
59
|
+
# search.conditions.first_name_contains = "Ben"
|
60
|
+
# search.per_page = 20
|
61
|
+
# search.page = 2
|
62
|
+
# search.order_by = {:user_group => :name}
|
63
|
+
# search.all # can call any search method: first, find(:all), find(:first), sum("id"), etc...
|
64
|
+
def build_search(options = {}, &block)
|
65
|
+
search = searchlogic_search
|
66
|
+
search.protect = true
|
67
|
+
search.options = options
|
68
|
+
yield search if block_given?
|
69
|
+
search
|
70
|
+
end
|
71
|
+
|
72
|
+
# See build_search. This is the same method but *without* protection. Do *NOT* pass in a params object to this method.
|
73
|
+
#
|
74
|
+
# This also has an alias "new_search!"
|
75
|
+
def build_search!(options = {}, &block)
|
76
|
+
search = searchlogic_search(options)
|
77
|
+
yield search if block_given?
|
78
|
+
search
|
79
|
+
end
|
80
|
+
|
81
|
+
# Similar to ActiveRecord's attr_protected, but for conditions. It will block any conditions in this array that are being mass assigned. Mass assignments are:
|
82
|
+
#
|
83
|
+
# === Examples
|
84
|
+
#
|
85
|
+
# search = User.new_search(:conditions => {:first_name_like => "Ben", :email_contains => "binarylogic.com"})
|
86
|
+
# search.options = {:conditions => {:first_name_like => "Ben", :email_contains => "binarylogic.com"}}
|
87
|
+
#
|
88
|
+
# If first_name_like is in the list of conditions_protected then it will be removed from the hash.
|
89
|
+
def conditions_protected(*conditions)
|
90
|
+
write_inheritable_attribute(:conditions_protected, Set.new(conditions.map(&:to_s)) + (protected_conditions || []))
|
91
|
+
end
|
92
|
+
|
93
|
+
def protected_conditions # :nodoc:
|
94
|
+
read_inheritable_attribute(:conditions_protected)
|
95
|
+
end
|
96
|
+
|
97
|
+
# This is the reverse of conditions_protected. You can specify conditions here and *only* these conditions will be allowed in mass assignment. Any condition not specified here will be blocked.
|
98
|
+
def conditions_accessible(*conditions)
|
99
|
+
write_inheritable_attribute(:conditions_accessible, Set.new(conditions.map(&:to_s)) + (accessible_conditions || []))
|
100
|
+
end
|
101
|
+
|
102
|
+
def accessible_conditions # :nodoc:
|
103
|
+
read_inheritable_attribute(:conditions_accessible)
|
104
|
+
end
|
105
|
+
|
106
|
+
private
|
107
|
+
def construct_finder_sql_with_included_associations_with_searchlogic(*args)
|
108
|
+
sql = construct_finder_sql_with_included_associations_without_searchlogic(*args)
|
109
|
+
remove_duplicate_joins(sql)
|
110
|
+
end
|
111
|
+
|
112
|
+
def construct_finder_sql_with_searchlogic(*args)
|
113
|
+
sql = construct_finder_sql_without_searchlogic(*args)
|
114
|
+
remove_duplicate_joins(sql)
|
115
|
+
end
|
116
|
+
|
117
|
+
def construct_calculation_sql_with_searchlogic(*args)
|
118
|
+
sql = construct_calculation_sql_without_searchlogic(*args)
|
119
|
+
remove_duplicate_joins(sql)
|
120
|
+
end
|
121
|
+
|
122
|
+
def remove_duplicate_joins(sql)
|
123
|
+
join_expr = /(LEFT OUTER JOIN|INNER JOIN)/i
|
124
|
+
sql_parts = sql.split(join_expr)
|
125
|
+
if !sql_parts.empty?
|
126
|
+
last_parts = sql_parts.pop.split(/ (?!ON|AS)([A-Z]+) /)
|
127
|
+
sql_parts += last_parts
|
128
|
+
is_join_statement = false
|
129
|
+
cleaned_parts = []
|
130
|
+
sql_parts.each do |part|
|
131
|
+
part = part.strip
|
132
|
+
if is_join_statement
|
133
|
+
if !includes_join?(cleaned_parts, part)
|
134
|
+
cleaned_parts << part
|
135
|
+
else
|
136
|
+
cleaned_parts.pop
|
137
|
+
end
|
138
|
+
else
|
139
|
+
cleaned_parts << part
|
140
|
+
end
|
141
|
+
is_join_statement = part =~ join_expr
|
142
|
+
end
|
143
|
+
sql = cleaned_parts.join(" ")
|
144
|
+
end
|
145
|
+
sql
|
146
|
+
end
|
147
|
+
|
148
|
+
def includes_join?(cleaned_parts, part)
|
149
|
+
cleaned_parts.each do |cleaned_part|
|
150
|
+
a = cleaned_part.gsub("`", "")
|
151
|
+
b = part.gsub("`", "")
|
152
|
+
return true if a == b
|
153
|
+
return true if a == b.gsub(/([a-z\._]*) = ([a-z\._]*)/, '\2 = \1')
|
154
|
+
end
|
155
|
+
false
|
156
|
+
end
|
157
|
+
|
158
|
+
def filter_options_with_searchlogic(options = {}, searching = true)
|
159
|
+
return options unless Searchlogic::Search::Base.needed?(self, options)
|
160
|
+
search = Searchlogic::Search::Base.create_virtual_class(self).new # call explicitly to avoid merging the scopes into the search
|
161
|
+
search.acting_as_filter = true
|
162
|
+
search.scope = scope(:find)
|
163
|
+
conditions = options.delete(:conditions) || options.delete("conditions") || {}
|
164
|
+
if conditions
|
165
|
+
case conditions
|
166
|
+
when Hash
|
167
|
+
conditions.each { |condition, value| search.conditions.send("#{condition}=", value) } # explicitly call to enforce blanks
|
168
|
+
else
|
169
|
+
search.conditions = conditions
|
170
|
+
end
|
171
|
+
end
|
172
|
+
search.options = options
|
173
|
+
search.sanitize(searching)
|
174
|
+
end
|
175
|
+
|
176
|
+
def searchlogic_search(options = {})
|
177
|
+
scope = {}
|
178
|
+
current_scope = scope(:find) && scope(:find).deep_dup
|
179
|
+
if current_scope
|
180
|
+
[:conditions, :include, :joins].each do |option|
|
181
|
+
value = current_scope.delete(option)
|
182
|
+
next if value.blank?
|
183
|
+
scope[option] = value
|
184
|
+
end
|
185
|
+
|
186
|
+
# Delete nil values in the scope, for some reason habtm relationships like to pass :limit => nil
|
187
|
+
new_scope = {}
|
188
|
+
current_scope.each { |k, v| new_scope[k] = v unless v.nil? }
|
189
|
+
current_scope = new_scope
|
190
|
+
end
|
191
|
+
search = Searchlogic::Search::Base.create_virtual_class(self).new
|
192
|
+
search.scope = scope
|
193
|
+
search.options = current_scope
|
194
|
+
search.options = options
|
195
|
+
search
|
196
|
+
end
|
197
|
+
end
|
198
|
+
end
|
199
|
+
end
|
200
|
+
|
201
|
+
ActiveRecord::Base.send(:extend, Searchlogic::ActiveRecord::Base)
|
202
|
+
|
203
|
+
module ActiveRecord #:nodoc: all
|
204
|
+
class Base
|
205
|
+
class << self
|
206
|
+
alias_method_chain :calculate, :searchlogic
|
207
|
+
alias_method_chain :construct_finder_sql, :searchlogic
|
208
|
+
alias_method_chain :construct_finder_sql_with_included_associations, :searchlogic
|
209
|
+
alias_method_chain :construct_calculation_sql, :searchlogic
|
210
|
+
alias_method_chain :find, :searchlogic
|
211
|
+
alias_method_chain :with_scope, :searchlogic
|
212
|
+
alias_method :new_search, :build_search
|
213
|
+
alias_method :new_search!, :build_search!
|
214
|
+
|
215
|
+
def valid_find_options
|
216
|
+
VALID_FIND_OPTIONS
|
217
|
+
end
|
218
|
+
|
219
|
+
def valid_calculations_options
|
220
|
+
Calculations::CALCULATIONS_OPTIONS
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
end
|
@@ -0,0 +1,176 @@
|
|
1
|
+
module Searchlogic
|
2
|
+
module ActiveRecord
|
3
|
+
module ConnectionAdapters # :nodoc: all
|
4
|
+
module MysqlAdapter
|
5
|
+
# Date / time functions
|
6
|
+
def microseconds_sql(column_name)
|
7
|
+
"MICROSECOND(#{column_name})"
|
8
|
+
end
|
9
|
+
|
10
|
+
def milliseconds_sql(column_name)
|
11
|
+
"(MICROSECOND(#{column_name}) / 1000)"
|
12
|
+
end
|
13
|
+
|
14
|
+
def second_sql(column_name)
|
15
|
+
"SECOND(#{column_name})"
|
16
|
+
end
|
17
|
+
|
18
|
+
def minute_sql(column_name)
|
19
|
+
"MINUTE(#{column_name})"
|
20
|
+
end
|
21
|
+
|
22
|
+
def hour_sql(column_name)
|
23
|
+
"HOUR(#{column_name})"
|
24
|
+
end
|
25
|
+
|
26
|
+
def day_of_week_sql(column_name)
|
27
|
+
"DAYOFWEEK(#{column_name})"
|
28
|
+
end
|
29
|
+
|
30
|
+
def day_of_month_sql(column_name)
|
31
|
+
"DAYOFMONTH(#{column_name})"
|
32
|
+
end
|
33
|
+
|
34
|
+
def day_of_year_sql(column_name)
|
35
|
+
"DAYOFYEAR(#{column_name})"
|
36
|
+
end
|
37
|
+
|
38
|
+
def week_sql(column_name)
|
39
|
+
"WEEK(#{column_name}, 2)"
|
40
|
+
end
|
41
|
+
|
42
|
+
def month_sql(column_name)
|
43
|
+
"MONTH(#{column_name})"
|
44
|
+
end
|
45
|
+
|
46
|
+
def year_sql(column_name)
|
47
|
+
"YEAR(#{column_name})"
|
48
|
+
end
|
49
|
+
|
50
|
+
# String functions
|
51
|
+
def char_length_sql(column_name)
|
52
|
+
"CHAR_LENGTH(#{column_name})"
|
53
|
+
end
|
54
|
+
|
55
|
+
def lower_sql(column_name)
|
56
|
+
"LOWER(#{column_name})"
|
57
|
+
end
|
58
|
+
|
59
|
+
def ltrim_sql(column_name)
|
60
|
+
"LTRIM(#{column_name})"
|
61
|
+
end
|
62
|
+
|
63
|
+
def md5_sql(column_name)
|
64
|
+
"MD5(#{column_name})"
|
65
|
+
end
|
66
|
+
|
67
|
+
def rtrim_sql(column_name)
|
68
|
+
"RTRIM(#{column_name})"
|
69
|
+
end
|
70
|
+
|
71
|
+
def trim_sql(column_name)
|
72
|
+
"TRIM(#{column_name})"
|
73
|
+
end
|
74
|
+
|
75
|
+
def upper_sql(column_name)
|
76
|
+
"UPPER(#{column_name})"
|
77
|
+
end
|
78
|
+
|
79
|
+
# Number functions
|
80
|
+
def absolute_sql(column_name)
|
81
|
+
"ABS(#{column_name})"
|
82
|
+
end
|
83
|
+
|
84
|
+
def acos_sql(column_name)
|
85
|
+
"ACOS(#{column_name})"
|
86
|
+
end
|
87
|
+
|
88
|
+
def asin_sql(column_name)
|
89
|
+
"ASIN(#{column_name})"
|
90
|
+
end
|
91
|
+
|
92
|
+
def atan_sql(column_name)
|
93
|
+
"ATAN(#{column_name})"
|
94
|
+
end
|
95
|
+
|
96
|
+
def avg_sql(column_name)
|
97
|
+
"AVG(#{column_name})"
|
98
|
+
end
|
99
|
+
|
100
|
+
def ceil_sql(column_name)
|
101
|
+
"CEIL(#{column_name})"
|
102
|
+
end
|
103
|
+
|
104
|
+
def cos_sql(column_name)
|
105
|
+
"COS(#{column_name})"
|
106
|
+
end
|
107
|
+
|
108
|
+
def cot_sql(column_name)
|
109
|
+
"COT(#{column_name})"
|
110
|
+
end
|
111
|
+
|
112
|
+
def degrees_sql(column_name)
|
113
|
+
"DEGREES(#{column_name})"
|
114
|
+
end
|
115
|
+
|
116
|
+
def exp_sql(column_name)
|
117
|
+
"EXP(#{column_name})"
|
118
|
+
end
|
119
|
+
|
120
|
+
def floor_sql(column_name)
|
121
|
+
"FLOOR(#{column_name})"
|
122
|
+
end
|
123
|
+
|
124
|
+
def hex_sql(column_name)
|
125
|
+
"HEX(#{column_name})"
|
126
|
+
end
|
127
|
+
|
128
|
+
def ln_sql(column_name)
|
129
|
+
"LN(#{column_name})"
|
130
|
+
end
|
131
|
+
|
132
|
+
def log_sql(column_name)
|
133
|
+
"LOG(#{column_name})"
|
134
|
+
end
|
135
|
+
|
136
|
+
def log2_sql(column_name)
|
137
|
+
"LOG2(#{column_name})"
|
138
|
+
end
|
139
|
+
|
140
|
+
def log10_sql(column_name)
|
141
|
+
"LOG10(#{column_name})"
|
142
|
+
end
|
143
|
+
|
144
|
+
def octal_sql(column_name)
|
145
|
+
"OCT(#{column_name})"
|
146
|
+
end
|
147
|
+
|
148
|
+
def radians_sql(column_name)
|
149
|
+
"RADIANS(#{column_name})"
|
150
|
+
end
|
151
|
+
|
152
|
+
def round_sql(column_name)
|
153
|
+
"ROUND(#{column_name})"
|
154
|
+
end
|
155
|
+
|
156
|
+
def sign_sql(column_name)
|
157
|
+
"SIGN(#{column_name})"
|
158
|
+
end
|
159
|
+
|
160
|
+
def sin_sql(column_name)
|
161
|
+
"SIN(#{column_name})"
|
162
|
+
end
|
163
|
+
|
164
|
+
def square_root_sql(column_name)
|
165
|
+
"SQRT(#{column_name})"
|
166
|
+
end
|
167
|
+
|
168
|
+
def tan_sql(column_name)
|
169
|
+
"TAN(#{column_name})"
|
170
|
+
end
|
171
|
+
end
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
::ActiveRecord::ConnectionAdapters::MysqlAdapter.send(:include, Searchlogic::ActiveRecord::ConnectionAdapters::MysqlAdapter)
|