mongoid_query_string_interface 0.4.0 → 0.4.1
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/Gemfile.lock +1 -1
 - data/lib/mongoid/helpers.rb +10 -0
 - data/lib/mongoid/parsers/filter_parser.rb +7 -5
 - data/lib/mongoid/parsers/filters_parser.rb +11 -3
 - data/lib/mongoid/query_string_interface.rb +4 -2
 - data/lib/version.rb +1 -1
 - metadata +3 -3
 
    
        data/Gemfile.lock
    CHANGED
    
    
    
        data/lib/mongoid/helpers.rb
    CHANGED
    
    | 
         @@ -9,6 +9,16 @@ module Mongoid 
     | 
|
| 
       9 
9 
     | 
    
         
             
                    hash = hash_with_indifferent_access(hash_with_attributes_to_replace)
         
     | 
| 
       10 
10 
     | 
    
         
             
                    hash.has_key?(attribute) ? hash[attribute] : attribute
         
     | 
| 
       11 
11 
     | 
    
         
             
                  end
         
     | 
| 
      
 12 
     | 
    
         
            +
             
     | 
| 
      
 13 
     | 
    
         
            +
                  def replaced_attribute_name(attribute, hash_with_attributes_to_replace)
         
     | 
| 
      
 14 
     | 
    
         
            +
                    attribute = replace_attribute(attribute, hash_with_attributes_to_replace)
         
     | 
| 
      
 15 
     | 
    
         
            +
                    attribute.is_a?(Hash) ? attribute[:to] : attribute
         
     | 
| 
      
 16 
     | 
    
         
            +
                  end
         
     | 
| 
      
 17 
     | 
    
         
            +
             
     | 
| 
      
 18 
     | 
    
         
            +
                  def replaced_attribute_value(attribute, value, hash_with_attributes_to_replace, raw_params)
         
     | 
| 
      
 19 
     | 
    
         
            +
                    attribute = replace_attribute(attribute, hash_with_attributes_to_replace)
         
     | 
| 
      
 20 
     | 
    
         
            +
                    attribute.is_a?(Hash) ? attribute[:convert_value_to].call(value, raw_params) : value
         
     | 
| 
      
 21 
     | 
    
         
            +
                  end
         
     | 
| 
       12 
22 
     | 
    
         
             
                end
         
     | 
| 
       13 
23 
     | 
    
         
             
              end
         
     | 
| 
       14 
24 
     | 
    
         
             
            end
         
     | 
| 
         @@ -22,14 +22,15 @@ module Mongoid 
     | 
|
| 
       22 
22 
     | 
    
         
             
                      Mongoid::QueryStringInterface::Parsers::BooleanAndNilParser.new
         
     | 
| 
       23 
23 
     | 
    
         
             
                    ]
         
     | 
| 
       24 
24 
     | 
    
         | 
| 
       25 
     | 
    
         
            -
                    def initialize(raw_attribute, raw_value, attributes_to_replace={})
         
     | 
| 
      
 25 
     | 
    
         
            +
                    def initialize(raw_attribute, raw_value, attributes_to_replace={}, raw_params={})
         
     | 
| 
       26 
26 
     | 
    
         
             
                      @raw_attribute = raw_attribute
         
     | 
| 
       27 
27 
     | 
    
         
             
                      @raw_value = raw_value
         
     | 
| 
       28 
28 
     | 
    
         
             
                      @attributes_to_replace = attributes_to_replace
         
     | 
| 
      
 29 
     | 
    
         
            +
                      @raw_params = raw_params
         
     | 
| 
       29 
30 
     | 
    
         
             
                    end
         
     | 
| 
       30 
31 
     | 
    
         | 
| 
       31 
32 
     | 
    
         
             
                    def attribute
         
     | 
| 
       32 
     | 
    
         
            -
                      @attribute ||=  
     | 
| 
      
 33 
     | 
    
         
            +
                      @attribute ||= replaced_attribute_name(parsed_attribute, @attributes_to_replace).to_s
         
     | 
| 
       33 
34 
     | 
    
         
             
                    end
         
     | 
| 
       34 
35 
     | 
    
         | 
| 
       35 
36 
     | 
    
         
             
                    def value
         
     | 
| 
         @@ -90,10 +91,10 @@ module Mongoid 
     | 
|
| 
       90 
91 
     | 
    
         
             
                          if or_attribute?
         
     | 
| 
       91 
92 
     | 
    
         
             
                            parsed_json_value
         
     | 
| 
       92 
93 
     | 
    
         
             
                          else
         
     | 
| 
       93 
     | 
    
         
            -
                            { operator => parsed_value }
         
     | 
| 
      
 94 
     | 
    
         
            +
                            { operator => replaced_attribute_value(parsed_attribute, parsed_value, @attributes_to_replace, @raw_params) }
         
     | 
| 
       94 
95 
     | 
    
         
             
                          end
         
     | 
| 
       95 
96 
     | 
    
         
             
                        else
         
     | 
| 
       96 
     | 
    
         
            -
                          parsed_value
         
     | 
| 
      
 97 
     | 
    
         
            +
                          replaced_attribute_value(parsed_attribute, parsed_value, @attributes_to_replace, @raw_params)
         
     | 
| 
       97 
98 
     | 
    
         
             
                        end
         
     | 
| 
       98 
99 
     | 
    
         
             
                      end
         
     | 
| 
       99 
100 
     | 
    
         | 
| 
         @@ -123,7 +124,7 @@ module Mongoid 
     | 
|
| 
       123 
124 
     | 
    
         
             
                        raise "$or query filters must be given as an array of hashes" unless valid_or_filters?(raw_or_data)
         
     | 
| 
       124 
125 
     | 
    
         | 
| 
       125 
126 
     | 
    
         
             
                        raw_or_data.map do |filters|
         
     | 
| 
       126 
     | 
    
         
            -
                          FiltersParser.new(filters, {}, @attributes_to_replace)
         
     | 
| 
      
 127 
     | 
    
         
            +
                          FiltersParser.new(filters, {}, @attributes_to_replace, @raw_params)
         
     | 
| 
       127 
128 
     | 
    
         
             
                        end
         
     | 
| 
       128 
129 
     | 
    
         
             
                      end
         
     | 
| 
       129 
130 
     | 
    
         | 
| 
         @@ -146,6 +147,7 @@ module Mongoid 
     | 
|
| 
       146 
147 
     | 
    
         
             
                          "$#{$1}"
         
     | 
| 
       147 
148 
     | 
    
         
             
                        end
         
     | 
| 
       148 
149 
     | 
    
         
             
                      end
         
     | 
| 
      
 150 
     | 
    
         
            +
             
     | 
| 
       149 
151 
     | 
    
         
             
                  end
         
     | 
| 
       150 
152 
     | 
    
         
             
                end
         
     | 
| 
       151 
153 
     | 
    
         
             
              end
         
     | 
| 
         @@ -2,21 +2,29 @@ module Mongoid 
     | 
|
| 
       2 
2 
     | 
    
         
             
              module QueryStringInterface
         
     | 
| 
       3 
3 
     | 
    
         
             
                module Parsers
         
     | 
| 
       4 
4 
     | 
    
         
             
                  class FiltersParser
         
     | 
| 
      
 5 
     | 
    
         
            +
                    include Mongoid::QueryStringInterface::Helpers
         
     | 
| 
      
 6 
     | 
    
         
            +
             
     | 
| 
       5 
7 
     | 
    
         
             
                    attr_reader :filters, :default_filters
         
     | 
| 
       6 
8 
     | 
    
         | 
| 
       7 
     | 
    
         
            -
                    def initialize(filters, default_filters={}, attributes_to_replace={})
         
     | 
| 
      
 9 
     | 
    
         
            +
                    def initialize(filters, default_filters={}, attributes_to_replace={}, raw_filters=nil)
         
     | 
| 
       8 
10 
     | 
    
         
             
                      @filters = filters.with_indifferent_access
         
     | 
| 
       9 
11 
     | 
    
         
             
                      @default_filters = default_filters.with_indifferent_access
         
     | 
| 
       10 
12 
     | 
    
         
             
                      @attributes_to_replace = attributes_to_replace.with_indifferent_access
         
     | 
| 
      
 13 
     | 
    
         
            +
                      @raw_filters = raw_filters.nil? ? @filters : raw_filters.with_indifferent_access
         
     | 
| 
       11 
14 
     | 
    
         
             
                    end
         
     | 
| 
       12 
15 
     | 
    
         | 
| 
       13 
16 
     | 
    
         
             
                    def parse
         
     | 
| 
       14 
     | 
    
         
            -
                      default_filters. 
     | 
| 
      
 17 
     | 
    
         
            +
                      result = default_filters.inject({}) do |result, item|
         
     | 
| 
      
 18 
     | 
    
         
            +
                        raw_attribute, raw_value = item
         
     | 
| 
      
 19 
     | 
    
         
            +
                        result[replaced_attribute_name(raw_attribute, @attributes_to_replace).to_s] = replaced_attribute_value(raw_attribute, raw_value, @attributes_to_replace, @raw_filters)
         
     | 
| 
      
 20 
     | 
    
         
            +
                        result
         
     | 
