mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-23 06:16:44 +00:00
move default validator options to constant
This commit is contained in:
parent
7d6967da90
commit
78baa7a3cc
@ -14,17 +14,18 @@ module ValidatesTimeliness
|
|||||||
:between => lambda {|v, r| (r.first..r.last).include?(v) }
|
:between => lambda {|v, r| (r.first..r.last).include?(v) }
|
||||||
}
|
}
|
||||||
|
|
||||||
VALID_OPTIONS = [
|
VALID_OPTION_KEYS = [
|
||||||
:on, :if, :unless, :allow_nil, :empty, :allow_blank,
|
:on, :if, :unless, :allow_nil, :empty, :allow_blank,
|
||||||
:with_time, :with_date, :ignore_usec, :format,
|
:with_time, :with_date, :ignore_usec, :format,
|
||||||
:invalid_time_message, :invalid_date_message, :invalid_datetime_message
|
:invalid_time_message, :invalid_date_message, :invalid_datetime_message
|
||||||
] + RESTRICTION_METHODS.keys.map {|option| [option, "#{option}_message".to_sym] }.flatten
|
] + RESTRICTION_METHODS.keys.map {|option| [option, "#{option}_message".to_sym] }.flatten
|
||||||
|
|
||||||
|
DEFAULT_OPTIONS = { :on => :save, :type => :datetime, :allow_nil => false, :allow_blank => false, :ignore_usec => false }
|
||||||
|
|
||||||
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, :ignore_usec => false }
|
@configuration = DEFAULT_OPTIONS.merge(configuration)
|
||||||
@configuration = defaults.merge(configuration)
|
|
||||||
@type = @configuration.delete(:type)
|
@type = @configuration.delete(:type)
|
||||||
validate_options(@configuration)
|
validate_options(@configuration)
|
||||||
end
|
end
|
||||||
@ -143,7 +144,7 @@ module ValidatesTimeliness
|
|||||||
invalid_for_type = ([:time, :date, :datetime] - [type]).map {|k| "invalid_#{k}_message".to_sym }
|
invalid_for_type = ([:time, :date, :datetime] - [type]).map {|k| "invalid_#{k}_message".to_sym }
|
||||||
invalid_for_type << :with_date unless type == :time
|
invalid_for_type << :with_date unless type == :time
|
||||||
invalid_for_type << :with_time unless type == :date
|
invalid_for_type << :with_time unless type == :date
|
||||||
options.assert_valid_keys(VALID_OPTIONS - invalid_for_type)
|
options.assert_valid_keys(VALID_OPTION_KEYS - invalid_for_type)
|
||||||
end
|
end
|
||||||
|
|
||||||
def implied_type
|
def implied_type
|
||||||
|
|||||||
@ -18,7 +18,7 @@ describe ValidatesTimeliness::Validator do
|
|||||||
|
|
||||||
describe "option keys validation" do
|
describe "option keys validation" do
|
||||||
before do
|
before do
|
||||||
keys = ValidatesTimeliness::Validator::VALID_OPTIONS - [:invalid_date_message, :invalid_time_message, :with_date, :with_time]
|
keys = ValidatesTimeliness::Validator::VALID_OPTION_KEYS - [:invalid_date_message, :invalid_time_message, :with_date, :with_time]
|
||||||
@valid_options = keys.inject({}) {|hash, opt| hash[opt] = nil; hash }
|
@valid_options = keys.inject({}) {|hash, opt| hash[opt] = nil; hash }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user