-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
aa4dbcd
commit acc1a6e
Showing
7 changed files
with
105 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,22 @@ | ||
module CafeCar::Controller::Filtering | ||
extend ActiveSupport::Concern | ||
|
||
class_methods do | ||
included do | ||
helper_method :dot_params | ||
end | ||
|
||
private | ||
|
||
def filtered(scope) | ||
scope.query(dot_params[""]) | ||
end | ||
|
||
def parsed_params | ||
@parsed_params ||= params.reject {|k, *| k.include?('.') }. | ||
merge(dot_params) | ||
end | ||
|
||
def dot_params | ||
@dot_params ||= CafeCar::ParamParser.new(request.params).parsed | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
class CafeCar::ParamParser | ||
def initialize(params) | ||
@params = params | ||
end | ||
|
||
def params = parsed | ||
|
||
def parsed | ||
@parsed ||= parse(@params) | ||
end | ||
|
||
def parse(params) | ||
params.then { _1.try(:to_unsafe_h) || _1 } | ||
.select {|k, *| k.include?('.') } | ||
.map {|k, v| k.split('.').reverse.reduce(value(v)) { {_2 => _1} } } | ||
.reduce({}) {|prms, p| prms.deep_merge(p, &method(:merge)) } | ||
.with_indifferent_access | ||
end | ||
|
||
def merge(_key, a, b) | ||
if a.is_a?(Array) || b.is_a?(Array) | ||
[*Array.wrap(a), *Array.wrap(b)] | ||
else | ||
b | ||
end | ||
end | ||
|
||
def value(v) | ||
case v | ||
when '""', "''" | ||
'' | ||
when 'nil', '' | ||
nil | ||
when /[{}\[\]]/ | ||
JSON.parse(v) | ||
when /,/ | ||
value v.split(',') | ||
when /\.\./ | ||
a, b = v.split('..').map(&:presence).map { value(_1) } | ||
a..b | ||
when /\A(\d*\.)?\d+\z/ | ||
v.try(:to_f) || v | ||
when /^\$(\w+)\.(\w+)$/ | ||
$1.constantize.arel_table[$2] | ||
when Array | ||
v.map { value(_1) } | ||
when Hash | ||
v.reject {|k, *| k.include?('.') }. | ||
transform_values { value(_1) }. | ||
merge(parse(v)). | ||
tap {|h| h.merge!(h.delete('')) if h.key?('') } | ||
else | ||
v | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters