From 0444bdc650bfe25fd75998539b95c23742b9411e Mon Sep 17 00:00:00 2001 From: Adam Meehan Date: Tue, 29 Dec 2015 15:49:18 +1100 Subject: [PATCH] Datetime select extension fixes with help from @johncarney's fork --- Gemfile | 1 - lib/validates_timeliness/extensions.rb | 2 +- .../extensions/date_time_select.rb | 11 ++---- spec/spec_helper.rb | 4 +-- spec/support/tag_matcher.rb | 35 +++++++++++++++++++ .../extensions/date_time_select_spec.rb | 7 ++-- 6 files changed, 44 insertions(+), 16 deletions(-) create mode 100644 spec/support/tag_matcher.rb diff --git a/Gemfile b/Gemfile index 9ec2b9a..635fa1c 100644 --- a/Gemfile +++ b/Gemfile @@ -6,7 +6,6 @@ gem 'rails', '~> 4.0.13' gem 'rspec', '~> 3.0.0' gem 'rspec-rails', '~> 3.0.0' gem 'timecop' -gem 'rspec_tag_matchers' gem 'byebug' gem 'appraisal' gem 'sqlite3' diff --git a/lib/validates_timeliness/extensions.rb b/lib/validates_timeliness/extensions.rb index 6623286..f535e99 100644 --- a/lib/validates_timeliness/extensions.rb +++ b/lib/validates_timeliness/extensions.rb @@ -4,7 +4,7 @@ module ValidatesTimeliness end def self.enable_date_time_select_extension! - ::ActionView::Helpers::InstanceTag.send(:include, ValidatesTimeliness::Extensions::DateTimeSelect) + ::ActionView::Helpers::Tags::DateSelect.send(:include, ValidatesTimeliness::Extensions::DateTimeSelect) 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 6a99765..7d64516 100644 --- a/lib/validates_timeliness/extensions/date_time_select.rb +++ b/lib/validates_timeliness/extensions/date_time_select.rb @@ -9,7 +9,6 @@ module ValidatesTimeliness # It's a minor usability improvement which is rarely an issue for the user. included do - alias_method_chain :datetime_selector, :timeliness alias_method_chain :value, :timeliness end @@ -33,15 +32,8 @@ module ValidatesTimeliness end end - def datetime_selector_with_timeliness(*args) - @timeliness_date_or_time_tag = true - datetime_selector_without_timeliness(*args) - end - def value_with_timeliness(object) - unless @timeliness_date_or_time_tag && @template_object.params[@object_name] - return value_without_timeliness(object) - end + return value_without_timeliness(object) unless @template_object.params[@object_name] @template_object.params[@object_name] @@ -56,6 +48,7 @@ module ValidatesTimeliness TimelinessDateTime.new(*values) end + end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 43ea313..1a2025b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -5,13 +5,13 @@ require 'active_model/validations' require 'active_record' require 'action_view' require 'timecop' -require 'rspec_tag_matchers' require 'validates_timeliness' require 'support/test_model' require 'support/model_helpers' require 'support/config_helper' +require 'support/tag_matcher' ValidatesTimeliness.setup do |c| c.extend_orms = [ :active_record ] @@ -86,7 +86,7 @@ end RSpec.configure do |c| c.mock_with :rspec - c.include(RspecTagMatchers) + c.include(TagMatcher) c.include(ModelHelpers) c.include(ConfigHelper) c.before do diff --git a/spec/support/tag_matcher.rb b/spec/support/tag_matcher.rb new file mode 100644 index 0000000..2fe9870 --- /dev/null +++ b/spec/support/tag_matcher.rb @@ -0,0 +1,35 @@ +require 'nokogiri' + +module TagMatcher + extend RSpec::Matchers::DSL + + matcher :have_tag do |selector| + match do |subject| + matches = doc(subject).search(selector) + + if @inner_text + matches = matches.select { |element| element.inner_text == @inner_text } + end + + matches.any? + end + + chain :with_inner_text do |inner_text| + @inner_text = inner_text + end + + private + + def body(subject) + if subject.respond_to?(:body) + subject.body + else + subject.to_s + end + end + + def doc(subject) + @doc ||= Nokogiri::HTML(body(subject)) + end + end +end diff --git a/spec/validates_timeliness/extensions/date_time_select_spec.rb b/spec/validates_timeliness/extensions/date_time_select_spec.rb index 5260321..4181480 100644 --- a/spec/validates_timeliness/extensions/date_time_select_spec.rb +++ b/spec/validates_timeliness/extensions/date_time_select_spec.rb @@ -1,6 +1,6 @@ require 'spec_helper' -describe ValidatesTimeliness::Extensions::DateTimeSelect do +describe 'ValidatesTimeliness::Extensions::DateTimeSelect' do include ActionView::Helpers::DateHelper attr_reader :person, :params @@ -150,14 +150,15 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do def should_have_datetime_selected(field, datetime_hash) datetime_hash.each do |key, value| index = {:year => 1, :month => 2, :day => 3, :hour => 4, :min => 5, :sec => 6}[key] - @output.should have_tag("select[id=person_#{field}_#{index}i] option[selected=selected]", value.to_s) + expect(@output).to have_tag("select[id=person_#{field}_#{index}i] option[selected=selected]", value.to_s) end end def should_not_have_datetime_selected(field, *attributes) attributes.each do |attribute| index = {:year => 1, :month => 2, :day => 3, :hour => 4, :min => 5, :sec => 6}[attribute] - @output.should_not have_tag("select[id=person_#{attribute}_#{index}i] option[selected=selected]") + expect(@output).not_to have_tag("select[id=person_#{attribute}_#{index}i] option[selected=selected]") end end + end