diff --git a/app/models/proposal_notification.rb b/app/models/proposal_notification.rb index d31b2e1a5..11bfea8ae 100644 --- a/app/models/proposal_notification.rb +++ b/app/models/proposal_notification.rb @@ -5,4 +5,13 @@ class ProposalNotification < ActiveRecord::Base validates :title, presence: true validates :body, presence: true validates :proposal, presence: true + validate :minimum_interval + + def minimum_interval + return true if proposal.notifications.blank? + if proposal.notifications.last.created_at > (Time.now - Setting[:proposal_notification_minimum_interval_in_days].to_i.days).to_datetime + errors.add(:minimum_interval, I18n.t('activerecord.errors.models.proposal_notification.minimum_interval', interval: Setting[:proposal_notification_minimum_interval])) + end + end + end \ No newline at end of file diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index 91c035dfb..1d46596c2 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -84,3 +84,6 @@ en: attributes: tag_list: less_than_or_equal_to: "tags must be less than or equal to %{count}" + proposal_notification: + attributes: + minimum_interval: "You have to wait a minium of %{interval} days between notifications" \ No newline at end of file diff --git a/config/locales/activerecord.es.yml b/config/locales/activerecord.es.yml index a7644f02e..001be7228 100644 --- a/config/locales/activerecord.es.yml +++ b/config/locales/activerecord.es.yml @@ -84,3 +84,6 @@ es: attributes: tag_list: less_than_or_equal_to: "los temas deben ser menor o igual que %{count}" + proposal_notification: + attributes: + minimum_interval: "Debes esperar un mínimo de %{interval} días entre notificaciones" \ No newline at end of file diff --git a/db/seeds.rb b/db/seeds.rb index 7555f6999..5a6db1574 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -74,3 +74,6 @@ Setting['banner-style.banner-style-three'] = "Banner style 3" Setting['banner-img.banner-img-one'] = "Banner image 1" Setting['banner-img.banner-img-two'] = "Banner image 2" Setting['banner-img.banner-img-three'] = "Banner image 3" + +# Proposal notifications +Setting['proposal_notification_minimum_interval_in_days '] = 3 diff --git a/spec/features/proposal_notifications_spec.rb b/spec/features/proposal_notifications_spec.rb index f40e0b81e..87a717ba2 100644 --- a/spec/features/proposal_notifications_spec.rb +++ b/spec/features/proposal_notifications_spec.rb @@ -84,4 +84,14 @@ feature 'Proposal Notifications' do expect(page).to have_content error_message end + context "Limits" do + + pending "Cannot send more than one notification within established interval" do + end + + pending "use timecop to make sure notifications can be sent after time interval" do + end + + end + end \ No newline at end of file diff --git a/spec/models/proposal_notification_spec.rb b/spec/models/proposal_notification_spec.rb index 5a7575219..f66b254b6 100644 --- a/spec/models/proposal_notification_spec.rb +++ b/spec/models/proposal_notification_spec.rb @@ -22,4 +22,38 @@ describe ProposalNotification do expect(notification).to_not be_valid end + describe "minimum interval between notifications" do + + before(:each) do + Setting[:proposal_notification_minimum_interval_in_days] = 3 + end + + it "should not be valid if below minium interval" do + proposal = create(:proposal) + + notification1 = create(:proposal_notification, proposal: proposal) + notification2 = build(:proposal_notification, proposal: proposal) + + proposal.reload + expect(notification2).to_not be_valid + end + + it "should be valid if notifications above minium interval" do + proposal = create(:proposal) + + notification1 = create(:proposal_notification, proposal: proposal, created_at: 4.days.ago) + notification2 = build(:proposal_notification, proposal: proposal) + + proposal.reload + expect(notification2).to be_valid + end + + it "should be valid if no notifications sent" do + notification1 = build(:proposal_notification) + + expect(notification1).to be_valid + end + + end + end