Class: RansackAbbreviator::Abbreviators::Encoder
- Inherits:
-
Object
- Object
- RansackAbbreviator::Abbreviators::Encoder
- Defined in:
- lib/ransack_abbreviator/abbreviators/encoder.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
- #encode_association_and_column(str) ⇒ Object
- #encode_associations(associations, ransack_name) ⇒ Object
- #encode_attribute(attr_name, parent = @context.klass) ⇒ Object
- #encode_parameter(param) ⇒ Object
-
#initialize(context) ⇒ Encoder
constructor
A new instance of Encoder.
Constructor Details
#initialize(context) ⇒ Encoder
Returns a new instance of Encoder.
6 7 8 |
# File 'lib/ransack_abbreviator/abbreviators/encoder.rb', line 6 def initialize(context) @context = context end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
4 5 6 |
# File 'lib/ransack_abbreviator/abbreviators/encoder.rb', line 4 def context @context end |
Instance Method Details
#encode_association_and_column(str) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/ransack_abbreviator/abbreviators/encoder.rb', line 24 def encode_association_and_column(str) encoded_str = "" associations, attr_name = @context.get_associations_and_attribute(str) parent_of_attribute = @context.klass if attr_name unless associations.blank? encoded_associations, parent_of_attribute = encode_associations(associations, str) encoded_str = "#{encoded_associations}." end encoded_str << encode_attribute(attr_name, parent_of_attribute) else encoded_str = str end encoded_str end |
#encode_associations(associations, ransack_name) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
# File 'lib/ransack_abbreviator/abbreviators/encoder.rb', line 42 def encode_associations(associations, ransack_name) klass = @context.klass encoded_str = "" associations.each_with_index do |assoc, i| encoded_str << "_" unless i == 0 encoded_str << klass.ransackable_assoc_abbr_for(assoc.name.to_s) if assoc.[:polymorphic] && (match = ransack_name.match(/_of_([^_]+?)_type.*$/)) # Polymorphic belongs_to format detected # Lookup the association abbreviation out of all association abbreviations, as the value # can be the name of any model (e.g. PersonItem...which is why we underscore) klass_name = match.captures.first encoded_str << "_of_#{RansackAbbreviator.assoc_abbreviation_for(klass_name.underscore)}_type" klass = Kernel.const_get(klass_name) else klass = assoc.klass end end [encoded_str, klass] end |
#encode_attribute(attr_name, parent = @context.klass) ⇒ Object
64 65 66 |
# File 'lib/ransack_abbreviator/abbreviators/encoder.rb', line 64 def encode_attribute(attr_name, parent=@context.klass) parent.ransackable_column_abbr_for(attr_name) end |
#encode_parameter(param) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/ransack_abbreviator/abbreviators/encoder.rb', line 10 def encode_parameter(param) str = param.is_a?(Symbol) ? param.to_s : param.dup pred = Ransack::Predicate.detect_and_strip_from_string!(str) encoded_param = "" conjunctions = str.split("_").select{|s| s == "and" || s == "or" } str.split(/_and_|_or_/).each do |s| encoded_param << self.context.encode_association_and_column(s) encoded_param << "_#{conjunctions.shift}_" if !conjunctions.blank? end encoded_param << "_#{pred}" if pred encoded_param end |