Skip to content
Extraits de code Groupes Projets
Valider 630de52f rédigé par alpaca-tc's avatar alpaca-tc Validation de Eugen
Parcourir les fichiers

Required foreign keys (#2003)

* Add `required: true` option to foreign column

* Fixes NoMethodError

```
> Favourite.new.valid?
NoMethodError: undefined method `reblog?' for nil:NilClass
```
parent f8546ad5
Aucune branche associée trouvée
Aucune étiquette associée trouvée
Aucune requête de fusion associée trouvée
...@@ -3,14 +3,14 @@ ...@@ -3,14 +3,14 @@
class Favourite < ApplicationRecord class Favourite < ApplicationRecord
include Paginable include Paginable
belongs_to :account, inverse_of: :favourites belongs_to :account, inverse_of: :favourites, required: true
belongs_to :status, inverse_of: :favourites, counter_cache: true belongs_to :status, inverse_of: :favourites, counter_cache: true, required: true
has_one :notification, as: :activity, dependent: :destroy has_one :notification, as: :activity, dependent: :destroy
validates :status_id, uniqueness: { scope: :account_id } validates :status_id, uniqueness: { scope: :account_id }
before_validation do before_validation do
self.status = status.reblog if status.reblog? self.status = status.reblog if status&.reblog?
end end
end end
...@@ -3,9 +3,8 @@ ...@@ -3,9 +3,8 @@
class Mute < ApplicationRecord class Mute < ApplicationRecord
include Paginable include Paginable
belongs_to :account belongs_to :account, required: true
belongs_to :target_account, class_name: 'Account' belongs_to :target_account, class_name: 'Account', required: true
validates :account, :target_account, presence: true
validates :account_id, uniqueness: { scope: :target_account_id } validates :account_id, uniqueness: { scope: :target_account_id }
end end
...@@ -10,7 +10,7 @@ class Status < ApplicationRecord ...@@ -10,7 +10,7 @@ class Status < ApplicationRecord
belongs_to :application, class_name: 'Doorkeeper::Application' belongs_to :application, class_name: 'Doorkeeper::Application'
belongs_to :account, inverse_of: :statuses, counter_cache: true belongs_to :account, inverse_of: :statuses, counter_cache: true, required: true
belongs_to :in_reply_to_account, foreign_key: 'in_reply_to_account_id', class_name: 'Account' belongs_to :in_reply_to_account, foreign_key: 'in_reply_to_account_id', class_name: 'Account'
belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies belongs_to :thread, foreign_key: 'in_reply_to_id', class_name: 'Status', inverse_of: :replies
...@@ -26,7 +26,6 @@ class Status < ApplicationRecord ...@@ -26,7 +26,6 @@ class Status < ApplicationRecord
has_one :notification, as: :activity, dependent: :destroy has_one :notification, as: :activity, dependent: :destroy
has_one :preview_card, dependent: :destroy has_one :preview_card, dependent: :destroy
validates :account, presence: true
validates :uri, uniqueness: true, unless: 'local?' validates :uri, uniqueness: true, unless: 'local?'
validates :text, presence: true, unless: 'reblog?' validates :text, presence: true, unless: 'reblog?'
validates_with StatusLengthValidator validates_with StatusLengthValidator
......
...@@ -4,7 +4,7 @@ class Subscription < ApplicationRecord ...@@ -4,7 +4,7 @@ class Subscription < ApplicationRecord
MIN_EXPIRATION = 3600 * 24 * 7 MIN_EXPIRATION = 3600 * 24 * 7
MAX_EXPIRATION = 3600 * 24 * 30 MAX_EXPIRATION = 3600 * 24 * 30
belongs_to :account belongs_to :account, required: true
validates :callback_url, presence: true validates :callback_url, presence: true
validates :callback_url, uniqueness: { scope: :account_id } validates :callback_url, uniqueness: { scope: :account_id }
......
...@@ -9,10 +9,9 @@ class User < ApplicationRecord ...@@ -9,10 +9,9 @@ class User < ApplicationRecord
otp_secret_encryption_key: ENV['OTP_SECRET'], otp_secret_encryption_key: ENV['OTP_SECRET'],
otp_number_of_backup_codes: 10 otp_number_of_backup_codes: 10
belongs_to :account, inverse_of: :user belongs_to :account, inverse_of: :user, required: true
accepts_nested_attributes_for :account accepts_nested_attributes_for :account
validates :account, presence: true
validates :locale, inclusion: I18n.available_locales.map(&:to_s), unless: 'locale.nil?' validates :locale, inclusion: I18n.available_locales.map(&:to_s), unless: 'locale.nil?'
validates :email, email: true validates :email, email: true
......
0% Chargement en cours ou .
You are about to add 0 people to the discussion. Proceed with caution.
Terminez d'abord l'édition de ce message.
Veuillez vous inscrire ou vous pour commenter