diff --git a/app/models/activity.rb b/app/models/activity.rb index 263f77c79..e9667dd64 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -6,7 +6,7 @@ class Activity < ApplicationRecord validates :action, presence: true, inclusion: { in: %w[create update destroy] } validates :project, presence: true validates :user, presence: true - validates :subject, presence: true, subject_changed: true + validates :subject, presence: true, changed: true serialize :subject_changes, Hash diff --git a/app/models/story.rb b/app/models/story.rb index 2ab3e3e10..063466f3e 100644 --- a/app/models/story.rb +++ b/app/models/story.rb @@ -102,10 +102,10 @@ def from_csv_row(row) enumerize :story_type, in: STORY_TYPES, predicates: true, scope: true validates :project, presence: true validates :title, presence: true - validates :requested_by_id, user_belongs_to_project: true - validates :owned_by_id, user_belongs_to_project: true + validates :requested_by_id, belongs_to_project: true + validates :owned_by_id, belongs_to_project: true validates :story_type, presence: true - validates :estimate, central_estimate: true, allow_nil: true + validates :estimate, estimate: true, allow_nil: true validate :validate_non_estimable_story def self.csv_headers diff --git a/app/validators/belongs_to_project_validator.rb b/app/validators/belongs_to_project_validator.rb new file mode 100644 index 000000000..dcc56637f --- /dev/null +++ b/app/validators/belongs_to_project_validator.rb @@ -0,0 +1,11 @@ +class BelongsToProjectValidator < ActiveModel::EachValidator + # Checks that the parameter being validated represents a User#id that + # is a member of the object.project + def validate_each(record, attribute, value) + return if record.project.blank? || value.blank? + + return if record.project.user_ids.include?(value) + + record.errors.add(attribute, message: 'user is not a member of this project') + end +end diff --git a/app/validators/central_estimate_validator.rb b/app/validators/central_estimate_validator.rb deleted file mode 100644 index 76045605e..000000000 --- a/app/validators/central_estimate_validator.rb +++ /dev/null @@ -1,11 +0,0 @@ -class CentralEstimateValidator < ActiveModel::EachValidator - # TODO: Change the name to EstimateValidator when remove support gem - # Checks that the estimate being validated is valid for record.project - def validate_each(record, attribute, value) - if record.project - unless record.project.point_values.include?(value) - record.errors.add(attribute, message: 'is not an allowed value for this project') - end - end - end -end diff --git a/app/validators/subject_changed_validator.rb b/app/validators/changed_validator.rb similarity index 51% rename from app/validators/subject_changed_validator.rb rename to app/validators/changed_validator.rb index 6bc83bc92..6f45ed03d 100644 --- a/app/validators/subject_changed_validator.rb +++ b/app/validators/changed_validator.rb @@ -1,9 +1,8 @@ -# TODO: Change the class name to ChangedValidator when remove cm42-central-support gem -class SubjectChangedValidator < ActiveModel::EachValidator +class ChangedValidator < ActiveModel::EachValidator def validate_each(record, attribute, value) - return if record.action != 'update' + return if record.action != 'update' return if value&.valid? && value&.saved_changes.present? - + record.errors.add(attribute, message: (options[:message] || "Record didn't change")) end end diff --git a/app/validators/estimate_validator.rb b/app/validators/estimate_validator.rb new file mode 100644 index 000000000..4d7abb386 --- /dev/null +++ b/app/validators/estimate_validator.rb @@ -0,0 +1,10 @@ +class EstimateValidator < ActiveModel::EachValidator + # Checks that the estimate being validated is valid for record.project + def validate_each(record, attribute, value) + return if record.project.blank? + + return if record.project.point_values.include?(value) + + record.errors.add(attribute, message: 'is not an allowed value for this project') + end +end diff --git a/app/validators/user_belongs_to_project_validator.rb b/app/validators/user_belongs_to_project_validator.rb deleted file mode 100644 index 20c6a9ac4..000000000 --- a/app/validators/user_belongs_to_project_validator.rb +++ /dev/null @@ -1,12 +0,0 @@ -class UserBelongsToProjectValidator < ActiveModel::EachValidator - # TODO: Change de name class to BelongsToProjectValidator when remove support gem - # Checks that the parameter being validated represents a User#id that - # is a member of the object.project - def validate_each(record, attribute, value) - if record.project && !value.nil? - unless record.project.user_ids.include?(value) - record.errors.add(attribute, message: 'user is not a member of this project') - end - end - end -end