ransack_abbreviator 0.0.7 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -23,5 +23,4 @@ require "ransack_abbreviator/ransack_extensions/nodes/condition"
23
23
  require "ransack_abbreviator/ransack_extensions/context"
24
24
  require "ransack_abbreviator/ransack_extensions/search"
25
25
  require 'ransack_abbreviator/adapters/active_record'
26
- require "ransack_abbreviator/view_helpers"
27
26
  require "ransack_abbreviator/ransack_extensions/helpers/form_builder"
@@ -82,7 +82,8 @@ module RansackAbbreviator
82
82
  private
83
83
 
84
84
  def get_polymorphic_assoc_and_class_type(possible_assoc_abbr)
85
- assoc_name = class_type = nil
85
+ assoc_name = nil
86
+ class_type = nil
86
87
  if (match = possible_assoc_abbr.match(/_of_([^_]+?)_type$/))
87
88
  assoc_name = @context.klass.ransackable_assoc_name_for(match.pre_match)
88
89
  class_type = RansackAbbreviator.assoc_name_for(match.captures.first).camelize
@@ -91,7 +92,8 @@ module RansackAbbreviator
91
92
  end
92
93
 
93
94
  def extract_possible_assoc_and_attribute_abbr(s)
94
- possible_assoc = possible_attr_name = nil
95
+ possible_assoc = nil
96
+ possible_attr_name = nil
95
97
  if s.include?(".")
96
98
  parts = s.split(".")
97
99
  possible_assoc = parts[0]
@@ -3,7 +3,8 @@ require "pathname"
3
3
  module RansackAbbreviator
4
4
  module Configuration
5
5
  mattr_accessor :column_abbreviations, :assoc_abbreviations
6
- self.column_abbreviations = self.assoc_abbreviations = {}
6
+ self.column_abbreviations = {}
7
+ self.assoc_abbreviations = {}
7
8
 
8
9
  def configure
9
10
  yield self
@@ -1,3 +1,3 @@
1
1
  module RansackAbbreviator
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
@@ -15,7 +15,7 @@ module RansackAbbreviator
15
15
 
16
16
  RansackAbbreviator.column_abbreviations.should have_key('tag_id')
17
17
  RansackAbbreviator.column_abbreviation_for(:tag_id).should eq 'tid'
18
- RansackAbbreviator.assoc_name_for('tid').should eq 'tag_id'
18
+ RansackAbbreviator.column_name_for('tid').should eq 'tag_id'
19
19
  end
20
20
 
21
21
  it "adds association abbreviations" do
@@ -0,0 +1,92 @@
1
+ require 'spec_helper'
2
+
3
+ module RansackAbbreviator
4
+ module Abbreviators
5
+ describe Decoder do
6
+ describe '#decode_parameter' do
7
+ context "a lookup of a defined column abbr" do
8
+ it "returns the full column name" do
9
+ search = Ransack::Search.new(Person)
10
+ search.context.decode_parameter("nm_eq").should == "name_eq"
11
+ search.context.decode_parameter("mn_eq").should == "middle_name_eq"
12
+ end
13
+ end
14
+
15
+ context "a lookup of an undefined column" do
16
+ it "returns the full column name" do
17
+ search = Ransack::Search.new(Person)
18
+ search.context.decode_parameter(:salary_eq).should == "salary_eq"
19
+ end
20
+ end
21
+
22
+ context "a lookup of a defined association abbr" do
23
+ context "a lookup of a defined column abbr" do
24
+ it "returns the full assoc & column pair" do
25
+ search = Ransack::Search.new(Article)
26
+ search.context.decode_parameter("pr.nm_eq").should == "person_name_eq"
27
+ search.context.decode_parameter("pr.mn_eq").should == "person_middle_name_eq"
28
+ search = Ransack::Search.new(Person)
29
+ search.context.decode_parameter("a_ac.vc_lteq").should == "authored_article_comments_vote_count_lteq"
30
+ end
31
+ end
32
+
33
+ context "a lookup of a column without an abbr" do
34
+ it "returns the table and full column" do
35
+ search = Ransack::Search.new(Article)
36
+ search.context.decode_parameter("pr.salary_eq").should == "person_salary_eq"
37
+ end
38
+ end
39
+ end
40
+
41
+ context "a lookup of an association without an abbr" do
42
+ context "a lookup of a column with an abbr" do
43
+ it "returns the full association and full column" do
44
+ search = Ransack::Search.new(Person)
45
+ search.context.decode_parameter("articles.tl_cont").should == "articles_title_cont"
46
+ end
47
+ end
48
+
49
+ context "a lookup of a column without an abbr" do
50
+ it "returns a full association & column pair" do
51
+ search = Ransack::Search.new(Person)
52
+ search.context.decode_parameter("articles.body_cont").should == "articles_body_cont"
53
+ end
54
+ end
55
+ end
56
+
57
+ context "a lookup of a multi-condition string" do
58
+ it "returns full forms for all conditions" do
59
+ search = Ransack::Search.new(Person)
60
+ search.context.decode_parameter("ch.nm_or_ch.salary_eq").should == "children_name_or_children_salary_eq"
61
+ end
62
+ end
63
+
64
+ context "a lookup of an abbreviated polymorphic belongs_to association" do
65
+ it "returns the full name for the polymorphic association" do
66
+ search = Ransack::Search.new(Note)
67
+ search.context.decode_parameter("nbl_of_pr_type.nm_eq").should == "notable_of_Person_type_name_eq"
68
+ end
69
+ end
70
+
71
+ context "a lookup of a nested condition" do
72
+ it "returns the full name for each association and column" do
73
+ search = Ransack::Search.new(Person)
74
+ search.context.decode_parameter("articles_cm.body_cont").should == "articles_comments_body_cont"
75
+ end
76
+
77
+ it "returns the full name for nested conditions for polymorphic associations" do
78
+ search = Ransack::Search.new(Note)
79
+ search.context.decode_parameter("nbl_of_pr_type_ch.nm_eq").should == "notable_of_Person_type_children_name_eq"
80
+ end
81
+ end
82
+
83
+ context "a lookup of something totally random" do
84
+ it "does absolutely nothing" do
85
+ search = Ransack::Search.new(Note)
86
+ search.context.decode_parameter("i_am_garbage").should == "i_am_garbage"
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,92 @@
1
+ require 'spec_helper'
2
+
3
+ module RansackAbbreviator
4
+ module Abbreviators
5
+ describe Encoder do
6
+ describe '#encode_parameter' do
7
+ context "a lookup of a defined column" do
8
+ it "returns an abbreviated column name" do
9
+ search = Ransack::Search.new(Person)
10
+ search.context.encode_parameter(:name_eq).should == "nm_eq"
11
+ search.context.encode_parameter(:middle_name_eq).should == "mn_eq"
12
+ end
13
+ end
14
+
15
+ context "a lookup of an undefined column" do
16
+ it "returns the full column name" do
17
+ search = Ransack::Search.new(Person)
18
+ search.context.encode_parameter(:salary_eq).should == "salary_eq"
19
+ end
20
+ end
21
+
22
+ context "a lookup of a defined association" do
23
+ context "a lookup of a defined column" do
24
+ it "returns an abbreviated assoc & column pair" do
25
+ search = Ransack::Search.new(Article)
26
+ search.context.encode_parameter(:person_name_eq).should == "pr.nm_eq"
27
+ search.context.encode_parameter(:person_middle_name_eq).should == "pr.mn_eq"
28
+ search = Ransack::Search.new(Person)
29
+ search.context.encode_parameter(:authored_article_comments_vote_count_lteq).should == "a_ac.vc_lteq"
30
+ end
31
+ end
32
+
33
+ context "a lookup of an undefined column" do
34
+ it "returns an abbreviated table but full column" do
35
+ search = Ransack::Search.new(Article)
36
+ search.context.encode_parameter(:person_salary_eq).should == "pr.salary_eq"
37
+ end
38
+ end
39
+ end
40
+
41
+ context "a lookup of an undefined association" do
42
+ context "a lookup of a defined column" do
43
+ it "returns the full association but abbreviated column" do
44
+ search = Ransack::Search.new(Person)
45
+ search.context.encode_parameter(:articles_title_cont).should == "articles.tl_cont"
46
+ end
47
+ end
48
+
49
+ context "a lookup of an undefined column" do
50
+ it "returns a full association & column pair" do
51
+ search = Ransack::Search.new(Person)
52
+ search.context.encode_parameter(:articles_body_cont).should == "articles.body_cont"
53
+ end
54
+ end
55
+ end
56
+
57
+ context "a lookup of a multi-condition string" do
58
+ it "returns abbreviated forms for all conditions" do
59
+ search = Ransack::Search.new(Person)
60
+ search.context.encode_parameter(:children_name_or_children_salary_eq).should == "ch.nm_or_ch.salary_eq"
61
+ end
62
+ end
63
+
64
+ context "a lookup of a defined polymorphic belongs_to association" do
65
+ it "returns the abbreviated name for the polymorphic association" do
66
+ search = Ransack::Search.new(Note)
67
+ search.context.encode_parameter(:notable_of_Person_type_name_eq).should == "nbl_of_pr_type.nm_eq"
68
+ end
69
+ end
70
+
71
+ context "a lookup of a nested condition" do
72
+ it "abbreviates each association and column" do
73
+ search = Ransack::Search.new(Person)
74
+ search.context.encode_parameter(:articles_comments_body_cont).should == "articles_cm.body_cont"
75
+ end
76
+
77
+ it "abbreviates nested conditions for polymorphic associations" do
78
+ search = Ransack::Search.new(Note)
79
+ search.context.encode_parameter(:notable_of_Person_type_children_name_eq).should == "nbl_of_pr_type_ch.nm_eq"
80
+ end
81
+ end
82
+
83
+ context "a lookup of something totally random" do
84
+ it "does absolutely nothing" do
85
+ search = Ransack::Search.new(Note)
86
+ search.context.encode_parameter(:i_am_garbage).should == "i_am_garbage"
87
+ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -6,7 +6,7 @@ module Ransack # We're testing Ransack's Search wih abbreviations
6
6
  context "with abbreviations" do
