Skip to content

Commit

Permalink
Use Rails conventions for ActiveRecord wheres and orders
Browse files Browse the repository at this point in the history
When using `where` with a `joins`, you can reference the the join
table as a key in a hash in `where`.

Updated `Mailboxer::Receipt`, `Mailboxer::Notification`, and
`Mailboxer::Conversation` to use this convention where possible.

Also use a symbol and/or a hash for `order` where applicable.
  • Loading branch information
t27duck authored and tapn2it committed Dec 19, 2016
1 parent 556ad5f commit 9ea3d34
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/models/mailboxer/conversation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def originator

#First message of the conversation.
def original_message
@original_message ||= messages.order('created_at').first
@original_message ||= messages.order(:created_at).first
end

#Sender of the last message.
Expand Down
12 changes: 6 additions & 6 deletions app/models/mailboxer/notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ class Mailboxer::Notification < ActiveRecord::Base
:length => { :maximum => Mailboxer.body_max_length }

scope :recipient, lambda { |recipient|
joins(:receipts).where('mailboxer_receipts.receiver_id' => recipient.id,'mailboxer_receipts.receiver_type' => recipient.class.base_class.to_s)
joins(:receipts).where(:mailboxer_receipts => { :receiver_id => recipient.id, :receiver_type => recipient.class.base_class.to_s })
}
scope :with_object, lambda { |obj|
where('notified_object_id' => obj.id,'notified_object_type' => obj.class.to_s)
where(:notified_object_id => obj.id, :notified_object_type => obj.class.to_s)
}
scope :not_trashed, lambda {
joins(:receipts).where('mailboxer_receipts.trashed' => false)
joins(:receipts).where(:mailboxer_receipts => { :trashed => false })
}
scope :unread, lambda {
joins(:receipts).where('mailboxer_receipts.is_read' => false)
joins(:receipts).where(:mailboxer_receipts => { :is_read => false })
}
scope :global, lambda { where(:global => true) }
scope :expired, lambda { where("mailboxer_notifications.expires < ?", Time.now) }
scope :expired, lambda { where("#{Mailboxer::Notification.quoted_table_name}.expires < ?", Time.now) }
scope :unexpired, lambda {
where("mailboxer_notifications.expires is NULL OR mailboxer_notifications.expires > ?", Time.now)
where("#{Mailboxer::Notification.quoted_table_name}.expires is NULL OR #{Mailboxer::Notification.quoted_table_name}.expires > ?", Time.now)
}

class << self
Expand Down
14 changes: 5 additions & 9 deletions app/models/mailboxer/receipt.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ class Mailboxer::Receipt < ActiveRecord::Base
}
#Notifications Scope checks type to be nil, not Notification because of STI behaviour
#with the primary class (no type is saved)
scope :notifications_receipts, lambda { joins(:notification).where('mailboxer_notifications.type' => nil) }
scope :messages_receipts, lambda { joins(:notification).where('mailboxer_notifications.type' => Mailboxer::Message.to_s) }
scope :notifications_receipts, lambda { joins(:notification).where(:mailboxer_notifications => { :type => nil }) }
scope :messages_receipts, lambda { joins(:notification).where(:mailboxer_notifications => { :type => Mailboxer::Message.to_s }) }
scope :notification, lambda { |notification|
where(:notification_id => notification.id)
}
scope :conversation, lambda { |conversation|
joins(:message).where('mailboxer_notifications.conversation_id' => conversation.id)
joins(:message).where(:mailboxer_notifications => { :conversation_id => conversation.id })
}
scope :sentbox, lambda { where(:mailbox_type => "sentbox") }
scope :inbox, lambda { where(:mailbox_type => "inbox") }
Expand Down Expand Up @@ -76,12 +76,8 @@ def move_to_sentbox(options={})
end

def update_receipts(updates, options={})
ids = where(options).map { |rcp| rcp.id }
unless ids.empty?
sql = ids.map { "#{table_name}.id = ? " }.join(' OR ')
conditions = [sql].concat(ids)
Mailboxer::Receipt.where(conditions).update_all(updates)
end
ids = where(options).pluck(:id)
Mailboxer::Receipt.where(:id => ids).update_all(updates) unless ids.empty?
end
end

Expand Down

0 comments on commit 9ea3d34

Please sign in to comment.