-
Notifications
You must be signed in to change notification settings - Fork 29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Notifications system for Evans #163
base: master
Are you sure you want to change the base?
Changes from all commits
4a62274
beed49b
e6df792
bda8425
68c5d28
e9f6fa8
85d5a87
da6d7f1
b241e98
ef45e2b
afd096f
e7bd240
27527cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class NotificationsController < ApplicationController | ||
def index | ||
end | ||
|
||
def show | ||
notification = Notification.find params[:id] | ||
notification.mark_as_read | ||
redirect_to notification.source | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ class Challenge < ActiveRecord::Base | |
|
||
validates :name, :description, presence: true | ||
|
||
after_create :post_notification | ||
|
||
class << self | ||
def in_chronological_order | ||
order('created_at ASC') | ||
|
@@ -16,4 +18,8 @@ def visible | |
def closed? | ||
closes_at.past? | ||
end | ||
end | ||
|
||
def post_notification | ||
Notification.create_notifications_for self, to: User.all, title: "Новo предизвикателство: #{name}" | ||
end | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Един празен ред на края на файла. |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class Notification < ActiveRecord::Base | ||
belongs_to :user | ||
belongs_to :source, polymorphic: true | ||
|
||
def mark_as_read | ||
self.read = true | ||
save! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Erghm, |
||
end | ||
|
||
class << self | ||
def unread_for_user(user) | ||
where(user_id: user.id, read: false) | ||
end | ||
|
||
def create_notifications_for(source, to: nil, title: nil) | ||
unless to.nil? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Даже, Маха едно ниво на влагане. |
||
to.find_each do |user| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тука аз suggest-нах това prematurely, очаквайки, че може да се случат повечко събития. Това всъщност на дали ще е така, пък и повече неща отговарят на |
||
Notification.create do |notification| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
notification.title = title | ||
notification.source = source | ||
notification.user = user | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,13 @@ | ||
class Reply < Post | ||
belongs_to :topic | ||
belongs_to :user | ||
|
||
attr_accessible :body | ||
|
||
validates :topic_id, presence: true | ||
|
||
after_create :post_notification | ||
|
||
def topic_title | ||
topic.title | ||
end | ||
|
@@ -13,4 +16,10 @@ def page_in_topic | |
replies_before_this = topic.replies.where('id < ?', id).count | ||
replies_before_this / Reply.per_page + 1 | ||
end | ||
|
||
private | ||
|
||
def post_notification | ||
Notification.create_notifications_for topic, to: topic.participants, title: "Нов отговор в тема: #{topic_title}" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Като го се замисля, това няма ли да създаде известие за човека, който е пуснал отговора? |
||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
class Topic < Post | ||
has_many :replies, -> { order 'created_at ASC' } | ||
|
||
has_many :participants, -> { uniq }, through: :replies, source: :user | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Вече тази релация ми стана сложна. Струва ми се, че ще е по-добра като метод. |
||
|
||
attr_accessible :title, :body | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
%h1 Известия | ||
|
||
%p | ||
Общо известия: | ||
%strong= @notifications.count | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ✂️ |
||
%table | ||
%tbody | ||
- @notifications.each do |notification| | ||
%tr | ||
%td.name.notification-name= link_to notification.title, notification |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class CreateNotifications < ActiveRecord::Migration | ||
include ForeignKeys | ||
|
||
def self.up | ||
create_table(:notifications) do |t| | ||
t.string :title, null: false | ||
t.references :source, polymorphic: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
t.boolean :read, default: false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
t.references :user | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
t.timestamps | ||
end | ||
|
||
foreign_key :notifications, :user_id | ||
end | ||
|
||
def self.down | ||
drop_table :notifications | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# language: bg | ||
Функционалност: Известия | ||
Студентите получават известия, когато бъде публикувана задача, | ||
предизвикателсвто или отговор във форум тема | ||
|
||
Сценарий: Получаване на известие при публикуване на задача | ||
Дадено че съм влязъл като администратор | ||
Когато създам задача: | ||
| Име | Първа задача | | ||
| Условие | Напишете програма | | ||
То трябва да съществува известие "Нова задача: Първа задача" | ||
|
||
Сценарий: Получаване на известие при публикуване на предизвикателство | ||
Дадено че съм влязъл като администратор | ||
Когато създам предизвикателство "Четене на субтитри" | ||
То трябва да съществува известие "Новo предизвикателство: Четене на субтитри" | ||
|
||
Сценарий: Получаване на известие при публикуване на отговор в тема | ||
Дадено че съм студент | ||
Дадено че съществува тема "Въпрос" | ||
И че темата "Въпрос" има "1" отговора, последния от които на "Петър Петров" | ||
Когато отговоря на "Въпрос" с: | ||
""" | ||
Моят текст | ||
""" | ||
То трябва да съществува известие "Нов отговор в тема: Въпрос" | ||
|
||
Сценарий: Потребителите виждат само непрочетени известия | ||
Дадено че съм студент | ||
Дадено че съществува известие "Прочетено известие" | ||
Дадено че съществува непрочетено известие "Ново непрочетено известие" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Дадено/И/И, не Дадено/Дадено/Дадено. Ditto за другите места. |
||
Когато отворя страницата с известията | ||
То трябва да виждам известие "Ново непрочетено известие" | ||
И трябва да не виждам известие "Прочетено известие" | ||
И броя на новите известия трябва да е 1 | ||
|
||
Сценарий: Когато избера ново известие съм пренасочен към страницата на първоизточника | ||
Дадено че съм влязъл като администратор | ||
Когато създам задача: | ||
| Име | Първа задача | | ||
| Условие | Напишете програма | | ||
И отворя страницата с известията | ||
И избера известието "Нова задача: Първа задача" | ||
То трябва да съм на задачата "Първа задача" | ||
И броя на новите известия трябва да е 0 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
Дадено 'че съществува известие "$title"' do |title| | ||
create :notification, title: title, user_id: current_user.id, read: true | ||
end | ||
|
||
Дадено 'че съществува непрочетено известие "$title"' do |title| | ||
create :notification, title: title, user_id: current_user.id, read: false | ||
end | ||
|
||
Когато 'отворя страницата с известията' do | ||
visit notifications_path | ||
end | ||
|
||
Когато 'избера известието "$title"' do |title| | ||
notification = Notification.find_by_title! title | ||
visit notification_path(notification) | ||
end | ||
|
||
То 'трябва да виждам известие "$title"' do |title| | ||
within ".notification-name" do | ||
page.should have_content title | ||
end | ||
end | ||
|
||
То 'трябва да не виждам известие "$title"' do |title| | ||
within ".notification-name" do | ||
page.should_not have_content title | ||
end | ||
end | ||
|
||
То 'броя на новите известия трябва да е $count' do |count| | ||
within ".notifications-link" do | ||
page.should have_content "(#{count})" | ||
end | ||
end | ||
|
||
То 'трябва да съществува известие "$title"' do |title| | ||
notification = Notification.find_by_title! title | ||
notification.should_not be_blank | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'spec_helper' | ||
|
||
describe NotificationsController do | ||
describe "GET index" do | ||
it "assigns unread user notifications to @notifications" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тоя тест е куц, защото контролена не прави това, което пише в името на теста. |
||
controller.stub_chain(:current_user).and_return(:user) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Има хелпър за това някъде. |
||
Notification.should_receive(:unread_for_user).with(:user).and_return('notifications') | ||
get :index | ||
assigns(:notifications).should eq 'notifications' | ||
end | ||
end | ||
|
||
describe "GET show" do | ||
notification = FactoryGirl.build_stubbed(:notification) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you even |
||
|
||
it "marks notification as read and redirects to its source" do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Тук не тестваш, че notification-а се маркира като прочетен. Отделно, това са два теста it "marks the notification as read"
it "redirects to the notification's source" |
||
Notification.should_receive(:find).with('3').and_return(notification) | ||
get :show, id: '3' | ||
response.should redirect_to notification.source | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
require 'spec_helper' | ||
|
||
describe Notification do | ||
it "can be marked as read" do | ||
notification = create(:notification) | ||
notification.mark_as_read | ||
notification.read.should eq true | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,5 +44,10 @@ | |
config.include EmailSpec::Helpers, type: :mailer | ||
config.include EmailSpec::Matchers, type: :mailer | ||
config.include CustomPaths, type: :mailer | ||
config.include Devise::TestHelpers, :type => :controller | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also nope. |
||
|
||
config.before(:each, type: :controller) do | ||
allow(Notification).to receive(:unread_for_user).and_return([]) | ||
end | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nope. |
||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Повтарям: такива глобални instance variable-и са гадни. Замени го с: