From c9ca900abf11fda461821b7e0c649c2035b96a83 Mon Sep 17 00:00:00 2001 From: AquisTech Date: Mon, 14 May 2018 19:52:24 +0530 Subject: [PATCH] Fix issue#166 alias_method_chain not working in Rails 5 --- lib/validates_timeliness/extensions.rb | 4 +- .../extensions/date_time_select.rb | 49 +++++-------------- 2 files changed, 15 insertions(+), 38 deletions(-) diff --git a/lib/validates_timeliness/extensions.rb b/lib/validates_timeliness/extensions.rb index f535e99..89eae1f 100644 --- a/lib/validates_timeliness/extensions.rb +++ b/lib/validates_timeliness/extensions.rb @@ -1,10 +1,10 @@ module ValidatesTimeliness module Extensions - autoload :DateTimeSelect, 'validates_timeliness/extensions/date_time_select' + autoload :TimelinessDateTimeSelect, 'validates_timeliness/extensions/date_time_select' end def self.enable_date_time_select_extension! - ::ActionView::Helpers::Tags::DateSelect.send(:include, ValidatesTimeliness::Extensions::DateTimeSelect) + ::ActionView::Helpers::Tags::DateSelect.send(:prepend, ValidatesTimeliness::Extensions::TimelinessDateTimeSelect) end def self.enable_multiparameter_extension! diff --git a/lib/validates_timeliness/extensions/date_time_select.rb b/lib/validates_timeliness/extensions/date_time_select.rb index 7d64516..93ca064 100644 --- a/lib/validates_timeliness/extensions/date_time_select.rb +++ b/lib/validates_timeliness/extensions/date_time_select.rb @@ -1,54 +1,31 @@ module ValidatesTimeliness module Extensions - module DateTimeSelect - extend ActiveSupport::Concern - + module TimelinessDateTimeSelect # Intercepts the date and time select helpers to reuse the values from # the params rather than the parsed value. This allows invalid date/time # values to be redisplayed instead of blanks to aid correction by the user. # It's a minor usability improvement which is rarely an issue for the user. + attr_accessor :object_name, :method_name, :template_object, :options, :html_options - included do - alias_method_chain :value, :timeliness + def initialize(object_name, method_name, template_object, options, html_options) + @object_name, @method_name = object_name.to_s.dup, method_name.to_s.dup + @template_object, @options, @html_options = template_object, options, html_options end - class TimelinessDateTime - attr_accessor :year, :month, :day, :hour, :min, :sec - - def initialize(year, month, day, hour, min, sec) - @year, @month, @day, @hour, @min, @sec = year, month, day, hour, min, sec - end - - # adapted from activesupport/lib/active_support/core_ext/date_time/calculations.rb, line 36 (3.0.7) - def change(options) - TimelinessDateTime.new( - options[:year] || year, - options[:month] || month, - options[:day] || day, - options[:hour] || hour, - options[:min] || (options[:hour] ? 0 : min), - options[:sec] || ((options[:hour] || options[:min]) ? 0 : sec) - ) - end - end - - def value_with_timeliness(object) - return value_without_timeliness(object) unless @template_object.params[@object_name] - - @template_object.params[@object_name] + def value + return super unless @template_object.params[@object_name] pairs = @template_object.params[@object_name].select {|k,v| k =~ /^#{@method_name}\(/ } - return value_without_timeliness(object) if pairs.empty? + return super if pairs.empty? - values = [nil] * 6 - pairs.map do |(param, value)| - position = param.scan(/\((\d+)\w+\)/).first.first - values[position.to_i-1] = value.to_i + values = {} + pairs.each_pair do |key, value| + position = key[/\((\d+)\w+\)/, 1] + values[::ActionView::Helpers::DateTimeSelector::POSITION.key(position.to_i)] = value.to_i end - TimelinessDateTime.new(*values) + values end - end end end