Files
grecia/app/models/notification.rb
rgarcia bb3c4c6399 adds consistency to ruby code style
Keep a blank line before and after private
Keep a blank line before and after protected
Remove extra empty line at class body end
Remove extra blank line
Add final newline
Use 2 (not 3) spaces for indentation
Use 2 (not 4) spaces for indentation
Remove space before comma
Add space after comma
Remove trailing whitespaces
Remove unnecessary spacing
Use snake_case for variable names
Do not use then for multi-line if
Remove unused block argument - i
Use the new Ruby 1.9 hash syntax
Remove unused assignment to variable
Indent when as deep as case
Align attributes
Align end with def
2016-11-15 11:18:43 +01:00

53 lines
1.2 KiB
Ruby

class Notification < ActiveRecord::Base
belongs_to :user, counter_cache: true
belongs_to :notifiable, polymorphic: true
scope :unread, -> { all }
scope :recent, -> { order(id: :desc) }
scope :not_emailed, -> { where(emailed_at: nil) }
scope :for_render, -> { includes(:notifiable) }
def timestamp
notifiable.created_at
end
def mark_as_read
self.destroy
end
def self.add(user_id, notifiable)
if notification = Notification.find_by(user_id: user_id, notifiable: notifiable)
Notification.increment_counter(:counter, notification.id)
else
Notification.create!(user_id: user_id, notifiable: notifiable)
end
end
def notifiable_title
case notifiable.class.name
when "ProposalNotification"
notifiable.proposal.title
when "Comment"
notifiable.commentable.title
else
notifiable.title
end
end
def notifiable_action
case notifiable_type
when "ProposalNotification"
"proposal_notification"
when "Comment"
"replies_to"
else
"comments_on"
end
end
def linkable_resource
notifiable.is_a?(ProposalNotification) ? notifiable.proposal : notifiable
end
end