ransack_abbreviator 0.0.3 → 0.0.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/README.md +1 -1
- data/lib/ransack_abbreviator/abbreviators/decoder.rb +7 -7
- data/lib/ransack_abbreviator/abbreviators/encoder.rb +15 -1
- data/lib/ransack_abbreviator/configuration.rb +20 -0
- data/lib/ransack_abbreviator/ransack_extensions/context.rb +2 -20
- data/lib/ransack_abbreviator/version.rb +1 -1
- data/lib/ransack_abbreviator/view_helpers.rb +2 -11
- data/lib/ransack_abbreviator.rb +0 -4
- data/spec/ransack_abbreviator/adapters/active_record/base_spec.rb +7 -1
- data/spec/ransack_abbreviator/search_spec.rb +32 -15
- metadata +2 -2
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
## Ransack Abbreviator [](http://travis-ci.org/jhdavids8/ransack-abbreviator)
|
2
2
|
===================
|
3
3
|
|
4
4
|
|
@@ -8,7 +8,7 @@ module RansackAbbreviator
|
|
8
8
|
end
|
9
9
|
|
10
10
|
def decode_parameter(param)
|
11
|
-
str = param.dup
|
11
|
+
str = param.is_a?(Symbol) ? param.to_s : param.dup
|
12
12
|
pred = Ransack::Predicate.detect_and_strip_from_string!(str)
|
13
13
|
decoded_param = nil
|
14
14
|
case str
|
@@ -18,7 +18,7 @@ module RansackAbbreviator
|
|
18
18
|
conjunctions = str.split("_").select{|s| s == "and" || s == "or" }
|
19
19
|
decoded_param = ""
|
20
20
|
str.split(/_and_|_or_/).each do |possible_abbr|
|
21
|
-
decoded_str = self.context.
|
21
|
+
decoded_str = self.context.decode_association_and_column(possible_abbr)
|
22
22
|
decoded_param << (!decoded_str.blank? ? decoded_str : possible_abbr)
|
23
23
|
decoded_param << "_#{conjunctions.shift}_" if !conjunctions.blank?
|
24
24
|
end
|
@@ -28,15 +28,15 @@ module RansackAbbreviator
|
|
28
28
|
decoded_param
|
29
29
|
end
|
30
30
|
|
31
|
-
def
|
31
|
+
def decode_association_and_column(possible_abbr)
|
32
32
|
possible_assoc_abbr, possible_attr_abbr = extract_possible_assoc_and_attribute_abbr(possible_abbr)
|
33
33
|
parent_of_attribute = @context.klass
|
34
34
|
decoded_str = ""
|
35
35
|
if possible_assoc_abbr
|
36
|
-
decoded_str, parent_of_attribute =
|
36
|
+
decoded_str, parent_of_attribute = decode_association(possible_assoc_abbr)
|
37
37
|
end
|
38
38
|
|
39
|
-
if attr_name =
|
39
|
+
if attr_name = decode_attribute(possible_attr_abbr, parent_of_attribute)
|
40
40
|
decoded_str << attr_name
|
41
41
|
else
|
42
42
|
decoded_str << possible_attr_abbr
|
@@ -45,7 +45,7 @@ module RansackAbbreviator
|
|
45
45
|
decoded_str
|
46
46
|
end
|
47
47
|
|
48
|
-
def
|
48
|
+
def decode_association(possible_assoc_abbr)
|
49
49
|
# possible_assoc_abbr can be a chain of abbreviated associations, so decode them all and reconstruct into
|
50
50
|
# the format expected by Ransack
|
51
51
|
decoded_str = ""
|
@@ -75,7 +75,7 @@ module RansackAbbreviator
|
|
75
75
|
[decoded_str, klass]
|
76
76
|
end
|
77
77
|
|
78
|
-
def
|
78
|
+
def decode_attribute(possible_attr_abbr, klass=@context.klass)
|
79
79
|
klass.ransackable_column_name_for(possible_attr_abbr)
|
80
80
|
end
|
81
81
|
|
@@ -7,7 +7,21 @@ module RansackAbbreviator
|
|
7
7
|
@context = context
|
8
8
|
end
|
9
9
|
|
10
|
-
def
|
10
|
+
def encode_parameter(param)
|
11
|
+
str = param.is_a?(Symbol) ? param.to_s : param.dup
|
12
|
+
pred = Ransack::Predicate.detect_and_strip_from_string!(str)
|
13
|
+
encoded_param = ""
|
14
|
+
conjunctions = str.split("_").select{|s| s == "and" || s == "or" }
|
15
|
+
str.split(/_and_|_or_/).each do |s|
|
16
|
+
encoded_param << self.context.encode_association_and_column(s)
|
17
|
+
encoded_param << "_#{conjunctions.shift}_" if !conjunctions.blank?
|
18
|
+
end
|
19
|
+
|
20
|
+
encoded_param << "_#{pred}" if pred
|
21
|
+
encoded_param
|
22
|
+
end
|
23
|
+
|
24
|
+
def encode_association_and_column(str)
|
11
25
|
encoded_str = ""
|
12
26
|
associations, attr_name = @context.get_associations_and_attribute(str)
|
13
27
|
parent_of_attribute = @context.klass
|
@@ -9,6 +9,26 @@ module RansackAbbreviator
|
|
9
9
|
yield self
|
10
10
|
end
|
11
11
|
|
12
|
+
def column_abbreviations=(abbreviations)
|
13
|
+
if abbreviations
|
14
|
+
if !(abbreviations.values & RansackAbbreviator::Constants::RESERVED_KEYWORDS).blank?
|
15
|
+
fail "You used a reserved keyword as a column abbreviation. Reserverd keywords: #{RansackAbbreviator::Constants::RESERVED_KEYWORDS.join(", ")}"
|
16
|
+
end
|
17
|
+
|
18
|
+
self.column_abbreviations = abbreviations
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def assoc_abbreviations=(abbreviations)
|
23
|
+
if abbreviations
|
24
|
+
if !(abbreviations.values & RansackAbbreviator::Constants::RESERVED_KEYWORDS).blank?
|
25
|
+
fail "You used a reserved keyword as an association abbreviation. Reserverd keywords: #{RansackAbbreviator::Constants::RESERVED_KEYWORDS.join(", ")}"
|
26
|
+
end
|
27
|
+
|
28
|
+
self.assoc_abbreviations = abbreviations
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
12
32
|
def add_column_abbreviation(column, abbr)
|
13
33
|
self.column_abbreviations[column.to_s] = abbr.to_s
|
14
34
|
end
|
@@ -4,11 +4,9 @@ require 'ransack_abbreviator/abbreviators/encoder'
|
|
4
4
|
module Ransack
|
5
5
|
class Context
|
6
6
|
attr_reader :decoder, :encoder
|
7
|
-
alias_method :full_ransackable_attribute?, :ransackable_attribute?
|
8
|
-
alias_method :full_ransackable_association?, :ransackable_association?
|
9
7
|
|
10
|
-
delegate :
|
11
|
-
delegate :decode_parameter, :
|
8
|
+
delegate :encode_parameter, :encode_association_and_column, to: :encoder
|
9
|
+
delegate :decode_parameter, :decode_association_and_column, to: :decoder
|
12
10
|
|
13
11
|
def get_associations_and_attribute(str, klass = @klass, associations = [])
|
14
12
|
attr_name = nil
|
@@ -37,22 +35,6 @@ module Ransack
|
|
37
35
|
@encoder ||= RansackAbbreviator::Abbreviators::Encoder.new(self)
|
38
36
|
end
|
39
37
|
|
40
|
-
def ransackable_attribute?(str, klass)
|
41
|
-
full_ransackable_attribute?(str, klass) || ransackable_attribute_abbreviation?(str, klass)
|
42
|
-
end
|
43
|
-
|
44
|
-
def ransackable_association?(str, klass)
|
45
|
-
full_ransackable_association?(str, klass) || ransackable_assoc_abbreviation?(str, klass)
|
46
|
-
end
|
47
|
-
|
48
|
-
def ransackable_attribute_abbreviation?(str, klass)
|
49
|
-
klass.ransackable_column_abbreviations.has_value?(str)
|
50
|
-
end
|
51
|
-
|
52
|
-
def ransackable_assoc_abbreviation?(str, klass)
|
53
|
-
klass.ransackable_assoc_abbreviations.has_value?(str)
|
54
|
-
end
|
55
|
-
|
56
38
|
def self.polymorphic_association_specified?(str)
|
57
39
|
str && str.match(/_of_([^_]+?)_type$/)
|
58
40
|
end
|
@@ -1,16 +1,7 @@
|
|
1
1
|
module RansackAbbreviator
|
2
2
|
module ViewHelpers
|
3
|
-
def ransack_abbreviation_for(ransack_search_object,
|
4
|
-
|
5
|
-
pred = Ransack::Predicate.detect_and_strip_from_string!(str)
|
6
|
-
conjunctions = str.split("_").select{|s| s == "and" || s == "or" }
|
7
|
-
abbr_str = ""
|
8
|
-
str.split(/_and_|_or_/).each do |s|
|
9
|
-
abbr_str << ransack_search_object.context.encode_ransack_str(s)
|
10
|
-
abbr_str << "_#{conjunctions.shift}_" if !conjunctions.blank?
|
11
|
-
end
|
12
|
-
|
13
|
-
pred ? "#{abbr_str}_#{pred}" : abbr_str
|
3
|
+
def ransack_abbreviation_for(ransack_search_object, param)
|
4
|
+
ransack_search_object.context.encode_parameter(param)
|
14
5
|
end
|
15
6
|
|
16
7
|
def ransack_abbreviations_for(ransack_search_object, params)
|
data/lib/ransack_abbreviator.rb
CHANGED
@@ -11,10 +11,6 @@ RansackAbbreviator.configure do |config|
|
|
11
11
|
if File.exist?(config.config_dir.join("ransack_abbreviator.yml"))
|
12
12
|
parsed_config = OpenStruct.new(YAML.load_file(config.config_dir.join("ransack_abbreviator.yml")))
|
13
13
|
ransack_abbreviations = parsed_config.ransack_abbreviations
|
14
|
-
if !(ransack_abbreviations["columns"].values & RansackAbbreviator::Constants::RESERVED_KEYWORDS).blank? ||
|
15
|
-
!(ransack_abbreviations["associations"].values & RansackAbbreviator::Constants::RESERVED_KEYWORDS).blank?
|
16
|
-
fail "You used a reserved keyword as an abbreviation. Reserverd keywords: #{RansackAbbreviator::Constants::RESERVED_KEYWORDS.join(", ")}"
|
17
|
-
end
|
18
14
|
config.column_abbreviations = ransack_abbreviations["columns"] if ransack_abbreviations["columns"]
|
19
15
|
config.assoc_abbreviations = ransack_abbreviations["associations"] if ransack_abbreviations["associations"]
|
20
16
|
end
|
@@ -58,7 +58,13 @@ module RansackAbbreviator
|
|
58
58
|
end
|
59
59
|
end
|
60
60
|
|
61
|
-
context 'when
|
61
|
+
context 'when a defined association' do
|
62
|
+
it "returns the full association name" do
|
63
|
+
Person.ransackable_assoc_name_for("articles").should eq "articles"
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
context 'when an undefined abbreviation and association' do
|
62
68
|
it "returns nil" do
|
63
69
|
Person.ransackable_assoc_name_for("you_fake").should be_nil
|
64
70
|
end
|
@@ -254,6 +254,11 @@ module Ransack # We're testing Ransack's Search wih abbreviations
|
|
254
254
|
search.result.should be_an ActiveRecord::Relation
|
255
255
|
where = search.result.where_values.first
|
256
256
|
where.to_sql.should match /"people"."name" = 'Ernie'/
|
257
|
+
|
258
|
+
search.build(ransack_abbreviation_for(search, :notable_of_Article_type_title_eq) => 'Test')
|
259
|
+
search.result.should be_an ActiveRecord::Relation
|
260
|
+
where = search.result.where_values.first
|
261
|
+
where.to_sql.should match /"articles"."title" = 'Test'/
|
257
262
|
end
|
258
263
|
|
259
264
|
it 'evaluates nested conditions' do
|
@@ -377,23 +382,35 @@ module Ransack # We're testing Ransack's Search wih abbreviations
|
|
377
382
|
end
|
378
383
|
|
379
384
|
describe '#method_missing' do
|
380
|
-
|
381
|
-
|
382
|
-
|
383
|
-
|
384
|
-
|
385
|
-
|
386
|
-
|
387
|
-
|
385
|
+
context "when sent valid abbreviations" do
|
386
|
+
before do
|
387
|
+
@search = Search.new(Person)
|
388
|
+
end
|
389
|
+
|
390
|
+
it 'sets condition attributes' do
|
391
|
+
abbr_search = ransack_abbreviation_for(@search, :middle_name_eq)
|
392
|
+
@search.send "#{abbr_search}=", 'Ernie'
|
393
|
+
@search.middle_name_eq.should eq 'Ernie'
|
388
394
|
|
389
|
-
|
390
|
-
|
391
|
-
|
395
|
+
abbr_search = ransack_abbreviation_for(@search, :authored_article_comments_vote_count_lteq)
|
396
|
+
@search.send "#{abbr_search}=", 10
|
397
|
+
@search.authored_article_comments_vote_count_lteq.should eq 10
|
392
398
|
|
393
|
-
|
394
|
-
|
395
|
-
|
396
|
-
|
399
|
+
note_search = Search.new(Note)
|
400
|
+
abbr_search = ransack_abbreviation_for(note_search, :notable_of_Person_type_name_eq)
|
401
|
+
note_search.send "#{abbr_search}=", 'Ernie'
|
402
|
+
note_search.notable_of_Person_type_name_eq.should eq 'Ernie'
|
403
|
+
end
|
404
|
+
end
|
405
|
+
context 'when sent valid attributes' do
|
406
|
+
before do
|
407
|
+
@search = Search.new(Person)
|
408
|
+
end
|
409
|
+
|
410
|
+
it 'sets condition attributes' do
|
411
|
+
@search.name_eq = 'Ernie'
|
412
|
+
@search.name_eq.should eq 'Ernie'
|
413
|
+
end
|
397
414
|
end
|
398
415
|
|
399
416
|
it 'raises NoMethodError when sent an invalid attribute/abreviation' do
|
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.
|
5
|
+
version: 0.0.4
|
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-
|
13
|
+
date: 2013-02-04 00:00:00 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: ransack
|