7
7
  it 'creates Conditions for top-level attributes' do
8
8
  search = Search.new(Person)
9
- search.build(ransack_abbreviation_for(search, :name_eq) => 'Ernie')
9
+ search.build(search.context.encode_parameter(:name_eq) => 'Ernie')
10
10
  condition = search.base[:name_eq]
11
11
  condition.should be_a Nodes::Condition
12
12
  condition.predicate.name.should eq 'eq'
@@ -16,7 +16,7 @@ module Ransack # We're testing Ransack's Search wih abbreviations
16
16
 
17
17
  it 'creates Conditions for association attributes' do
18
18
  search = Search.new(Person)
19
- search.build(ransack_abbreviation_for(search, :children_name_eq) => 'Ernie')
19
+ search.build(search.context.encode_parameter(:children_name_eq) => 'Ernie')
20
20
  condition = search.base[:children_name_eq]
21
21
  condition.should be_a Nodes::Condition
22
22
  condition.predicate.name.should eq 'eq'
@@ -26,7 +26,7 @@ module Ransack # We're testing Ransack's Search wih abbreviations
26
26
 
27
27
  it 'creates Conditions for polymorphic belongs_to association attributes' do
28
28
  search = Search.new(Note)
29
- search.build(ransack_abbreviation_for(search, :notable_of_Person_type_name_eq) => 'Ernie')
29
+ search.build(search.context.encode_parameter(:notable_of_Person_type_name_eq) => 'Ernie')
30
30
  condition = search.base[:notable_of_Person_type_name_eq]
