Module: Sinatra::DateForms::Helpers
- Defined in:
- lib/sinatra/support/dateforms.rb
Instance Method Summary collapse
-
#day_choices(max = 31) ⇒ Array
Returns an array of date pairs.
-
#month_choices(month_names = settings.default_month_names) ⇒ Array
Returns an array of pairs.
-
#year_choices(loffset = settings.default_year_loffset, uoffset = settings.default_year_uoffset) ⇒ Array
Returns an array of pairs.
Instance Method Details
#day_choices(max = 31) ⇒ Array
Returns an array of date pairs. Best used with HtmlHelpers#select_options.
An example output looks like:
- [1, 1]
- [2, 2]
- ...
- [31, 31]
79 80 81 82 |
# File 'lib/sinatra/support/dateforms.rb', line 79 def day_choices(max=31) days = (1..max).to_a days.zip(days) end |
#month_choices(month_names = settings.default_month_names) ⇒ Array
Returns an array of pairs.
You may pass in @Date::ABBR_MONTHNAMES@ if you want the shortened month names.
99 100 101 102 103 |
# File 'lib/sinatra/support/dateforms.rb', line 99 def month_choices(month_names = settings.default_month_names) month_names.map. with_index { |month, idx| [month, idx] }. tap { |arr| arr.shift } end |
#year_choices(loffset = settings.default_year_loffset, uoffset = settings.default_year_uoffset) ⇒ Array
Returns an array of pairs.
137 138 139 140 141 142 143 |
# File 'lib/sinatra/support/dateforms.rb', line 137 def year_choices(loffset = settings.default_year_loffset, uoffset = settings.default_year_uoffset) start = loffset + Date.today.year finish = uoffset + Date.today.year enum = start < finish ? start.upto(finish) : start.downto(finish) enum.map { |e| [e, e] } end |