Guest User

Untitled

a guest
Feb 21st, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.66 KB | None | 0 0
  1. class CreateComments < ActiveRecord::Migration
  2. def change
  3. create_table :comments do |t|
  4. t.text :text
  5. t.references :post, index: true, foreign_key: true
  6. t.references :commentable, polymorphic: true, index: true
  7. t.references :author, index: true
  8.  
  9. t.timestamps null: false
  10. end
  11.  
  12. add_foreign_key :comments, :users, column: :author_id
  13. end
  14. end
  15.  
  16. class Comment < ActiveRecord::Base
  17. belongs_to :post
  18. belongs_to :author, class_name: "User"
  19. belongs_to :commentable, polymorphic: true
  20.  
  21. validates :text, presence: true
  22.  
  23. default_scope -> { order(created_at: "desc") }
  24. scope :persisted, lambda { where.not(id: nil) }
  25.  
  26. end
  27.  
  28. class CommentsController < ApplicationController
  29. before_action :set_post
  30.  
  31. def create
  32. @comment = @post.comments.build(comment_params)
  33. @comment.author = current_user
  34. authorize @comment, :create?
  35.  
  36. if @comment.save
  37. flash[:notice] = "Comment has been created."
  38. redirect_to @post
  39. else
  40. flash.now[:alert] = "Comment has not been created."
  41. render "posts/show"
  42. end
  43. end
  44.  
  45. private
  46.  
  47. def set_post
  48. @post = Post.find(params[:post_id])
  49. end
  50.  
  51. def comment_params
  52. params.require(:comment).permit(:text)
  53. end
  54. end
  55.  
  56. .header
  57. h3 New Comment
  58.  
  59. = simple_form_for [post, comment] do |f|
  60. = f.input :text, label: "Comment", placeholder: "Write your comment here", input_html: { rows: 6 }
  61. = f.submit class: "btn btn-primary"
  62. <br>
  63.  
  64. resources :posts, only: [:index, :show, :edit, :update] do
  65. resources :comments, only: [:create]
  66. end
  67.  
  68. .
  69. .
  70. .
  71. #comments
  72. = render "comments/form", post: @post, comment: @comment
  73.  
  74. - if @post.comments.persisted.any?
  75. h4
  76. = t(:available_comments, count: @post.comments.count)
  77. = render @post.comments.persisted
  78. - else
  79. p
  80. There are no comments for this post.
  81.  
  82. class Role < ActiveRecord::Base
  83. belongs_to :user
  84. belongs_to :post
  85.  
  86. def self.available_roles
  87. %w(manager editor viewer)
  88. end
  89. end
  90.  
  91. class PostPolicy < ApplicationPolicy
  92.  
  93. class Scope < Scope
  94. def resolve
  95. # scope is the argument to policy_scope, in this case the Post model.
  96. # none is a convenience method provided by Active Record to automatically return no records,
  97. # no matter what other conditions may be added later
  98. return scope.none if user.nil?
  99. return scope.all if user.admin?
  100.  
  101. scope.joins(:roles).where(roles: {user_id: user})
  102. end
  103. end
  104.  
  105. def show?
  106. user.try(:admin?) || record.has_member?(user)
  107. end
  108.  
  109. def update?
  110. user.try(:admin?) || record.has_manager?(user) || record.has_editor?(user)
  111. end
  112. end
  113.  
  114. class CommentPolicy < PostPolicy
  115. class Scope < Scope
  116. def resolve
  117. scope
  118. end
  119. end
  120.  
  121. def create?
  122. user.try(:admin?) || record.post.has_manager?(user) || record.post.has_editor?(user)
  123. end
  124. end
  125.  
  126. require "rails_helper"
  127.  
  128. RSpec.feature "Users can comment on posts" do
  129. let(:user) { create(:user) }
  130. let(:post) { create(:post, author: user) }
  131.  
  132. before do
  133. login_as(user)
  134. assign_role!(user, :manager, post)
  135. end
  136.  
  137. scenario "with valid attributes" do
  138. visit post_path(post)
  139. fill_in "Comment", with: "Added a comment!"
  140. click_button "Create Comment"
  141.  
  142. expect(page).to have_content "Comment has been created."
  143.  
  144. within("#comments") do
  145. expect(page).to have_content "Added a comment!"
  146. end
  147. end
  148.  
  149. scenario "with invalid attributes" do
  150. visit post_path(post)
  151. click_button "Create Comment"
  152.  
  153. expect(page).to have_content "Comment has not been created."
  154. end
  155. end
  156.  
  157. Failures:
  158. Failure/Error: user.try(:admin?) || record.post.has_manager?(user) || record.post.has_editor?(user)
  159.  
  160. NoMethodError:
  161. undefined method `has_manager?' for nil:NilClass
Add Comment
Please, Sign In to add comment