This gem allows you to mark some validations as optional using very simple syntax and let them be skipped by setting one attribute.
Works with active model and active record.
Example:
class OptionalData < Struct.new(:telephone, :address)
include ActiveModel::Validations
include ActiveModel::Warnings
validates_presence_of :telephone, :warning => true
warnings do
validates_presence_of :address
end
end
data = OptionalData.new() data.valid? => false data.skip_warnings = true data.valid? => true
You have three ways of activating the gem:
1) require ‘activemodel-warnings’ to use regular attr_writer :skip_warnings for storing the value.
With bundler: # Gemfile gem ‘activemodel-warnings’, ‘~> 1.0.0’
The two other ways are useful if skip_warnings attribute is set by user on form checkbox generated by rails:
2) require ‘activemodel-warnings/boolean’ if you want the attribute of skip_warning=(attribute) method to be parsed with global Boolean() method ex. provided by global_boolean gem after activating it with “GlobalBoolean.kernel!”
With bundler: # Gemfile gem ‘global_boolean’, ‘~> 0.1.1’ gem ‘activemodel-warnings’, ‘~> 1.0.0’, :require => “activemodel-warnings/boolean”
# application.rb GlobalBoolean.kernel!
3) require ‘activemodel-warnings/global_boolean’ if you want the attribute of skip_warnings=(attribute) to be parsed with GlobalBoolean.Boolean() method ex. provided by global_boolean gem but without messing with Kernel namespace
With bundler: # Gemfile gem ‘global_boolean’, ‘~> 0.1.1’ gem ‘activemodel-warnings’, ‘~> 1.0.0’, :require => “activemodel-warnings/global_boolean”
Be sure to require global_boolean (if using it) before requiring this gem. If using bundler then just put the declaration of global_boolean in Gemfile above the declaration of activemodel-warnings.