Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <%= form.text_field :check_in_date %>
- ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
- :default => '%m/%d/%Y'
- )
- <%= f.text_field :some_date, :value => @model_instance.some_date.strftime("%d-%m-%Y") %>
- # Default format for displaying dates and times
- Date::DATE_FORMATS[:default] = "%m/%d/%Y"
- Time::DATE_FORMATS[:default] = "%m/%d/%Y"
- MyModel < ActiveRecord::Base
- # ...
- self.columns.each do |column|
- if column.type == :date
- define_method "#{column.name}_before_type_cast" do
- self.send(column.name).to_s
- end
- end
- end
- end
- def date_mdY(date)
- if date.nil?
- ""
- else
- date.strftime("%m-%d-%Y")
- end
- end
- <%= f.text_field :some_date, :value => date_mdY(@model_instance.some_date) %>
- ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS[:default] = '%m/%d/%Y'
- <%= form.text_field :check_in_date, value: model.check_in_date.to_s %>
- <%= f.text_field :date3, value: ( l @model.date3 if @model.date3? ) %>
- en:
- date:
- formats:
- default: "%d/%m/%Y"
- ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(
- :default => '%m-%d-%Y' )
- def start_date
- super.strftime "%Y/%m/%d %H:%M"
- end
- <%= f.text_field :start_date, class: 'form-control' %>
- after_initialize :init
- private
- def init
- unless time.nil?
- @attributes['time'] = I18n.l(time, format: :short)
- end
- end
- Date::DATE_FORMATS[:default] = "%m/%d/%Y"
- class EasyDate < ActiveRecord::Type::Date
- def cast_value(value)
- default = super
- parsed = Chronic.parse(value)&.to_date if value.is_a?(String)
- parsed || default
- end
- end
- class Pony < ApplicationRecord
- attribute :birth_date, EasyDate.new
- end
- { column_name: instance_of_a_type_object, ... }
- #<ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Money:0x007ff974d62688
- date_attributes = attribute_types.select { |_, type| ActiveRecord::Type::Date === type }
- date_attributes.each do |attr_name, _type|
- attribute attr_name, EasyDate.new
- end
- def inherited(klass)
- super
- klass.attribute_types.select { |_, type| ActiveRecord::Type::Date === type }.each do |name, _type|
- klass.attribute name, EasyDate.new
- end
- end
- module FixDateFormatting
- # the above `inherited` method here
- end
- # Either this...
- class ApplicationRecord < ActiveRecord::Base
- extend FixDateFormatting
- end
- # ...or this:
- ActiveRecord::Base.extend(FixDateFormatting)
- Date::DATE_FORMATS[:default] = "%m/%d/%Y"
- class EasyDate < ActiveRecord::Type::Date
- def cast_value(value)
- default = super
- parsed = Chronic.parse(value)&.to_date if value.is_a?(String)
- parsed || default
- end
- end
- module FixDateFormatting
- def inherited(subclass)
- super
- date_attributes = subclass.attribute_types.select { |_, type| ActiveRecord::Type::Date === type }
- date_attributes.each do |name, _type|
- subclass.attribute name, EasyDate.new
- end
- end
- end
- ActiveRecord::Base.extend(FixDateFormatting)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement