searchgasm 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (36) hide show
  1. data/CHANGELOG.rdoc +12 -2
  2. data/Manifest +5 -4
  3. data/README.rdoc +15 -17
  4. data/Rakefile +1 -2
  5. data/lib/searchgasm/active_record/associations.rb +19 -31
  6. data/lib/searchgasm/active_record/base.rb +40 -9
  7. data/lib/searchgasm/condition/base.rb +1 -1
  8. data/lib/searchgasm/condition/inclusive_descendant_of.rb +0 -2
  9. data/lib/searchgasm/condition/sibling_of.rb +0 -2
  10. data/lib/searchgasm/conditions/base.rb +21 -34
  11. data/lib/searchgasm/conditions/protection.rb +8 -2
  12. data/lib/searchgasm/helpers/control_types/links.rb +9 -6
  13. data/lib/searchgasm/helpers/control_types/remote_link.rb +1 -0
  14. data/lib/searchgasm/helpers/control_types/remote_links.rb +3 -0
  15. data/lib/searchgasm/helpers/control_types/remote_select.rb +3 -0
  16. data/lib/searchgasm/helpers/control_types/select.rb +3 -0
  17. data/lib/searchgasm/helpers/form.rb +11 -4
  18. data/lib/searchgasm/search/base.rb +26 -46
  19. data/lib/searchgasm/search/conditions.rb +4 -1
  20. data/lib/searchgasm/search/pagination.rb +2 -1
  21. data/lib/searchgasm/search/scoping.rb +0 -0
  22. data/lib/searchgasm/search.rb +7 -0
  23. data/lib/searchgasm/shared/searching.rb +25 -0
  24. data/lib/searchgasm/shared/utilities.rb +32 -0
  25. data/lib/searchgasm/shared/virtual_classes.rb +39 -0
  26. data/lib/searchgasm/version.rb +1 -1
  27. data/lib/searchgasm.rb +6 -2
  28. data/searchgasm.gemspec +15 -10
  29. data/test/test_active_record_associations.rb +18 -1
  30. data/test/test_conditions_base.rb +19 -11
  31. data/test/test_search_base.rb +8 -8
  32. metadata +14 -9
  33. data/benchmarks/benchmark.rb +0 -43
  34. data/benchmarks/benchmark_helper.rb +0 -52
  35. data/benchmarks/profile.rb +0 -15
  36. data/lib/searchgasm/utilities.rb +0 -30
@@ -5,16 +5,15 @@ module Searchgasm #:nodoc:
5
5
  # Please refer the README.rdoc for usage, examples, and installation.
6
6
 
7
7
  class Base
8
- include Searchgasm::Utilities
8
+ include Searchgasm::Shared::Utilities
9
+ include Searchgasm::Shared::Searching
10
+ include Searchgasm::Shared::VirtualClasses
9
11
 
10
12
  # Options that ActiveRecord doesn't suppport, but Searchgasm does
11
13
  SPECIAL_FIND_OPTIONS = [:order_by, :order_as, :page, :per_page]
12
14
 
13
15
  # Valid options you can use when searching
14
- VALID_FIND_OPTIONS = ::ActiveRecord::Base.valid_find_options + SPECIAL_FIND_OPTIONS
15
-
16
- # Use these methods just like you would in ActiveRecord
17
- SEARCH_METHODS = [:all, :average, :calculate, :count, :find, :first, :maximum, :minimum, :sum]
16
+ VALID_FIND_OPTIONS = SPECIAL_FIND_OPTIONS + ::ActiveRecord::Base.valid_find_options # the order is very important, these options get set in this order
18
17
 
19
18
  attr_accessor *::ActiveRecord::Base.valid_find_options
20
19
 
@@ -25,51 +24,15 @@ module Searchgasm #:nodoc:
25
24
  SPECIAL_FIND_OPTIONS.each do |option|
26
25
  return true if options.symbolize_keys.keys.include?(option)
27
26
  end
28
-
27
+
29
28
  Searchgasm::Conditions::Base.needed?(model_class, options[:conditions])
30
29
  end
