Skip to content

Instantly share code, notes, and snippets.

@fukajun
Last active December 14, 2015 20:09
Show Gist options
  • Save fukajun/5142075 to your computer and use it in GitHub Desktop.
Save fukajun/5142075 to your computer and use it in GitHub Desktop.
和暦の日付選択
 module ApplicationHelper
   
   def date_select_ja(object_name, method, options = {}, html_options = {})
     t = date_select(object_name, method, options, html_options)
     t.gsub(/>([12]\d\d\d)</) do |m|
       year = m.match(/>(\d\d\d\d)</)[1].to_i
       wareki = case year
       when 0..1911 # 明治
         "明治#{year - 1867}"
       when 1912 # 大正
         "明治45/大正元年"
       when 1913..1925 # 大正
         "大正#{year - 1911}"
       when 1926 # 昭和                                                                   
         "大正15/昭和元年"
       when 1927..1988 # 昭和
         "昭和#{year - 1925}"
       when 1989 # 昭和
         "昭和64/平成元年"
       else # 平成
         "平成#{year - 1988}"
       end
       ">#{year} (#{wareki})<"
     end.html_safe
   end
end
   describe "#date_select_ja" do
 
     subject do
       date_select_ja(:user, :birthday, {
         :use_month_numbers => true,
         :start_year => start_year,
         :end_year => end_year,
         :default => Date.new(2013, 3, 12),
         :discard_day => true,
         :discard_month => true})
     end
 
     context "明治 - 大正" do
       let (:start_year) { 1911 }
       let (:end_year)   { 1913 }
       it { subject.should be_a(ActiveSupport::SafeBuffer) }
       it do
         expected_result = <<-EOS
 <select id="user_birthday_1i" name="user[birthday(1i)]">
 <option value="1911">1911 (明治44)</option>
 <option value="1912">1912 (明治45/大正元年)</option>
 <option value="1913">1913 (大正2)</option>
 </select>
 <input id="user_birthday_2i" name="user[birthday(2i)]" type="hidden" value="3" />
 <input id="user_birthday_3i" name="user[birthday(3i)]" type="hidden" value="12" />
         EOS
         should eq(expected_result)
       end
     end
 
     context "大正 - 昭和" do
       let (:start_year) { 1925 }
       let (:end_year)   { 1927 }
       it do
         expected_result = <<-EOS
 <select id="user_birthday_1i" name="user[birthday(1i)]">
 <option value="1925">1925 (大正14)</option>
 <option value="1926">1926 (大正15/昭和元年)</option>
 <option value="1927">1927 (昭和2)</option>
 </select>
 <input id="user_birthday_2i" name="user[birthday(2i)]" type="hidden" value="3" />
 <input id="user_birthday_3i" name="user[birthday(3i)]" type="hidden" value="12" />
         EOS
         should eq(expected_result)
       end
     end
 
     context "昭和 - 平成" do
       let (:start_year) { 1988 }
       let (:end_year)   { 1990 }
       it do
         expected_result = <<-EOS
 <select id="user_birthday_1i" name="user[birthday(1i)]">
 <option value="1988">1988 (昭和63)</option>
 <option value="1989">1989 (昭和64/平成元年)</option>
 <option value="1990">1990 (平成2)</option>
 </select>
 <input id="user_birthday_2i" name="user[birthday(2i)]" type="hidden" value="3" />
 <input id="user_birthday_3i" name="user[birthday(3i)]" type="hidden" value="12" />
         EOS
         should eq(expected_result)
       end
     end
   end
 end
                                                                                         
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment