mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-22 22:06:45 +00:00
added option key validation to prevent silly validation problems due to bad key name
This commit is contained in:
parent
b3e235a8a1
commit
a1ae5f9313
@ -49,7 +49,7 @@ module ValidatesTimeliness
|
||||
private
|
||||
|
||||
def validates_timeliness_of(attr_names, configuration)
|
||||
validator = ValidatesTimeliness::Validator.new(configuration)
|
||||
validator = ValidatesTimeliness::Validator.new(configuration.symbolize_keys)
|
||||
|
||||
# bypass handling of allow_nil and allow_blank to validate raw value
|
||||
configuration.delete(:allow_nil)
|
||||
|
||||
@ -19,12 +19,17 @@ module ValidatesTimeliness
|
||||
:between => lambda {|v, r| (r.first..r.last).include?(v) }
|
||||
}
|
||||
|
||||
VALID_OPTIONS = [
|
||||
:on, :allow_nil, :empty, :allow_blank, :blank, :invalid_time_message, :invalid_date_message, :invalid_datetime_message
|
||||
] + RESTRICTION_METHODS.keys.map {|option| [option, "#{option}_message".to_sym] }.flatten
|
||||
|
||||
attr_reader :configuration, :type
|
||||
|
||||
def initialize(configuration)
|
||||
defaults = { :on => :save, :type => :datetime, :allow_nil => false, :allow_blank => false }
|
||||
@configuration = defaults.merge(configuration)
|
||||
@type = @configuration.delete(:type)
|
||||
validate_options(@configuration)
|
||||
end
|
||||
|
||||
def call(record, attr_name)
|
||||
@ -159,5 +164,12 @@ module ValidatesTimeliness
|
||||
end
|
||||
end
|
||||
|
||||
def validate_options(options)
|
||||
invalid_types = [:time, :date, :datetime]
|
||||
invalid_types.delete(@type)
|
||||
valid_options = VALID_OPTIONS.reject {|option| invalid_types.include?("#{option}_message".to_sym) }
|
||||
options.assert_valid_keys(valid_options)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@ -16,6 +16,23 @@ describe ValidatesTimeliness::Validator do
|
||||
@person = Person.new
|
||||
end
|
||||
|
||||
describe "option keys validation" do
|
||||
before do
|
||||
@valid_options = ValidatesTimeliness::Validator::VALID_OPTIONS.inject({}) {|hash, opt| hash[opt] = nil; hash }
|
||||
@valid_options.delete(:invalid_date_message)
|
||||
@valid_options.delete(:invalid_time_message)
|
||||
end
|
||||
|
||||
it "should raise error if invalid option key passed" do
|
||||
@valid_options.update(:invalid_key => 'will not open lock')
|
||||
lambda { Person.validates_datetime(@valid_options) }.should raise_error(ArgumentError)
|
||||
end
|
||||
|
||||
it "should not raise error if option keys are valid" do
|
||||
lambda { Person.validates_datetime(@valid_options) }.should_not raise_error(ArgumentError)
|
||||
end
|
||||
end
|
||||
|
||||
describe "restriction_value" do
|
||||
it "should return Time object when restriction is Time object" do
|
||||
restriction_value(Time.now, :datetime).should be_kind_of(Time)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user