notifications work now!

This commit is contained in:
nilsding 2014-12-14 14:34:51 +01:00
parent 6b75a40afa
commit ab9347f053
5 changed files with 51 additions and 0 deletions

View File

@ -3,4 +3,8 @@ class Answer < ActiveRecord::Base
belongs_to :question
has_many :comments, dependent: :destroy
has_many :smiles, dependent: :destroy
def notification_type(*_args)
Notifications::QuestionAnswered
end
end

View File

@ -6,6 +6,7 @@ class Inbox < ActiveRecord::Base
answer = Answer.create!(content: answer,
user: user,
question: self.question)
Notification.notify self.question.user, answer
user.increment! :answered_count
self.question.increment! :answer_count
self.destroy

View File

@ -1,2 +1,29 @@
class Notification < ActiveRecord::Base
belongs_to :recipient, class_name: 'User'
belongs_to :target, :polymorphic => true
class << self
def for(recipient, options={})
self.where(options.merge!(recipient: recipient)).order(:updated_at).reverse_order
end
def notify(recipient, target)
return nil unless target.respond_to? :notification_type
notif_type = target.notification_type
return nil unless notif_type
make_notification(recipient, target, notif_type)
end
private
def make_notification(recipient, target, notification_type)
n = notification_type.new(target: target,
recipient: recipient,
new: true)
n.save!
n
end
end
end

View File

@ -21,6 +21,7 @@ class User < ActiveRecord::Base
has_many :followers, through: :passive_relationships, source: :source
has_many :smiles
has_many :services
has_many :notifications, foreign_key: :recipient_id
SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/
WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i

View File

@ -2,6 +2,24 @@
= render 'notification_tabs'
.col-md-9.col-xs-12.col-sm-9
%ul.list-group
- Notification.for(current_user).each do |notification|
%li.list-group-item
.media
.pull-left
%img.notification--img
.media-body
- case notification.target_type
- when "Answer"
%h6.media-heading.notification--user
= notification.target.user.screen_name
%p.notification--text
answered
%a{href: show_user_answer_path(username: notification.target.user.screen_name, id: notification.target.id)}
your question
= time_ago_in_words notification.target.created_at
ago
.notification--icon
%i.fa.fa-exclamation
%li.list-group-item
.media
.pull-left