31
31
  condition.should be_a Nodes::Condition
32
32
  condition.predicate.name.should eq 'eq'
@@ -36,7 +36,7 @@ module Ransack # We're testing Ransack's Search wih abbreviations
36
36
 
37
37
  it 'creates Conditions for multiple polymorphic belongs_to association attributes' do
38
38
  search = Search.new(Note)
39
- search.build(ransack_abbreviation_for(search, :notable_of_Person_type_name_or_notable_of_Article_type_title_eq) => 'Ernie')
39
+ search.build(search.context.encode_parameter(:notable_of_Person_type_name_or_notable_of_Article_type_title_eq) => 'Ernie')
40
40
  condition = search.base[:notable_of_Person_type_name_or_notable_of_Article_type_title_eq]
41
41
  condition.should be_a Nodes::Condition
42
42
  condition.predicate.name.should eq 'eq'
@@ -47,7 +47,7 @@ module Ransack # We're testing Ransack's Search wih abbreviations
47
47
 
48
48
  it 'discards empty conditions' do
49
49
  search = Search.new(Person)
50
- search.build(ransack_abbreviation_for(search, :children_name_eq) => '')
50
+ search.build(search.context.encode_parameter(:children_name_eq) => '')
51
51
  condition = search.base[:children_name_eq]
52
52
  condition.should be_nil
53
53
  end
@@ -56,8 +56,8 @@ module Ransack # We're testing Ransack's Search wih abbreviations
56
56
  search = Search.new(Person)
57
57
  search.build(
58
58
  :g => [
59
- ransack_abbreviations_for(search, :m => 'or', :name_eq => 'Ernie', :children_name_eq => 'Ernie'),
60
- ransack_abbreviations_for(search, :m => 'or', :name_eq => 'Bert', :children_name_eq => 'Bert')
59
+ {:m => 'or', search.context.encode_parameter(:name_eq) => 'Ernie', search.context.encode_parameter(:children_name_eq) => 'Ernie'},
60
+ {:m => 'or', search.context.encode_parameter(:name_eq) => 'Bert', search.context.encode_parameter(:children_name_eq) => 'Bert'}
61
61
  ]
62
62
  )
63
63
  ors = search.groupings
@@ -73,8 +73,8 @@ module Ransack # We're testing Ransack's Search wih abbreviations
73
73
  search = Search.new(Person)
74
74
  search.build(
75
75
  :g => {
76
- '0' => ransack_abbreviations_for(search, :m => 'or', :name_eq => 'Ernie', :children_name_eq => 'Ernie'),
77
- '1' => ransack_abbreviations_for(search, :m => 'or', :name_eq => 'Bert', :children_name_eq => 'Bert'),
76
+ '0' => {:m => 'or', search.context.encode_parameter(:name_eq) => 'Ernie', search.context.encode_parameter(:children_name_eq) => 'Ernie'},
77
+ '1' => {:m => 'or', search.context.encode_parameter(:name_eq) => 'Bert', search.context.encode_parameter(:children_name_eq) => 'Bert'}
78
78
  }
79
79
  )
80
80
  ors = search.groupings
@@ -86,19 +86,6 @@ module Ransack # We're testing Ransack's Search wih abbreviations
86
86
  or2.combinator.should eq 'or'
87
87
  end
88
88
 
89
- it 'accepts "attributes" hashes for conditions' do
90
- search = Search.new(Person)
91
- search.build(
92
- :c => {
93
- '0' => {:a => ransack_abbreviations_for(search, ['name']), :p => 'eq', :v => ['Ernie']},
94
- '1' => {:a => ransack_abbreviations_for(search, ['children_name', 'parent_name']), :p => 'eq', :v => ['Ernie'], :m => 'or'}
95
- }
96
- )
97
- conditions = search.base.conditions
98
- conditions.should have(2).items
99
- conditions.map {|c| c.class}.should eq [Nodes::Condition, Nodes::Condition]
100
- end
101
-
102
89
  it 'creates Conditions for custom predicates that take arrays' do
