没有任何数据可供显示
开源项目社区 | 当前位置 : |
|
oss.trustie.net/open_source_projects | 主页 > 开源项目社区 > validation-scenarios |
validation-scenarios
|
0 | 0 | 20 |
贡献者 | 讨论 | 代码提交 |
SummaryAdds validation scenarios support to Active Record models
SVN Repositoryhttp://validation-scenarios.googlecode.com/svn/
Installation> ruby ./script/plugin install http://validation-scenarios.googlecode.com/svn/tags/validation-scenarios
UsageFirst, in your models, you declare validation scenarios details using when_scenario_is method. In the following example, both User and Address models have user_registration and profile_update scenarios declared:
class User < ActiveRecord::Base
has_one :address
when_scenario_is :user_registration do |this|
this.validates_presence_of :email
this.validates_associated :address
end
when_scenario_is :profile_update do |this|
this.validates_presence_of :first_name :unless => :something_here
end
endclass Address < ActiveRecord::Base
belongs_to :user
when_scenario_is :user_registration do |this|
this.validates_numericality_of :postcode
end
when_scenario_is :profile_update do |this|
this.validates_presence_of :street_address_1
this.validates_presence_of :city
end
endThen, when you want to process, for example, the user_registration scenario - you just start it using: ActiveRecord::Base.with_validation_scenario method. In the following example it validates User object per rules declared for user_registration scenario.
Please also note that associated Address instance is also aware that the current validation scenario is user_registration. That's why the following test passes:
ActiveRecord::Base.with_validation_scenario(:user_registration) do
user = User.new(:email => "foo@bar.baz")
user.build_address(:postcode => 'intentionally_not_numerical')
assert !user.save
assert user.errors.invalid?(:address)
assert_match(/is not a number/i, user.address.errors.on(:postcode))
end