fork download
  1. lib/active_record_extensions.rb
  2.  
  3. require 'i18n'
  4. require 'active_record_extensions/time_parser'
  5. ActiveRecord::Base.send(:include, ActiveRecordExtensions::TimeParser)
  6.  
  7. lib/active_record_extensions/time_parser.rb
  8.  
  9. module ActiveRecordExtensions
  10. module TimeParser
  11. def self.included(base)
  12. base.extend(ClassMethods)
  13. end
  14. module ClassMethods
  15. # add your static(class) methods here
  16. def datepicker_attributes(*args)
  17. options = args.extract_options!
  18. args.delete_at(-1) if args[-1].is_a?(Hash)
  19. args.each do |arg|
  20. column_type = self.columns_hash[arg.to_s].type
  21. raise StandardError, "Wrong column declaration, specify a
  22. column with date or datetime type" if column_type != :date &&
  23. column_type != :datetime
  24. define_method "#{arg.to_s}=" do |date|
  25. aux_date = nil
  26. ## FIXME: NEVER ENTERS INSIDE THAT LOOP I18n.t...
  27. I18n.t('time.datepicker_formats').values do |format|
  28. aux_date = Time.strptime(date, format) rescue nil
  29. break unless aux_date.nil?
  30. end
  31. write_attribute(arg.to_sym,aux_date || date)
  32. end
  33. end
  34. end
  35. end
  36. end
  37. end
  38.  
  39.  
  40.  
  41. config/locales/en/model:
  42.  
  43. en:
  44. time:
  45. datepicker_formats:
  46. default: "%d/%m/%Y %H:%M"
  47.  
  48. config/environment.rb:
  49.  
  50. require "active_record_extensions"
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty