Skip to content

Instantly share code, notes, and snippets.

@linyows
Created April 4, 2012 05:26
Show Gist options
  • Save linyows/2298002 to your computer and use it in GitHub Desktop.
Save linyows/2298002 to your computer and use it in GitHub Desktop.
module ActionView
module Helpers
class DateTimeSelector
# Override
# クレジットカード用に年を2桁で表示する "use_short_year" オプションを追加した
def select_year
if !@datetime || @datetime == 0
val = '1'
middle_year = Date.today.year
else
val = middle_year = year
end
if @options[:use_hidden] || @options[:discard_year]
build_hidden(:year, val)
else
options = {}
options[:start] = @options[:start_year] || middle_year - 5
options[:end] = @options[:end_year] || middle_year + 5
options[:step] = options[:start] < options[:end] ? 1 : -1
options[:leading_zeros] = false
options[:max_years_allowed] = @options[:max_years_allowed] || 1000
if @options[:use_short_year]
options[:start] = options[:start].to_s.gsub(/^\d{2}(\d{2})$/, '\1').to_i
options[:end] = options[:end].to_s.gsub(/^\d{2}(\d{2})$/, '\1').to_i
end
if (options[:end] - options[:start]).abs > options[:max_years_allowed]
raise ArgumentError, "There're too many years options to be built. Are you sure you haven't mistyped something? You can provide the :max_years_allowed parameter"
end
build_options_and_select(:year, val, options)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment