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 :content
- 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 :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 = @commentable.comments.new(comment_params)
- @comment.author = current_user
- authorize @comment, :create?
- if @comment.save
- flash[:notice] = "Comment has been created."
- redirect_to @commentable
- 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
- .
- .
- .
- #comments
- = render partial: "comments/form", locals: {commentable: @post}
- - if @post.comments.persisted.any?
- h4
- = t(:available_comments, count: @post.comments.count)
- = render partial: "comments/comments", locals: {commentable: @post}
- - else
- p
- There are no comments for this post.
- .header
- h3 New Comment
- = simple_form_for [commentable, Comment.new] do |f|
- .form-group
- = f.input :content, label: "Comment", placeholder: "Add a comment", input_html: { rows: 6 }
- = f.submit class: "btn btn-primary"
- <br>
- resources :posts, only: [:index, :show, :edit, :update]
- resources :posts, only: [] do
- resources :comments, only: [:create], module: :posts
- end
- class Posts::CommentsController < CommentsController
- before_action :set_commentable
- private
- def set_commentable
- @commentable = Post.find(params[:post_id])
- end
- end
- class Role < ActiveRecord::Base
- belongs_to :user
- belongs_to :post
- def self.available_roles
- %w(manager editor viewer)
- end
- end
- FactoryGirl.define do
- factory :comment do
- content { "A comment!" }
- trait :post_comment do
- association :commentable, factory: :post
- #commentable_type 'Post'
- association :author_id, factory: :user
- end
- end
- end
- class CommentPolicy < ApplicationPolicy
- class Scope < Scope
- def resolve
- scope
- end
- end
- def create?
- user.try(:admin?) || record.commentable.has_manager?(user)
- end
- end
- require 'rails_helper'
- RSpec.describe CommentPolicy do
- context "permissions" do
- subject { CommentPolicy.new(user, comment) }
- let(:user) { create(:user) }
- let(:post) { create(:post)}
- let(:post_comment) { create(:comment, :post_comment)}
- context "for anonymous users" do
- let(:user) { nil }
- it { should_not permit_action :create }
- end
- context "for viewers of the post_comment" do
- before { assign_role!(user, :viewer, post) }
- it { should_not permit_action :create }
- end
- context "for editors of the post" do
- before { assign_role!(user, :editor, post) }
- it { should permit_action :create }
- end
- context "for managers of the post" do
- before { assign_role!(user, :manager, post) }
- it { should permit_action :create }
- end
- context "for managers of other post" do
- before do
- assign_role!(user, :manager, create(:post))
- end
- it { should_not permit_action :create }
- end
- context "for administrators" do
- let(:user) { create(:user, :admin) }
- it { should permit_action :create }
- end
- end
- end
- `rspec spec/policies/comment_policy_spec.rb`
- Run options: exclude {:slow=>true}
- FFFFFF
- Failure/Error: subject { CommentPolicy.new(user, comment) }
- NameError:
- undefined local variable or method `comment' for #<RSpec::ExampleGroups::CommentPolicy::Permissions::.....
- Run options: exclude {:slow=>true}
- FFFFF.
- Failure/Error: user.try(:admin?) || record.commentable.has_manager?(user)
- NoMethodError:
- undefined method `commentable' for #<Post:0x007f9cc3dba328>
- Did you mean? comments
- class Comment < ActiveRecord::Base
- belongs_to :commentable, polymorphic: true
- #etc...
- end
- class Post < ActiveRecord::Base
- has_many :comments, as: :commentable, dependent: :destroy
- #etc...
- end
- belongs_to :post
- belongs_to :commentable, polymorphic: true
- @post.comments.build
- #this will set comment.commentable_id = @post.id, and comment.commentable_type = "Post"
- #but will not set comment.post_id
- scope.joins(:roles).where(roles: {user_id: user}
- scope.joins(:roles).where(roles: {user_id: user.id}
Add Comment
Please, Sign In to add comment