Skip to content

Commit

Permalink
CV2-3177: fix newsletter validation
Browse files Browse the repository at this point in the history
  • Loading branch information
melsawy committed Sep 11, 2023
1 parent 632e501 commit 74a8f73
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
6 changes: 3 additions & 3 deletions app/models/tipline_newsletter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ class TiplineNewsletter < ApplicationRecord
validates_inclusion_of :language, in: ->(newsletter) { newsletter.team.get_languages.to_a }
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_presence_of :send_on, if: ->(newsletter) { newsletter.content_type == 'static' }
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
obj.validate :not_scheduled_for_the_past, unless: proc { |newsletter| newsletter.time.blank? || newsletter.timezone.blank? }
end

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

def parsed_timezone
timezone = self.timezone
Expand Down
15 changes: 15 additions & 0 deletions test/models/tipline_newsletter_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -463,4 +463,19 @@ def teardown
)
assert_equal '2024-01-01 06:00', newsletter.scheduled_time.strftime("%Y-%m-%d %H:%M")
end

test "should validate some fields only for enabled newsletter" do
assert_difference 'TiplineNewsletter.count' do
create_tipline_newsletter enabled: false, send_on: nil, time: nil, timezone: nil, send_every: nil
end
assert_raises ActiveRecord::RecordInvalid do
create_tipline_newsletter send_on: nil, time: nil, timezone: nil, send_every: nil
end
assert_raises ActiveRecord::RecordInvalid do
create_tipline_newsletter send_on: nil, send_every: nil
end
assert_raises ActiveRecord::RecordInvalid do
create_tipline_newsletter send_every: nil
end
end
end

0 comments on commit 74a8f73

Please sign in to comment.