-
Notifications
You must be signed in to change notification settings - Fork 1
Model Validations based on scenarios
License
freetwix/validation_scenarios
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
= Validation Scenarios
Define Validations on an ActiveRecord Model for special scenarios or exclude main Validations when in a scenario.
= Howto
Use validates_ methods in a Model in scenarios. The validation will only occur, if the
Model is used in a scenario:
Model:
class Event < ActiveRecord::Base
validates_presence_of :title
in_scenario :reviewer do |s|
s.validates_presence_of :reviewer_note
end
Controller:
class ReviewController < ApplicationController
def create
@event = Event.new(params[:event])
@event.save!
end
def update
with_scenario :reviewer do
@event.save!
end
end
The :symbol given to #in_scenario is the identifier for the scenario, which is then activated
via the #with_scenario block.
If you want to exclude a default validation within a scenario, you may do so with the following
code:
Model:
class Event < ActiveRecord::Base
validates_uniqueness_of :title, :unless => Proc.new { in_scenario? :bulk_insert }
validates_presence_of :description, :unless => Proc.new { in_scenarios? :bulk_insert, :reviewer }
in_scenario :reviewer do |s|
s.validates_presence_of :reviewer_note
end
You may have noticed the #in_scenarios? method, which can be used for multiple assignments of
scenarios.
Another use of scenarios in state machines will give you a clean implementation of scenario based
transitions.
If in the above reviewer scenario only a state transition should be allowed as reviewer(using
the awesome state_machine plugin http://github.com/pluginaweek/state_machine), it is possible to
express this like:
Model:
class Event < ActiveRecord::Base
event :review do
transition :open => :reviewed, :if => lambda { |event| event.in_scenario?(:reviewer) }
end
state :open
state :reviewed
Controller:
class ReviewController < ApplicationController
def update
with_scenario :reviewer do
@event.review!
end
end
This is even more handy when the attribute update is more coupled to the current state of the model
object.
More nifty stuff for the model (if you're not a block fan):
Controller:
class ReviewController < ApplicationController
def update
@event.save_in_scenario(:reviewer)
end
def create
@event.save_in_scenario!(:admin)
end
thx to theflow for this nice idea.
= Notes
This plugin reuses the name and some ideas of the http://code.google.com/p/validation-scenarios/ plugin, cause i was to lazy to find another name (just rubified it and asking for re-usage of the name).
= Rails Support
The tests are running with the following Rails versions:
* 2.3.2
* 2.3.1
* 2.2
* 2.1
= License
MIT-License, see MIT-LICENSE file
much fun with the plugin, love and peace for the world :D
any suggestions to: freetwix at web dot de
thx
About
Model Validations based on scenarios
Resources
License
Stars
Watchers
Forks
Packages 0
No packages published