mirror of
https://github.com/rails/rails.git
synced 2022-11-09 12:12:34 -05:00
Refactor date related helpers
This commit is contained in:
parent
d7de7a79c5
commit
a9a9e8a3cb
6 changed files with 26 additions and 50 deletions
|
@ -1,13 +1,12 @@
|
|||
module ActionView
|
||||
module Helpers
|
||||
module Tags
|
||||
class DateField < TextField #:nodoc:
|
||||
def render
|
||||
options = @options.stringify_keys
|
||||
options["value"] = @options.fetch("value") { value(object).try(:to_date) }
|
||||
@options = options
|
||||
super
|
||||
end
|
||||
class DateField < DatetimeField #:nodoc:
|
||||
private
|
||||
|
||||
def format_date(value)
|
||||
value.try(:strftime, "%Y-%m-%d")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -4,16 +4,16 @@ module ActionView
|
|||
class DatetimeField < TextField #:nodoc:
|
||||
def render
|
||||
options = @options.stringify_keys
|
||||
options["value"] = @options.fetch("value") { format_global_date_time_string(value(object)) }
|
||||
options["min"] = format_global_date_time_string(options["min"])
|
||||
options["max"] = format_global_date_time_string(options["max"])
|
||||
options["value"] = @options.fetch("value") { format_date(value(object)) }
|
||||
options["min"] = format_date(options["min"])
|
||||
options["max"] = format_date(options["max"])
|
||||
@options = options
|
||||
super
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def format_global_date_time_string(value)
|
||||
def format_date(value)
|
||||
value.try(:strftime, "%Y-%m-%dT%T.%L%z")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,20 +1,16 @@
|
|||
module ActionView
|
||||
module Helpers
|
||||
module Tags
|
||||
class DatetimeLocalField < TextField #:nodoc:
|
||||
def render
|
||||
options = @options.stringify_keys
|
||||
options["type"] = "datetime-local"
|
||||
options["value"] = @options.fetch("value") { format_local_date_time_string(value(object)) }
|
||||
options["min"] = format_local_date_time_string(options["min"])
|
||||
options["max"] = format_local_date_time_string(options["max"])
|
||||
@options = options
|
||||
super
|
||||
class DatetimeLocalField < DatetimeField #:nodoc:
|
||||
class << self
|
||||
def field_type
|
||||
@field_type ||= "datetime-local"
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def format_local_date_time_string(value)
|
||||
def format_date(value)
|
||||
value.try(:strftime, "%Y-%m-%dT%T")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,19 +1,10 @@
|
|||
module ActionView
|
||||
module Helpers
|
||||
module Tags
|
||||
class MonthField < TextField #:nodoc:
|
||||
def render
|
||||
options = @options.stringify_keys
|
||||
options["value"] = @options.fetch("value") { format_month_string(value(object)) }
|
||||
options["min"] = format_month_string(options["min"])
|
||||
options["max"] = format_month_string(options["max"])
|
||||
@options = options
|
||||
super
|
||||
end
|
||||
|
||||
class MonthField < DatetimeField #:nodoc:
|
||||
private
|
||||
|
||||
def format_month_string(value)
|
||||
def format_date(value)
|
||||
value.try(:strftime, "%Y-%m")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,13 +1,12 @@
|
|||
module ActionView
|
||||
module Helpers
|
||||
module Tags
|
||||
class TimeField < TextField #:nodoc:
|
||||
def render
|
||||
options = @options.stringify_keys
|
||||
options["value"] = @options.fetch("value") { value(object).try(:strftime, "%T.%L") }
|
||||
@options = options
|
||||
super
|
||||
end
|
||||
class TimeField < DatetimeField #:nodoc:
|
||||
private
|
||||
|
||||
def format_date(value)
|
||||
value.try(:strftime, "%T.%L")
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,19 +1,10 @@
|
|||
module ActionView
|
||||
module Helpers
|
||||
module Tags
|
||||
class WeekField < TextField #:nodoc:
|
||||
def render
|
||||
options = @options.stringify_keys
|
||||
options["value"] = @options.fetch("value") { format_week_string(value(object)) }
|
||||
options["min"] = format_week_string(options["min"])
|
||||
options["max"] = format_week_string(options["max"])
|
||||
@options = options
|
||||
super
|
||||
end
|
||||
|
||||
class WeekField < DatetimeField #:nodoc:
|
||||
private
|
||||
|
||||
def format_week_string(value)
|
||||
def format_date(value)
|
||||
value.try(:strftime, "%Y-W%W")
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue