Module: Enumerize::SequelSupport::InstanceMethods

Defined in:
lib/enumerize/sequel.rb

Instance Method Summary collapse

Instance Method Details

#_set_default_value_for_enumerized_attributesObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/enumerize/sequel.rb', line 38

def _set_default_value_for_enumerized_attributes
  _enumerized_values_for_validation.delete_if do |k, v|
    v.nil?
  end

  if defined?(Sequel::Plugins::Serialization::InstanceMethods)
    modules = self.class.ancestors
    plugin_idx = modules.index(Sequel::Plugins::Serialization::InstanceMethods)

    if plugin_idx && plugin_idx < modules.index(Enumerize::SequelSupport::InstanceMethods)
      abort "ERROR: You need to enable the Sequel serialization plugin before calling any enumerize methods on a model."
    end

    plugin_idx = modules.index(Sequel::Plugins::ValidationHelpers::InstanceMethods)

    if plugin_idx && plugin_idx < modules.index(Enumerize::SequelSupport::InstanceMethods)
      abort "ERROR: You need to enable the Sequel validation_helpers plugin before calling any enumerize methods on a model."
    end
  end

  super
end

#validateObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/enumerize/sequel.rb', line 20

def validate
  super

  self.class.enumerized_attributes.each do |attr|
    skip_validations = Utils.call_if_callable(attr.skip_validations_value, self)
    next if skip_validations

    value = read_attribute_for_validation(attr.name)
    next if value.blank?

    if attr.kind_of? Multiple
      errors.add attr.name, "is invalid" unless value.respond_to?(:all?) && value.all? { |v| v.blank? || attr.find_value(v) }
    else
      errors.add attr.name, "is not included in the list" unless attr.find_value(value)
    end
  end
end