-
-
Save FrankFang/0bd09cb027c139d3e29d6b4555c6ecaa to your computer and use it in GitHub Desktop.
Custom ransacker predicates to query date range
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
<%= search_form_for @q, url: production_status_changes_path, class: 'form-inline' do |f| %> | |
<%= f.label 'Create At' %> | |
<%= f.search_field :created_at_beginning_of_day, class: 'form-control input-sm', 'datepicker' => true %> | |
<%= f.search_field :created_at_end_of_day_lt, class: 'form-control input-sm', 'datepicker' => true %> | |
<% 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
# config/initializers/ransack.rb | |
# | |
# Custom ransack predicate to simplify query for date range | |
# | |
# See lib/ransack.rb in ransack gem | |
# See wiki https://github.com/activerecord-hackery/ransack/wiki/Custom-Predicates | |
Ransack.configure do |config| | |
config.add_predicate 'beginning_of_day_gteq', | |
arel_predicate: 'gteq', | |
formatter: proc { |v| v.to_date.beginning_of_day }, | |
validator: proc { |v| v.present? }, | |
type: :string | |
config.add_predicate 'end_of_day_lt', | |
arel_predicate: 'lt', | |
formatter: proc { |v| v.to_date.end_of_day }, | |
validator: proc { |v| v.present? }, | |
type: :string | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment