Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class CreateComments < ActiveRecord::Migration
- def change
- create_table :comments do |t|
- t.text :text
- t.references :post, index: true, foreign_key: true
- t.references :commentable, polymorphic: true, index: true
- t.references :author, index: true
- t.timestamps null: false
- end
- add_foreign_key :comments, :users, column: :author_id
- end
- end
- class Comment < ActiveRecord::Base
- belongs_to :post
- belongs_to :author, class_name: "User"
- belongs_to :commentable, polymorphic: true
- validates :text, presence: true
- default_scope -> { order(created_at: "desc") }
- scope :persisted, lambda { where.not(id: nil) }
- end
- class CommentsController < ApplicationController
- before_action :set_post
- def create
- @comment = @post.comments.build(comment_params)
- @comment.author = current_user
- authorize @comment, :create?
- if @comment.save
- flash[:notice] = "Comment has been created."
- redirect_to @post
- else
- flash.now[:alert] = "Comment has not been created."
- render "posts/show"
- end
- end
- private
- def set_post
- @post = Post.find(params[:post_id])
- end
- def comment_params
- params.require(:comment).permit(:text)
- end
- end
- .header
- h3 New Comment
- = simple_form_for [post, comment] do |f|
- = f.input :text, label: "Comment", placeholder: "Write your comment here", input_html: { rows: 6 }
- = f.submit class: "btn btn-primary"
- <br>
- resources :posts, only: [:index, :show, :edit, :update] do
- resources :comments, only: [:create]
- end
- .
- .
- .
- #comments
- = render "comments/form", post: @post, comment: @comment
- - if @post.comments.persisted.any?
- h4
- = t(:available_comments, count: @post.comments.count)
- = render @post.comments.persisted
- - else
- p
- There are no comments for this post.
- class Role < ActiveRecord::Base
- belongs_to :user
- belongs_to :post
- def self.available_roles
- %w(manager editor viewer)
- end
- end
- class PostPolicy < ApplicationPolicy
- class Scope < Scope
- def resolve
- # scope is the argument to policy_scope, in this case the Post model.
- # none is a convenience method provided by Active Record to automatically return no records,
- # no matter what other conditions may be added later
- return scope.none if user.nil?
- return scope.all if user.admin?
- scope.joins(:roles).where(roles: {user_id: user})
- end
- end
- def show?
- user.try(:admin?) || record.has_member?(user)
- end
- def update?
- user.try(:admin?) || record.has_manager?(user) || record.has_editor?(user)
- end
- end
- class CommentPolicy < PostPolicy
- class Scope < Scope
- def resolve
- scope
- end
- end
- def create?
- user.try(:admin?) || record.post.has_manager?(user) || record.post.has_editor?(user)
- end
- end
- require "rails_helper"
- RSpec.feature "Users can comment on posts" do
- let(:user) { create(:user) }
- let(:post) { create(:post, author: user) }
- before do
- login_as(user)
- assign_role!(user, :manager, post)
- end
- scenario "with valid attributes" do
- visit post_path(post)
- fill_in "Comment", with: "Added a comment!"
- click_button "Create Comment"
- expect(page).to have_content "Comment has been created."
- within("#comments") do
- expect(page).to have_content "Added a comment!"
- end
- end
- scenario "with invalid attributes" do
- visit post_path(post)
- click_button "Create Comment"
- expect(page).to have_content "Comment has not been created."
- end
- end
- Failures:
- Failure/Error: user.try(:admin?) || record.post.has_manager?(user) || record.post.has_editor?(user)
- NoMethodError:
- undefined method `has_manager?' for nil:NilClass
Add Comment
Please, Sign In to add comment