searchlogic 1.6.6 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (178) hide show
  1. data/.gitignore +6 -0
  2. data/CHANGELOG.rdoc +17 -0
  3. data/{MIT-LICENSE → LICENSE} +2 -2
  4. data/README.rdoc +128 -379
  5. data/Rakefile +56 -20
  6. data/VERSION.yml +4 -0
  7. data/init.rb +1 -1
  8. data/lib/searchlogic.rb +18 -98
  9. data/lib/searchlogic/core_ext/object.rb +33 -13
  10. data/lib/searchlogic/core_ext/proc.rb +11 -0
  11. data/lib/searchlogic/named_scopes/alias_scope.rb +63 -0
  12. data/lib/searchlogic/named_scopes/associations.rb +126 -0
  13. data/lib/searchlogic/named_scopes/conditions.rb +215 -0
  14. data/lib/searchlogic/named_scopes/ordering.rb +53 -0
  15. data/lib/searchlogic/rails_helpers.rb +69 -0
  16. data/lib/searchlogic/search.rb +146 -0
  17. data/rails/init.rb +1 -0
  18. data/searchlogic.gemspec +69 -0
  19. data/spec/core_ext/object_spec.rb +7 -0
  20. data/spec/core_ext/proc_spec.rb +9 -0
  21. data/spec/named_scopes/alias_scope_spec.rb +15 -0
  22. data/spec/named_scopes/associations_spec.rb +120 -0
  23. data/spec/named_scopes/conditions_spec.rb +253 -0
  24. data/spec/named_scopes/ordering_spec.rb +23 -0
  25. data/spec/search_spec.rb +283 -0
  26. data/spec/spec_helper.rb +78 -0
  27. metadata +40 -231
  28. data/Manifest.txt +0 -158
  29. data/TODO.rdoc +0 -4
  30. data/lib/searchlogic/active_record/associations.rb +0 -52
  31. data/lib/searchlogic/active_record/base.rb +0 -224
  32. data/lib/searchlogic/active_record/connection_adapters/mysql_adapter.rb +0 -176
  33. data/lib/searchlogic/active_record/connection_adapters/postgresql_adapter.rb +0 -172
  34. data/lib/searchlogic/active_record/connection_adapters/sqlite_adapter.rb +0 -80
  35. data/lib/searchlogic/condition/base.rb +0 -165
  36. data/lib/searchlogic/condition/begins_with.rb +0 -17
  37. data/lib/searchlogic/condition/blank.rb +0 -24
  38. data/lib/searchlogic/condition/child_of.rb +0 -11
  39. data/lib/searchlogic/condition/descendant_of.rb +0 -11
  40. data/lib/searchlogic/condition/ends_with.rb +0 -17
  41. data/lib/searchlogic/condition/equals.rb +0 -33
  42. data/lib/searchlogic/condition/greater_than.rb +0 -15
  43. data/lib/searchlogic/condition/greater_than_or_equal_to.rb +0 -15
  44. data/lib/searchlogic/condition/inclusive_descendant_of.rb +0 -10
  45. data/lib/searchlogic/condition/keywords.rb +0 -52
  46. data/lib/searchlogic/condition/less_than.rb +0 -15
  47. data/lib/searchlogic/condition/less_than_or_equal_to.rb +0 -15
  48. data/lib/searchlogic/condition/like.rb +0 -15
  49. data/lib/searchlogic/condition/nested_set.rb +0 -17
  50. data/lib/searchlogic/condition/nil.rb +0 -21
  51. data/lib/searchlogic/condition/not_begin_with.rb +0 -20
  52. data/lib/searchlogic/condition/not_blank.rb +0 -19
  53. data/lib/searchlogic/condition/not_end_with.rb +0 -20
  54. data/lib/searchlogic/condition/not_equal.rb +0 -27
  55. data/lib/searchlogic/condition/not_have_keywords.rb +0 -20
  56. data/lib/searchlogic/condition/not_like.rb +0 -20
  57. data/lib/searchlogic/condition/not_nil.rb +0 -19
  58. data/lib/searchlogic/condition/sibling_of.rb +0 -14
  59. data/lib/searchlogic/conditions/any_or_all.rb +0 -42
  60. data/lib/searchlogic/conditions/base.rb +0 -244
  61. data/lib/searchlogic/conditions/groups.rb +0 -74
  62. data/lib/searchlogic/conditions/magic_methods.rb +0 -286
  63. data/lib/searchlogic/conditions/multiparameter_attributes.rb +0 -105
  64. data/lib/searchlogic/conditions/protection.rb +0 -36
  65. data/lib/searchlogic/config.rb +0 -31
  66. data/lib/searchlogic/config/helpers.rb +0 -338
  67. data/lib/searchlogic/config/search.rb +0 -53
  68. data/lib/searchlogic/core_ext/hash.rb +0 -75
  69. data/lib/searchlogic/helpers/control_types/link.rb +0 -310
  70. data/lib/searchlogic/helpers/control_types/links.rb +0 -242
  71. data/lib/searchlogic/helpers/control_types/remote_link.rb +0 -87
  72. data/lib/searchlogic/helpers/control_types/remote_links.rb +0 -72
  73. data/lib/searchlogic/helpers/control_types/remote_select.rb +0 -36
  74. data/lib/searchlogic/helpers/control_types/select.rb +0 -82
  75. data/lib/searchlogic/helpers/form.rb +0 -208
  76. data/lib/searchlogic/helpers/utilities.rb +0 -197
  77. data/lib/searchlogic/modifiers/absolute.rb +0 -15
  78. data/lib/searchlogic/modifiers/acos.rb +0 -11
  79. data/lib/searchlogic/modifiers/asin.rb +0 -11
  80. data/lib/searchlogic/modifiers/atan.rb +0 -11
  81. data/lib/searchlogic/modifiers/avg.rb +0 -15
  82. data/lib/searchlogic/modifiers/base.rb +0 -27
  83. data/lib/searchlogic/modifiers/ceil.rb +0 -15
  84. data/lib/searchlogic/modifiers/char_length.rb +0 -15
  85. data/lib/searchlogic/modifiers/cos.rb +0 -15
  86. data/lib/searchlogic/modifiers/cot.rb +0 -15
  87. data/lib/searchlogic/modifiers/count.rb +0 -11
  88. data/lib/searchlogic/modifiers/day_of_month.rb +0 -15
  89. data/lib/searchlogic/modifiers/day_of_week.rb +0 -15
  90. data/lib/searchlogic/modifiers/day_of_year.rb +0 -15
  91. data/lib/searchlogic/modifiers/degrees.rb +0 -11
  92. data/lib/searchlogic/modifiers/exp.rb +0 -15
  93. data/lib/searchlogic/modifiers/floor.rb +0 -15
  94. data/lib/searchlogic/modifiers/hex.rb +0 -11
  95. data/lib/searchlogic/modifiers/hour.rb +0 -11
  96. data/lib/searchlogic/modifiers/log.rb +0 -15
  97. data/lib/searchlogic/modifiers/log10.rb +0 -11
  98. data/lib/searchlogic/modifiers/log2.rb +0 -11
  99. data/lib/searchlogic/modifiers/lower.rb +0 -15
  100. data/lib/searchlogic/modifiers/ltrim.rb +0 -15
  101. data/lib/searchlogic/modifiers/md5.rb +0 -11
  102. data/lib/searchlogic/modifiers/microseconds.rb +0 -11
  103. data/lib/searchlogic/modifiers/milliseconds.rb +0 -11
  104. data/lib/searchlogic/modifiers/minute.rb +0 -15
  105. data/lib/searchlogic/modifiers/month.rb +0 -15
  106. data/lib/searchlogic/modifiers/octal.rb +0 -15
  107. data/lib/searchlogic/modifiers/radians.rb +0 -11
  108. data/lib/searchlogic/modifiers/round.rb +0 -11
  109. data/lib/searchlogic/modifiers/rtrim.rb +0 -15
  110. data/lib/searchlogic/modifiers/second.rb +0 -15
  111. data/lib/searchlogic/modifiers/sign.rb +0 -11
  112. data/lib/searchlogic/modifiers/sin.rb +0 -11
  113. data/lib/searchlogic/modifiers/square_root.rb +0 -15
  114. data/lib/searchlogic/modifiers/sum.rb +0 -11
  115. data/lib/searchlogic/modifiers/tan.rb +0 -15
  116. data/lib/searchlogic/modifiers/trim.rb +0 -15
  117. data/lib/searchlogic/modifiers/upper.rb +0 -15
  118. data/lib/searchlogic/modifiers/week.rb +0 -11
  119. data/lib/searchlogic/modifiers/year.rb +0 -11
  120. data/lib/searchlogic/search/base.rb +0 -148
  121. data/lib/searchlogic/search/conditions.rb +0 -53
  122. data/lib/searchlogic/search/ordering.rb +0 -244
  123. data/lib/searchlogic/search/pagination.rb +0 -121
  124. data/lib/searchlogic/search/protection.rb +0 -89
  125. data/lib/searchlogic/search/searching.rb +0 -32
  126. data/lib/searchlogic/shared/utilities.rb +0 -57
  127. data/lib/searchlogic/shared/virtual_classes.rb +0 -39
  128. data/lib/searchlogic/version.rb +0 -79
  129. data/test/active_record_tests/associations_test.rb +0 -94
  130. data/test/active_record_tests/base_test.rb +0 -115
  131. data/test/condition_tests/base_test.rb +0 -62
  132. data/test/condition_tests/begins_with_test.rb +0 -11
  133. data/test/condition_tests/blank_test.rb +0 -31
  134. data/test/condition_tests/child_of_test.rb +0 -17
  135. data/test/condition_tests/descendant_of_test.rb +0 -12
  136. data/test/condition_tests/ends_with_test.rb +0 -11
  137. data/test/condition_tests/equals_test.rb +0 -28
  138. data/test/condition_tests/greater_than_or_equal_to_test.rb +0 -11
  139. data/test/condition_tests/greater_than_test.rb +0 -11
  140. data/test/condition_tests/inclusive_descendant_of_test.rb +0 -12
  141. data/test/condition_tests/keywords_test.rb +0 -23
  142. data/test/condition_tests/less_than_or_equal_to_test.rb +0 -11
  143. data/test/condition_tests/less_than_test.rb +0 -11
  144. data/test/condition_tests/like_test.rb +0 -11
  145. data/test/condition_tests/nil_test.rb +0 -31
  146. data/test/condition_tests/not_begin_with_test.rb +0 -8
  147. data/test/condition_tests/not_blank_test.rb +0 -8
  148. data/test/condition_tests/not_end_with_test.rb +0 -8
  149. data/test/condition_tests/not_equal_test.rb +0 -19
  150. data/test/condition_tests/not_have_keywords_test.rb +0 -8
  151. data/test/condition_tests/not_like_test.rb +0 -8
  152. data/test/condition_tests/not_nil_test.rb +0 -13
  153. data/test/condition_tests/sibling_of_test.rb +0 -15
  154. data/test/conditions_tests/any_or_all_test.rb +0 -23
  155. data/test/conditions_tests/base_test.rb +0 -185
  156. data/test/conditions_tests/groups_test.rb +0 -68
  157. data/test/conditions_tests/magic_methods_test.rb +0 -36
  158. data/test/conditions_tests/multiparameter_attributes_test.rb +0 -15
  159. data/test/conditions_tests/protection_test.rb +0 -18
  160. data/test/config_test.rb +0 -23
  161. data/test/fixtures/accounts.yml +0 -12
  162. data/test/fixtures/animals.yml +0 -7
  163. data/test/fixtures/orders.yml +0 -12
  164. data/test/fixtures/user_groups.yml +0 -5
  165. data/test/fixtures/users.yml +0 -45
  166. data/test/libs/awesome_nested_set.rb +0 -545
  167. data/test/libs/awesome_nested_set/.autotest +0 -13
  168. data/test/libs/awesome_nested_set/compatability.rb +0 -29
  169. data/test/libs/awesome_nested_set/helper.rb +0 -40
  170. data/test/libs/awesome_nested_set/named_scope.rb +0 -140
  171. data/test/libs/rexml_fix.rb +0 -14
  172. data/test/modifier_tests/day_of_month_test.rb +0 -16
  173. data/test/search_tests/base_test.rb +0 -241
  174. data/test/search_tests/conditions_test.rb +0 -21
  175. data/test/search_tests/ordering_test.rb +0 -167
  176. data/test/search_tests/pagination_test.rb +0 -74
  177. data/test/search_tests/protection_test.rb +0 -26
  178. data/test/test_helper.rb +0 -122
