From 2a876991da1b6fb965b0f4a3127c39b3e43ef521 Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 17 Dec 2017 17:31:07 -0600 Subject: [PATCH] Accept `search[created_at]` and `search[updated_at]` params in all controllers. --- app/models/application_record.rb | 4 +++- app/models/tag.rb | 2 +- test/unit/application_record.rb | 5 +++++ 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/app/models/application_record.rb b/app/models/application_record.rb index e51f32e01..31ebfcbe2 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -9,7 +9,7 @@ class ApplicationRecord < ActiveRecord::Base column = column_for_attribute(attribute) qualified_column = "#{table_name}.#{column.name}" - parsed_range = Tag.parse_helper(range, :integer) + parsed_range = Tag.parse_helper(range, column.type) PostQueryBuilder.new(nil).add_range_relation(parsed_range, qualified_column, self) end @@ -19,6 +19,8 @@ class ApplicationRecord < ActiveRecord::Base q = all q = q.attribute_matches(:id, params[:id]) + q = q.attribute_matches(:created_at, params[:created_at]) if attribute_names.include?("created_at") + q = q.attribute_matches(:updated_at, params[:updated_at]) if attribute_names.include?("updated_at") q end end diff --git a/app/models/tag.rb b/app/models/tag.rb index d9e36a686..e49014db1 100644 --- a/app/models/tag.rb +++ b/app/models/tag.rb @@ -275,7 +275,7 @@ class Tag < ApplicationRecord when :float object.to_f - when :date + when :date, :datetime begin Time.zone.parse(object) rescue Exception diff --git a/test/unit/application_record.rb b/test/unit/application_record.rb index 350c4e30c..bbcb7dded 100644 --- a/test/unit/application_record.rb +++ b/test/unit/application_record.rb @@ -15,5 +15,10 @@ class ApplicationRecordTest < ActiveSupport::TestCase assert_equal(@tags.reverse, Tag.search(id: "#{@tags[0].id}..#{@tags[2].id}")) assert_equal(@tags.reverse, Tag.search(id: @tags.map(&:id).join(","))) end + + should "support the created_at and updated_at params" do + assert_equal(@tags.reverse, Tag.search(created_at: ">=#{@tags.first.created_at}")) + assert_equal(@tags.reverse, Tag.search(updated_at: ">=#{@tags.first.updated_at}")) + end end end