Class: Releaf::Builders::Utilities::DateFields
- Inherits:
-
Object
- Object
- Releaf::Builders::Utilities::DateFields
- Defined in:
- app/builders/releaf/builders/utilities/date_fields.rb
Constant Summary collapse
- RUBY_TO_JQUERY_DATE_FORMAT_MAP =
{ '-d' => 'd', '-j' => 'o', '-m' => 'm', '3N' => 'l', ':z' => 'Z', 'B' => 'MM', 'D' => 'mm/dd/y', 'F' => 'yy-mm-dd', 'H' => 'HH', 'I' => 'h', 'L' => 'l', 'M' => 'mm', 'P' => 'tt', 'R' => 'HH:mm', 'S' => 'ss', 'T' => 'HH:mm:ss', 'X' => 'HH:mm:ss', 'Y' => 'yy', '_m' => 'm', 'b' => 'M', 'c' => 'M d HH:mm:ss yy', # not exact translation 'd' => 'dd', 'e' => 'd', 'h' => 'M', 'j' => 'oo', 'k' => 'H', 'l' => 'hh', 'm' => 'mm', 'p' => 'TT', 'r' => 'h:mm:ss TT', 's' => '@', 'x' => 'mm/dd/y', 'y' => 'y' }
Class Method Summary collapse
- .date_format_for_jquery ⇒ Object
- .date_or_time_default_format(type) ⇒ Object
- .format_date_or_time_value(value, type) ⇒ Object
-
.jquery_date_format(ruby_date_format) ⇒ Object
converts date format used by ruby to dateformat used by jquery (when possible).
- .normalize_date_or_time_value(value, type) ⇒ Object
- .ruby_date_format_regexp ⇒ Object
- .time_format_for_jquery ⇒ Object
Class Method Details
.date_format_for_jquery ⇒ Object
84 85 86 87 |
# File 'app/builders/releaf/builders/utilities/date_fields.rb', line 84 def self.date_format_for_jquery format = date_or_time_default_format(:date) jquery_date_format(I18n.t("default", scope: "date.formats", default: format)) end |
.date_or_time_default_format(type) ⇒ Object
89 90 91 92 93 94 95 96 97 98 |
# File 'app/builders/releaf/builders/utilities/date_fields.rb', line 89 def self.date_or_time_default_format(type) case type when :date "%Y-%m-%d" when :datetime "%Y-%m-%d %H:%M" when :time "%H:%M" end end |
.format_date_or_time_value(value, type) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'app/builders/releaf/builders/utilities/date_fields.rb', line 66 def self.format_date_or_time_value(value, type) return value if value.nil? default_format = date_or_time_default_format(type) value = normalize_date_or_time_value(value, type) if type == :time value.strftime(default_format) else I18n.l(value, format: default_format) end end |
.jquery_date_format(ruby_date_format) ⇒ Object
converts date format used by ruby to dateformat used by jquery (when possible)
references:
http://www.ruby-doc.org/core-2.0/Time.html#strftime-method
http://api.jqueryui.com/datepicker/#utility-formatDate
http://trentrichardson.com/examples/timepicker/#tp-formatting
45 46 47 48 49 |
# File 'app/builders/releaf/builders/utilities/date_fields.rb', line 45 def self.jquery_date_format(ruby_date_format) ruby_date_format.gsub(ruby_date_format_regexp) do |match| RUBY_TO_JQUERY_DATE_FORMAT_MAP[match[1..-1]] end end |
.normalize_date_or_time_value(value, type) ⇒ Object
55 56 57 58 59 60 61 62 63 64 |
# File 'app/builders/releaf/builders/utilities/date_fields.rb', line 55 def self.normalize_date_or_time_value(value, type) case type when :date value.to_date when :datetime value.to_datetime when :time value.to_time end end |
.ruby_date_format_regexp ⇒ Object
51 52 53 |
# File 'app/builders/releaf/builders/utilities/date_fields.rb', line 51 def self.ruby_date_format_regexp @@jquery_date_replacement_regexp ||= Regexp.new("%(#{RUBY_TO_JQUERY_DATE_FORMAT_MAP.keys.join('|')})") end |
.time_format_for_jquery ⇒ Object
79 80 81 82 |
# File 'app/builders/releaf/builders/utilities/date_fields.rb', line 79 def self.time_format_for_jquery format = date_or_time_default_format(:time) jquery_date_format(format) end |