| 
      
 21 
     | 
    
         
            +
                      end
         
     | 
| 
      
 22 
     | 
    
         
            +
                      result.merge(parsed_filters)
         
     | 
| 
       15 
23 
     | 
    
         
             
                    end
         
     | 
| 
       16 
24 
     | 
    
         | 
| 
       17 
25 
     | 
    
         
             
                    def filter_parsers
         
     | 
| 
       18 
26 
     | 
    
         
             
                      @filter_parsers ||= filters.map do |raw_attribute, raw_value|
         
     | 
| 
       19 
     | 
    
         
            -
                        FilterParser.new(raw_attribute, raw_value, @attributes_to_replace)
         
     | 
| 
      
 27 
     | 
    
         
            +
                        FilterParser.new(raw_attribute, raw_value, @attributes_to_replace, @raw_filters)
         
     | 
| 
       20 
28 
     | 
    
         
             
                      end
         
     | 
| 
       21 
29 
     | 
    
         
             
                    end
         
     | 
| 
       22 
30 
     | 
    
         | 
| 
         @@ -89,7 +89,7 @@ module Mongoid 
     | 
|
| 
       89 
89 
     | 
    
         
             
                  end
         
     | 
| 
       90 
90 
     | 
    
         | 
| 
       91 
91 
     | 
    
         
             
                  def sorting_options(options)
         
     | 
| 
       92 
     | 
    
         
            -
                    parse_order_by(options) || default_sorting_options
         
     | 
| 
      
 92 
     | 
    
         
            +
                    parse_order_by(options) || default_sorting_options.map { |field| sorting_operator_for(field) }
         
     | 
| 
       93 
93 
     | 
    
         
             
                  end
         
     | 
| 
       94 
94 
     | 
    
         | 
| 
       95 
95 
     | 
    
         
             
                  def only_filtering(options)
         
     | 
| 
         @@ -105,7 +105,9 @@ module Mongoid 
     | 
|
| 
       105 
105 
     | 
    
         
             
                  end
         
     | 
| 
       106 
106 
     | 
    
         | 
| 
       107 
107 
     | 
    
         
             
                  def sorting_operator_for(field)
         
     | 
| 
       108 
     | 
    
         
            -
                    if  
     | 
| 
      
 108 
     | 
    
         
            +
                    if field.is_a?(Mongoid::Criterion::Complex)
         
     | 
| 
      
 109 
     | 
    
         
            +
                      replace_attribute(field.key, sorting_attributes_to_replace).to_sym.send(field.operator)
         
     | 
| 
      
 110 
     | 
    
         
            +
                    elsif match = field.match(/(.*)\.(#{SORTING_OPERATORS.join('|')})/)
         
     | 
| 
       109 
111 
     | 
    
         
             
                      replace_attribute(match[1], sorting_attributes_to_replace).to_sym.send(match[2])
         
     | 
| 
       110 
112 
     | 
    
         
             
                    else
         
     | 
| 
       111 
113 
     | 
    
         
             
                      replace_attribute(field, sorting_attributes_to_replace).to_sym.asc
         
     | 
    
        data/lib/version.rb
    CHANGED
    
    
    
        metadata
    CHANGED
    
    | 
         @@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version 
     | 
|
| 
       5 
5 
     | 
    
         
             
              segments: 
         
     | 
| 
       6 
6 
     | 
    
         
             
              - 0
         
     | 
| 
       7 
7 
     | 
    
         
             
              - 4
         
     | 
| 
       8 
     | 
    
         
            -
              -  
     | 
| 
       9 
     | 
    
         
            -
              version: 0.4. 
     | 
| 
      
 8 
     | 
    
         
            +
              - 1
         
     | 
| 
      
 9 
     | 
    
         
            +
              version: 0.4.1
         
     | 
| 
       10 
10 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       11 
11 
     | 
    
         
             
            authors: 
         
     | 
| 
       12 
12 
     | 
    
         
             
            - Vicente Mundim
         
     | 
| 
         @@ -14,7 +14,7 @@ autorequire: 
     | 
|
| 
       14 
14 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       15 
15 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       16 
16 
     | 
    
         | 
| 
       17 
     | 
    
         
            -
            date: 2011-06- 
     | 
| 
      
 17 
     | 
    
         
            +
            date: 2011-06-07 00:00:00 -03:00
         
     | 
| 
       18 
18 
     | 
    
         
             
            default_executable: 
         
     | 
| 
       19 
19 
     | 
    
         
             
            dependencies: 
         
     | 
| 
       20 
20 
     | 
    
         
             
            - !ruby/object:Gem::Dependency 
         
     |