mongoid_query_string_interface 0.3.0 → 0.4.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mongoid_query_string_interface (0.3.0)
4
+ mongoid_query_string_interface (0.4.0)
5
5
  json (>= 1.4.6)
6
6
  mongoid (~> 2.0.0.rc)
7
7
 
@@ -0,0 +1,14 @@
1
+ module Mongoid
2
+ module QueryStringInterface
3
+ module Helpers
4
+ def hash_with_indifferent_access(params)
5
+ params.is_a?(HashWithIndifferentAccess) ? params : params.with_indifferent_access
6
+ end
7
+
8
+ def replace_attribute(attribute, hash_with_attributes_to_replace)
9
+ hash = hash_with_indifferent_access(hash_with_attributes_to_replace)
10
+ hash.has_key?(attribute) ? hash[attribute] : attribute
11
+ end
12
+ end
13
+ end
14
+ end
@@ -10,6 +10,8 @@ module Mongoid
10
10
  module QueryStringInterface
11
11
  module Parsers
12
12
  class FilterParser
13
+ include Mongoid::QueryStringInterface::Helpers
14
+
13
15
  attr_reader :raw_attribute, :raw_value
14
16
 
15
17
  PARSERS = [
@@ -20,13 +22,14 @@ module Mongoid
20
22
  Mongoid::QueryStringInterface::Parsers::BooleanAndNilParser.new
21
23
  ]
22
24
 
23
- def initialize(raw_attribute, raw_value)
25
+ def initialize(raw_attribute, raw_value, attributes_to_replace={})
24
26
  @raw_attribute = raw_attribute
25
27
  @raw_value = raw_value
28
+ @attributes_to_replace = attributes_to_replace
26
29
  end
27
30
 
28
31
  def attribute
29
- @attribute ||= parsed_attribute
32
+ @attribute ||= replace_attribute(parsed_attribute, @attributes_to_replace).to_s
30
33
  end
31
34
 
32
35
  def value
@@ -120,7 +123,7 @@ module Mongoid
120
123
  raise "$or query filters must be given as an array of hashes" unless valid_or_filters?(raw_or_data)
121
124
 
122
125
  raw_or_data.map do |filters|
123
- FiltersParser.new(filters)
126
+ FiltersParser.new(filters, {}, @attributes_to_replace)
124
127
  end
125
128
  end
126
129
 
@@ -146,4 +149,4 @@ module Mongoid
146
149
  end
147
150
  end
148
151
  end
149
- end
152
+ end
@@ -4,9 +4,10 @@ module Mongoid
4
4
  class FiltersParser
5
5
  attr_reader :filters, :default_filters
6
6
 
7
- def initialize(filters, default_filters={})
7
+ def initialize(filters, default_filters={}, attributes_to_replace={})
8
8
  @filters = filters.with_indifferent_access
9
9
  @default_filters = default_filters.with_indifferent_access
10
+ @attributes_to_replace = attributes_to_replace.with_indifferent_access
10
11
  end
11
12
 
12
13
  def parse
@@ -15,7 +16,7 @@ module Mongoid
15
16
 
16
17
  def filter_parsers
17
18
  @filter_parsers ||= filters.map do |raw_attribute, raw_value|
18
- FilterParser.new(raw_attribute, raw_value)
19
+ FilterParser.new(raw_attribute, raw_value, @attributes_to_replace)
19
20
  end
20
21
  end
21
22
 
@@ -65,4 +66,4 @@ module Mongoid
65
66
  end
66
67
  end
67
68
  end
68
- end
69
+ end
@@ -1,8 +1,11 @@
1
+ require File.expand_path(File.join('helpers'), File.dirname(__FILE__))
1
2
  require File.expand_path(File.join('parsers', 'filter_parser'), File.dirname(__FILE__))
2
3
  require File.expand_path(File.join('parsers', 'filters_parser'), File.dirname(__FILE__))
3
4
 
4
5
  module Mongoid
5
6
  module QueryStringInterface
7
+ include Mongoid::QueryStringInterface::Helpers
8
+
6
9
  NORMAL_CONDITIONAL_OPERATORS = [:exists, :gte, :gt, :lte, :lt, :ne, :size, :near, :within]
7
10
  ARRAY_CONDITIONAL_OPERATORS = [:all, :in, :nin]
8
11
  CONDITIONAL_OPERATORS = ARRAY_CONDITIONAL_OPERATORS + NORMAL_CONDITIONAL_OPERATORS
@@ -17,8 +20,9 @@ module Mongoid
17
20
  ORDER_BY_PARAMETER = :order_by
18
21
  PAGINATION_PARAMTERS = [:per_page, :page]
19
22
  FRAMEWORK_PARAMETERS = [:controller, :action, :format]
