ransack 0.5.4 → 0.5.5

Sign up to get free protection for your applications and to get access to all the features.
@@ -135,7 +135,7 @@ module Ransack
135
135
  found_association = @join_dependency.join_associations.detect do |assoc|
136
136
  assoc.reflection.name == name &&
137
137
  assoc.parent == parent &&
138
- (!klass || assoc.klass == klass)
138
+ (!klass || assoc.reflection.klass == klass)
139
139
  end
140
140
  unless found_association
141
141
  @join_dependency.send(:build_polymorphic, name.to_sym, parent, Arel::Nodes::OuterJoin, klass)
@@ -140,7 +140,7 @@ module Ransack
140
140
  found_association = @join_dependency.join_associations.detect do |assoc|
141
141
  assoc.reflection.name == name &&
142
142
  assoc.parent == parent &&
143
- (!klass || assoc.klass == klass)
143
+ (!klass || assoc.reflection.klass == klass)
144
144
  end
145
145
  unless found_association
146
146
  @join_dependency.send(:build_polymorphic, name.to_sym, parent, Arel::Nodes::OuterJoin, klass)
@@ -95,7 +95,7 @@ module Ransack
95
95
  end
96
96
 
97
97
  def unpolymorphize_association(str)
98
- if (match = str.match(/_of_(.+?)_type$/))
98
+ if (match = str.match(/_of_([^_]+?)_type$/))
99
99
  [match.pre_match, Kernel.const_get(match.captures.first)]
100
100
  else
101
101
  [str, nil]
@@ -73,6 +73,10 @@ module Ransack
73
73
  search_fields(:s, args, block)
74
74
  end
75
75
 
76
+ def sort_link(attribute, *args)
77
+ @template.sort_link @object, attribute, *args
78
+ end
79
+
76
80
  def condition_fields(*args, &block)
77
81
  search_fields(:c, args, block)
78
82
  end
@@ -1,6 +1,7 @@
1
1
  module Ransack
2
2
  module Helpers
3
3
  module FormHelper
4
+
4
5
  def search_form_for(record, options = {}, &proc)
5
6
  if record.is_a?(Ransack::Search)
6
7
  search = record
@@ -23,6 +24,52 @@ module Ransack
23
24
  form_for(record, options, &proc)
24
25
  end
25
26
 
27
+ def sort_link(search, attribute, *args)
28
+ raise TypeError, "First argument must be a Ransack::Search!" unless Search === search
29
+
30
+ search_params = params[:q] || {}.with_indifferent_access
31
+
32
+ attr_name = attribute.to_s
33
+
34
+ name = (args.size > 0 && !args.first.is_a?(Hash)) ? args.shift.to_s : Translate.attribute(attr_name, :context => search.context)
35
+
36
+ if existing_sort = search.sorts.detect {|s| s.name == attr_name}
37
+ prev_attr, prev_dir = existing_sort.name, existing_sort.dir
38
+ end
39
+
40
+ options = args.first.is_a?(Hash) ? args.shift.dup : {}
41
+ default_order = options.delete :default_order
42
+ current_dir = prev_attr == attr_name ? prev_dir : nil
43
+
44
+ if current_dir
45
+ new_dir = current_dir == 'desc' ? 'asc' : 'desc'
46
+ else
47
+ new_dir = default_order || 'asc'
48
+ end
49
+
50
+ html_options = args.first.is_a?(Hash) ? args.shift.dup : {}
51
+ css = ['sort_link', current_dir].compact.join(' ')
52
+ html_options[:class] = [css, html_options[:class]].compact.join(' ')
53
+ options.merge!(
54
+ :q => search_params.merge(:s => "#{attr_name} #{new_dir}")
55
+ )
56
+ link_to [ERB::Util.h(name), order_indicator_for(current_dir)].compact.join(' ').html_safe,
57
+ url_for(options),
58
+ html_options
59
+ end
60
+
61
+ private
62
+
63
+ def order_indicator_for(order)
64
+ if order == 'asc'
65
+ '▲'
66
+ elsif order == 'desc'
67
+ '▼'
68
+ else
69
+ nil
70
+ end
71
+ end
72
+
26
73
  end
