mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 07:16:41 +00:00
added option key validation to prevent silly validation problems due to bad key name
This commit is contained in:
@@ -49,7 +49,7 @@ module ValidatesTimeliness
|
|||||||
private
|
private
|
||||||
|
|
||||||
def validates_timeliness_of(attr_names, configuration)
|
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
|
# bypass handling of allow_nil and allow_blank to validate raw value
|
||||||
configuration.delete(:allow_nil)
|
configuration.delete(:allow_nil)
|
||||||
|
|||||||
@@ -19,12 +19,17 @@ module ValidatesTimeliness
|
|||||||
:between => lambda {|v, r| (r.first..r.last).include?(v) }
|
: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
|
attr_reader :configuration, :type
|
||||||
|
|
||||||
def initialize(configuration)
|
def initialize(configuration)
|
||||||
defaults = { :on => :save, :type => :datetime, :allow_nil => false, :allow_blank => false }
|
defaults = { :on => :save, :type => :datetime, :allow_nil => false, :allow_blank => false }
|
||||||
@configuration = defaults.merge(configuration)
|
@configuration = defaults.merge(configuration)
|
||||||
@type = @configuration.delete(:type)
|
@type = @configuration.delete(:type)
|
||||||
|
validate_options(@configuration)
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(record, attr_name)
|
def call(record, attr_name)
|
||||||
@@ -159,5 +164,12 @@ module ValidatesTimeliness
|
|||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -16,6 +16,23 @@ describe ValidatesTimeliness::Validator do
|
|||||||
@person = Person.new
|
@person = Person.new
|
||||||
end
|
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
|
describe "restriction_value" do
|
||||||
it "should return Time object when restriction is Time object" do
|
it "should return Time object when restriction is Time object" do
|
||||||
restriction_value(Time.now, :datetime).should be_kind_of(Time)
|
restriction_value(Time.now, :datetime).should be_kind_of(Time)
|
||||||
|
|||||||
Reference in New Issue
Block a user