20
- RESERVED_PARAMETERS = FRAMEWORK_PARAMETERS + PAGINATION_PARAMTERS + [ORDER_BY_PARAMETER]
21
-
23
+ CONTROL_PARAMETERS = [:disable_default_filters]
24
+ RESERVED_PARAMETERS = FRAMEWORK_PARAMETERS + PAGINATION_PARAMTERS + [ORDER_BY_PARAMETER] + CONTROL_PARAMETERS
25
+
22
26
  def filter_by(params={})
23
27
  params = hash_with_indifferent_access(params)
24
28
  filter_only_and_order_by(params).paginate(pagination_options(params))
@@ -55,10 +59,10 @@ module Mongoid
55
59
 
56
60
  def paginated_collection_with_filter_by(params={})
57
61
  params = hash_with_indifferent_access(params)
58
-
62
+
59
63
  pagination = pagination_options(params)
60
64
  pager = WillPaginate::Collection.new pagination[:page], pagination[:per_page], where(filtering_options(params)).count
61
-
65
+
62
66
  PAGER_ATTRIBUTES.inject({}) do |result, attr|
63
67
  result[attr] = pager.send(attr)
64
68
  result
@@ -68,31 +72,30 @@ module Mongoid
68
72
  def default_filtering_options; {}; end
69
73
  def default_sorting_options; []; end
70
74
  def default_pagination_options; { :per_page => 12, :page => 1 }; end
75
+ def sorting_attributes_to_replace; {} end
76
+ def filtering_attributes_to_replace; {} end
71
77
 
72
78
  protected
73
79
  def pagination_options(options)
74
80
  hash_with_indifferent_access(default_pagination_options).merge(options)
75
81
  end
76
-
82
+
77
83
  def filtering_options(options)
78
84
  Mongoid::QueryStringInterface::Parsers::FiltersParser.new(
79
85
  only_filtering(options),
80
- default_filtering_options
86
+ options.has_key?(:disable_default_filters) ? {} : default_filtering_options,
87
+ filtering_attributes_to_replace
81
88
  ).parse
82
89
  end
83
-
90
+
84
91
  def sorting_options(options)
85
92
  parse_order_by(options) || default_sorting_options
86
93
  end
87
-
88
- def hash_with_indifferent_access(params)
89
- params.is_a?(HashWithIndifferentAccess) ? params : params.with_indifferent_access
90
- end
91
-
94
+
92
95
  def only_filtering(options)
93
96
  options.except(*RESERVED_PARAMETERS)
94
97
  end
95
-
98
+
96
99
  def parse_order_by(options)
97
100
  if options.has_key?('order_by')
98
101
  options['order_by'].split('|').map do |field|
@@ -103,9 +106,9 @@ module Mongoid
103
106
 
104
107
  def sorting_operator_for(field)
105
108
  if match = field.match(/(.*)\.(#{SORTING_OPERATORS.join('|')})/)
106
- match[1].to_sym.send(match[2])
109
+ replace_attribute(match[1], sorting_attributes_to_replace).to_sym.send(match[2])
107
110
  else
108
- field.to_sym.asc
111
+ replace_attribute(field, sorting_attributes_to_replace).to_sym.asc
109
112
  end
110
113
  end
111
114
  end
data/lib/version.rb CHANGED
@@ -1,6 +1,6 @@
1
1
  # encoding: utf-8
2
2
  module Mongoid #:nodoc
3
3
  module QueryStringInterface #:nodoc
4
- VERSION = "0.3.0"
4
+ VERSION = "0.4.0"
5
5
  end
6
- end
6
+ end
metadata CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
4
4
  prerelease: false
5
5
  segments:
6
6
  - 0
7
- - 3
7
+ - 4
8
8
  - 0
9
- version: 0.3.0
9
+ version: 0.4.0
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-03-29 00:00:00 -03:00
17
+ date: 2011-06-04 00:00:00 -03:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency
@@ -88,16 +88,17 @@ extensions: []
88
88
  extra_rdoc_files: []
89
89
 
90
90
  files:
91
+ - lib/version.rb
92
+ - lib/mongoid_query_string_interface.rb
91
93
  - lib/mongoid/parsers/array_parser.rb
92
- - lib/mongoid/parsers/boolean_and_nil_parser.rb
93
94
  - lib/mongoid/parsers/date_time_parser.rb
94
- - lib/mongoid/parsers/filter_parser.rb
95
95
  - lib/mongoid/parsers/filters_parser.rb
96
96
  - lib/mongoid/parsers/number_parser.rb
97
+ - lib/mongoid/parsers/filter_parser.rb
98
+ - lib/mongoid/parsers/boolean_and_nil_parser.rb
97
99
  - lib/mongoid/parsers/regex_parser.rb
98
100
  - lib/mongoid/query_string_interface.rb
99
- - lib/mongoid_query_string_interface.rb
100
- - lib/version.rb
101
+ - lib/mongoid/helpers.rb
101
102
  - MIT_LICENSE
102
103
  - README.rdoc
103
104
  - Gemfile