Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CV2-3177: fix newsletter validation #1649

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/graph/types/tipline_newsletter_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def send_on
field :time, GraphQL::Types::String, null: true

def time
object.time.strftime("%H:%M")
object.time ? object.time.strftime("%H:%M") : nil
end
field :subscribers_count, GraphQL::Types::Int, null: true
field :footer, GraphQL::Types::String, null: true
Expand Down
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
Loading