madeintandem/hstore_accessor

Support for date_select

Closed this issue · 6 comments

date_select pass the params like
"birth_date(1i)"=>"1980", "birth_date(2i)"=>"1", "birth_date(3i)"=>"1"

in model

hstore_accessor :profile,
                  birth_date: :time,

how can i save the birth_date?

This seems like something that would be the responsibility of the controller, but maybe I just need more context. Which date_select method are you referring to and what is the precise use case?

@foxzool, is this still an issue for you?

@crismali I think this is still an improvement that could be made. We used it in another project. It required a private method in the controller to clean up the input. It works but would be nicer if hstore saw the *i inputs and converted them using Date.civil similar to rails.

  # change value of DateFormAnswers to a Date string from the date_select format for hstore Date
  def fix_date_answer_values
    if params["immigration_section"] && params["immigration_section"]["form_answers_attributes"]
      params["immigration_section"]["form_answers_attributes"].each do |_, form_answer_attrs|
        if %w(DateFormAnswer DateToPresentFormAnswer).include?(form_answer_attrs["type"]) && form_answer_attrs["value(1i)"].present?
          form_answer_attrs["value"] = Date.civil(form_answer_attrs.delete("value(1i)").to_i, form_answer_attrs.delete("value(2i)").to_i, form_answer_attrs.delete("value(3i)").to_i).to_s
        end
      end
    end
  end

Okay, I see. This is definitely a good idea, but I want to avoid slamming this logic into the setters.

I'm looking into a way to essentially get ActiveRecord to do this work for us. Right now it's looking like it will require overriding type_for_attribute. Plus if we do it that way we won't have to worry implementing more logic to handle Times and DateTimes

Also, this will probably only be a feature in the 1.0.0 release since ActiveRecord 4.2 has some surprising internal differences from 4.1

this should handle take care of this: ef7e666

@foxzool new version of the gem with this version is available now