meta_search 0.5.3 → 0.5.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/CHANGELOG CHANGED
@@ -1,3 +1,8 @@
1
+ Changes since 0.5.3 (2010-07-26):
2
+ *Add is_true/is_false for boolean columns
3
+ * Add is_present and is_blank for non-boolean columns
4
+ * Fix behavior of equals when used with boolean columns.
5
+
1
6
  Changes since 0.5.2 (2010-07-22):
2
7
  * Handle nested/namespaced form_for better. Formerly, you could use
3
8
  "form_for @search" in a view, but not "form_for [:admin, @search]"
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.3
1
+ 0.5.4
@@ -32,6 +32,16 @@ module MetaSearch
32
32
  # * _less_than_ (alias: _lt_) - Less than.
33
33
  # * _less_than_or_equal_to_ (aliases: _lte_, _lteq_) - Less than or equal to.
34
34
  #
35
+ # === Booleans
36
+ #
37
+ # * _is_true_ - Is true. Useful for a checkbox like "only show admin users".
38
+ # * _is_false_ - The complement of _is_true_.
39
+ #
40
+ # === Non-boolean data types
41
+ #
42
+ # * _is_present_ - As with _is_true_, useful with a checkbox. Not NULL or the empty string.
43
+ # * _is_blank_ - Returns records with a value of NULL or the empty string in the column.
44
+ #
35
45
  # So, given a model like this...
36
46
  #
37
47
  # class Article < ActiveRecord::Base
@@ -65,12 +75,17 @@ module MetaSearch
65
75
  @validator = where[:validator]
66
76
  @formatter = where[:formatter]
67
77
  @splat_param = where[:splat_param]
78
+ @skip_compounds = where[:skip_compounds]
68
79
  end
69
80
 
70
81
  def splat_param?
71
82
  !!@splat_param
72
83
  end
73
84
 
85
+ def skip_compounds?
86
+ !!@skip_compounds
87
+ end
88
+
74
89
  # Format a parameter for searching using the Where's defined formatter.
75
90
  def format_param(param)
76
91
  formatter.call(param)
@@ -135,25 +150,19 @@ module MetaSearch
135
150
  # false, but is used when automatically creating compound Wheres (*_any, *_all) so that the
136
151
  # Arel attribute method gets the correct parameter list.
137
152
  #
153
+ # <tt>skip_compounds</tt> will prevent creation of compound condition methods (ending in
154
+ # _any_ or _all_) for situations where they wouldn't make sense, such as the built-in
155
+ # conditions <tt>is_true</tt> and <tt>is_false</tt>.
156
+ #
138
157
  # <tt>cast</tt> will override the normal cast of the parameter when using this Where
139
158
  # condition. Normally, the value supplied to a condition is cast to the type of the column
140
159
  # it's being compared against. In cases where this isn't desirable, because the value you
141
160
  # intend to accept isn't the same kind of data you'll be comparing against, you can override
142
- # that cast here. For example, this is a Where that you can use with a check_box to allow
143
- # searching for records with a blank column. It's important to always cast to integer here,
144
- # as otherwise the default checked check_box value, "1", would cast to an invalid date when
145
- # searching against date columns, and fall back to nil, making your check_box do nothing:
146
- #
147
- # MetaSearch::Where.add :is_blank,
148
- # :predicate => :in,
149
- # :cast => :integer,
150
- # :formatter => Proc.new {|param| [nil, '']},
151
- # :validator => Proc.new {|param|
152
- # !param.zero?
153
- # }
161
+ # that cast here, using one of the standard DB type symbols such as :integer, :string, :boolean
162
+ # and so on.
154
163
  def add(*args)
155
164
  where = create_where_from_args(*args)
156
- create_where_compounds_for(where)
165
+ create_where_compounds_for(where) unless where.skip_compounds?
157
166
  end
158
167
 
159
168
  # Returns the complete array of Wheres
@@ -193,6 +202,7 @@ module MetaSearch
193
202
  opts[:cast] = opts[:cast]
194
203
  opts[:predicate] ||= :eq
195
204
  opts[:splat_param] ||= false
205
+ opts[:skip_compounds] ||= false
196
206
  opts[:formatter] ||= Proc.new {|param| param}
197
207
  if opts[:formatter].is_a?(String)
198
208
  formatter = opts[:formatter]
data/lib/meta_search.rb CHANGED
@@ -10,7 +10,7 @@ module MetaSearch
10
10
  MAX_JOIN_DEPTH = 5
11
11
 
12
12
  DEFAULT_WHERES = [
13
- ['equals', 'eq'],
13
+ ['equals', 'eq', {:validator => Proc.new {|param| !param.blank? || (param == false)}}],
14
14
  ['does_not_equal', 'ne', 'not_eq', {:types => ALL_TYPES, :predicate => :not_eq}],
15
15
  ['contains', 'like', 'matches', {:types => STRINGS, :predicate => :matches, :formatter => '"%#{param}%"'}],
16
16
  ['does_not_contain', 'nlike', 'not_matches', {:types => STRINGS, :predicate => :not_matches, :formatter => '"%#{param}%"'}],
@@ -23,7 +23,11 @@ module MetaSearch
23
23
  ['greater_than_or_equal_to', 'gte', 'gteq', {:types => (NUMBERS + DATES + TIMES), :predicate => :gteq}],
24
24
  ['less_than_or_equal_to', 'lte', 'lteq', {:types => (NUMBERS + DATES + TIMES), :predicate => :lteq}],
25
25
  ['in', {:types => ALL_TYPES, :predicate => :in}],
26
- ['not_in', 'ni', 'not_in', {:types => ALL_TYPES, :predicate => :not_in}]
26
+ ['not_in', 'ni', 'not_in', {:types => ALL_TYPES, :predicate => :not_in}],
27
+ ['is_true', {:types => BOOLEANS, :skip_compounds => true}],
28
+ ['is_false', {:types => BOOLEANS, :skip_compounds => true, :formatter => Proc.new {|param| !param}}],
29
+ ['is_present', {:types => (ALL_TYPES - BOOLEANS), :predicate => :not_eq_all, :splat_param => true, :skip_compounds => true, :cast => :boolean, :formatter => Proc.new {|param| [nil, '']}}],
30
+ ['is_blank', {:types => (ALL_TYPES - BOOLEANS), :predicate => :eq_any, :splat_param => true, :skip_compounds => true, :cast => :boolean, :formatter => Proc.new {|param| [nil, '']}}]
27
31
  ]
28
32
 
29
33
  RELATION_METHODS = [:joins, :includes, :to_a, :all, :count, :to_sql, :paginate, :autojoin, :find_each, :first, :last, :each, :arel]
