Skip to content

Commit

Permalink
better range parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffpeterson committed Nov 21, 2024
1 parent 6b9ef9f commit ab2eccb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion app/views/application/_debug.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
= ui.card title: "Debug" do |card|
= card.section do
= ui.field label: "Params" do
= ui.code request.params.pretty_inspect
= ui.code parsed_params.to_unsafe_h.pretty_inspect

= ui.field label: "Query" do
= ui.code scope.to_sql
Expand Down
2 changes: 1 addition & 1 deletion lib/cafe_car/controller/filtering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module CafeCar::Controller::Filtering
extend ActiveSupport::Concern

included do
helper_method :dot_params
helper_method :dot_params, :parsed_params
end

private
Expand Down
5 changes: 2 additions & 3 deletions lib/cafe_car/param_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ def value(v)
JSON.parse(v)
when /,/
value v.split(',')
when /\.\./
a, b = v.split('..').map(&:presence).map { value(_1) }
a..b
when /^(.*?)\.\.(\.?)(.*)$/
Range.new(*[$1, $3].map(&:presence).map { value(_1) }, $2.present?)
when /^\$(\w+)\.(\w+)$/
$1.constantize.arel_table[$2]
when Array
Expand Down
16 changes: 12 additions & 4 deletions lib/cafe_car/query_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,18 @@ def parse_time(value)
end

def parse_value(key, value)
case column(key)&.type
when :datetime then parse_time(value) || value
when :integer then value.to_i
when :float then value.to_f
case value
when Range
Range.new(parse_value(key, value.begin), parse_value(key, value.end), value.exclude_end?)
when Array
value.map { parse_value(key, _1) }
when String
case column(key)&.type
when :datetime then parse_time(value) || value
when :integer then value.to_i
when :float then value.to_f
else value
end
else value
end
end
Expand Down

0 comments on commit ab2eccb

Please sign in to comment.