103
90
  Ransack.configure do |config|
104
91
  config.add_predicate 'ary_pred',
@@ -106,7 +93,7 @@ module Ransack # We're testing Ransack's Search wih abbreviations
106
93
  end
107
94
 
108
95
  search = Search.new(Person)
109
- search.build(ransack_abbreviation_for(search, :name_ary_pred) => ['Ernie', 'Bert'])
96
+ search.build(search.context.encode_parameter(:name_ary_pred) => ['Ernie', 'Bert'])
110
97
  condition = search.base[:name_ary_pred]
111
98
  condition.should be_a Nodes::Condition
112
99
  condition.predicate.name.should eq 'ary_pred'
@@ -116,7 +103,7 @@ module Ransack # We're testing Ransack's Search wih abbreviations
116
103
 
117
104
  it 'does not evaluate the query on #inspect' do
118
105
  search = Search.new(Person)
119
- search.build(ransack_abbreviation_for(search, :children_id_in) => [1, 2, 3])
106
+ search.build(search.context.encode_parameter(:children_id_in) => [1, 2, 3])
120
107
  search.inspect.should_not match /ActiveRecord/
121
108
  end
122
109
  end
@@ -235,7 +222,7 @@ module Ransack # We're testing Ransack's Search wih abbreviations
235
222
  context "with abbreviations" do
236
223
  it "evaluates a basic condition" do
237
224
  search = Search.new(Person)
238
- search.build(ransack_abbreviation_for(search, :name_eq) => 'Ernie')
225
+ search.build(search.context.encode_parameter(:name_eq) => 'Ernie')
239
226
  search.result.should be_an ActiveRecord::Relation
240
227
  where = search.result.where_values.first
241
228
  where.to_sql.should match /"people"\."name" = 'Ernie'/
@@ -243,7 +230,7 @@ module Ransack # We're testing Ransack's Search wih abbreviations
243
230
 
244
231
  it 'evaluates conditions contextually' do
245
232
  search = Search.new(Person)
246
- search.build(ransack_abbreviation_for(search, :children_name_eq) => 'Ernie')
233
+ search.build(search.context.encode_parameter(:children_name_eq) => 'Ernie')
247
234
  search.result.should be_an ActiveRecord::Relation
248
235
  where = search.result.where_values.first
249
236
  where.to_sql.should match /"children_people"\."name" = 'Ernie'/
@@ -251,12 +238,12 @@ module Ransack # We're testing Ransack's Search wih abbreviations
251
238
 
252
239
  it 'evaluates polymorphic belongs_to association conditions contextually' do
253
240
  search = Search.new(Note)
254
- search.build(ransack_abbreviation_for(search, :notable_of_Person_type_name_eq) => 'Ernie')
241
+ search.build(search.context.encode_parameter(:notable_of_Person_type_name_eq) => 'Ernie')
255
242
  search.result.should be_an ActiveRecord::Relation
256
243
  where = search.result.where_values.first
257
244
  where.to_sql.should match /"people"."name" = 'Ernie'/
258
245
 
259
- search.build(ransack_abbreviation_for(search, :notable_of_Article_type_title_eq) => 'Test')
246
+ search.build(search.context.encode_parameter(:notable_of_Article_type_title_eq) => 'Test')
260
247
  search.result.should be_an ActiveRecord::Relation
261
248
  where = search.result.where_values.first
262
249
  where.to_sql.should match /"articles"."title" = 'Test'/
@@ -265,9 +252,9 @@ module Ransack # We're testing Ransack's Search wih abbreviations
265
252
  it 'evaluates nested conditions' do
266
253
  search = Search.new(Person)
