lib/str_enum/model.rb in str_enum-0.1.6 vs lib/str_enum/model.rb in str_enum-0.2.0
- old
+ new
@@ -6,22 +6,26 @@
class_methods do
def str_enum(column, values, validate: true, scopes: true, accessor_methods: true, update_methods: true, prefix: false, suffix: false, default: true, allow_nil: false)
values = values.map(&:to_s)
if validate
- validate_options = {inclusion: {in: values}}
+ validate_options = {}
if allow_nil
validate_options[:allow_nil] = true
else
validate_options[:presence] = true
end
+ validate_options[:inclusion] = {in: values}
validates column, validate_options
end
values.each do |value|
prefix = column if prefix == true
suffix = column if suffix == true
method_name = [prefix, value, suffix].select { |v| v }.join("_")
- scope method_name, -> { where(column => value) } if scopes && !respond_to?(method_name)
+ if scopes
+ scope method_name, -> { where(column => value) } unless respond_to?(method_name)
+ scope "not_#{method_name}", -> { where.not(column => value) } unless respond_to?("not_#{method_name}")
+ end
if accessor_methods && !method_defined?("#{method_name}?")
define_method "#{method_name}?" do
read_attribute(column) == value
end
end