Datetime select extension fixes with help from @johncarney's fork

This commit is contained in:
Adam Meehan 2015-12-29 15:49:18 +11:00
parent feb508f171
commit 0444bdc650
6 changed files with 44 additions and 16 deletions

View File

@ -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'

View File

@ -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!

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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