267
254
  search.build(
268
- ransack_abbreviation_for(search, :children_name_eq) => 'Ernie',
255
+ search.context.encode_parameter(:children_name_eq) => 'Ernie',
269
256
  :g => [
270
- ransack_abbreviations_for(search, :m => 'or', :name_eq => 'Ernie', :children_children_name_eq => 'Ernie')
257
+ {:m => 'or', search.context.encode_parameter(:name_eq) => 'Ernie', search.context.encode_parameter(:children_children_name_eq) => 'Ernie'}
271
258
  ]
272
259
  )
273
260
  search.result.should be_an ActiveRecord::Relation
@@ -281,8 +268,8 @@ module Ransack # We're testing Ransack's Search wih abbreviations
281
268
  search = Search.new(Person)
282
269
  search.build(
283
270
  :g => [
284
- ransack_abbreviations_for(search, :m => 'or', :name_eq => 'Ernie', :children_name_eq => 'Ernie'),
285
- ransack_abbreviations_for(search, :m => 'or', :name_eq => 'Bert', :children_name_eq => 'Bert'),
271
+ {:m => 'or', search.context.encode_parameter(:name_eq) => 'Ernie', search.context.encode_parameter(:children_name_eq) => 'Ernie'},
272
+ {:m => 'or', search.context.encode_parameter(:name_eq) => 'Bert', search.context.encode_parameter(:children_name_eq) => 'Bert'}
286
273
  ]
287
274
  )
288
275
  search.result.should be_an ActiveRecord::Relation
@@ -298,7 +285,7 @@ module Ransack # We're testing Ransack's Search wih abbreviations
298
285
  it 'returns distinct records when passed :distinct => true' do
299
286
  search = Search.new(Person)
300
287
  search.build(
301
- :g => [ransack_abbreviations_for(search, :m => 'or', :comments_body_cont => 'e', :articles_comments_body_cont => 'e')]
288
+ :g => [{:m => 'or', search.context.encode_parameter(:comments_body_cont) => 'e', search.context.encode_parameter(:articles_comments_body_cont) => 'e'}]
302
289
  )
303
290
  search.result.all.should have(920).items
304
291
  search.result(:distinct => true).should have(330).items
@@ -372,12 +359,12 @@ module Ransack # We're testing Ransack's Search wih abbreviations
372
359
  # Just some random sanity checks
373
360
  search = Search.new(Person, :authored_article_comments_vote_count_lteq => 10)
374
361
  abbr_search = Search.new(Person)
375
- abbr_search.build(ransack_abbreviation_for(abbr_search, :authored_article_comments_vote_count_lteq) => 10)
362
+ abbr_search.build(abbr_search.context.encode_parameter(:authored_article_comments_vote_count_lteq) => 10)
376
363
  search.result.where_values.first.to_sql.should eq abbr_search.result.where_values.first.to_sql
377
364
 
378
365
  search = Search.new(Note, :notable_of_Person_type_children_name_eq => "Ernie")
379
366
  abbr_search = Search.new(Note)
380
- abbr_search.build(ransack_abbreviation_for(abbr_search, :notable_of_Person_type_children_name_eq) => "Ernie")
367
+ abbr_search.build(abbr_search.context.encode_parameter(:notable_of_Person_type_children_name_eq) => "Ernie")
381
368
  search.result.where_values.first.to_sql.should eq abbr_search.result.where_values.first.to_sql
382
369
  end
383
370
  end
@@ -389,16 +376,16 @@ module Ransack # We're testing Ransack's Search wih abbreviations
389
376
  end
390
377
 
391
378
  it 'sets condition attributes' do
392
- abbr_search = ransack_abbreviation_for(@search, :middle_name_eq)
379
+ abbr_search = @search.context.encode_parameter(:middle_name_eq)
393
380
  @search.send "#{abbr_search}=", 'Ernie'
394
381
  @search.middle_name_eq.should eq 'Ernie'
395
382
 
396
- abbr_search = ransack_abbreviation_for(@search, :authored_article_comments_vote_count_lteq)
383
+ abbr_search = @search.context.encode_parameter(:authored_article_comments_vote_count_lteq)
397
384
  @search.send "#{abbr_search}=", 10
