diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-07-04 14:51:12 -0600 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-07-04 16:10:45 -0600 |
commit | 00f55516509770f58f9ce462d45afb80d8f649ca (patch) | |
tree | 800d823a63deec4d35e229cf43f547c038d9202a /activerecord/lib/active_record/associations/builder | |
parent | a6cc7b0ebd608a5100df17b4cee182b67643baac (diff) | |
download | rails-00f55516509770f58f9ce462d45afb80d8f649ca.tar.gz rails-00f55516509770f58f9ce462d45afb80d8f649ca.tar.bz2 rails-00f55516509770f58f9ce462d45afb80d8f649ca.zip |
Add a `required` option to singular associations
In addition to defining the association, a `required` association will
also have its presence validated.
Before:
```ruby
belongs_to :account
validates_presence_of :account
```
After:
```ruby
belongs_to :account, required: true
```
This helps to draw a distinction between types of validations, since
validations on associations are generally for data integrity purposes,
and aren't usually set through form inputs.
Diffstat (limited to 'activerecord/lib/active_record/associations/builder')
-rw-r--r-- | activerecord/lib/active_record/associations/builder/association.rb | 5 | ||||
-rw-r--r-- | activerecord/lib/active_record/associations/builder/singular_association.rb | 9 |
2 files changed, 13 insertions, 1 deletions
diff --git a/activerecord/lib/active_record/associations/builder/association.rb b/activerecord/lib/active_record/associations/builder/association.rb index e474236939..947d61ee7b 100644 --- a/activerecord/lib/active_record/associations/builder/association.rb +++ b/activerecord/lib/active_record/associations/builder/association.rb @@ -36,6 +36,7 @@ module ActiveRecord::Associations::Builder reflection = builder.build(model) define_accessors model, reflection define_callbacks model, reflection + define_validations model, reflection builder.define_extensions model reflection end @@ -124,6 +125,10 @@ module ActiveRecord::Associations::Builder CODE end + def self.define_validations(model, reflection) + # noop + end + def self.valid_dependent_options raise NotImplementedError end diff --git a/activerecord/lib/active_record/associations/builder/singular_association.rb b/activerecord/lib/active_record/associations/builder/singular_association.rb index e655c389a6..17f36acf40 100644 --- a/activerecord/lib/active_record/associations/builder/singular_association.rb +++ b/activerecord/lib/active_record/associations/builder/singular_association.rb @@ -3,7 +3,7 @@ module ActiveRecord::Associations::Builder class SingularAssociation < Association #:nodoc: def valid_options - super + [:remote, :dependent, :primary_key, :inverse_of] + super + [:remote, :dependent, :primary_key, :inverse_of, :required] end def self.define_accessors(model, reflection) @@ -27,5 +27,12 @@ module ActiveRecord::Associations::Builder end CODE end + + def self.define_validations(model, reflection) + super + if reflection.options[:required] + model.validates_presence_of reflection.name + end + end end end |