ransack 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +17 -7
- data/lib/ransack/nodes/attribute.rb +0 -4
- data/lib/ransack/nodes/condition.rb +10 -10
- data/lib/ransack/nodes/value.rb +2 -2
- data/lib/ransack/predicate.rb +3 -7
- data/lib/ransack/version.rb +1 -1
- metadata +17 -19
data/README.md
CHANGED
@@ -4,10 +4,11 @@ Ransack is a rewrite of [MetaSearch](http://metautonomo.us/projects/metasearch).
|
|
4
4
|
supports many of the same features as MetaSearch, its underlying implementation differs
|
5
5
|
greatly from MetaSearch, and _backwards compatibility is not a design goal._
|
6
6
|
|
7
|
-
Ransack enables the creation of both simple and advanced
|
8
|
-
application's models. If you're looking for something that
|
9
|
-
at the model or controller layer, you're probably not looking
|
10
|
-
for that matter). Try
|
7
|
+
Ransack enables the creation of both simple and [advanced](http://ransack-demo.heroku.com)
|
8
|
+
search forms against your application's models. If you're looking for something that
|
9
|
+
simplifies query generation at the model or controller layer, you're probably not looking
|
10
|
+
for Ransack (or MetaSearch, for that matter). Try
|
11
|
+
[Squeel](http://metautonomo.us/projects/squeel) instead.
|
11
12
|
|
12
13
|
## Getting started
|
13
14
|
|
@@ -55,13 +56,21 @@ If you're coming from MetaSearch, things to note:
|
|
55
56
|
is passed to it.
|
56
57
|
3. Common ActiveRecord::Relation methods are no longer delegated by the search object.
|
57
58
|
Instead, you will get your search results (an ActiveRecord::Relation in the case of
|
58
|
-
the ActiveRecord adapter) via a call to `Search#result`.
|
59
|
+
the ActiveRecord adapter) via a call to `Search#result`. If passed `:distinct => true`,
|
60
|
+
`result` will generate a `SELECT DISTINCT` to avoid returning duplicate rows, even if
|
61
|
+
conditions on a join would otherwise result in some.
|
62
|
+
|
63
|
+
Please note that for many databases, a sort on an associated table's columns will
|
64
|
+
result in invalid SQL with `:distinct => true` -- in those cases, you're on your own,
|
65
|
+
and will need to modify the result as needed to allow these queries to work. Thankfully,
|
66
|
+
9 times out of 10, sort against the search's base is sufficient, though, as that's
|
67
|
+
generally what's being displayed on your results page.
|
59
68
|
|
60
69
|
In your controller:
|
61
70
|
|
62
71
|
def index
|
63
72
|
@q = Person.search(params[:q])
|
64
|
-
@people = @q.result
|
73
|
+
@people = @q.result(:distinct => true)
|
65
74
|
end
|
66
75
|
|
67
76
|
In your view:
|
@@ -106,7 +115,8 @@ This means you'll need to tweak your routes...
|
|
106
115
|
:html => {:method => :post} do |f| %>
|
107
116
|
|
108
117
|
Once you've done so, you can make use of the helpers in Ransack::Helpers::FormBuilder to
|
109
|
-
construct much more complex search forms
|
118
|
+
construct much more complex search forms, such as the one on the
|
119
|
+
[demo page](http://ransack-demo.heroku.com).
|
110
120
|
|
111
121
|
**more docs to come**
|
112
122
|
|
@@ -4,8 +4,6 @@ module Ransack
|
|
4
4
|
i18n_word :attribute, :predicate, :combinator, :value
|
5
5
|
i18n_alias :a => :attribute, :p => :predicate, :m => :combinator, :v => :value
|
6
6
|
|
7
|
-
delegate :cast_value, :to => :first_attribute
|
8
|
-
|
9
7
|
attr_reader :predicate
|
10
8
|
|
11
9
|
class << self
|
@@ -20,7 +18,9 @@ module Ransack
|
|
20
18
|
:m => combinator,
|
21
19
|
:v => [values]
|
22
20
|
)
|
23
|
-
|
21
|
+
# TODO: Figure out what to do with multiple types of attributes, if anything.
|
22
|
+
# Tempted to go with "garbage in, garbage out" on this one
|
23
|
+
predicate.validate(condition.values, condition.default_type) ? condition : nil
|
24
24
|
end
|
25
25
|
end
|
26
26
|
|
@@ -37,17 +37,13 @@ module Ransack
|
|
37
37
|
end
|
38
38
|
|
39
39
|
def valid?
|
40
|
-
attributes.detect(&:valid?) && predicate && valid_arity? && predicate.validate(values) && valid_combinator?
|
40
|
+
attributes.detect(&:valid?) && predicate && valid_arity? && predicate.validate(values, default_type) && valid_combinator?
|
41
41
|
end
|
42
42
|
|
43
43
|
def valid_arity?
|
44
44
|
values.size <= 1 || predicate.compound || %w(in not_in).include?(predicate.name)
|
45
45
|
end
|
46
46
|
|
47
|
-
def first_attribute
|
48
|
-
attributes.first
|
49
|
-
end
|
50
|
-
|
51
47
|
def attributes
|
52
48
|
@attributes ||= []
|
53
49
|
end
|
@@ -117,7 +113,7 @@ module Ransack
|
|
117
113
|
end
|
118
114
|
|
119
115
|
def value
|
120
|
-
predicate.compound ? values.map {|v|
|
116
|
+
predicate.compound ? values.map {|v| v.cast(default_type)} : values.first.cast(default_type)
|
121
117
|
end
|
122
118
|
|
123
119
|
def build(params)
|
@@ -188,7 +184,7 @@ module Ransack
|
|
188
184
|
end
|
189
185
|
|
190
186
|
def casted_values_for_attribute(attr)
|
191
|
-
validated_values.map {|v| v.
|
187
|
+
validated_values.map {|v| v.cast(predicate.type || attr.type)}
|
192
188
|
end
|
193
189
|
|
194
190
|
def formatted_values_for_attribute(attr)
|
@@ -199,6 +195,10 @@ module Ransack
|
|
199
195
|
end
|
200
196
|
end
|
201
197
|
|
198
|
+
def default_type
|
199
|
+
predicate.type || (attributes.first && attributes.first.type)
|
200
|
+
end
|
201
|
+
|
202
202
|
private
|
203
203
|
|
204
204
|
def valid_combinator?
|
data/lib/ransack/nodes/value.rb
CHANGED
@@ -2,7 +2,7 @@ module Ransack
|
|
2
2
|
module Nodes
|
3
3
|
class Value < Node
|
4
4
|
attr_accessor :value
|
5
|
-
delegate :
|
5
|
+
delegate :present?, :blank?, :to => :value
|
6
6
|
|
7
7
|
def initialize(context, value = nil)
|
8
8
|
super(context)
|
@@ -23,7 +23,7 @@ module Ransack
|
|
23
23
|
value.hash
|
24
24
|
end
|
25
25
|
|
26
|
-
def
|
26
|
+
def cast(type)
|
27
27
|
case type
|
28
28
|
when :date
|
29
29
|
cast_to_date(value)
|
data/lib/ransack/predicate.rb
CHANGED
@@ -39,7 +39,7 @@ module Ransack
|
|
39
39
|
@arel_predicate = opts[:arel_predicate]
|
40
40
|
@type = opts[:type]
|
41
41
|
@formatter = opts[:formatter]
|
42
|
-
@validator = opts[:validator]
|
42
|
+
@validator = opts[:validator] || lambda { |v| v.present? }
|
43
43
|
@compound = opts[:compound]
|
44
44
|
end
|
45
45
|
|
@@ -61,12 +61,8 @@ module Ransack
|
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
64
|
-
def validate(vals)
|
65
|
-
|
66
|
-
vals.select {|v| validator.call(v.value)}.any?
|
67
|
-
else
|
68
|
-
vals.select {|v| v.present?}.any?
|
69
|
-
end
|
64
|
+
def validate(vals, type = @type)
|
65
|
+
vals.select {|v| validator.call(type ? v.cast(type) : v.value)}.any?
|
70
66
|
end
|
71
67
|
|
72
68
|
end
|
data/lib/ransack/version.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ransack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.2
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,12 +9,11 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2011-06-
|
13
|
-
default_executable:
|
12
|
+
date: 2011-06-23 00:00:00.000000000Z
|
14
13
|
dependencies:
|
15
14
|
- !ruby/object:Gem::Dependency
|
16
15
|
name: activerecord
|
17
|
-
requirement: &
|
16
|
+
requirement: &2156140180 !ruby/object:Gem::Requirement
|
18
17
|
none: false
|
19
18
|
requirements:
|
20
19
|
- - ~>
|
@@ -22,10 +21,10 @@ dependencies:
|
|
22
21
|
version: '3.0'
|
23
22
|
type: :runtime
|
24
23
|
prerelease: false
|
25
|
-
version_requirements: *
|
24
|
+
version_requirements: *2156140180
|
26
25
|
- !ruby/object:Gem::Dependency
|
27
26
|
name: activesupport
|
28
|
-
requirement: &
|
27
|
+
requirement: &2156139280 !ruby/object:Gem::Requirement
|
29
28
|
none: false
|
30
29
|
requirements:
|
31
30
|
- - ~>
|
@@ -33,10 +32,10 @@ dependencies:
|
|
33
32
|
version: '3.0'
|
34
33
|
type: :runtime
|
35
34
|
prerelease: false
|
36
|
-
version_requirements: *
|
35
|
+
version_requirements: *2156139280
|
37
36
|
- !ruby/object:Gem::Dependency
|
38
37
|
name: actionpack
|
39
|
-
requirement: &
|
38
|
+
requirement: &2156138640 !ruby/object:Gem::Requirement
|
40
39
|
none: false
|
41
40
|
requirements:
|
42
41
|
- - ~>
|
@@ -44,10 +43,10 @@ dependencies:
|
|
44
43
|
version: '3.0'
|
45
44
|
type: :runtime
|
46
45
|
prerelease: false
|
47
|
-
version_requirements: *
|
46
|
+
version_requirements: *2156138640
|
48
47
|
- !ruby/object:Gem::Dependency
|
49
48
|
name: rspec
|
50
|
-
requirement: &
|
49
|
+
requirement: &2156137960 !ruby/object:Gem::Requirement
|
51
50
|
none: false
|
52
51
|
requirements:
|
53
52
|
- - ~>
|
@@ -55,10 +54,10 @@ dependencies:
|
|
55
54
|
version: 2.5.0
|
56
55
|
type: :development
|
57
56
|
prerelease: false
|
58
|
-
version_requirements: *
|
57
|
+
version_requirements: *2156137960
|
59
58
|
- !ruby/object:Gem::Dependency
|
60
59
|
name: machinist
|
61
|
-
requirement: &
|
60
|
+
requirement: &2156137140 !ruby/object:Gem::Requirement
|
62
61
|
none: false
|
63
62
|
requirements:
|
64
63
|
- - ~>
|
@@ -66,10 +65,10 @@ dependencies:
|
|
66
65
|
version: 1.0.6
|
67
66
|
type: :development
|
68
67
|
prerelease: false
|
69
|
-
version_requirements: *
|
68
|
+
version_requirements: *2156137140
|
70
69
|
- !ruby/object:Gem::Dependency
|
71
70
|
name: faker
|
72
|
-
requirement: &
|
71
|
+
requirement: &2156136500 !ruby/object:Gem::Requirement
|
73
72
|
none: false
|
74
73
|
requirements:
|
75
74
|
- - ~>
|
@@ -77,10 +76,10 @@ dependencies:
|
|
77
76
|
version: 0.9.5
|
78
77
|
type: :development
|
79
78
|
prerelease: false
|
80
|
-
version_requirements: *
|
79
|
+
version_requirements: *2156136500
|
81
80
|
- !ruby/object:Gem::Dependency
|
82
81
|
name: sqlite3
|
83
|
-
requirement: &
|
82
|
+
requirement: &2156135640 !ruby/object:Gem::Requirement
|
84
83
|
none: false
|
85
84
|
requirements:
|
86
85
|
- - ~>
|
@@ -88,7 +87,7 @@ dependencies:
|
|
88
87
|
version: 1.3.3
|
89
88
|
type: :development
|
90
89
|
prerelease: false
|
91
|
-
version_requirements: *
|
90
|
+
version_requirements: *2156135640
|
92
91
|
description: Ransack is the successor to the MetaSearch gem. It improves and expands
|
93
92
|
upon MetaSearch's functionality, but does not have a 100%-compatible API.
|
94
93
|
email:
|
@@ -154,7 +153,6 @@ files:
|
|
154
153
|
- spec/spec_helper.rb
|
155
154
|
- spec/support/en.yml
|
156
155
|
- spec/support/schema.rb
|
157
|
-
has_rdoc: true
|
158
156
|
homepage: http://metautonomo.us/projects/ransack
|
159
157
|
licenses: []
|
160
158
|
post_install_message:
|
@@ -175,7 +173,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
175
173
|
version: '0'
|
176
174
|
requirements: []
|
177
175
|
rubyforge_project: ransack
|
178
|
-
rubygems_version: 1.
|
176
|
+
rubygems_version: 1.8.5
|
179
177
|
signing_key:
|
180
178
|
specification_version: 3
|
181
179
|
summary: Object-based searching for ActiveRecord (currently).
|