data/meta_search.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{meta_search}
8
- s.version = "0.5.3"
8
+ s.version = "0.5.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Ernie Miller"]
12
- s.date = %q{2010-07-26}
12
+ s.date = %q{2010-07-28}
13
13
  s.description = %q{
14
14
  Allows simple search forms to be created against an AR3 model
15
15
  and its associations, has useful view helpers for sort links
@@ -62,7 +62,7 @@ Gem::Specification.new do |s|
62
62
  s.homepage = %q{http://metautonomo.us/projects/metasearch/}
63
63
  s.rdoc_options = ["--charset=UTF-8"]
64
64
  s.require_paths = ["lib"]
65
- s.rubygems_version = %q{1.3.6}
65
+ s.rubygems_version = %q{1.3.7}
66
66
  s.summary = %q{ActiveRecord 3 object-based searching for your form_for enjoyment.}
67
67
  s.test_files = [
68
68
  "test/fixtures/company.rb",
@@ -80,7 +80,7 @@ Gem::Specification.new do |s|
80
80
  current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
81
81
  s.specification_version = 3
82
82
 
83
- if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
83
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
84
84
  s.add_development_dependency(%q<shoulda>, [">= 0"])
85
85
  s.add_runtime_dependency(%q<activerecord>, [">= 3.0.0.beta4"])
86
86
  s.add_runtime_dependency(%q<activesupport>, [">= 3.0.0.beta4"])
@@ -7,18 +7,28 @@ virus:
7
7
  estimated_hours: 80
8
8
  name : Virus
9
9
  id : 2
10
-
10
+
11
11
  awesome:
12
12
  estimated_hours: 100
13
13
  name : Do something awesome
14
14
  id : 3
15
-
15
+
16
16
  metasearch:
17
17
  estimated_hours: 100
18
18
  name : MetaSearch Development
19
19
  id : 4
20
-
20
+
21
21
  another:
22
22
  estimated_hours: 120
23
23
  name : Another Project
24
- id : 5
24
+ id : 5
25
+
26
+ nil:
27
+ estimated_hours: 1000
28
+ name :
29
+ id : 6
30
+
31
+ blank:
32
+ estimated_hours: 1000
33
+ name : ""
34
+ id : 7
data/test/test_search.rb CHANGED
@@ -398,6 +398,48 @@ class TestSearch < Test::Unit::TestCase
398
398
  end
399
399
  end
400
400
 
401
+ context "where boolean column is_true" do
402
+ setup do
403
+ @s.bln_is_true = true
404
+ end
405
+
406
+ should "return five results" do
407
+ assert_equal 5, @s.all.size
408
+ end
409
+
410
+ should "contain no results with a false boolean column" do
411
+ assert_does_not_contain @s.all.collect {|r| r.bln}, false
412
+ end
413
+ end
414
+
415
+ context "where boolean column equals false" do
416
+ setup do
417
+ @s.bln_equals = false
418
+ end
419
+
420
+ should "return four results" do
421
+ assert_equal 4, @s.all.size
422
+ end
423
+
424
+ should "contain no results with a true boolean column" do
425
+ assert_does_not_contain @s.all.collect {|r| r.bln}, true
426
+ end
427
+ end
428
+
429
+ context "where boolean column is_false" do
430
+ setup do
431
+ @s.bln_is_false = true
432
+ end
433
+
434
+ should "return four results" do
435
+ assert_equal 4, @s.all.size
436
+ end
437
+
438
+ should "contain no results with a true boolean column" do
439
+ assert_does_not_contain @s.all.collect {|r| r.bln}, true
440
+ end
441
+ end
442
+
401
443
  context "where date column is Christmas 2009 by array" do
402
444
  setup do
403
445
  @s.dat_equals = [2009, 12, 25]
@@ -600,4 +642,37 @@ class TestSearch < Test::Unit::TestCase
600
642
  @s.first
601
643
  end
602
644
  end
645
+
646
+ [{:name => 'Project', :object => Project},
647
+ {:name => 'Project as a Relation', :object => Project.scoped}].each do |object|
648
+ context_a_search_against object[:name], object[:object] do
649
+ context "where name is present" do
650
+ setup do
651
+ @s.name_is_present = true
652
+ end
653
+
654
+ should "return 5 results" do
655
+ assert_equal 5, @s.all.size
656
+ end
657
+
658
+ should "contain no results with a blank name column" do
659
+ assert_equal 0, @s.all.select {|r| r.name.blank?}.size
660
+ end
661
+ end
662
+
663
+ context "where name is blank" do
664
+ setup do
665
+ @s.name_is_blank = true
666
+ end
667
+
668
+ should "return 5 results" do
669
+ assert_equal 2, @s.all.size
670
+ end
671
+
672
+ should "contain no results with a present name column" do
673
+ assert_equal 0, @s.all.select {|r| r.name.present?}.size
674
+ end
675
+ end
676
+ end
677
+ end
603
678
  end
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 5
8
- - 3
9
- version: 0.5.3
8
+ - 4
9
+ version: 0.5.4
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ernie Miller
@@ -14,13 +14,14 @@ autorequire:
14
14
  bindir: bin
15
15
  cert_chain: []
16
16
 
17
- date: 2010-07-26 00:00:00 -04:00
17
+ date: 2010-07-28 00:00:00 -04:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
21
21
  name: shoulda
22
22
  prerelease: false
23
23
  requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
24
25
  requirements:
25
26
  - - ">="
26
27
  - !ruby/object:Gem::Version
@@ -33,6 +34,7 @@ dependencies:
33
34
  name: activerecord
34
35
  prerelease: false
35
36
  requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
36
38
  requirements:
37
39
  - - ">="
38
40
  - !ruby/object:Gem::Version
@@ -48,6 +50,7 @@ dependencies:
48
50
  name: activesupport
49
51
  prerelease: false
50
52
  requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
51
54
  requirements:
52
55
  - - ">="
53
56
  - !ruby/object:Gem::Version
@@ -63,6 +66,7 @@ dependencies:
63
66
  name: actionpack
64
67
  prerelease: false
65
68
  requirement: &id004 !ruby/object:Gem::Requirement
69
+ none: false
66
70
  requirements:
67
71
  - - ">="
68
72
  - !ruby/object:Gem::Version
@@ -78,6 +82,7 @@ dependencies:
78
82
  name: arel
79
83
  prerelease: false
80
84
  requirement: &id005 !ruby/object:Gem::Requirement
85
+ none: false
81
86
  requirements:
82
87
  - - ">="
83
88
  - !ruby/object:Gem::Version
@@ -145,6 +150,7 @@ rdoc_options:
145
150
  require_paths:
146
151
  - lib
147
152
  required_ruby_version: !ruby/object:Gem::Requirement
153
+ none: false
148
154
  requirements:
149
155
  - - ">="
150
156
  - !ruby/object:Gem::Version
@@ -152,6 +158,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
152
158
  - 0
153
159
  version: "0"
154
160
  required_rubygems_version: !ruby/object:Gem::Requirement
161
+ none: false
155
162
  requirements:
156
163
  - - ">="
157
164
  - !ruby/object:Gem::Version
@@ -161,7 +168,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
161
168
  requirements: []
162
169
 
163
170
  rubyforge_project:
164
- rubygems_version: 1.3.6
171
+ rubygems_version: 1.3.7
165
172
  signing_key:
166
173
  specification_version: 3
167
174
  summary: ActiveRecord 3 object-based searching for your form_for enjoyment.