Search code examples
ruby-on-railsactivescaffold

Active Scaffold: field search on column with timestamp


I enabled field search functionality in my controller according the FieldSearch-API-Documentation.

I have enabled field search on two columns as below

class FooController < ApplicationController
   active_scaffold 'foo' do |config|
     # allowed actions
     config.actions = [:create, :update, :delete, :field_search, :list, :nested, :show]

     # columns
     config.columns = [:date, :created_at]

     # searchable columns
     config.field_search.columns = :date, :created_at
     config.list.always_show_search = true

     # list columns
     config.list.columns = [:date, :created_at]

     [...]

   end
end

The column date is of type date and the column created_at is of type timestamp within the postgresql database.

With the configuration parameters above I get successfully a search form on the view rendered by active scaffold. On the search form there is an dropdown with operators (e.g. BETWEEN) and two input controls for selecting the range.

The Problem now is, that searching by date works fine and searching by created_at doesn't work.

After an exhaustive research I found a hint which describes that active scaffold uses by default the datetime format which is configured under ./config/locales/en.yml and shows in my case like this.

en:
   time:
       formats:
           default: "%Y%m%d%H%M%S"

Obviously it does matter how the input format is. The search input for searching by created_at is e.g: from 20180101000000 to 20181231000000.

On the Column-API-Documentation I found the following:

options[:format] can be set for: date and time columns, and it will be used as the format argument of I18n.localize to format them.

I not understand what kind of value I have to define in the option parameter within the controller?

config.columns[:created_at].options[:format] = ???

Solution

  • Just found the answer while writing the question :-)

    If I define in ./config/locales/en.yml a new entry e.g. timestamp as below

    en:
        time:
            formats:
                 timestamp: "%Y%m%d%H%M"
    

    and do in my controller the following

    config.columns[:created_at].options[:format] = "timestamp"
    

    then I am successfully able to search by created_at with the input 201801010000 and 201812310000.