mirror of
https://github.com/Retrospring/retrospring.git
synced 2025-03-16 06:50:00 +01:00
notifications work now!
This commit is contained in:
parent
6b75a40afa
commit
ab9347f053
5 changed files with 51 additions and 0 deletions
|
@ -3,4 +3,8 @@ class Answer < ActiveRecord::Base
|
||||||
belongs_to :question
|
belongs_to :question
|
||||||
has_many :comments, dependent: :destroy
|
has_many :comments, dependent: :destroy
|
||||||
has_many :smiles, dependent: :destroy
|
has_many :smiles, dependent: :destroy
|
||||||
|
|
||||||
|
def notification_type(*_args)
|
||||||
|
Notifications::QuestionAnswered
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -6,6 +6,7 @@ class Inbox < ActiveRecord::Base
|
||||||
answer = Answer.create!(content: answer,
|
answer = Answer.create!(content: answer,
|
||||||
user: user,
|
user: user,
|
||||||
question: self.question)
|
question: self.question)
|
||||||
|
Notification.notify self.question.user, answer
|
||||||
user.increment! :answered_count
|
user.increment! :answered_count
|
||||||
self.question.increment! :answer_count
|
self.question.increment! :answer_count
|
||||||
self.destroy
|
self.destroy
|
||||||
|
|
|
@ -1,2 +1,29 @@
|
||||||
class Notification < ActiveRecord::Base
|
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
|
end
|
||||||
|
|
|
@ -21,6 +21,7 @@ class User < ActiveRecord::Base
|
||||||
has_many :followers, through: :passive_relationships, source: :source
|
has_many :followers, through: :passive_relationships, source: :source
|
||||||
has_many :smiles
|
has_many :smiles
|
||||||
has_many :services
|
has_many :services
|
||||||
|
has_many :notifications, foreign_key: :recipient_id
|
||||||
|
|
||||||
SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/
|
SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/
|
||||||
WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i
|
WEBSITE_REGEX = /https?:\/\/([A-Za-z.\-]+)\/?(?:.*)/i
|
||||||
|
|
|
@ -2,6 +2,24 @@
|
||||||
= render 'notification_tabs'
|
= render 'notification_tabs'
|
||||||
.col-md-9.col-xs-12.col-sm-9
|
.col-md-9.col-xs-12.col-sm-9
|
||||||
%ul.list-group
|
%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
|
%li.list-group-item
|
||||||
.media
|
.media
|
||||||
.pull-left
|
.pull-left
|
||||||
|
|
Loading…
Reference in a new issue