mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
Datetime select extension fixes with help from @johncarney's fork
This commit is contained in:
parent
feb508f171
commit
0444bdc650
1
Gemfile
1
Gemfile
@ -6,7 +6,6 @@ gem 'rails', '~> 4.0.13'
|
|||||||
gem 'rspec', '~> 3.0.0'
|
gem 'rspec', '~> 3.0.0'
|
||||||
gem 'rspec-rails', '~> 3.0.0'
|
gem 'rspec-rails', '~> 3.0.0'
|
||||||
gem 'timecop'
|
gem 'timecop'
|
||||||
gem 'rspec_tag_matchers'
|
|
||||||
gem 'byebug'
|
gem 'byebug'
|
||||||
gem 'appraisal'
|
gem 'appraisal'
|
||||||
gem 'sqlite3'
|
gem 'sqlite3'
|
||||||
|
|||||||
@ -4,7 +4,7 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
|
|
||||||
def self.enable_date_time_select_extension!
|
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
|
end
|
||||||
|
|
||||||
def self.enable_multiparameter_extension!
|
def self.enable_multiparameter_extension!
|
||||||
|
|||||||
@ -9,7 +9,6 @@ module ValidatesTimeliness
|
|||||||
# It's a minor usability improvement which is rarely an issue for the user.
|
# It's a minor usability improvement which is rarely an issue for the user.
|
||||||
|
|
||||||
included do
|
included do
|
||||||
alias_method_chain :datetime_selector, :timeliness
|
|
||||||
alias_method_chain :value, :timeliness
|
alias_method_chain :value, :timeliness
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -33,15 +32,8 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
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)
|
def value_with_timeliness(object)
|
||||||
unless @timeliness_date_or_time_tag && @template_object.params[@object_name]
|
return value_without_timeliness(object) unless @template_object.params[@object_name]
|
||||||
return value_without_timeliness(object)
|
|
||||||
end
|
|
||||||
|
|
||||||
@template_object.params[@object_name]
|
@template_object.params[@object_name]
|
||||||
|
|
||||||
@ -56,6 +48,7 @@ module ValidatesTimeliness
|
|||||||
|
|
||||||
TimelinessDateTime.new(*values)
|
TimelinessDateTime.new(*values)
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -5,13 +5,13 @@ require 'active_model/validations'
|
|||||||
require 'active_record'
|
require 'active_record'
|
||||||
require 'action_view'
|
require 'action_view'
|
||||||
require 'timecop'
|
require 'timecop'
|
||||||
require 'rspec_tag_matchers'
|
|
||||||
|
|
||||||
require 'validates_timeliness'
|
require 'validates_timeliness'
|
||||||
|
|
||||||
require 'support/test_model'
|
require 'support/test_model'
|
||||||
require 'support/model_helpers'
|
require 'support/model_helpers'
|
||||||
require 'support/config_helper'
|
require 'support/config_helper'
|
||||||
|
require 'support/tag_matcher'
|
||||||
|
|
||||||
ValidatesTimeliness.setup do |c|
|
ValidatesTimeliness.setup do |c|
|
||||||
c.extend_orms = [ :active_record ]
|
c.extend_orms = [ :active_record ]
|
||||||
@ -86,7 +86,7 @@ end
|
|||||||
|
|
||||||
RSpec.configure do |c|
|
RSpec.configure do |c|
|
||||||
c.mock_with :rspec
|
c.mock_with :rspec
|
||||||
c.include(RspecTagMatchers)
|
c.include(TagMatcher)
|
||||||
c.include(ModelHelpers)
|
c.include(ModelHelpers)
|
||||||
c.include(ConfigHelper)
|
c.include(ConfigHelper)
|
||||||
c.before do
|
c.before do
|
||||||
|
|||||||
35
spec/support/tag_matcher.rb
Normal file
35
spec/support/tag_matcher.rb
Normal 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
|
||||||
@ -1,6 +1,6 @@
|
|||||||
require 'spec_helper'
|
require 'spec_helper'
|
||||||
|
|
||||||
describe ValidatesTimeliness::Extensions::DateTimeSelect do
|
describe 'ValidatesTimeliness::Extensions::DateTimeSelect' do
|
||||||
include ActionView::Helpers::DateHelper
|
include ActionView::Helpers::DateHelper
|
||||||
attr_reader :person, :params
|
attr_reader :person, :params
|
||||||
|
|
||||||
@ -150,14 +150,15 @@ describe ValidatesTimeliness::Extensions::DateTimeSelect do
|
|||||||
def should_have_datetime_selected(field, datetime_hash)
|
def should_have_datetime_selected(field, datetime_hash)
|
||||||
datetime_hash.each do |key, value|
|
datetime_hash.each do |key, value|
|
||||||
index = {:year => 1, :month => 2, :day => 3, :hour => 4, :min => 5, :sec => 6}[key]
|
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
|
||||||
end
|
end
|
||||||
|
|
||||||
def should_not_have_datetime_selected(field, *attributes)
|
def should_not_have_datetime_selected(field, *attributes)
|
||||||
attributes.each do |attribute|
|
attributes.each do |attribute|
|
||||||
index = {:year => 1, :month => 2, :day => 3, :hour => 4, :min => 5, :sec => 6}[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
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user