Created
October 24, 2013 22:23
-
-
Save joshmcarthur/7146179 to your computer and use it in GitHub Desktop.
This Rails helper allows simple parsing of params that have originated from ActionView's `date_select` helper method into a `Date` or `DateTime` object in a consistent and clean way.
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
module DateHelper | |
def date_from_date_select_params(date_params, key) | |
string_from_date_select_params(date_params, key).to_date | |
end | |
def date_time_from_date_time_select_params(date_params, key) | |
string_from_date_select_params(date_params, key).to_datetime | |
end | |
private | |
def string_from_date_select_params(params, key) | |
date_parts = params.select { |k,v| k.to_s =~ /\A#{key}\([1-6]{1}i\)/ }.values | |
date_parts[0..2].join('-') + ' ' + date_parts[3..-1].join(':') | |
end | |
end |
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
require "spec_helper" | |
describe DateHelper do | |
describe "#date_from_date_select_params" do | |
let(:params) { {"test(1i)" => "2013", "test(2i)" => "10", "test(3i)" => "15"} } | |
subject { helper.date_from_date_select_params(params, :test) } | |
it { subject.should be_a(Date) } | |
it { subject.year.should eq 2013 } | |
it { subject.month.should eq 10 } | |
it { subject.day.should eq 15 } | |
context "extra params provided" do | |
let(:extra_params) { params.merge({"test(4i)" => "20"}) } | |
subject { helper.date_from_date_select_params(extra_params, :test) } | |
it { subject.should be_a(Date) } | |
it { subject.strftime("%d/%m/%Y").should eq "15/10/2013" } | |
end | |
context "params not from date select provided" do | |
let(:extra_params) { params.merge({"test(something)" => "test"}) } | |
subject { helper.date_from_date_select_params(extra_params, :test) } | |
it { subject.should be_a(Date) } | |
it { subject.to_s.should_not include "test" } | |
end | |
end | |
describe "#date_time_from_date_time_select_params" do | |
let(:params) { {"test(1i)" => "2013", "test(2i)" => "10", "test(3i)" => "15", "test(4i)" => "23", "test(5i)" => "59", "test(6i)" => "59" } } | |
subject { helper.date_time_from_date_time_select_params(params, :test) } | |
it { subject.should be_a(DateTime) } | |
it { subject.year.should eq 2013 } | |
it { subject.month.should eq 10 } | |
it { subject.day.should eq 15 } | |
it { subject.hour.should eq 23 } | |
it { subject.second.should eq 59 } | |
it { subject.minute.should eq 59 } | |
context "extra params provided" do | |
let(:extra_params) { params.merge({"test(7i)" => "20"}) } | |
subject { helper.date_time_from_date_time_select_params(extra_params, :test) } | |
it { subject.should be_a(DateTime) } | |
it { subject.strftime("%d/%m/%Y %H:%M:%S").should eq "15/10/2013 23:59:59" } | |
end | |
context "params not from date select provided" do | |
let(:extra_params) { params.merge({"test(something)" => "test"}) } | |
subject { helper.date_time_from_date_time_select_params(extra_params, :test) } | |
it { subject.should be_a(DateTime) } | |
it { subject.to_s.should_not include "test" } | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment