ransack 0.1.0 → 0.2.0
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/lib/ransack.rb +1 -1
- data/lib/ransack/adapters/active_record.rb +18 -2
- data/lib/ransack/adapters/active_record/3.0/base.rb +34 -0
- data/lib/ransack/adapters/active_record/3.0/compat.rb +23 -0
- data/lib/ransack/adapters/active_record/3.0/context.rb +168 -0
- data/lib/ransack/adapters/active_record/3.0/join_association.rb +44 -0
- data/lib/ransack/adapters/active_record/3.0/join_dependency.rb +63 -0
- data/lib/ransack/adapters/active_record/base.rb +19 -2
- data/lib/ransack/adapters/active_record/context.rb +45 -33
- data/lib/ransack/adapters/active_record/join_association.rb +44 -0
- data/lib/ransack/adapters/active_record/join_dependency.rb +63 -0
- data/lib/ransack/context.rb +25 -65
- data/lib/ransack/helpers/form_builder.rb +10 -4
- data/lib/ransack/helpers/form_helper.rb +1 -0
- data/lib/ransack/locale/en.yml +1 -0
- data/lib/ransack/nodes.rb +1 -0
- data/lib/ransack/nodes/attribute.rb +21 -4
- data/lib/ransack/nodes/bindable.rb +29 -0
- data/lib/ransack/nodes/condition.rb +30 -28
- data/lib/ransack/nodes/sort.rb +5 -3
- data/lib/ransack/nodes/value.rb +84 -100
- data/lib/ransack/predicate.rb +1 -11
- data/lib/ransack/ransacker.rb +26 -0
- data/lib/ransack/search.rb +3 -2
- data/lib/ransack/version.rb +1 -1
- data/lib/ransack/visitor.rb +64 -0
- data/ransack.gemspec +3 -3
- data/spec/console.rb +1 -2
- data/spec/ransack/adapters/active_record/base_spec.rb +18 -0
- data/spec/ransack/adapters/active_record/context_spec.rb +2 -2
- data/spec/ransack/helpers/form_builder_spec.rb +4 -0
- data/spec/ransack/search_spec.rb +25 -2
- data/spec/spec_helper.rb +2 -3
- data/spec/support/schema.rb +8 -0
- metadata +16 -8
data/spec/ransack/search_spec.rb
CHANGED
@@ -22,6 +22,15 @@ module Ransack
|
|
22
22
|
condition.value.should eq 'Ernie'
|
23
23
|
end
|
24
24
|
|
25
|
+
it 'creates Conditions for polymorphic belongs_to association attributes' do
|
26
|
+
search = Search.new(Note, :notable_of_Person_type_name_eq => 'Ernie')
|
27
|
+
condition = search.base[:notable_of_Person_type_name_eq]
|
28
|
+
condition.should be_a Nodes::Condition
|
29
|
+
condition.predicate.name.should eq 'eq'
|
30
|
+
condition.attributes.first.name.should eq 'notable_of_Person_type_name'
|
31
|
+
condition.value.should eq 'Ernie'
|
32
|
+
end
|
33
|
+
|
25
34
|
it 'discards empty conditions' do
|
26
35
|
search = Search.new(Person, :children_name_eq => '')
|
27
36
|
condition = search.base[:children_name_eq]
|
@@ -84,6 +93,13 @@ module Ransack
|
|
84
93
|
where.to_sql.should match /"children_people"\."name" = 'Ernie' OR "people"\."name" = 'Ernie'/
|
85
94
|
end
|
86
95
|
|
96
|
+
it 'evaluates polymorphic belongs_to association conditions contextually' do
|
97
|
+
search = Search.new(Note, :notable_of_Person_type_name_eq => 'Ernie')
|
98
|
+
search.result.should be_an ActiveRecord::Relation
|
99
|
+
where = search.result.where_values.first
|
100
|
+
where.to_sql.should match /"people"."name" = 'Ernie'/
|
101
|
+
end
|
102
|
+
|
87
103
|
it 'evaluates nested conditions' do
|
88
104
|
search = Search.new(Person, :children_name_eq => 'Ernie',
|
89
105
|
:o => [{
|
@@ -93,7 +109,7 @@ module Ransack
|
|
93
109
|
)
|
94
110
|
search.result.should be_an ActiveRecord::Relation
|
95
111
|
where = search.result.where_values.first
|
96
|
-
where.to_sql.should match
|
112
|
+
where.to_sql.should match /"children_people"."name" = 'Ernie' AND \("people"."name" = 'Ernie' OR "children_people_2"."name" = 'Ernie'\)/
|
97
113
|
end
|
98
114
|
|
99
115
|
it 'evaluates arrays of groupings' do
|
@@ -105,7 +121,14 @@ module Ransack
|
|
105
121
|
)
|
106
122
|
search.result.should be_an ActiveRecord::Relation
|
107
123
|
where = search.result.where_values.first
|
108
|
-
where.to_sql.should match /\(
|
124
|
+
where.to_sql.should match /\("people"."name" = 'Ernie' OR "children_people"."name" = 'Ernie'\) AND \("people"."name" = 'Bert' OR "children_people"."name" = 'Bert'\)/
|
125
|
+
end
|
126
|
+
|
127
|
+
it 'returns distinct records when passed :distinct => true' do
|
128
|
+
search = Search.new(Person, :o => [{:comments_body_cont => 'e', :articles_comments_body_cont => 'e'}])
|
129
|
+
search.result.should have(920).items
|
130
|
+
search.result(:distinct => true).should have(330).items
|
131
|
+
search.result.all.uniq.should eq search.result(:distinct => true).all
|
109
132
|
end
|
110
133
|
end
|
111
134
|
|
data/spec/spec_helper.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
require 'machinist/active_record'
|
2
2
|
require 'sham'
|
3
3
|
require 'faker'
|
4
|
+
require 'ransack'
|
4
5
|
|
5
6
|
Time.zone = 'Eastern Time (US & Canada)'
|
6
7
|
|
@@ -23,6 +24,4 @@ RSpec.configure do |config|
|
|
23
24
|
config.before(:each) { Sham.reset(:before_each) }
|
24
25
|
|
25
26
|
config.include RansackHelper
|
26
|
-
end
|
27
|
-
|
28
|
-
require 'ransack'
|
27
|
+
end
|
data/spec/support/schema.rb
CHANGED
@@ -13,6 +13,14 @@ class Person < ActiveRecord::Base
|
|
13
13
|
has_many :authored_article_comments, :through => :articles,
|
14
14
|
:class_name => 'Comment', :foreign_key => :person_id
|
15
15
|
has_many :notes, :as => :notable
|
16
|
+
|
17
|
+
ransacker :reversed_name, :formatter => proc {|v| v.reverse} do |parent|
|
18
|
+
parent.table[:name]
|
19
|
+
end
|
20
|
+
|
21
|
+
ransacker :doubled_name do |parent|
|
22
|
+
Arel::Nodes::InfixOperation.new('||', parent.table[:name], parent.table[:name])
|
23
|
+
end
|
16
24
|
end
|
17
25
|
|
18
26
|
class Article < ActiveRecord::Base
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: ransack
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.
|
5
|
+
version: 0.2.0
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Ernie Miller
|
@@ -10,8 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-
|
14
|
-
default_executable:
|
13
|
+
date: 2011-04-18 00:00:00 Z
|
15
14
|
dependencies:
|
16
15
|
- !ruby/object:Gem::Dependency
|
17
16
|
name: activerecord
|
@@ -21,7 +20,7 @@ dependencies:
|
|
21
20
|
requirements:
|
22
21
|
- - ~>
|
23
22
|
- !ruby/object:Gem::Version
|
24
|
-
version: 3.
|
23
|
+
version: "3.0"
|
25
24
|
type: :runtime
|
26
25
|
version_requirements: *id001
|
27
26
|
- !ruby/object:Gem::Dependency
|
@@ -32,7 +31,7 @@ dependencies:
|
|
32
31
|
requirements:
|
33
32
|
- - ~>
|
34
33
|
- !ruby/object:Gem::Version
|
35
|
-
version: 3.
|
34
|
+
version: "3.0"
|
36
35
|
type: :runtime
|
37
36
|
version_requirements: *id002
|
38
37
|
- !ruby/object:Gem::Dependency
|
@@ -43,7 +42,7 @@ dependencies:
|
|
43
42
|
requirements:
|
44
43
|
- - ~>
|
45
44
|
- !ruby/object:Gem::Version
|
46
|
-
version: 3.
|
45
|
+
version: "3.0"
|
47
46
|
type: :runtime
|
48
47
|
version_requirements: *id003
|
49
48
|
- !ruby/object:Gem::Dependency
|
@@ -107,8 +106,15 @@ files:
|
|
107
106
|
- Rakefile
|
108
107
|
- lib/ransack.rb
|
109
108
|
- lib/ransack/adapters/active_record.rb
|
109
|
+
- lib/ransack/adapters/active_record/3.0/base.rb
|
110
|
+
- lib/ransack/adapters/active_record/3.0/compat.rb
|
111
|
+
- lib/ransack/adapters/active_record/3.0/context.rb
|
112
|
+
- lib/ransack/adapters/active_record/3.0/join_association.rb
|
113
|
+
- lib/ransack/adapters/active_record/3.0/join_dependency.rb
|
110
114
|
- lib/ransack/adapters/active_record/base.rb
|
111
115
|
- lib/ransack/adapters/active_record/context.rb
|
116
|
+
- lib/ransack/adapters/active_record/join_association.rb
|
117
|
+
- lib/ransack/adapters/active_record/join_dependency.rb
|
112
118
|
- lib/ransack/configuration.rb
|
113
119
|
- lib/ransack/constants.rb
|
114
120
|
- lib/ransack/context.rb
|
@@ -120,6 +126,7 @@ files:
|
|
120
126
|
- lib/ransack/nodes.rb
|
121
127
|
- lib/ransack/nodes/and.rb
|
122
128
|
- lib/ransack/nodes/attribute.rb
|
129
|
+
- lib/ransack/nodes/bindable.rb
|
123
130
|
- lib/ransack/nodes/condition.rb
|
124
131
|
- lib/ransack/nodes/grouping.rb
|
125
132
|
- lib/ransack/nodes/node.rb
|
@@ -127,9 +134,11 @@ files:
|
|
127
134
|
- lib/ransack/nodes/sort.rb
|
128
135
|
- lib/ransack/nodes/value.rb
|
129
136
|
- lib/ransack/predicate.rb
|
137
|
+
- lib/ransack/ransacker.rb
|
130
138
|
- lib/ransack/search.rb
|
131
139
|
- lib/ransack/translate.rb
|
132
140
|
- lib/ransack/version.rb
|
141
|
+
- lib/ransack/visitor.rb
|
133
142
|
- ransack.gemspec
|
134
143
|
- spec/blueprints/articles.rb
|
135
144
|
- spec/blueprints/comments.rb
|
@@ -150,7 +159,6 @@ files:
|
|
150
159
|
- spec/ransack/search_spec.rb
|
151
160
|
- spec/spec_helper.rb
|
152
161
|
- spec/support/schema.rb
|
153
|
-
has_rdoc: true
|
154
162
|
homepage: http://metautonomo.us/projects/ransack
|
155
163
|
licenses: []
|
156
164
|
|
@@ -174,7 +182,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
174
182
|
requirements: []
|
175
183
|
|
176
184
|
rubyforge_project: ransack
|
177
|
-
rubygems_version: 1.
|
185
|
+
rubygems_version: 1.7.2
|
178
186
|
signing_key:
|
179
187
|
specification_version: 3
|
180
188
|
summary: Object-based searching. Like MetaSearch, but this time, with a better name.
|