Skip to content

Commit

Permalink
Don't hardcode model / table name unless necessary
Browse files Browse the repository at this point in the history
Added a regression test because I was not sure removing the class name
from the `belongs_to` would work. It does.
  • Loading branch information
David Rodríguez authored and tapn2it committed Dec 19, 2016
1 parent 5ce88f3 commit 3f263a2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
7 changes: 4 additions & 3 deletions app/models/mailboxer/conversation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Mailboxer::Conversation < ActiveRecord::Base

scope :participant, lambda {|participant|
where('mailboxer_notifications.type'=> Mailboxer::Message.name).
order("mailboxer_conversations.updated_at DESC").
order(updated_at: :desc).
joins(:receipts).merge(Mailboxer::Receipt.recipient(participant)).distinct
}
scope :inbox, lambda {|participant|
Expand All @@ -33,10 +33,11 @@ class Mailboxer::Conversation < ActiveRecord::Base
participant(participant).merge(Mailboxer::Receipt.not_trash)
}
scope :between, lambda {|participant_one, participant_two|
joins("INNER JOIN (#{Mailboxer::Notification.recipient(participant_two).to_sql}) participant_two_notifications ON participant_two_notifications.conversation_id = mailboxer_conversations.id AND participant_two_notifications.type IN ('Mailboxer::Message')").
joins("INNER JOIN (#{Mailboxer::Notification.recipient(participant_two).to_sql}) participant_two_notifications " \
"ON participant_two_notifications.conversation_id = #{table_name}.id AND participant_two_notifications.type IN ('Mailboxer::Message')").
joins("INNER JOIN mailboxer_receipts ON mailboxer_receipts.notification_id = participant_two_notifications.id").
merge(Mailboxer::Receipt.recipient(participant_one)).
order("mailboxer_conversations.updated_at DESC").distinct
order(updated_at: :desc).distinct
}

#Mark the conversation as read for one of the participants
Expand Down
2 changes: 1 addition & 1 deletion app/models/mailboxer/message.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Mailboxer::Message < Mailboxer::Notification
attr_accessible :attachment if Mailboxer.protected_attributes?
self.table_name = :mailboxer_notifications

belongs_to :conversation, :class_name => "Mailboxer::Conversation", :validate => true, :autosave => true
belongs_to :conversation, :validate => true, :autosave => true
validates_presence_of :sender

class_attribute :on_deliver_callback
Expand Down
4 changes: 4 additions & 0 deletions spec/models/message_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
expect(@message1.is_deleted?(@entity1)).to be true
end

it "creates a conversation" do
expect(@message1.conversation).to eq(Mailboxer::Conversation.last)
end

it "should send email only to receivers" do
expect(ActionMailer::Base.deliveries.count).to eq 1
end
Expand Down

0 comments on commit 3f263a2

Please sign in to comment.