mirror of
https://github.com/ditkrg/validates_timeliness.git
synced 2026-01-25 07:16:41 +00:00
restriction shorthand symbols
This commit is contained in:
@@ -26,6 +26,13 @@ module ValidatesTimeliness
|
||||
mattr_accessor :ignore_restriction_errors
|
||||
@@ignore_restriction_errors = defined?(Rails) ? !Rails.env.test? : false
|
||||
|
||||
# Shorthand time and date symbols for restrictions
|
||||
mattr_accessor :restriction_shorthand_symbols
|
||||
@@restriction_shorthand_symbols = {
|
||||
:now => lambda { Time.now },
|
||||
:today => lambda { Date.today }
|
||||
}
|
||||
|
||||
# Setup method for plugin configuration
|
||||
def self.setup
|
||||
yield self
|
||||
|
||||
@@ -31,7 +31,11 @@ module ValidatesTimeliness
|
||||
when String
|
||||
value.to_time(:local)
|
||||
when Symbol
|
||||
evaluate_option_value(record.send(value), record)
|
||||
if !record.respond_to?(value) && restriction_shorthand?(value)
|
||||
ValidatesTimeliness.restriction_shorthand_symbols[value].call
|
||||
else
|
||||
evaluate_option_value(record.send(value), record)
|
||||
end
|
||||
when Proc
|
||||
result = value.arity > 0 ? value.call(record) : value.call
|
||||
evaluate_option_value(result, record)
|
||||
@@ -40,5 +44,9 @@ module ValidatesTimeliness
|
||||
end
|
||||
end
|
||||
|
||||
def restriction_shorthand?(symbol)
|
||||
ValidatesTimeliness.restriction_shorthand_symbols.keys.include?(symbol)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user