27
74
  end
28
75
  end
@@ -49,8 +49,10 @@ module Ransack
49
49
  sort = Nodes::Sort.new(@context).build(attrs)
50
50
  self.sorts << sort
51
51
  end
52
- else
52
+ when String
53
53
  self.sorts = [args]
54
+ else
55
+ raise ArgumentError, "Invalid argument (#{args.class}) supplied to sorts="
54
56
  end
55
57
  end
56
58
  alias :s= :sorts=
@@ -12,7 +12,7 @@ module Ransack
12
12
 
13
13
  def self.attribute(key, options = {})
14
14
  unless context = options.delete(:context)
15
- raise ArgumentError, "A context is required to translate associations"
15
+ raise ArgumentError, "A context is required to translate attributes"
16
16
  end
17
17
 
18
18
  original_name = key.to_s
@@ -1,3 +1,3 @@
1
1
  module Ransack
2
- VERSION = "0.5.4"
2
+ VERSION = "0.5.5"
3
3
  end
@@ -47,6 +47,14 @@ module Ransack
47
47
 
48
48
  end
49
49
 
50
+ describe '#sort_link' do
51
+ subject { @f.sort_link :name, :controller => 'people' }
52
+
53
+ it { should match /people\?q%5Bs%5D=name\+asc/}
54
+ it { should match /sort_link/}
55
+ it { should match /Full Name<\/a>/}
56
+ end
57
+
50
58
  describe '#submit' do
51
59
 
52
60
  it 'localizes :search when no default value given' do
