Guest User

Untitled

a guest
Feb 20th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.83 KB | None | 0 0
  1. ## notifications_controller.rb
  2.  
  3. class NotificationsController < ApplicationController
  4.  
  5. def create
  6. notification = Notification.create :user_id => current_user.id, :discussion_id => params[:discussion_id],
  7. :discussion_type => params[:discussion_type]
  8. @discussion = notification.discussion
  9. render :partial => 'form'
  10. end
  11.  
  12. def destroy
  13. notification = Notification.find(params[:id])
  14. @discussion = notification.discussion
  15. notification.destroy
  16. render :partial => 'form'
  17. end
  18. end
  19.  
  20. ## notificaton.rb
  21.  
  22. class Notification < ActiveRecord::Base
  23. belongs_to :user
  24. belongs_to :discussion, :polymorphic => true
  25. belongs_to :forum, :class_name => 'Forum', :foreign_key => 'discussion_id'
  26. belongs_to :topic, :class_name => 'Topic', :foreign_key => 'discussion_id'
  27. end
  28.  
  29.  
  30. ## _form.rhtml
  31.  
  32. <% if logged_in? -%>
  33. Notify on update?
  34. <% discussion = @discussion if @discussion
  35. notification = discussion.notifications.find_by_user_id(current_user.id)
  36. if notification -%>
  37. Yes | <%= link_to_remote 'No',
  38. :url => { :controller => 'notifications', :action => 'destroy', :_method => :delete, :id => notification },
  39. :update => { :success => 'notifications' } %>
  40. <% else -%>
  41. <%= link_to_remote 'Yes',
  42. :url => { :controller => 'notifications', :action => 'create',
  43. :discussion_id => discussion.id, :discussion_type => discussion.class },
  44. :update => { :success => 'notifications' } %> | No
  45. <% end -%>
  46. <% end -%>
  47.  
  48.  
  49. ## 031_create_notifications.rb
  50.  
  51. class CreateNotifications < ActiveRecord::Migration
  52. def self.up
  53. create_table :notifications do |t|
  54. # t.column :name, :string
  55. t.column :user_id, :integer
  56. t.column :discussion_id, :integer
  57. t.column :discussion_type, :string
  58. end
  59. end
  60.  
  61. def self.down
  62. drop_table :notifications
  63. end
  64. end
  65.  
  66.  
  67. ## notification_test.rb
  68.  
  69. require File.dirname(__FILE__) + '/../test_helper'
  70.  
  71. class NotificationTest < Test::Unit::TestCase
  72. all_fixtures
  73.  
  74. def test_create_user_watching_forum
  75. rails = forums(:rails)
  76. rails.observers<<users(:sam)
  77. assert_equal 1, Notification.count
  78. end
  79.  
  80. def test_create_user_watching_topic
  81. sticky = topics(:sticky)
  82. sticky.observers<<users(:sam)
  83. assert_equal 1, Notification.count
  84. end
  85.  
  86. def test_remove_user_watching_forum
  87. test_create_user_watching_forum
  88. rails = Forum.find(forums(:rails).id)
  89. rails.notifications.find_by_user_id(users(:sam).id).destroy
  90. assert_equal 0, Notification.count
  91. end
  92.  
  93. def test_remove_user_watching_topic
  94. test_create_user_watching_topic
  95. sticky = Topic.find(topics(:sticky).id)
  96. sticky.notifications.find_by_user_id(users(:sam).id).destroy
  97. assert_equal 0, Notification.count
  98. end
  99.  
  100. end
Add Comment
Please, Sign In to add comment