31
-
32
- # Creates virtual classes for the class passed to it. This is a neccesity for keeping dynamically created method
33
- # names specific to models. It provides caching and helps a lot with performance.
34
- def create_virtual_class(model_class)
35
- class_search_name = "::Searchgasm::Cache::#{model_class.name}Search"
36
-
37
- begin
38
- eval(class_search_name)
39
- rescue NameError
40
- # The method definitions are for performance, bottlenecks found with ruby-prof
41
- eval <<-end_eval
42
- class #{class_search_name} < ::Searchgasm::Search::Base
43
- def self.klass
44
- #{model_class.name}
45
- end
46
-
47
- def klass
48
- #{model_class.name}
49
- end
50
- end
51
-
52
- #{class_search_name}
53
- end_eval
54
- end
55
- end
56
30
  end
57
31
 
58
32
  def initialize(init_options = {})
59
33
  self.options = init_options
60
34
  end
61
35
 
62
- # Setup methods for searching
63
- SEARCH_METHODS.each do |method|
64
- class_eval <<-"end_eval", __FILE__, __LINE__
65
- def #{method}(*args)
66
- self.options = args.extract_options!
67
- args << sanitize(:#{method})
68
- klass.#{method}(*args)
69
- end
70
- end_eval
71
- end
72
-
73
36
  # Flag to determine if searchgasm is acting as a filter for the ActiveRecord search methods.
74
37
  # The purpose of this is to determine if Config.per_page should be implemented.
75
38
  def acting_as_filter=(value)
@@ -83,8 +46,14 @@ module Searchgasm #:nodoc:
83
46
 
84
47
  # Makes using searchgasm in the console less annoying and keeps the output meaningful and useful
85
48
  def inspect
86
- options_as_nice_string = ::ActiveRecord::Base.valid_find_options.collect { |name| "#{name}: #{send(name)}" }.join(", ")
87
- "#<#{klass} #{options_as_nice_string}>"
49
+ current_find_options = {}
50
+ ::ActiveRecord::Base.valid_find_options.each do |option|
51
+ value = send(option)
52
+ next if value.nil?
53
+ current_find_options[option] = value
54
+ end
55
+ current_find_options[:scope] = scope unless scope.blank?
56
+ "#<#{klass}Search #{current_find_options.inspect}>"
88
57
  end
89
58
 
90
59
  def limit=(value)
@@ -98,13 +67,20 @@ module Searchgasm #:nodoc:
98
67
  end
99
68
 
100
69
  def offset=(value)
101
- @offset = value.to_i
70
+ @offset = value.blank? ? nil : value.to_i
102
71
  end
103
72
 
104
73
  def options=(values)
105
74
  return unless values.is_a?(Hash)
106
75
  values.symbolize_keys.fast_assert_valid_keys(VALID_FIND_OPTIONS)
107
- values.each { |option, value| send("#{option}=", value) }
76
+
77
+ # Do the special options first, and then the core options last, since the core options take precendence
78
+ VALID_FIND_OPTIONS.each do |option|
79
+ next unless values.has_key?(option)
80
+ send("#{option}=", values[option])
81
+ end
82
+
83
+ values
108
84
  end
109
85
 
110
86
  # Sanitizes everything down into options ActiveRecord::Base.find can understand
@@ -117,6 +93,10 @@ module Searchgasm #:nodoc:
117
93
  end
118
94
  find_options
119
95
  end
96
+
97
+ def scope
98
+ @scope ||= {}
99
+ end
120
100
  end
121
101
  end
122
102
  end
@@ -53,7 +53,10 @@ module Searchgasm
53
53
 
54
54
  def sanitize_with_conditions(for_method = nil) # :nodoc:
55
55
  find_options = sanitize_without_conditions(for_method)
56
- find_options[:conditions] = find_options[:conditions].sanitize if find_options[:conditions]
56
+ if conditions_obj = find_options.delete(:conditions)
57
+ new_conditions = conditions_obj.sanitize
58
+ find_options[:conditions] = new_conditions unless new_conditions.blank?
59
+ end
57
60
  find_options
58
61
  end
59
62
 
@@ -35,7 +35,8 @@ module Searchgasm
35
35
  # Lets you change the page for the next search
36
36
  def page=(value)
37
37
  # Have to use @offset, since self.offset= resets @page
38
- if value.nil?
38
+ if value.blank?
39
+ value = nil
39
40
  @page = value
40
41
  return @offset = value
41
42
  end
File without changes
@@ -0,0 +1,7 @@
1
+ module Searchgasm
2
+ # = Searchgasm Shared Functionality
3
+ #
4
+ # Encapsulates shared functions / code that is used throughout searchgasm, so that we dont repeat ourself
5
+ module Shared
6
+ end
7
+ end
@@ -0,0 +1,25 @@
1
+ module Searchgasm
2
+ module Shared
3
+ # = Searchgasm Searching
4
+ #
5
+ # Implements searching functionality for searchgasm. Searchgasm::Search::Base and Searchgasm::Conditions::Base can both search and include
6
+ # this module.
7
+ module Searching
8
+ # Use these methods just like you would in ActiveRecord
9
+ SEARCH_METHODS = [:all, :average, :calculate, :count, :find, :first, :maximum, :minimum, :sum]
10
+
11
+ # Setup methods for searching
12
+ SEARCH_METHODS.each do |method|
13
+ class_eval <<-"end_eval", __FILE__, __LINE__
14
+ def #{method}(*args)
15
+ options = args.extract_options!
16
+ klass.send(:with_scope, :find => options) do
17
+ args << sanitize(:#{method})
18
+ klass.#{method}(*args)
19
+ end
20
+ end
21
+ end_eval
22
+ end
23
+ end
24
+ end
25
+ end
@@ -0,0 +1,32 @@
1
+ module Searchgasm
2
+ module Shared
3
+ module Utilities # :nodoc:
4
+ private
5
+ def merge_conditions(*conditions)
6
+ options = conditions.extract_options!
7
+ conditions.delete_if { |condition| condition.blank? }
8
+ return if conditions.blank?
9
+ return conditions.first if conditions.size == 1
10
+
11
+ conditions_strs = []
12
+ conditions_subs = []
13
+
14
+ conditions.each do |condition|
15
+ next if condition.blank?
16
+ arr_condition = [condition].flatten
17
+ conditions_strs << arr_condition.first
18
+ conditions_subs += arr_condition[1..-1]
19
+ end
20
+
21
+ return if conditions_strs.blank?
22
+
23
+ join = options[:any] ? "OR" : "AND"
24
+ conditions_str = "(#{conditions_strs.join(") #{join} (")})"
25
+
26
+ return conditions_str if conditions_subs.blank?
27
+
28
+ [conditions_str, *conditions_subs]
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,39 @@
1
+ module Searchgasm
2
+ module Shared
3
+ # = Searchgasm Virtual Classes
4
+ #
5
+ # Creates virtual classes for each model, to implementing a type of caching. So that object instantiation for searchgasm searches is cached. This is lazy, meaning
6
+ # it will only cache when it needs. So the first instantion will be much slow than the following ones. This is cached in the RAM, so if the process is restarted the caching is cleared.
7
+ module VirtualClasses
8
+ def self.included(klass)
9
+ klass.extend ClassMethods
10
+ end
11
+
12
+ module ClassMethods
13
+ # Creates virtual classes for the class passed to it. This is a neccesity for keeping dynamically created method
14
+ # names specific to models. It provides caching and helps a lot with performance.
15
+ def create_virtual_class(model_class)
16
+ class_search_name = "::Searchgasm::Cache::#{model_class.name}" + name.split(/::/)[1]
17
+
18
+ begin
19
+ eval(class_search_name)
20
+ rescue NameError
21
+ eval <<-end_eval
22
+ class #{class_search_name} < ::#{name}
23
+ def self.klass
24
+ #{model_class.name}
25
+ end
26
+
27
+ def klass
28
+ #{model_class.name}
29
+ end
30
+ end
31
+
32
+ #{class_search_name}
33
+ end_eval
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
@@ -67,7 +67,7 @@ module Searchgasm
67
67
 
68
68
  MAJOR = 1
69
69
  MINOR = 0
70
- TINY = 2
70
+ TINY = 3
71
71
 
72
72
  # The current version as a Version instance
73
73
  CURRENT = new(MAJOR, MINOR, TINY)
data/lib/searchgasm.rb CHANGED
@@ -6,10 +6,14 @@ require "active_support"
6
6
  # Core Ext
7
7
  require "searchgasm/core_ext/hash"
8
8
 
9
- # Utilties
9
+ # Shared
10
+ require "searchgasm/shared/utilities"
11
+ require "searchgasm/shared/searching"
12
+ require "searchgasm/shared/virtual_classes"
13
+
14
+ # Base classes
10
15
  require "searchgasm/version"
11
16
  require "searchgasm/config"
12
- require "searchgasm/utilities"
13
17
 
14
18
  # ActiveRecord
15
19
  require "searchgasm/active_record/base"
data/searchgasm.gemspec CHANGED
@@ -1,18 +1,18 @@
1
1
 
2
- # Gem::Specification for Searchgasm-1.0.2
2
+ # Gem::Specification for Searchgasm-1.0.3
3
3
  # Originally generated by Echoe
4
4
 
5
5
  --- !ruby/object:Gem::Specification
6
6
  name: searchgasm
7
7
  version: !ruby/object:Gem::Version
8
- version: 1.0.2
8
+ version: 1.0.3
9
9
  platform: ruby
10
10
  authors:
11
11
  - Ben Johnson of Binary Logic
12
12
  autorequire:
13
13
  bindir: bin
14
14
 
15
- date: 2008-09-12 00:00:00 -04:00
15
+ date: 2008-09-18 00:00:00 -04:00
16
16
  default_executable:
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
@@ -45,7 +45,7 @@ dependencies:
45
45
  - !ruby/object:Gem::Version
46
46
  version: "0"
47
47
  version:
48
- description: Makes ActiveRecord searching easier, robust, and powerful. Automatic conditions, pagination support, object based searching, and more.
48
+ description: Object based ActiveRecord searching, ordering, pagination, and more!
49
49
  email: bjohnson@binarylogic.com
50
50
  executables: []
51
51
 
@@ -91,14 +91,15 @@ extra_rdoc_files:
91
91
  - lib/searchgasm/search/ordering.rb
92
92
  - lib/searchgasm/search/pagination.rb
93
93
  - lib/searchgasm/search/protection.rb
94
- - lib/searchgasm/utilities.rb
94
+ - lib/searchgasm/search/scoping.rb
95
+ - lib/searchgasm/search.rb
96
+ - lib/searchgasm/shared/searching.rb
97
+ - lib/searchgasm/shared/utilities.rb
98
+ - lib/searchgasm/shared/virtual_classes.rb
95
99
  - lib/searchgasm/version.rb
96
100
  - lib/searchgasm.rb
97
101
  - README.rdoc
98
102
  files:
99
- - benchmarks/benchmark.rb
100
- - benchmarks/benchmark_helper.rb
101
- - benchmarks/profile.rb
102
103
  - CHANGELOG.rdoc
103
104
  - examples/README.rdoc
104
105
  - init.rb
@@ -140,7 +141,11 @@ files:
140
141
  - lib/searchgasm/search/ordering.rb
141
142
  - lib/searchgasm/search/pagination.rb
142
143
  - lib/searchgasm/search/protection.rb
143
- - lib/searchgasm/utilities.rb
144
+ - lib/searchgasm/search/scoping.rb
145
+ - lib/searchgasm/search.rb
146
+ - lib/searchgasm/shared/searching.rb
147
+ - lib/searchgasm/shared/utilities.rb
148
+ - lib/searchgasm/shared/virtual_classes.rb
144
149
  - lib/searchgasm/version.rb
145
150
  - lib/searchgasm.rb
146
151
  - Manifest
@@ -196,7 +201,7 @@ requirements: []
196
201
  rubyforge_project: searchgasm
197
202
  rubygems_version: 1.2.0
198
203
  specification_version: 2
199
- summary: Orgasmic ActiveRecord searching
204
+ summary: Object based ActiveRecord searching, ordering, pagination, and more!
200
205
  test_files:
201
206
  - test/test_active_record_associations.rb
202
207
  - test/test_active_record_base.rb
@@ -16,7 +16,7 @@ class TestActiveRecordAssociations < Test::Unit::TestCase
16
16
  search = Account.find(1).users.build_search
17
17
  assert_kind_of Searchgasm::Search::Base, search
18
18
  assert_equal User, search.klass
19
- assert_equal "\"users\".account_id = 1", search.conditions.scope
19
+ assert_equal "\"users\".account_id = 1", search.conditions.sql
20
20
 
21
21
  search.conditions.first_name_contains = "Ben"
22
22
  assert_equal({:conditions => ["(\"users\".\"first_name\" LIKE ?) AND (\"users\".account_id = 1)", "%Ben%"]}, search.sanitize)
@@ -35,4 +35,21 @@ class TestActiveRecordAssociations < Test::Unit::TestCase
35
35
  assert_equal 1, Account.find(1).users.sum("id", :conditions => {:first_name_begins_with => "Ben"})
36
36
  assert_equal 1, Account.find(1).users.average("id", :conditions => {:first_name_begins_with => "Ben"})
37
37
  end
38
+
39
+ def test_has_many_through
40
+ assert_equal 1, Account.find(1).orders.count
41
+ assert_equal 1, Account.find(1).orders.all(:conditions => {:total_gt => 100}).size
42
+ assert_equal 0, Account.find(1).orders.all(:conditions => {:total_gt => 1000}).size
43
+ assert_equal 1, Account.find(1).orders.sum("id", :conditions => {:total_gt => 100})
44
+ assert_equal 0, Account.find(1).orders.sum("id", :conditions => {:total_gt => 1000})
45
+ assert_equal 1, Account.find(1).orders.average("id", :conditions => {:total_gt => 100})
46
+ end
47
+
48
+ def test_habtm
49
+
50
+ end
51
+
52
+ def test_special_options
53
+ #order, see AR doc, etc
54
+ end
38
55
  end
@@ -42,10 +42,18 @@ class TestConditionsBase < Test::Unit::TestCase
42
42
  end
43
43
 
44
44
  def test_accessible_protected_conditions
45
- #Account.conditions_accessible << :name_contains
46
- #conditions = Account.new_conditions
47
- #conditions.conditions = {:created_after => Time.now, :name_contains => "Binary"}
48
- #assert({:name_contains => "Binary"}, conditions.value)
45
+ Account.conditions_accessible << :name_contains
46
+ conditions = Account.new_conditions
47
+ conditions.conditions = {:created_after => Time.now, :name_contains => "Binary"}
48
+ assert({:name_contains => "Binary"}, conditions.conditions)
49
+ Account.send(:write_inheritable_attribute, :conditions_accessible, nil)
50
+
51
+ Account.conditions_protected << :name_contains
52
+ conditions = Account.new_conditions
53
+ now = Time.now
54
+ conditions.conditions = {:created_after => now, :name_contains => "Binary"}
55
+ assert({:created_after => now}, conditions.conditions)
56
+ Account.send(:write_inheritable_attribute, :conditions_protected, nil)
49
57
  end
50
58
 
51
59
  def test_assert_valid_values
@@ -136,18 +144,18 @@ class TestConditionsBase < Test::Unit::TestCase
136
144
  conditions.conditions = v
137
145
  assert_equal v, conditions.conditions
138
146
 
139
- scope = "id in (1,2,3,4)"
140
- conditions.conditions = scope
141
- assert_equal v, conditions.conditions, v
142
- assert_equal scope, conditions.scope
147
+ sql = "id in (1,2,3,4)"
148
+ conditions.conditions = sql
149
+ assert_equal v, conditions.conditions
150
+ assert_equal sql, conditions.sql
143
151
 
144
152
  v2 = {:id_less_than => 5, :name_begins_with => "Beginning of string"}
145
153
  conditions.conditions = v2
146
154
  assert_equal v.merge(v2), conditions.conditions
147
155
 
148
- scope2 = "id > 5 and name = 'Test'"
149
- conditions.conditions = scope2
150
- assert_equal scope2, conditions.scope
156
+ sql2 = "id > 5 and name = 'Test'"
157
+ conditions.conditions = sql2
158
+ assert_equal sql2, conditions.sql
151
159
  end
152
160
 
153
161
  def test_searching
@@ -60,7 +60,7 @@ class TestSearchBase < Test::Unit::TestCase
60
60
  search.limit = 50
61
61
  assert_equal 2, search.page
62
62
  search.offset = nil
63
- assert_equal 0, search.offset
63
+ assert_equal nil, search.offset
64
64
  assert_equal 1, search.page
65
65
 
66
66
  search.per_page = 2
@@ -137,14 +137,14 @@ class TestSearchBase < Test::Unit::TestCase
137
137
 
138
138
  def test_scope
139
139
  search = Account.new_search!
140
- search.conditions = "some scope"
141
- assert_equal "some scope", search.conditions.scope
140
+ search.conditions = "some sql"
141
+ assert_equal "some sql", search.conditions.sql
142
142
  search.conditions = nil
143
- assert_equal nil, search.conditions.scope
144
- search.conditions = "some scope"
145
- assert_equal "some scope", search.conditions.scope
146
- search.conditions = "some scope2"
147
- assert_equal "some scope2", search.conditions.scope
143
+ assert_equal nil, search.conditions.sql
144
+ search.conditions = "some sql"
145
+ assert_equal "some sql", search.conditions.sql
146
+ search.conditions = "some sql"
147
+ assert_equal "some sql", search.conditions.sql
148
148
  end
149
149
 
150
150
  def test_searching
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: searchgasm
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
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: 2008-09-12 00:00:00 -04:00
12
+ date: 2008-09-18 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -42,7 +42,7 @@ dependencies:
42
42
  - !ruby/object:Gem::Version
43
43
  version: "0"
44
44
  version:
45
- description: Makes ActiveRecord searching easier, robust, and powerful. Automatic conditions, pagination support, object based searching, and more.
45
+ description: Object based ActiveRecord searching, ordering, pagination, and more!
46
46
  email: bjohnson@binarylogic.com
47
47
  executables: []
48
48
 
@@ -88,14 +88,15 @@ extra_rdoc_files:
88
88
  - lib/searchgasm/search/ordering.rb
89
89
  - lib/searchgasm/search/pagination.rb
90
90
  - lib/searchgasm/search/protection.rb
91
- - lib/searchgasm/utilities.rb
91
+ - lib/searchgasm/search/scoping.rb
92
+ - lib/searchgasm/search.rb
93
+ - lib/searchgasm/shared/searching.rb
94
+ - lib/searchgasm/shared/utilities.rb
95
+ - lib/searchgasm/shared/virtual_classes.rb
92
96
  - lib/searchgasm/version.rb
93
97
  - lib/searchgasm.rb
94
98
  - README.rdoc
95
99
  files:
96
- - benchmarks/benchmark.rb
97
- - benchmarks/benchmark_helper.rb
98
- - benchmarks/profile.rb
99
100
  - CHANGELOG.rdoc
100
101
  - examples/README.rdoc
101
102
  - init.rb
@@ -137,7 +138,11 @@ files:
137
138
  - lib/searchgasm/search/ordering.rb
138
139
  - lib/searchgasm/search/pagination.rb
139
140
  - lib/searchgasm/search/protection.rb
140
- - lib/searchgasm/utilities.rb
141
+ - lib/searchgasm/search/scoping.rb
142
+ - lib/searchgasm/search.rb
143
+ - lib/searchgasm/shared/searching.rb
144
+ - lib/searchgasm/shared/utilities.rb
145
+ - lib/searchgasm/shared/virtual_classes.rb
141
146
  - lib/searchgasm/version.rb
142
147
  - lib/searchgasm.rb
143
148
  - Manifest
@@ -194,7 +199,7 @@ rubyforge_project: searchgasm
194
199
  rubygems_version: 1.2.0
195
200
  signing_key:
196
201
  specification_version: 2
197
- summary: Orgasmic ActiveRecord searching
202
+ summary: Object based ActiveRecord searching, ordering, pagination, and more!
198
203
  test_files:
199
204
  - test/test_active_record_associations.rb
200
205
  - test/test_active_record_base.rb
@@ -1,43 +0,0 @@
1
- require File.dirname(__FILE__) + '/benchmark_helper.rb'
2
-
3
- times = 1
4
-
5
- Benchmark.bm(20) do |x|
6
- x.report("1st instantiation:") { Account.new_search }
7
- x.report("2nd instantiation:") { Account.new_search }
8
-
9
- # Now that we see the benefits of caching, lets cache the rest of the classes and perform the rest of the tests,
10
- # so that they are fair
11
- User.new_search
12
- Order.new_search
13
-
14
- x.report("Local ordering:") do
15
- times.times do
16
- Account.new_search(:order_by => :name).sanitize
17
- end
18
- end
19
-
20
- x.report("Advanced ordering:") do
21
- times.times do
22
- Account.new_search(:order_by => {:users => {:orders => :total}}).sanitize
23
- end
24
- end
25
-
26
- x.report("Local conditions:") do
27
- times.times do
28
- Account.new_search(:conditions => {:name_like => "Binary"}).sanitize
29
- end
30
- end
31
-
32
- x.report("Advanced conditions:") do
33
- times.times do
34
- Account.new_search(:conditions => {:users => {:orders => {:total_gt => 1}}}).sanitize
35
- end
36
- end
37
-
38
- x.report("Its complicated:") do
39
- times.times do
40
- Account.new_search(:conditions => {:users => {:orders => {:total_gt => 1, :created_at_after => Time.now}, :first_name_like => "Ben"}, :name_begins_with => "Awesome"}, :per_page => 20, :page => 2, :order_by => {:users => {:orders => :total}}, :order_as => "ASC").sanitize
41
- end
42
- end
43
- end
@@ -1,52 +0,0 @@
1
- require "rubygems"
2
- require "benchmark"
3
- require "ruby-prof"
4
- require "activerecord"
5
- require File.dirname(__FILE__) + '/../test/libs/acts_as_tree'
6
- require File.dirname(__FILE__) + '/../lib/searchgasm'
7
-
8
- ActiveRecord::Base.establish_connection(:adapter => "sqlite3", :dbfile => ":memory:")
9
-
10
- ActiveRecord::Schema.define(:version => 1) do
11
- create_table :accounts do |t|
12
- t.datetime :created_at
13
- t.datetime :updated_at
14
- t.string :name
15
- t.boolean :active
16
- end
17
-
18
- create_table :users do |t|
19
- t.datetime :created_at
20
- t.datetime :updated_at
21
- t.integer :account_id
22
- t.integer :parent_id
23
- t.string :first_name
24
- t.string :last_name
25
- t.boolean :active
26
- t.text :bio
27
- end
28
-
29
- create_table :orders do |t|
30
- t.datetime :created_at
31
- t.datetime :updated_at
32
- t.integer :user_id
33
- t.float :total
34
- t.text :description
35
- t.binary :receipt
36
- end
37
- end
38
-
39
- class Account < ActiveRecord::Base
40
- has_many :users, :dependent => :destroy
41
- has_many :orders, :through => :users
42
- end
43
-
44
- class User < ActiveRecord::Base
45
- acts_as_tree
46
- belongs_to :account
47
- has_many :orders, :dependent => :destroy
48
- end
49
-
50
- class Order < ActiveRecord::Base
51
- belongs_to :user
52
- end
@@ -1,15 +0,0 @@
1
- require File.dirname(__FILE__) + '/benchmark_helper.rb'
2
- require "ruby-prof"
3
-
4
- Account.new_search
5
- User.new_search
6
- Order.new_search
7
-
8
- RubyProf.start
9
-
10
- # Put profile code here
11
-
12
- result = RubyProf.stop
13
-
14
- printer = RubyProf::FlatPrinter.new(result)
15
- printer.print(STDOUT, 0)