@@ -0,0 +1,38 @@
1
+ require 'spec_helper'
2
+
3
+ module Ransack
4
+ module Helpers
5
+ describe FormHelper do
6
+
7
+ router = ActionDispatch::Routing::RouteSet.new
8
+ router.draw do
9
+ resources :people
10
+ match ':controller(/:action(/:id(.:format)))'
11
+ end
12
+
13
+ include router.url_helpers
14
+
15
+ # FIXME: figure out a cleaner way to get this behavior
16
+ before do
17
+ @controller = ActionView::TestCase::TestController.new
18
+ @controller.instance_variable_set(:@_routes, router)
19
+ @controller.class_eval do
20
+ include router.url_helpers
21
+ end
22
+
23
+ @controller.view_context_class.class_eval do
24
+ include router.url_helpers
25
+ end
26
+ end
27
+
28
+ describe '#sort_link' do
29
+ subject { @controller.view_context.sort_link(Person.search(:sorts => ['name desc']), :name, :controller => 'people') }
30
+
31
+ it { should match /people\?q%5Bs%5D=name\+asc/}
32
+ it { should match /sort_link desc/}
33
+ it { should match /Full Name &#9660;/}
34
+ end
35
+
36
+ end
37
+ end
38
+ end
@@ -31,6 +31,16 @@ module Ransack
31
31
  condition.value.should eq 'Ernie'
32
32
  end
33
33
 
34
+ it 'creates Conditions for multiple polymorphic belongs_to association attributes' do
35
+ search = Search.new(Note, :notable_of_Person_type_name_or_notable_of_Article_type_title_eq => 'Ernie')
36
+ condition = search.base[:notable_of_Person_type_name_or_notable_of_Article_type_title_eq]
37
+ condition.should be_a Nodes::Condition
38
+ condition.predicate.name.should eq 'eq'
39
+ condition.attributes.first.name.should eq 'notable_of_Person_type_name'
40
+ condition.attributes.last.name.should eq 'notable_of_Article_type_title'
41
+ condition.value.should eq 'Ernie'
42
+ end
43
+
34
44
  it 'discards empty conditions' do
35
45
  search = Search.new(Person, :children_name_eq => '')
36
46
  condition = search.base[:children_name_eq]
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
4
+ version: 0.5.5
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-07-24 00:00:00.000000000 -04:00
13
- default_executable:
12
+ date: 2011-08-14 00:00:00.000000000Z
14
13
  dependencies:
15
14
  - !ruby/object:Gem::Dependency
16
15
  name: activerecord
17
- requirement: &70355886036760 !ruby/object:Gem::Requirement
16
+ requirement: &70121926259000 !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: *70355886036760
24
+ version_requirements: *70121926259000
26
25
  - !ruby/object:Gem::Dependency
27
26
  name: activesupport
28
- requirement: &70355886036220 !ruby/object:Gem::Requirement
27
+ requirement: &70121926257860 !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: *70355886036220
35
+ version_requirements: *70121926257860
37
36
  - !ruby/object:Gem::Dependency
38
37
  name: actionpack
39
- requirement: &70355886035700 !ruby/object:Gem::Requirement
38
+ requirement: &70121926257080 !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: *70355886035700
46
+ version_requirements: *70121926257080
48
47
  - !ruby/object:Gem::Dependency
49
48
  name: rspec
50
- requirement: &70355886035200 !ruby/object:Gem::Requirement
49
+ requirement: &70121926256140 !ruby/object:Gem::Requirement
51
50
  none: false
52
51
  requirements:
53
52
  - - ~>
@@ -55,10 +54,10 @@ dependencies:
55
54
  version: 2.6.0
56
55
  type: :development
57
56
  prerelease: false
58
- version_requirements: *70355886035200
57
+ version_requirements: *70121926256140
59
58
  - !ruby/object:Gem::Dependency
60
59
  name: machinist
61
- requirement: &70355886034700 !ruby/object:Gem::Requirement
60
+ requirement: &70121926255080 !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: *70355886034700
68
+ version_requirements: *70121926255080
70
69
  - !ruby/object:Gem::Dependency
71
70
  name: faker
72
- requirement: &70355886034140 !ruby/object:Gem::Requirement
71
+ requirement: &70121926254280 !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: *70355886034140
79
+ version_requirements: *70121926254280
81
80
  - !ruby/object:Gem::Dependency
82
81
  name: sqlite3
83
- requirement: &70355886033680 !ruby/object:Gem::Requirement
82
+ requirement: &70121926253160 !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: *70355886033680
90
+ version_requirements: *70121926253160
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:
@@ -147,6 +146,7 @@ files:
147
146
  - spec/ransack/adapters/active_record/context_spec.rb
148
147
  - spec/ransack/configuration_spec.rb
149
148
  - spec/ransack/helpers/form_builder_spec.rb
149
+ - spec/ransack/helpers/form_helper_spec.rb
150
150
  - spec/ransack/nodes/compound_condition_spec.rb
151
151
  - spec/ransack/nodes/condition_spec.rb
152
152
  - spec/ransack/nodes/grouping_spec.rb
@@ -155,7 +155,6 @@ files:
155
155
  - spec/spec_helper.rb
156
156
  - spec/support/en.yml
157
157
  - spec/support/schema.rb
158
- has_rdoc: true
159
158
  homepage: http://metautonomo.us/projects/ransack
160
159
  licenses: []
161
160
  post_install_message:
@@ -176,7 +175,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
176
175
  version: '0'
177
176
  requirements: []
178
177
  rubyforge_project: ransack
179
- rubygems_version: 1.6.2
178
+ rubygems_version: 1.8.6
180
179
  signing_key:
181
180
  specification_version: 3
182
181
  summary: Object-based searching for ActiveRecord (currently).
@@ -192,6 +191,7 @@ test_files:
192
191
  - spec/ransack/adapters/active_record/context_spec.rb
193
192
  - spec/ransack/configuration_spec.rb
194
193
  - spec/ransack/helpers/form_builder_spec.rb
194
+ - spec/ransack/helpers/form_helper_spec.rb
195
195
  - spec/ransack/nodes/compound_condition_spec.rb
196
196
  - spec/ransack/nodes/condition_spec.rb
197
197
  - spec/ransack/nodes/grouping_spec.rb