Skip to content

Commit

Permalink
CV2-3177: replace with_options validation with inline conditions (#1670)
Browse files Browse the repository at this point in the history
* CV2-3177: replace with_option with inline conditions

* CV2-3177: fix tests
  • Loading branch information
melsawy authored Sep 27, 2023
1 parent 6693968 commit 1ae10ed
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
16 changes: 7 additions & 9 deletions app/models/tipline_newsletter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,13 @@ class TiplineNewsletter < ApplicationRecord
validates_inclusion_of :content_type, in: ['static', 'rss']
# Should be executed only when `enabled: 1`
validates_presence_of :send_on, if: ->(newsletter) { newsletter.content_type == 'static' && newsletter.enabled }
with_options if: :enabled do |obj|
obj.validates_presence_of :time, :timezone, :introduction
obj.validates_inclusion_of :number_of_articles, in: 0..3, allow_blank: true, allow_nil: true
obj.validates :first_article, length: { maximum: proc { |newsletter| MAXIMUM_ARTICLE_LENGTH[newsletter.number_of_articles].to_i } }, allow_blank: true, allow_nil: true, if: proc { |newsletter| newsletter.number_of_articles >= 1 }
obj.validates :second_article, length: { maximum: proc { |newsletter| MAXIMUM_ARTICLE_LENGTH[newsletter.number_of_articles].to_i } }, allow_blank: true, allow_nil: true, if: proc { |newsletter| newsletter.number_of_articles >= 2 }
obj.validates :third_article, length: { maximum: proc { |newsletter| MAXIMUM_ARTICLE_LENGTH[newsletter.number_of_articles].to_i } }, allow_blank: true, allow_nil: true, if: proc { |newsletter| newsletter.number_of_articles == 3 }
obj.validate :send_every_is_a_list_of_days_of_the_week
obj.validate :not_scheduled_for_the_past, unless: proc { |newsletter| newsletter.time.blank? || newsletter.timezone.blank? }
end
validates_presence_of :time, :timezone, :introduction, if: ->(newsletter) { newsletter.enabled }
validates_inclusion_of :number_of_articles, in: 0..3, allow_blank: true, allow_nil: true, if: ->(newsletter) { newsletter.enabled }
validates :first_article, length: { maximum: proc { |newsletter| MAXIMUM_ARTICLE_LENGTH[newsletter.number_of_articles].to_i } }, allow_blank: true, allow_nil: true, if: proc { |newsletter| newsletter.number_of_articles >= 1 && newsletter.enabled }
validates :second_article, length: { maximum: proc { |newsletter| MAXIMUM_ARTICLE_LENGTH[newsletter.number_of_articles].to_i } }, allow_blank: true, allow_nil: true, if: proc { |newsletter| newsletter.number_of_articles >= 2 && newsletter.enabled }
validates :third_article, length: { maximum: proc { |newsletter| MAXIMUM_ARTICLE_LENGTH[newsletter.number_of_articles].to_i } }, allow_blank: true, allow_nil: true, if: proc { |newsletter| newsletter.number_of_articles == 3 && newsletter.enabled }
validate :send_every_is_a_list_of_days_of_the_week, if: ->(newsletter) { newsletter.enabled }
validate :not_scheduled_for_the_past, unless: proc { |newsletter| newsletter.time.blank? || newsletter.timezone.blank? || !newsletter.enabled }

after_save :reschedule_delivery, unless: proc { |newsletter| newsletter.time.blank? || newsletter.timezone.blank? }

Expand Down
1 change: 1 addition & 0 deletions test/models/tipline_newsletter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def teardown
test 'should validate maximum length of articles' do
@newsletter.content_type = 'static'
@newsletter.introduction = 'Foo'
@newsletter.enabled = true

# 1 article
@newsletter.number_of_articles = 1
Expand Down

0 comments on commit 1ae10ed

Please sign in to comment.