398
385
  @search.authored_article_comments_vote_count_lteq.should eq 10
399
386
 
400
387
  note_search = Search.new(Note)
401
- abbr_search = ransack_abbreviation_for(note_search, :notable_of_Person_type_name_eq)
388
+ abbr_search = note_search.context.encode_parameter(:notable_of_Person_type_name_eq)
402
389
  note_search.send "#{abbr_search}=", 'Ernie'
403
390
  note_search.notable_of_Person_type_name_eq.should eq 'Ernie'
404
391
  end
data/spec/spec_helper.rb CHANGED
@@ -20,8 +20,6 @@ Sham.define do
20
20
  end
21
21
 
22
22
  RSpec.configure do |config|
23
- config.include RansackAbbreviator::ViewHelpers
24
-
25
23
  config.before(:suite) do
26
24
  puts '=' * 80
27
25
  puts "Running specs against ActiveRecord #{ActiveRecord::VERSION::STRING} and ARel #{Arel::VERSION}..."
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: ransack_abbreviator
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.7
5
+ version: 0.0.8
6
6
  platform: ruby
7
7
  authors:
8
8
  - Jamie Davidson
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2013-02-08 00:00:00 Z
13
+ date: 2013-02-14 00:00:00 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: ransack
@@ -108,7 +108,6 @@ files:
108
108
  - lib/ransack_abbreviator/ransack_extensions/nodes/condition.rb
109
109
  - lib/ransack_abbreviator/ransack_extensions/search.rb
110
110
  - lib/ransack_abbreviator/version.rb
111
- - lib/ransack_abbreviator/view_helpers.rb
112
111
  - ransack_abbreviator.gemspec
113
112
  - spec/blueprints/articles.rb
114
113
  - spec/blueprints/comments.rb
@@ -117,7 +116,8 @@ files:
117
116
  - spec/blueprints/tags.rb
118
117
  - spec/ransack_abbreviator/adapters/active_record/base_spec.rb
119
118
  - spec/ransack_abbreviator/configuration_spec.rb
120
- - spec/ransack_abbreviator/helper_spec.rb
119
+ - spec/ransack_abbreviator/decoder_spec.rb
120
+ - spec/ransack_abbreviator/encoder_spec.rb
121
121
  - spec/ransack_abbreviator/helpers/form_builder_spec.rb
122
122
  - spec/ransack_abbreviator/search_spec.rb
123
123
  - spec/spec_helper.rb
@@ -157,7 +157,8 @@ test_files:
157
157
  - spec/blueprints/tags.rb
158
158
  - spec/ransack_abbreviator/adapters/active_record/base_spec.rb
159
159
  - spec/ransack_abbreviator/configuration_spec.rb
160
- - spec/ransack_abbreviator/helper_spec.rb
160
+ - spec/ransack_abbreviator/decoder_spec.rb
161
+ - spec/ransack_abbreviator/encoder_spec.rb
161
162
  - spec/ransack_abbreviator/helpers/form_builder_spec.rb
162
163
  - spec/ransack_abbreviator/search_spec.rb
163
164
  - spec/spec_helper.rb
@@ -1,30 +0,0 @@
1
- module RansackAbbreviator
2
- module ViewHelpers
3
- def ransack_abbreviation_for(ransack_search_object, param)
4
- ransack_search_object.context.encode_parameter(param)
5
- end
6
-
7
- def ransack_abbreviations_for(ransack_search_object, params)
8
- new_params = nil
9
-
10
- case params
11
- when Hash
12
- new_params = {}
13
- params.each do |ransack_name, value|
14
- new_params[ransack_abbreviation_for(ransack_search_object, ransack_name)] = value
15
- end
16
- when Array
17
- new_params = []
18
- params.each do |ransack_name|
19
- new_params << ransack_abbreviation_for(ransack_search_object, ransack_name)
20
- end
21
- else
22
- raise ArgumentError, "don't know how to interpret abbreviations for #{params}"
23
- end
24
-
25
- new_params
26
- end
27
- end
28
- end
29
-
30
- ActionView::Base.send :include, RansackAbbreviator::ViewHelpers
@@ -1,90 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module RansackAbbreviator
4
- module ViewHelpers
5
- describe "#ransack_abbreviation_for" do
6
- context "a lookup of a defined column" do
7
- it "returns an abbreviated column name" do
8
- search = Ransack::Search.new(Person)
9
- ransack_abbreviation_for(search, :name_eq).should == "nm_eq"
10
- ransack_abbreviation_for(search, :middle_name_eq).should == "mn_eq"
11
- end
12
- end
13
-
14
- context "a lookup of an undefined column" do
15
- it "returns the full column name" do
16
- search = Ransack::Search.new(Person)
17
- ransack_abbreviation_for(search, :salary_eq).should == "salary_eq"
18
- end
19
- end
20
-
21
- context "a lookup of a defined association" do
22
- context "a lookup of a defined column" do
23
- it "returns an abbreviated assoc & column pair" do
24
- search = Ransack::Search.new(Article)
25
- ransack_abbreviation_for(search, :person_name_eq).should == "pr.nm_eq"
26
- ransack_abbreviation_for(search, :person_middle_name_eq).should == "pr.mn_eq"
27
- search = Ransack::Search.new(Person)
28
- ransack_abbreviation_for(search, :authored_article_comments_vote_count_lteq).should == "a_ac.vc_lteq"
29
- end
30
- end
31
-
32
- context "a lookup of an undefined column" do
33
- it "returns an abbreviated table but full column" do
34
- search = Ransack::Search.new(Article)
35
- ransack_abbreviation_for(search, :person_salary_eq).should == "pr.salary_eq"
36
- end
37
- end
38
- end
39
-
40
- context "a lookup of an undefined association" do
41
- context "a lookup of a defined column" do
42
- it "returns the full association but abbreviated column" do
43
- search = Ransack::Search.new(Person)
44
- ransack_abbreviation_for(search, :articles_title_cont).should == "articles.tl_cont"
45
- end
46
- end
47
-
48
- context "a lookup of an undefined column" do
49
- it "returns a full association & column pair" do
50
- search = Ransack::Search.new(Person)
51
- ransack_abbreviation_for(search, :articles_body_cont).should == "articles.body_cont"
52
- end
53
- end
54
- end
55
-
56
- context "a lookup of a multi-condition string" do
57
- it "returns abbreviated forms for all conditions" do
58
- search = Ransack::Search.new(Person)
59
- ransack_abbreviation_for(search, :children_name_or_children_salary_eq).should == "ch.nm_or_ch.salary_eq"
60
- end
61
- end
62
-
63
- context "a lookup of a defined polymorphic belongs_to association" do
64
- it "returns the abbreviated name for the polymorphic association" do
65
- search = Ransack::Search.new(Note)
66
- ransack_abbreviation_for(search, :notable_of_Person_type_name_eq).should == "nbl_of_pr_type.nm_eq"
67
- end
68
- end
69
-
70
- context "a lookup of a nested condition" do
71
- it "abbreviates each association and column" do
72
- search = Ransack::Search.new(Person)
73
- ransack_abbreviation_for(search, :articles_comments_body_cont).should == "articles_cm.body_cont"
74
- end
75
-
76
- it "abbreviates nested conditions for polymorphic associations" do
77
- search = Ransack::Search.new(Note)
78
- ransack_abbreviation_for(search, :notable_of_Person_type_children_name_eq).should == "nbl_of_pr_type_ch.nm_eq"
79
- end
80
- end
81
-
82
- context "a lookup of something totally random" do
83
- it "does absolutely nothing" do
84
- search = Ransack::Search.new(Note)
85
- ransack_abbreviation_for(search, :i_am_garbage).should == "i_am_garbage"
86
- end
87
- end
88
- end
89
- end
90
- end