limits maximum number of messages able to send per day

This commit is contained in:
rgarcia
2016-06-16 11:10:26 +02:00
parent fd55817832
commit 25ebb325d1
7 changed files with 143 additions and 1 deletions

View File

@@ -1,4 +1,22 @@
class DirectMessage < ActiveRecord::Base class DirectMessage < ActiveRecord::Base
belongs_to :sender, class_name: 'User', foreign_key: 'sender_id' belongs_to :sender, class_name: 'User', foreign_key: 'sender_id'
belongs_to :receiver, class_name: 'User', foreign_key: 'receiver_id' belongs_to :receiver, class_name: 'User', foreign_key: 'receiver_id'
validates :title, presence: true
validates :body, presence: true
validates :sender, presence: true
validates :receiver, presence: true
validate :max_per_day
scope :today, lambda { where('DATE(created_at) = ?', Date.today) }
def max_per_day
return if errors.any?
max = Setting[:direct_message_max_per_day]
if sender.direct_messages_sent.today.count >= max.to_i
errors.add(:title, I18n.t('activerecord.errors.models.direct_message.attributes.max_per_day.invalid', max: max))
end
end
end end

View File

@@ -23,7 +23,8 @@ class User < ActiveRecord::Base
has_many :spending_proposals, foreign_key: :author_id has_many :spending_proposals, foreign_key: :author_id
has_many :failed_census_calls has_many :failed_census_calls
has_many :notifications has_many :notifications
has_many :direct_messages has_many :direct_messages_sent, class_name: 'DirectMessage', foreign_key: :sender_id
has_many :direct_messages_received, class_name: 'DirectMessage', foreign_key: :receiver_id
belongs_to :geozone belongs_to :geozone
validates :username, presence: true, if: :username_required? validates :username, presence: true, if: :username_required?

View File

@@ -78,6 +78,10 @@ en:
attributes: attributes:
tag_list: tag_list:
less_than_or_equal_to: "tags must be less than or equal to %{count}" less_than_or_equal_to: "tags must be less than or equal to %{count}"
direct_message:
attributes:
max_per_day:
invalid: "You can only send a maximum of %{max} direct messages per day"
proposal: proposal:
attributes: attributes:
tag_list: tag_list:

View File

@@ -78,6 +78,10 @@ es:
attributes: attributes:
tag_list: tag_list:
less_than_or_equal_to: "los temas deben ser menor o igual que %{count}" less_than_or_equal_to: "los temas deben ser menor o igual que %{count}"
direct_message:
attributes:
max_per_day:
invalid: "Sólo puedes enviar %{max} mensajes directos por día"
proposal: proposal:
attributes: attributes:
tag_list: tag_list:

View File

@@ -77,3 +77,4 @@ Setting['banner-img.banner-img-three'] = "Banner image 3"
# Proposal notifications # Proposal notifications
Setting['proposal_notification_minimum_interval_in_days '] = 3 Setting['proposal_notification_minimum_interval_in_days '] = 3
Setting['direct_message_max_per_day '] = 3

View File

@@ -72,4 +72,33 @@ feature 'Direct messages' do
expect(page).to have_content error_message expect(page).to have_content error_message
end end
context "Limits" do
background do
Setting[:direct_message_max_per_day] = 3
end
scenario "Can only send a maximum number of direct messages per day" do
sender = create(:user, :level_two)
receiver = create(:user, :level_two)
3.times { create(:direct_message, sender: sender) }
login_as(sender)
visit user_path(receiver)
click_link "Send private message"
expect(page).to have_content "Send private message to #{receiver.name}"
fill_in 'direct_message_title', with: "Hey!"
fill_in 'direct_message_body', with: "How are you doing?"
click_button "Send message"
expect(page).to have_content "You can only send a maximum of 3 direct messages per day"
expect(page).to_not have_content "You message has been sent successfully."
end
end
end end

View File

@@ -0,0 +1,85 @@
require 'rails_helper'
describe DirectMessage do
let(:direct_message) { build(:direct_message) }
before(:each) do
Setting[:direct_message_max_per_day] = 3
end
it "should be valid" do
expect(direct_message).to be_valid
end
it "should not be valid without a title" do
direct_message.title = nil
expect(direct_message).to_not be_valid
end
it "should not be valid without a body" do
direct_message.body = nil
expect(direct_message).to_not be_valid
end
it "should not be valid without an associated sender" do
direct_message.sender = nil
expect(direct_message).to_not be_valid
end
it "should not be valid without an associated receiver" do
direct_message.receiver = nil
expect(direct_message).to_not be_valid
end
describe "maximum number of direct messages per day" do
it "should not be valid if above maximum" do
sender = create(:user)
direct_message1 = create(:direct_message, sender: sender)
direct_message2 = create(:direct_message, sender: sender)
direct_message3 = create(:direct_message, sender: sender)
direct_message4 = build(:direct_message, sender: sender)
expect(direct_message4).to_not be_valid
end
it "should be valid if below maximum" do
sender = create(:user)
direct_message1 = create(:direct_message, sender: sender)
direct_message2 = create(:direct_message, sender: sender)
direct_message3 = build(:direct_message)
expect(direct_message).to be_valid
end
it "should be valid if no direct_messages sent" do
direct_message = build(:direct_message)
expect(direct_message).to be_valid
end
end
describe "scopes" do
describe "today" do
it "should return direct messages created today" do
direct_message1 = create(:direct_message, created_at: Time.zone.now.beginning_of_day + 3.hours)
direct_message2 = create(:direct_message, created_at: Time.zone.now)
direct_message3 = create(:direct_message, created_at: Time.zone.now.end_of_day)
expect(DirectMessage.today.count).to eq 3
end
it "should not return direct messages created another day" do
direct_message1 = create(:direct_message, created_at: 1.day.ago)
direct_message2 = create(:direct_message, created_at: 1.day.from_now)
expect(DirectMessage.today.count).to eq 0
end
end
end
end