@@ -1,13 +0,0 @@
1
- Autotest.add_hook :initialize do |at|
2
- at.clear_mappings
3
-
4
- at.add_mapping %r%^lib/(.*)\.rb$% do |_, m|
5
- at.files_matching %r%^test/#{m[1]}_test.rb$%
6
- end
7
-
8
- at.add_mapping(%r%^test/.*\.rb$%) {|filename, _| filename }
9
-
10
- at.add_mapping %r%^test/fixtures/(.*)s.yml% do |_, _|
11
- at.files_matching %r%^test/.*\.rb$%
12
- end
13
- end
@@ -1,29 +0,0 @@
1
- # Rails <2.x doesn't define #except
2
- class Hash #:nodoc:
3
- # Returns a new hash without the given keys.
4
- def except(*keys)
5
- clone.except!(*keys)
6
- end unless method_defined?(:except)
7
-
8
- # Replaces the hash without the given keys.
9
- def except!(*keys)
10
- keys.map! { |key| convert_key(key) } if respond_to?(:convert_key)
11
- keys.each { |key| delete(key) }
12
- self
13
- end unless method_defined?(:except!)
14
- end
15
-
16
- # NamedScope is new to Rails 2.1
17
- unless defined? ActiveRecord::NamedScope
18
- require 'awesome_nested_set/named_scope'
19
- ActiveRecord::Base.class_eval do
20
- include CollectiveIdea::NamedScope
21
- end
22
- end
23
-
24
- # Rails 1.2.x doesn't define #quoted_table_name
25
- class ActiveRecord::Base #:nodoc:
26
- def self.quoted_table_name
27
- self.connection.quote_column_name(self.table_name)
28
- end unless methods.include?('quoted_table_name')
29
- end
@@ -1,40 +0,0 @@
1
- module CollectiveIdea #:nodoc:
2
- module Acts #:nodoc:
3
- module NestedSet #:nodoc:
4
- # This module provides some helpers for the model classes using acts_as_nested_set.
5
- # It is included by default in all views.
6
- #
7
- module Helper
8
- # Returns options for select.
9
- # You can exclude some items from the tree.
10
- # You can pass a block receiving an item and returning the string displayed in the select.
11
- #
12
- # == Params
13
- # * +class_or_item+ - Class name or top level times
14
- # * +mover+ - The item that is being move, used to exlude impossible moves
15
- # * +&block+ - a block that will be used to display: { |item| ... item.name }
16
- #
17
- # == Usage
18
- #
19
- # <%= f.select :parent_id, nested_set_options(Category, @category) {|i|
20
- # "#{'–' * i.level} #{i.name}"
21
- # }) %>
22
- #
23
- def nested_set_options(class_or_item, mover = nil)
24
- class_or_item = class_or_item.roots if class_or_item.is_a?(Class)
25
- items = Array(class_or_item)
26
- result = []
27
- items.each do |root|
28
- result += root.self_and_descendants.map do |i|
29
- if mover.nil? || mover.new_record? || mover.move_possible?(i)
30
- [yield(i), i.id]
31
- end
32
- end.compact
33
- end
34
- result
35
- end
36
-
37
- end
38
- end
39
- end
40
- end
@@ -1,140 +0,0 @@
1
- # Taken from Rails 2.1
2
- module CollectiveIdea #:nodoc:
3
- module NamedScope #:nodoc:
4
- # All subclasses of ActiveRecord::Base have two named_scopes:
5
- # * <tt>all</tt>, which is similar to a <tt>find(:all)</tt> query, and
6
- # * <tt>scoped</tt>, which allows for the creation of anonymous scopes, on the fly:
7
- #
8
- # Shirt.scoped(:conditions => {:color => 'red'}).scoped(:include => :washing_instructions)
9
- #
10
- # These anonymous scopes tend to be useful when procedurally generating complex queries, where passing
11
- # intermediate values (scopes) around as first-class objects is convenient.
12
- def self.included(base)
13
- base.class_eval do
14
- extend ClassMethods
15
- named_scope :scoped, lambda { |scope| scope }
16
- end
17
- end
18
-
19
- module ClassMethods
20
- def scopes
21
- read_inheritable_attribute(:scopes) || write_inheritable_attribute(:scopes, {})
22
- end
23
-
24
- # Adds a class method for retrieving and querying objects. A scope represents a narrowing of a database query,
25
- # such as <tt>:conditions => {:color => :red}, :select => 'shirts.*', :include => :washing_instructions</tt>.
26
- #
27
- # class Shirt < ActiveRecord::Base
28
- # named_scope :red, :conditions => {:color => 'red'}
29
- # named_scope :dry_clean_only, :joins => :washing_instructions, :conditions => ['washing_instructions.dry_clean_only = ?', true]
30
- # end
31
- #
32
- # The above calls to <tt>named_scope</tt> define class methods <tt>Shirt.red</tt> and <tt>Shirt.dry_clean_only</tt>. <tt>Shirt.red</tt>,
33
- # in effect, represents the query <tt>Shirt.find(:all, :conditions => {:color => 'red'})</tt>.
34
- #
35
- # Unlike Shirt.find(...), however, the object returned by <tt>Shirt.red</tt> is not an Array; it resembles the association object
36
- # constructed by a <tt>has_many</tt> declaration. For instance, you can invoke <tt>Shirt.red.find(:first)</tt>, <tt>Shirt.red.count</tt>,
37
- # <tt>Shirt.red.find(:all, :conditions => {:size => 'small'})</tt>. Also, just
38
- # as with the association objects, name scopes acts like an Array, implementing Enumerable; <tt>Shirt.red.each(&block)</tt>,
39
- # <tt>Shirt.red.first</tt>, and <tt>Shirt.red.inject(memo, &block)</tt> all behave as if Shirt.red really were an Array.
40
- #
41
- # These named scopes are composable. For instance, <tt>Shirt.red.dry_clean_only</tt> will produce all shirts that are both red and dry clean only.
42
- # Nested finds and calculations also work with these compositions: <tt>Shirt.red.dry_clean_only.count</tt> returns the number of garments
43
- # for which these criteria obtain. Similarly with <tt>Shirt.red.dry_clean_only.average(:thread_count)</tt>.
44
- #
45
- # All scopes are available as class methods on the ActiveRecord descendent upon which the scopes were defined. But they are also available to
46
- # <tt>has_many</tt> associations. If,
47
- #
48
- # class Person < ActiveRecord::Base
49
- # has_many :shirts
50
- # end
51
- #
52
- # then <tt>elton.shirts.red.dry_clean_only</tt> will return all of Elton's red, dry clean
53
- # only shirts.
54
- #
55
- # Named scopes can also be procedural.
56
- #
57
- # class Shirt < ActiveRecord::Base
58
- # named_scope :colored, lambda { |color|
59
- # { :conditions => { :color => color } }
60
- # }
61
- # end
62
- #
63
- # In this example, <tt>Shirt.colored('puce')</tt> finds all puce shirts.
64
- #
65
- # Named scopes can also have extensions, just as with <tt>has_many</tt> declarations:
66
- #
67
- # class Shirt < ActiveRecord::Base
68
- # named_scope :red, :conditions => {:color => 'red'} do
69
- # def dom_id
70
- # 'red_shirts'
71
- # end
72
- # end
73
- # end
74
- #
75
- #
76
- # For testing complex named scopes, you can examine the scoping options using the
77
- # <tt>proxy_options</tt> method on the proxy itself.
78
- #
79
- # class Shirt < ActiveRecord::Base
80
- # named_scope :colored, lambda { |color|
81
- # { :conditions => { :color => color } }
82
- # }
83
- # end
84
- #
85
- # expected_options = { :conditions => { :colored => 'red' } }
86
- # assert_equal expected_options, Shirt.colored('red').proxy_options
87
- def named_scope(name, options = {}, &block)
88
- scopes[name] = lambda do |parent_scope, *args|
89
- Scope.new(parent_scope, case options
90
- when Hash
91
- options
92
- when Proc
93
- options.call(*args)
94
- end, &block)
95
- end
96
- (class << self; self end).instance_eval do
97
- define_method name do |*args|
98
- scopes[name].call(self, *args)
99
- end
100
- end
101
- end
102
- end
103
-
104
- class Scope
105
- attr_reader :proxy_scope, :proxy_options
106
- [].methods.each { |m| delegate m, :to => :proxy_found unless m =~ /(^__|^nil\?|^send|class|extend|find|count|sum|average|maximum|minimum|paginate)/ }
107
- delegate :scopes, :with_scope, :to => :proxy_scope
108
-
109
- def initialize(proxy_scope, options, &block)
110
- [options[:extend]].flatten.each { |extension| extend extension } if options[:extend]
111
- extend Module.new(&block) if block_given?
112
- @proxy_scope, @proxy_options = proxy_scope, options.except(:extend)
113
- end
114
-
115
- def reload
116
- load_found; self
117
- end
118
-
119
- protected
120
- def proxy_found
121
- @found || load_found
122
- end
123
-
124
- private
125
- def method_missing(method, *args, &block)
126
- if scopes.include?(method)
127
- scopes[method].call(self, *args)
128
- else
129
- with_scope :find => proxy_options do
130
- proxy_scope.send(method, *args, &block)
131
- end
132
- end
133
- end
134
-
135
- def load_found
136
- @found = find(:all)
137
- end
138
- end
139
- end
140
- end
@@ -1,14 +0,0 @@
1
- module REXML
2
- module Formatters
3
- class Pretty < Default
4
- private
5
- def wrap(string, width)
6
- # Recursivly wrap string at width.
7
- return string if string.length <= width
8
- place = string.rindex(/\s+/, width) # Position in string with last ' ' before cutoff
9
- return string if place.nil?
10
- return string[0,place] + "\n" + wrap(string[place+1..-1], width)
11
- end
12
- end
13
- end
14
- end
@@ -1,16 +0,0 @@
1
- require File.dirname(__FILE__) + '/../test_helper.rb'
2
-
3
- module ConditionsTests
4
- class DayOfMonthTest < ActiveSupport::TestCase
5
- def test_modifier_names
6
-
7
- end
8
-
9
- def test_usage
10
- search = User.new_search
11
- search.conditions.dom_of_created_at_gt = "1"
12
- assert_equal({:conditions => ["(strftime('%d', \"users\".\"created_at\") * 1) > ?", 1], :limit => 25}, search.sanitize)
13
- search.all
14
- end
15
- end
16
- end
@@ -1,241 +0,0 @@
1
- require File.dirname(__FILE__) + '/../test_helper.rb'
2
-
3
- module SearchTests
4
- class BaseTest < ActiveSupport::TestCase
5
- def test_needed
6
- assert Searchlogic::Search::Base.needed?(Account, :page => 2, :conditions => {:name => "Ben"})
7
- assert !Searchlogic::Search::Base.needed?(Account, :conditions => {:name => "Ben"})
8
- assert Searchlogic::Search::Base.needed?(Account, :limit => 2, :conditions => {:name_contains => "Ben"})
9
- assert !Searchlogic::Search::Base.needed?(Account, :limit => 2)
10
- assert Searchlogic::Search::Base.needed?(Account, :per_page => 2)
11
- end
12
-
13
- def test_initialize
14
- assert_nothing_raised { Account.new_search }
15
- search = Account.new_search!(:conditions => {:name_like => "binary"}, :page => 2, :limit => 10, :readonly => true)
16
- assert_equal Account, search.klass
17
- assert_equal "binary", search.conditions.name_like
18
- assert_equal 2, search.page
19
- assert_equal 10, search.limit
20
- assert_equal true, search.readonly
21
- end
22
-
23
- def test_acting_as_filter
24
- search = Account.new_search
25
- search.acting_as_filter = true
26
- assert search.acting_as_filter?
27
- search.acting_as_filter = false
28
- assert !search.acting_as_filter?
29
- end
30
-
31
- def test_setting_first_level_options
32
- search = Account.new_search!(:include => :users, :joins => :users, :offset => 5, :limit => 20, :order => "name ASC", :select => "name", :readonly => true, :group => "name", :from => "accounts", :lock => true)
33
- assert_equal :users, search.include
34
- assert_equal :users, search.joins
35
- assert_equal 5, search.offset
36
- assert_equal 20, search.limit
37
- assert_equal "name ASC", search.order
38
- assert_equal "name", search.select
39
- assert_equal true, search.readonly
40
- assert_equal "name", search.group
41
- assert_equal "accounts", search.from
42
- assert_equal true, search.lock
43
-
44
- search = Account.new_search(:per_page => nil)
45
-
46
- search.include = :users
47
- assert_equal :users, search.include
48
-
49
- # treat it like SQL, just like AR
50
- search.joins = "users"
51
- assert_equal "users", search.joins
52
-
53
- search.page = 5
54
- assert_equal 1, search.page
55
- assert_nil search.offset
56
-
57
- search.limit = 20
58
- assert_equal search.limit, 20
59
- assert_equal search.per_page, 20
60
- assert_equal search.page, 5
61
- assert_equal search.offset, 80
62
- search.limit = nil
63
- assert_nil search.limit
64
- assert_nil search.per_page
65
- assert_equal 1, search.page
66
- assert_nil search.offset
67
-
68
- search.offset = 50
69
- assert_equal 50, search.offset
70
- assert_equal 1, search.page
71
- search.limit = 50
72
- assert_equal 2, search.page
73
- search.offset = nil
74
- assert_nil search.offset
75
- assert_equal 1, search.page
76
-
77
- search.per_page = 2
78
- assert_equal 2, search.per_page
79
- assert_equal 2, search.limit
80
- search.offset = 50
81
- assert_equal 26, search.page
82
- assert_equal 50, search.offset
83
-
84
- search.order = "name ASC"
85
- assert_equal search.order, "name ASC"
86
-
87
- search.select = "name"
88
- assert_equal search.select, "name"
89
-
90
- search.readonly = true
91
- assert_equal search.readonly, true
92
-
93
- search.group = "name"
94
- assert_equal search.group, "name"
95
-
96
- search.from = "accounts"
97
- assert_equal search.from, "accounts"
98
-
99
- search.lock = true
100
- assert_equal search.lock, true
101
- end
102
-
103
- def test_joins
104
- search = Account.new_search
105
- assert_nil search.joins
106
- search.conditions.name_contains = "Binary"
107
- assert_nil search.joins
108
- search.conditions.users.first_name_contains = "Ben"
109
- assert_equal(:users, search.joins)
110
- search.conditions.users.orders.id_gt = 2
111
- assert_equal({:users => :orders}, search.joins)
112
- search.conditions.users.reset_orders!
113
- assert_equal(:users, search.joins)
114
- search.conditions.users.orders.id_gt = 2
115
- search.conditions.reset_users!
116
- assert_nil search.joins
117
- end
118
-
119
- def test_limit
120
- search = Account.new_search
121
- search.limit = 10
122
- assert_equal 10, search.limit
123
- search.page = 2
124
- assert_equal 10, search.offset
125
- search.limit = 25
126
- assert_equal 25, search.offset
127
- assert_equal 2, search.page
128
- search.page = 5
129
- assert_equal 5, search.page
130
- assert_equal 25, search.limit
131
- search.limit = 3
132
- assert_equal 12, search.offset
133
- end
134
-
135
- def test_options
136
- end
137
-
138
- def test_sanitize
139
- search = Account.new_search
140
- search.per_page = 2
141
- search.conditions.name_like = "Binary"
142
- search.conditions.users.id_greater_than = 2
143
- search.page = 3
144
- search.readonly = true
145
- assert_equal({:select => "DISTINCT \"accounts\".*", :joins => :users, :offset => 4, :readonly => true, :conditions => ["\"accounts\".\"name\" LIKE ? AND \"users\".\"id\" > ?", "%Binary%", 2], :limit => 2 }, search.sanitize)
146
- end
147
-
148
- def test_scope
149
- search = Account.new_search!
150
- search.conditions = "some sql"
151
- conditions = search.conditions
152
- assert_equal "some sql", search.conditions.conditions
153
- search.conditions = nil
154
- assert_equal({}, search.conditions.conditions)
155
- search.conditions = "some sql"
156
- assert_equal "some sql", search.conditions.conditions
157
- search.conditions = "some sql"
158
- assert_equal "some sql", search.conditions.conditions
159
- end
160
-
161
- def test_searching
162
- binary_logic = accounts(:binary_logic)
163
- neco = accounts(:neco)
164
- binary_fun = accounts(:binary_fun)
165
-
166
- search = Account.new_search
167
- search.conditions.name_like = "Binary"
168
- assert_equal [binary_logic, binary_fun], search.all
169
- assert_equal [binary_logic, binary_fun], search.find(:all)
170
- assert_equal binary_logic, search.first
171
- assert_equal binary_logic, search.find(:first)
172
-
173
- search.per_page = 20
174
- search.page = 2
175
-
176
- assert_equal [], search.all
177
- assert_equal [], search.find(:all)
178
- assert_nil search.first
179
- assert_nil search.find(:first)
180
-
181
- search.per_page = 0
182
- search.page = nil
183
- search.conditions.users.first_name_contains = "Ben"
184
- search.conditions.users.orders.description_keywords = "products, &*ap#ple $%^&*"
185
- assert_equal [binary_logic], search.all
186
- assert_equal [binary_logic], search.find(:all)
187
- assert_equal binary_logic, search.first
188
- assert_equal binary_logic, search.find(:first)
189
-
190
- search = Account.new_search
191
- search.select = "id, name"
192
- assert_equal Account.all, search.all
193
-
194
- search = Account.scope1.new_search!(:conditions => {:users => {:first_name_starts_with => "Ben"}})
195
- assert_equal [binary_logic], search.all
196
- search2 = search.dup
197
- assert_equal [binary_logic], search2.all
198
- end
199
-
200
- def test_calculations
201
- binary_logic = accounts(:binary_logic)
202
- binary_fun = accounts(:binary_fun)
203
- bens_order = orders(:bens_order)
204
-
205
- search = Account.new_search
206
- search.conditions.name_like = "Binary"
207
- assert_equal (binary_logic.id + binary_fun.id) / 2.0, search.average('id')
208
- assert_equal (binary_logic.id + binary_fun.id) / 2.0, search.calculate(:avg, 'id')
209
- assert_equal binary_fun.id, search.calculate(:max, 'id')
210
- assert_equal 2, search.count
211
- assert_equal binary_fun.id, search.maximum('id')
212
- assert_equal binary_logic.id, search.minimum('id')
213
- assert_equal binary_logic.id + binary_fun.id, search.sum('id')
214
-
215
- search.readonly = true
216
- assert_equal binary_logic.id + binary_fun.id, search.sum('id')
217
-
218
- search = Account.new_search(:conditions => {:users => {:orders => {:id_gt => bens_order.id}}})
219
- assert_equal 1, search.count
220
-
221
- search = Order.new_search(:conditions => {:user => {:account => {:id_gt => binary_logic.id}}})
222
- assert_equal 1, search.count
223
-
224
- search = UserGroup.new_search(:conditions => {:users => {:orders => {:id_gt => bens_order.id}}})
225
- assert_equal 1, search.count
226
- end
227
-
228
- def test_inspect
229
- search = Account.new_search
230
- assert_nothing_raised { search.inspect }
231
- end
232
-
233
- def test_sti
234
-
235
- end
236
-
237
- def test_include_in_relationships
238
-
239
- end
240
- end
241
- end