Advertisement
Guest User

Untitled

a guest
Jul 20th, 2017
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.93 KB | None | 0 0
  1. <%= form.text_field :check_in_date %>
  2.  
  3. ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
  4. :default => '%m/%d/%Y'
  5. )
  6.  
  7. <%= f.text_field :some_date, :value => @model_instance.some_date.strftime("%d-%m-%Y") %>
  8.  
  9. # Default format for displaying dates and times
  10. Date::DATE_FORMATS[:default] = "%m/%d/%Y"
  11. Time::DATE_FORMATS[:default] = "%m/%d/%Y"
  12.  
  13. MyModel < ActiveRecord::Base
  14. # ...
  15. self.columns.each do |column|
  16. if column.type == :date
  17. define_method "#{column.name}_before_type_cast" do
  18. self.send(column.name).to_s
  19. end
  20. end
  21. end
  22. end
  23.  
  24. def date_mdY(date)
  25. if date.nil?
  26. ""
  27. else
  28. date.strftime("%m-%d-%Y")
  29. end
  30. end
  31.  
  32. <%= f.text_field :some_date, :value => date_mdY(@model_instance.some_date) %>
  33.  
  34. ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS[:default] = '%m/%d/%Y'
  35.  
  36. <%= form.text_field :check_in_date, value: model.check_in_date.to_s %>
  37.  
  38. <%= f.text_field :date3, value: ( l @model.date3 if @model.date3? ) %>
  39.  
  40. en:
  41. date:
  42. formats:
  43. default: "%d/%m/%Y"
  44.  
  45. ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
  46. :default => '%m-%d-%Y' )
  47.  
  48. def start_date
  49. super.strftime "%Y/%m/%d %H:%M"
  50. end
  51.  
  52. <%= f.text_field :start_date, class: 'form-control' %>
  53.  
  54. after_initialize :init
  55.  
  56. private
  57. def init
  58. unless time.nil?
  59. @attributes['time'] = I18n.l(time, format: :short)
  60. end
  61. end
  62.  
  63. Date::DATE_FORMATS[:default] = "%m/%d/%Y"
  64.  
  65. class EasyDate < ActiveRecord::Type::Date
  66. def cast_value(value)
  67. default = super
  68. parsed = Chronic.parse(value)&.to_date if value.is_a?(String)
  69. parsed || default
  70. end
  71. end
  72.  
  73. class Pony < ApplicationRecord
  74. attribute :birth_date, EasyDate.new
  75. end
  76.  
  77. { column_name: instance_of_a_type_object, ... }
  78.  
  79. #<ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Money:0x007ff974d62688
  80.  
  81. date_attributes = attribute_types.select { |_, type| ActiveRecord::Type::Date === type }
  82.  
  83. date_attributes.each do |attr_name, _type|
  84. attribute attr_name, EasyDate.new
  85. end
  86.  
  87. def inherited(klass)
  88. super
  89. klass.attribute_types.select { |_, type| ActiveRecord::Type::Date === type }.each do |name, _type|
  90. klass.attribute name, EasyDate.new
  91. end
  92. end
  93.  
  94. module FixDateFormatting
  95. # the above `inherited` method here
  96. end
  97.  
  98. # Either this...
  99. class ApplicationRecord < ActiveRecord::Base
  100. extend FixDateFormatting
  101. end
  102.  
  103. # ...or this:
  104. ActiveRecord::Base.extend(FixDateFormatting)
  105.  
  106. Date::DATE_FORMATS[:default] = "%m/%d/%Y"
  107.  
  108. class EasyDate < ActiveRecord::Type::Date
  109. def cast_value(value)
  110. default = super
  111. parsed = Chronic.parse(value)&.to_date if value.is_a?(String)
  112. parsed || default
  113. end
  114. end
  115.  
  116. module FixDateFormatting
  117. def inherited(subclass)
  118. super
  119. date_attributes = subclass.attribute_types.select { |_, type| ActiveRecord::Type::Date === type }
  120. date_attributes.each do |name, _type|
  121. subclass.attribute name, EasyDate.new
  122. end
  123. end
  124. end
  125.  
  126. ActiveRecord::Base.extend(FixDateFormatting)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement