Class: RansackAbbreviator::Abbreviators::Decoder
- Inherits:
-
Object
- Object
- RansackAbbreviator::Abbreviators::Decoder
- Defined in:
- lib/ransack_abbreviator/abbreviators/decoder.rb
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
Instance Method Summary collapse
- #decode_association(possible_assoc_abbr) ⇒ Object
- #decode_association_and_column(possible_abbr) ⇒ Object
- #decode_attribute(possible_attr_abbr, klass = @context.klass) ⇒ Object
- #decode_parameter(param) ⇒ Object
-
#initialize(context) ⇒ Decoder
constructor
A new instance of Decoder.
Constructor Details
#initialize(context) ⇒ Decoder
Returns a new instance of Decoder.
6 7 8 |
# File 'lib/ransack_abbreviator/abbreviators/decoder.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/decoder.rb', line 4 def context @context end |
Instance Method Details
#decode_association(possible_assoc_abbr) ⇒ Object
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/ransack_abbreviator/abbreviators/decoder.rb', line 48 def decode_association(possible_assoc_abbr) # possible_assoc_abbr can be a chain of abbreviated associations, so decode them all and reconstruct into # the format expected by Ransack decoded_str = "" segments = possible_assoc_abbr.split(/_/) association_parts = [] klass = @context.klass while segments.size > 0 && association_parts << segments.shift assoc_to_test = association_parts.join('_') if Ransack::Context.polymorphic_association_specified?(assoc_to_test) assoc_name, class_type = get_polymorphic_assoc_and_class_type(assoc_to_test) klass = Kernel.const_get(class_type) decoded_str << "of_#{class_type}_type_" association_parts.clear elsif assoc_name = klass.ransackable_assoc_name_for(assoc_to_test) assoc = klass.reflect_on_all_associations.find{|a| a.name.to_s == assoc_name} decoded_str << "#{assoc_name}_" unless assoc.[:polymorphic] # Get the model for this association, as the next association/attribute will be related to it klass = assoc.klass association_parts.clear end end end decoded_str = "#{possible_assoc_abbr}_" if decoded_str.blank? [decoded_str, klass] end |
#decode_association_and_column(possible_abbr) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/ransack_abbreviator/abbreviators/decoder.rb', line 31 def decode_association_and_column(possible_abbr) possible_assoc_abbr, possible_attr_abbr = extract_possible_assoc_and_attribute_abbr(possible_abbr) parent_of_attribute = @context.klass decoded_str = "" if possible_assoc_abbr decoded_str, parent_of_attribute = decode_association(possible_assoc_abbr) end if attr_name = decode_attribute(possible_attr_abbr, parent_of_attribute) decoded_str << attr_name else decoded_str << possible_attr_abbr end decoded_str end |
#decode_attribute(possible_attr_abbr, klass = @context.klass) ⇒ Object
78 79 80 |
# File 'lib/ransack_abbreviator/abbreviators/decoder.rb', line 78 def decode_attribute(possible_attr_abbr, klass=@context.klass) klass.ransackable_column_name_for(possible_attr_abbr) end |
#decode_parameter(param) ⇒ Object
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/ransack_abbreviator/abbreviators/decoder.rb', line 10 def decode_parameter(param) str = param.is_a?(Symbol) ? param.to_s : param.dup pred = Ransack::Predicate.detect_and_strip_from_string!(str) decoded_param = nil case str when /^(g|c|m|groupings|conditions|combinator)=?$/ decoded_param = param else conjunctions = str.split("_").select{|s| s == "and" || s == "or" } decoded_param = "" str.split(/_and_|_or_/).each do |possible_abbr| decoded_str = self.context.decode_association_and_column(possible_abbr) decoded_param << (!decoded_str.blank? ? decoded_str : possible_abbr) decoded_param << "_#{conjunctions.shift}_" if !conjunctions.blank? end end decoded_param << "_#{pred}" if pred decoded_param end |