Module: SelectableAttrRails::Validatable::Base::ClassMethods

Defined in:
lib/selectable_attr_rails/validatable/base.rb

Instance Method Summary collapse

Instance Method Details

#define_enum_with_validatable(context) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
# File 'lib/selectable_attr_rails/validatable/base.rb', line 15

def define_enum_with_validatable(context)
  enum = context[:enum]
  if options = enum.validates_format_options
    options[:with] = Regexp.union(*enum.entries.map{|entry| /#{Regexp.escape(entry.id)}/})
    entry_format = options.delete(:entry_format) || '#{entry.name}'
    entries = enum.entries.map{|entry| instance_eval("\"#{entry_format}\"")}.join(', ')
    message = options.delete(:message) || 'is invalid, must be one of #{entries}'
    options[:message] = instance_eval("\"#{message}\"")
    validates_format_of(context[:attr], options)
  end
end