Created
April 16, 2019 05:01
-
-
Save kt103099/1b43c46c3ef6ac674111e72a31cbae3c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# app > controllers > concerns > convert_date_select_params.rb | |
module ConvertDateSelectParams | |
extend ActiveSupport::Concern | |
included do | |
before_action :convert_date | |
end | |
protected | |
def convert_date | |
self.params = ActionController::Parameters.new(build_date(params.to_unsafe_h)) | |
end | |
def build_date(params) | |
return params.map{|e| build_date(e)} if params.is_a? Array | |
return params unless params.is_a? Hash | |
params.reduce({}) do |hash, (key, value)| | |
if result = (/(.*)\(\di\)\z/).match(key) | |
params_name = result[1] | |
date_params = (1..3).map do |index| | |
params.delete("#{params_name}(#{index}i)").to_i | |
end | |
hash[params_name] = Date.civil(*date_params) | |
else | |
hash[key] = build_date(value) | |
end | |
hash | |
end | |
end | |
end | |
# include it to your controller or application_controller.rb: | |
class ApplicationController < ActionController::Base | |
include ConvertDateSelectParams | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment