searchgasm 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,5 @@
1
+ v0.9.4. Cleaned up search methods, removed reset! method for base and conditions.
2
+
1
3
  v0.9.3. Changed structure of conditions to have their own class. Making it easier to add your own conditions.
2
4
 
3
5
  v0.9.2. Enhanced protection
data/README.mdown CHANGED
@@ -113,6 +113,7 @@ Using the object from above:
113
113
  Or do it from your model:
114
114
 
115
115
  User.count(:conditions => {:first_name_contains => "Ben"})
116
+ User.sum('id', :conditions => {:first_name_contains => "Ben"})
116
117
  # ... all other calcualtions, etc.
117
118
 
118
119
  ## Different ways to search, take your pick
@@ -19,19 +19,26 @@ module BinaryLogic
19
19
  self.options = options
20
20
  end
21
21
 
22
+ # Setup methods for all options for finding
22
23
  (::ActiveRecord::Base.valid_find_options - [:conditions]).each do |option|
23
24
  class_eval <<-end_eval
24
25
  def #{option}(sanitize = false); options[:#{option}]; end
25
26
  def #{option}=(value); self.options[:#{option}] = value; end
26
27
  end_eval
27
28
  end
28
-
29
+
29
30
  alias_method :per_page, :limit
30
31
 
31
- def all
32
- klass.all(sanitize)
32
+ # Setup methods for searching
33
+ [:all, :average, :calculate, :count, :find, :first, :maximum, :minimum, :sum].each do |method|
34
+ class_eval <<-end_eval
35
+ def #{method}(*args)
36
+ self.options = args.extract_options!
37
+ args << sanitize
38
+ klass.#{method}(*args)
39
+ end
40
+ end_eval
33
41
  end
34
- alias_method :search, :all
35
42
 
36
43
  def conditions=(value)
37
44
  case value
@@ -46,18 +53,6 @@ module BinaryLogic
46
53
  sanitize ? @conditions.sanitize : @conditions
47
54
  end
48
55
 
49
- def find(target)
50
- case target
51
- when :all then all
52
- when :first then first
53
- else raise(ArgumentError, "The argument must be :all or :first")
54
- end
55
- end
56
-
57
- def first
58
- klass.first(sanitize)
59
- end
60
-
61
56
  def include(sanitize = false)
62
57
  includes = [self.options[:include], conditions.includes].flatten.compact
63
58
  includes.blank? ? nil : (includes.size == 1 ? includes.first : includes)
@@ -133,11 +128,6 @@ module BinaryLogic
133
128
  protect == true
134
129
  end
135
130
 
136
- def reset!
137
- conditions.reset!
138
- self.options = {}
139
- end
140
-
141
131
  def sanitize
142
132
  find_options = {}
143
133
  ::ActiveRecord::Base.valid_find_options.each do |find_option|
@@ -56,18 +56,6 @@ module BinaryLogic
56
56
  protect == true
57
57
  end
58
58
 
59
- def reset!
60
- dupped_objects = objects.dup
61
- dupped_objects.each do |object|
62
- if object.is_a?(self.class)
63
- send("reset_#{object.relationship_name}!")
64
- else
65
- send("reset_#{object.name}!")
66
- end
67
- end
68
- objects
69
- end
70
-
71
59
  def sanitize
72
60
  conditions = merge_conditions(*objects.collect { |object| object.sanitize })
73
61
  return scope if conditions.blank?
@@ -75,9 +63,6 @@ module BinaryLogic
75
63
  end
76
64
 
77
65
  def value=(values)
78
- reset!
79
- self.scope = nil
80
-
81
66
  case values
82
67
  when Hash
83
68
  assert_valid_values(values)
@@ -68,7 +68,7 @@ module BinaryLogic
68
68
 
69
69
  MAJOR = 0
70
70
  MINOR = 9
71
- TINY = 3
71
+ TINY = 4
72
72
 
73
73
  # The current version as a Version instance
74
74
  CURRENT = new(MAJOR, MINOR, TINY)
data/lib/searchgasm.rb CHANGED
@@ -26,4 +26,6 @@ require "searchgasm/search/condition_types/tree_condition"
26
26
  require "searchgasm/search/condition_types/child_of_condition"
27
27
  require "searchgasm/search/condition_types/descendant_of_condition"
28
28
  require "searchgasm/search/condition_types/inclusive_descendant_of_condition"
29
- require "searchgasm/search/condition_types/sibling_of_condition"
29
+ require "searchgasm/search/condition_types/sibling_of_condition"
30
+
31
+ Searchgasm = BinaryLogic::Searchgasm::Search
data/searchgasm.gemspec CHANGED
@@ -1,18 +1,18 @@
1
1
 
2
- # Gem::Specification for Searchgasm-0.9.3
2
+ # Gem::Specification for Searchgasm-0.9.4
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: 0.9.3
8
+ version: 0.9.4
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-02 00:00:00 -04:00
15
+ date: 2008-09-03 00:00:00 -04:00
16
16
  default_executable:
17
17
  dependencies:
18
18
  - !ruby/object:Gem::Dependency
@@ -102,10 +102,6 @@ class TestSearchgasmBase < Test::Unit::TestCase
102
102
 
103
103
  end
104
104
 
105
- def test_reset
106
-
107
- end
108
-
109
105
  def test_sanitize
110
106
  search = BinaryLogic::Searchgasm::Search::Base.new(Account)
111
107
  search.per_page = 2
@@ -146,6 +142,18 @@ class TestSearchgasmBase < Test::Unit::TestCase
146
142
  assert_equal search.find(:first), Account.find(1)
147
143
  end
148
144
 
145
+ def test_calculations
146
+ search = BinaryLogic::Searchgasm::Search::Base.new(Account)
147
+ search.conditions.name_like = "Binary"
148
+ assert_equal 2, search.average('id')
149
+ assert_equal 2, search.calculate(:avg, 'id')
150
+ assert_equal 3, search.calculate(:max, 'id')
151
+ assert_equal 2, search.count
152
+ assert_equal 3, search.maximum('id')
153
+ assert_equal 1, search.minimum('id')
154
+ assert_equal 4, search.sum('id')
155
+ end
156
+
149
157
  def test_protection
150
158
  assert_raise(ArgumentError) { Account.build_search(:conditions => "(DELETE FROM users)", :page => 2, :per_page => 15) }
151
159
  BinaryLogic::Searchgasm::Search::Base::VULNERABLE_OPTIONS.each { |option| assert_raise(ArgumentError) { Account.build_search(option => "(DELETE FROM users)") } }
@@ -82,22 +82,26 @@ class TestSearchgasmConditions < Test::Unit::TestCase
82
82
  conditions = BinaryLogic::Searchgasm::Search::Conditions.new(Account)
83
83
 
84
84
  conditions.name_contains = "Binary"
85
- assert_equal conditions.objects.size, 1
85
+ assert_equal 1, conditions.objects.size
86
86
 
87
87
  conditions.reset_name_contains!
88
- assert_equal conditions.objects, []
88
+ assert_equal [], conditions.objects
89
89
 
90
90
  conditions.users.first_name_like = "Ben"
91
- assert_equal conditions.objects.size, 1
91
+ assert_equal 1, conditions.objects.size
92
92
 
93
93
  conditions.reset_users!
94
- assert_equal conditions.objects, []
94
+ assert_equal [], conditions.objects
95
95
 
96
96
  conditions.name_begins_with ="Binary"
97
97
  conditions.users.orders.total_gt = 200
98
- conditions.users.first_name_keywords = "Silly name"
99
- conditions.reset!
100
- assert_equal conditions.objects, []
98
+ assert_equal 2, conditions.objects.size
99
+
100
+ conditions.reset_name_begins_with!
101
+ assert_equal 1, conditions.objects.size
102
+
103
+ conditions.reset_users!
104
+ assert_equal [], conditions.objects
101
105
  end
102
106
 
103
107
  def test_sanitize
@@ -118,12 +122,22 @@ class TestSearchgasmConditions < Test::Unit::TestCase
118
122
  def test_value
119
123
  conditions = BinaryLogic::Searchgasm::Search::Conditions.new(Account)
120
124
  now = Time.now
121
- conditions.value = {:name_like => "Binary", :created_at_after => now}
122
- assert_equal conditions.value, {:name_contains => "Binary", :created_at_greater_than => now}
123
-
124
- conditions.value = "id in (1,2,3,4)"
125
- assert_equal conditions.value, {}
126
- assert_equal conditions.scope, "id in (1,2,3,4)"
125
+ v = {:name_contains => "Binary", :created_at_greater_than => now}
126
+ conditions.value = v
127
+ assert_equal v, conditions.value
128
+
129
+ scope = "id in (1,2,3,4)"
130
+ conditions.value = scope
131
+ assert_equal v, conditions.value, v
132
+ assert_equal scope, conditions.scope
133
+
134
+ v2 = {:id_less_than => 5, :name_begins_with => "Beginning of string"}
135
+ conditions.value = v2
136
+ assert_equal v.merge(v2), conditions.value
137
+
138
+ scope2 = "id > 5 and name = 'Test'"
139
+ conditions.value = scope2
140
+ assert_equal scope2, conditions.scope
127
141
  end
128
142
 
129
143
  def test_protection
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: 0.9.3
4
+ version: 0.9.4
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-02 00:00:00 -04:00
12
+ date: 2008-09-03 00:00:00 -04:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency