README.md in conditional_validation-0.0.5 vs README.md in conditional_validation-0.1.0

- old
+ new

@@ -1,88 +1,100 @@ # Conditional Validation [![Gem Version](https://badge.fury.io/rb/conditional_validation.png)](http://badge.fury.io/rb/conditional_validation) -Conditional Validation allows controllers to communicate with models about -whether or not certain validations should be run. This is great for multi-page -wizards and context-dependent validations. +Conditional Validation allows validation flags to be enabled to determine when +certain validations should be run on a model. The idea being that, while models +tend to have a set of core validations that should always be run, some +validations may be specific to a certain context or state of the object. Typical +use-case, then, is to flag a model's non-core validations to run from specific +controller actions, while they default to not run from all others. + ## Compatibility Tested with: * Ruby: MRI 1.9.3 * Ruby: MRI 2.0.0 + -* Rails: 3.0.0 + -* Rails: 4.0.0 + +* Rails: 3+ +* Rails: 4+ + ## Installation Add this line to your application's Gemfile: ```ruby -gem "conditional_validation" +gem 'conditional_validation' ``` And then execute: ```ruby bundle ``` + ## Usage -First, define a validation accessor: +First, define a validation flag: ```ruby # app/models/some_model.rb class SomeModel - validation_accessor :some_grouping_name + validation_flag :<flag_name> end ``` -Then, this model will receive the following methods for conditional validation: +Then, the following methods will be defined on SomeModel for conditional +validation: ```ruby -enable_some_grouping_name_validation # Enables conditional validation -disable_some_grouping_name_validation # Disables conditional validation -validate_on_some_grouping_name? # Check if conditional validation is enabled +enable_<flag_name>_validation # Enables conditional validation +disable_<flag_name>_validation # Disables conditional validation +validate_on_<flag_name>? # Check if conditional validation is enabled ``` -<b>A "Real World" Example</b> +### A "Real World" Example + ```ruby # app/models/user.rb User < ActiveRecord::Base - validation_accessor :address_attributes # Initialize conditional validation on address attributes + # Initialize conditional validation on address attributes + validation_flag :address_attributes with_options if: :validate_on_address_attributes? do |obj| obj.validates :street, presence: true obj.validates :city, presence: true # ... end end # app/controllers/user_controller.rb def update - current_user.enable_address_attributes_validation # Enable conditional validation on address attributes + # Enable conditional validation on address attributes + current_user.enable_address_attributes_validation if current_user.save - current_user.disable_address_attributes_validation # Not necessarily needed, but disables conditional validation on address attributes + # Not typically needed, but disables conditional validation on address attributes + current_user.disable_address_attributes_validation # ... end end ``` -<b>Method Chaining</b> +### Method Chaining The enable and disable methods allow for method chaining so that multiple -validation accessors may be enabled/disabled at once: +validation flags may be enabled/disabled at once: ```ruby if current_user.enable_address_attributes_validation.enable_some_other_validation.save # ... end ``` + ## Author - Paul Dobbins