diff --git a/Gemfile b/Gemfile index c66453a..4177192 100644 --- a/Gemfile +++ b/Gemfile @@ -36,6 +36,7 @@ gem 'sinatra', require: false gem 'whenever' gem 'dotenv' gem 'dotenv-deployment', require: 'dotenv/deployment' +gem 'redis-rails' gem 'mysql2' # Don't worry, it's for Sphinx only! gem 'thinking-sphinx' diff --git a/Gemfile.lock b/Gemfile.lock index a9c62b5..8a078e0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -342,6 +342,22 @@ GEM rdoc (4.2.2) json (~> 1.4) redis (3.3.1) + redis-actionpack (5.0.0) + actionpack (>= 4.0.0, < 6) + redis-rack (~> 2.0.0.pre) + redis-store (~> 1.2.0.pre) + redis-activesupport (5.0.1) + activesupport (>= 3, < 6) + redis-store (~> 1.2.0) + redis-rack (2.0.0.pre) + rack (> 1.5, < 3) + redis-store (~> 1.2.0.pre) + redis-rails (5.0.1) + redis-actionpack (~> 5.0.0) + redis-activesupport (~> 5.0.0) + redis-store (~> 1.2.0) + redis-store (1.2.0) + redis (>= 2.2) remotipart (1.3.0) request_store (1.3.1) require_all (1.3.3) @@ -524,6 +540,7 @@ DEPENDENCIES rails (= 4.2.6) rails_best_practices rb-fsevent + redis-rails remotipart responders rspec-rails (~> 3.0) diff --git a/app/controllers/concerns/rated.rb b/app/controllers/concerns/rated.rb index 212c77c..58a8cd2 100644 --- a/app/controllers/concerns/rated.rb +++ b/app/controllers/concerns/rated.rb @@ -2,7 +2,6 @@ module Rated extend ActiveSupport::Concern included do before_action :find_rateable, only: [:rate_inc, :rate_dec, :rate_revoke] - before_action :protect_forbidden_rate, only: [:rate_inc, :rate_dec] end def rate_inc @@ -18,8 +17,8 @@ def rate_dec end def rate_revoke - authorize @rateable, :rate_revoke? - return head :forbidden unless current_user? || rate_exists? + authorize @rateable, :rate? + return head :forbidden unless current_user? || own_rate_exists? @rateable.revoke_rate!(current_user) render json: json_data(true) end @@ -34,15 +33,11 @@ def current_user? @rateable.user_id == current_user.id end - def rate_exists? + def own_rate_exists? Rating.exists?(rateable: @rateable, user_id: current_user.id) end def find_rateable @rateable = controller_name.classify.constantize.find(params[:id]) end - - def protect_forbidden_rate - head :forbidden if current_user? || rate_exists? - end -end \ No newline at end of file +end diff --git a/app/jobs/mail_digest_job.rb b/app/jobs/mail_digest_job.rb index ce7f257..c53fc5d 100644 --- a/app/jobs/mail_digest_job.rb +++ b/app/jobs/mail_digest_job.rb @@ -2,7 +2,7 @@ class MailDigestJob < ActiveJob::Base queue_as :mailers def perform - question_ids = Question.where(created_at: Time.now.yesterday.all_day).order(created_at: :desc).map(&:id) + question_ids = Question.where(created_at: Time.now.yesterday.all_day).order(:created_at).map(&:id) User.find_each do |user| CustomMailer.digest(user, question_ids).deliver_later diff --git a/app/models/answer.rb b/app/models/answer.rb index d93cb72..1442a87 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -3,7 +3,7 @@ class Answer < ActiveRecord::Base include Commentable belongs_to :user - belongs_to :question + belongs_to :question, touch: true has_many :attachments, as: :attachable, dependent: :destroy accepts_nested_attributes_for :attachments, reject_if: :all_blank diff --git a/app/models/attachment.rb b/app/models/attachment.rb index 1fac4ce..9b58253 100644 --- a/app/models/attachment.rb +++ b/app/models/attachment.rb @@ -1,5 +1,5 @@ class Attachment < ActiveRecord::Base - belongs_to :attachable, polymorphic: true + belongs_to :attachable, polymorphic: true, touch: true mount_uploader :file, FileUploader diff --git a/app/models/comment.rb b/app/models/comment.rb index 3b5bd2c..b187b22 100644 --- a/app/models/comment.rb +++ b/app/models/comment.rb @@ -1,6 +1,6 @@ class Comment < ActiveRecord::Base belongs_to :user - belongs_to :commentable, polymorphic: true + belongs_to :commentable, polymorphic: true, touch: true validates :user_id, presence: true validates :body, presence: true, length: (2..2_000) diff --git a/app/models/subscription.rb b/app/models/subscription.rb index 506d0a7..e23a9b3 100644 --- a/app/models/subscription.rb +++ b/app/models/subscription.rb @@ -1,6 +1,6 @@ class Subscription < ActiveRecord::Base belongs_to :user - belongs_to :question + belongs_to :question, touch: true validates :question_id, :user_id, presence: true diff --git a/app/policies/rateable_policy.rb b/app/policies/rateable_policy.rb index b9cef5e..f7fb1d7 100644 --- a/app/policies/rateable_policy.rb +++ b/app/policies/rateable_policy.rb @@ -2,8 +2,4 @@ module RateablePolicy def rate? admin? || user? && !owner? end - - def rate_revoke? - admin? || owner? - end -end \ No newline at end of file +end diff --git a/app/views/comments/_comment.slim b/app/views/comments/_comment.slim index e41e630..ff6d134 100644 --- a/app/views/comments/_comment.slim +++ b/app/views/comments/_comment.slim @@ -1,9 +1,10 @@ -p id="comment-#{comment.id}" - = comment.body - span.little style="color:gray" - ' - ' — - => comment.user.email - ' at - = l comment.created_at, format: :short -hr style="margin:0 0 8px 0" +- cache [:comment_form, comment] do + p id="comment-#{comment.id}" + = comment.body + span.little style="color:gray" + ' + ' — + => comment.user.email + ' at + = l comment.created_at, format: :short + hr style="margin:0 0 8px 0" diff --git a/app/views/comments/_list.slim b/app/views/comments/_list.slim index 5656baf..62d7361 100644 --- a/app/views/comments/_list.slim +++ b/app/views/comments/_list.slim @@ -1,10 +1,11 @@ .comments id="#{commentable.entity}-comments-#{commentable.id}-container" - div id="#{commentable.entity}-comments-#{commentable.id}-header" style="#{commentable.comments.any? ? '' : 'display:none'}" - hr style="margin:2px 4px 0 4px" - h4 Comments: - div id="#{commentable.entity}-comments-#{commentable.id}" style="margin-left:40px" - - commentable.comments.each do |comment| - = render 'comments/comment', comment: comment + - cache [commentable, :comment_list] do + div id="#{commentable.entity}-comments-#{commentable.id}-header" style="#{commentable.comments.any? ? '' : 'display:none'}" + hr style="margin:2px 4px 0 4px" + h4 Comments: + div id="#{commentable.entity}-comments-#{commentable.id}" style="margin-left:40px" + - commentable.comments.each do |comment| + = render 'comments/comment', comment: comment - if policy(Comment).create? div id="#{commentable.entity}-comments-#{commentable.id}-errors" diff --git a/app/views/questions/_answer.slim b/app/views/questions/_answer.slim index 39ce324..3fd9ccf 100644 --- a/app/views/questions/_answer.slim +++ b/app/views/questions/_answer.slim @@ -1,35 +1,36 @@ -div id="answer-#{answer.id}" class="#{answer.starred ? 'answer-starred' : ''}" - .row - .col-xs-12 - - if answer.starred - p.best-answer - strong Best Answer: - .pull-right id="answer-buttons-#{answer.id}" - - if comet - javascript: - $("#answer-buttons-#{answer.id}").html(JST["answer_buttons"]({ - question_id: "#{answer.question.id}", - question_user_id: "#{answer.question.user.id}", - answer_id: "#{answer.id}", - answer_user_id: "#{answer.user.id}" - })); - - else - - if policy(answer).destroy? - => link_to 'Delete', answer_path(answer), id: "delete-answer-#{answer.id}", class: "btn btn-danger btn-sm", method: :delete, remote: true - - if policy(answer).edit? - => link_to 'Edit', '#', id: "edit-answer-link-#{answer.id}", class: "btn btn-info btn-sm edit-answer-link", data: {answer_id: answer.id} - - if policy(answer).star? - = link_to 'Star', star_answer_path(answer), id: "star-answer-link-#{answer.id}", class: "btn btn-warning btn-sm star-answer-link", data: {answer_id: answer.id}, method: :patch, remote: true - = render 'shared/rating', rateable: answer - blockquote - = answer.body - = render 'questions/attachments', attachable: answer - = render 'comments/list', commentable: answer, comet: false - - if policy(answer).update? - div - = form_for answer, remote: true do |f| - = f.label :body, 'Answer' - .little id="answer-errors-#{answer.id}" - = f.text_area :body, class: 'form-control' - = f.submit 'Save', class: "btn btn-info btn-sm pull-right" - hr +- cache [answer, policy(answer).edit?, policy(answer).destroy?, policy(answer).star?, comet] + div id="answer-#{answer.id}" class="#{answer.starred ? 'answer-starred' : ''}" + .row + .col-xs-12 + - if answer.starred + p.best-answer + strong Best Answer: + .pull-right id="answer-buttons-#{answer.id}" + - if comet + javascript: + $("#answer-buttons-#{answer.id}").html(JST["answer_buttons"]({ + question_id: "#{answer.question.id}", + question_user_id: "#{answer.question.user.id}", + answer_id: "#{answer.id}", + answer_user_id: "#{answer.user.id}" + })); + - else + - if policy(answer).destroy? + => link_to 'Delete', answer_path(answer), id: "delete-answer-#{answer.id}", class: 'btn btn-danger btn-sm', method: :delete, remote: true + - if policy(answer).edit? + => link_to 'Edit', '#', id: "edit-answer-link-#{answer.id}", class: 'btn btn-info btn-sm edit-answer-link', data: {answer_id: answer.id} + - if policy(answer).star? + = link_to 'Star', star_answer_path(answer), id: "star-answer-link-#{answer.id}", class: 'btn btn-warning btn-sm star-answer-link', data: {answer_id: answer.id}, method: :patch, remote: true + = render 'shared/rating', rateable: answer + blockquote + = answer.body + = render 'questions/attachments', attachable: answer + = render 'comments/list', commentable: answer, comet: false + - if policy(answer).edit? + div + = form_for answer, remote: true do |f| + = f.label :body, 'Answer' + .little id="answer-errors-#{answer.id}" + = f.text_area :body, class: 'form-control' + = f.submit 'Save', class: 'btn btn-info btn-sm pull-right' + hr diff --git a/app/views/questions/_attachments.slim b/app/views/questions/_attachments.slim index c4ae608..9e26a10 100644 --- a/app/views/questions/_attachments.slim +++ b/app/views/questions/_attachments.slim @@ -2,8 +2,9 @@ hr p | Attachments: - - attachable.attachments.each do |a| - div id="attachment-#{a.id}" - => glyph :paperclip - => link_to a.file.filename, a.file.url - = link_to '×'.html_safe, attachment_path(a), remote: true, method: :delete, id: "delete-attachment-#{a.id}", style: 'font-size:1.3em;font-weight:bold;color:red' + - attachable.attachments.each do |attachment| + - cache attachment do + div id="attachment-#{attachment.id}" + => glyph :paperclip + => link_to attachment.file.filename, attachment.file.url + = link_to '×'.html_safe, attachment_path(attachment), remote: true, method: :delete, id: "delete-attachment-#{attachment.id}", style: 'font-size:1.3em;font-weight:bold;color:red' diff --git a/app/views/questions/_question.slim b/app/views/questions/_question.slim index 4368d5e..0c9687a 100644 --- a/app/views/questions/_question.slim +++ b/app/views/questions/_question.slim @@ -1,30 +1,32 @@ -div id="question-#{question.id}" - row - .col-sm-10 - p.lead - = link_to question_path(question) - = question.topic - - if policy(question).update? - div - = form_for question, remote: true do |f| - .little id="question-errors-#{question.id}" - = f.label :topic, 'Topic' - = f.text_field :topic, class: 'form-control' - = f.label :body, 'Question' - = f.text_area :body, class: 'form-control' - = f.submit 'Save', class: "btn btn-info btn-sm pull-right" - .col-sm-2 - .pull-right id="question-buttons-#{question.id}" - - if comet - javascript: - $("#question-buttons-#{question.id}").html(JST["question_buttons"]({ - question_id: "#{question.id}", - question_user_id: "#{question.user.id}" - })); - - else - - if policy(question).edit? - = link_to 'Edit', '#', id: "edit-question-link-#{question.id}", class: "btn btn-info btn-sm edit-question-link pull-right", data: {question_id: question.id} - = render 'shared/rating', rateable: question - row - .col-xs-12 - hr +- cache [question, policy(question).update?, comet] + div id="question-#{question.id}" + .row + .col-sm-10 + p.lead + = link_to question_path(question) + = question.topic + - if policy(question).update? + div + = form_for question, remote: true do |f| + .little id="question-errors-#{question.id}" + = f.label :topic, 'Topic' + = f.text_field :topic, class: 'form-control' + = f.label :body, 'Question' + = f.text_area :body, class: 'form-control' + = f.submit 'Save', class: "btn btn-info btn-sm pull-right" + .col-sm-2 + .pull-right id="question-buttons-#{question.id}" + - if comet + javascript: + $("#question-buttons-#{question.id}").html(JST["question_buttons"]({ + question_id: "#{question.id}", + question_user_id: "#{question.user.id}" + })); + - else + - if policy(question).edit? + = link_to 'Edit', '#', id: "edit-question-link-#{question.id}", \ + class: 'btn btn-info btn-sm edit-question-link pull-right', data: {question_id: question.id} + = render 'shared/rating', rateable: question + .row + .col-xs-12 + hr diff --git a/app/views/questions/show.slim b/app/views/questions/show.slim index c8ef8dd..9ea67c2 100644 --- a/app/views/questions/show.slim +++ b/app/views/questions/show.slim @@ -1,58 +1,62 @@ -.row - .col-sm-12 - = link_to questions_path, class: "btn btn-info btn-sm pull-right" - ' Back to the Questions - -.row - .col-sm-12 - h3 = @question.topic - hr - p - = @question.body - #question-attachments - = render 'attachments', attachable: @question +- cache [@question, policy(@question).edit?, policy(@question).destroy?, policy(@answer).create?] do + .row + .col-sm-12 + = link_to questions_path, class: 'btn btn-info btn-sm pull-right' + ' Back to the Questions -.row - .col-sm-12 - = render 'comments/list', commentable: @question, comet: false + .row + .col-sm-12 + h3 = @question.topic + hr + p + = @question.body + #question-attachments + = render 'attachments', attachable: @question -.row - .col-sm-12 - hr - p.lead.pull-left Answers: - - if policy(@question).destroy? - .pull-right#delete-question-container - => link_to question_path(@question.id), id: 'delete-question', class: 'btn btn-danger', method: :delete, style: 'margin-left:1em' - ' Delete Question - - if policy(@question).subscribe? - .pull-right#subscribe-question-container - => button_to \ - @subscription ? unsubscribe_question_path(@question.id) : subscribe_question_path(@question.id), \ - method: @subscription ? :delete : :post, id: 'subscribe-question', class: 'btn btn-info' - ' #{@subscription ? 'Uns' : 'S' }ubscribe -#answers - - @question.answers.order(starred: :desc).each do |answer| - - if answer.persisted? - = render 'answer', answer: answer, comet: false + .row + .col-sm-12 + = render 'comments/list', commentable: @question, comet: false -- if policy(@answer).create? - .row#afterAnswers + .row .col-sm-12 - = form_for [@question, @answer], remote: true do |f| - .form-group - = f.label :body, 'Your answer:', class: 'lead' - #answerErrors - = f.text_area :body, class: 'form-control input new-answer-body', rows: 10, placeholder: 'Place your answer here' - h4 Answer attachments: - = f.fields_for :attachments, @answer.attachments.build do |attachment| - #attachments - = render 'attachment_fields', f: attachment - .links - = link_to_add_association 'Add attachment', f, :attachments, class: 'btn btn-sm btn-info' - button.btn.btn-primary.pull-right type="submit" Send an Answer + hr + p.lead.pull-left Answers: + - if policy(@question).destroy? + .pull-right#delete-question-container + => link_to question_path(@question.id), id: 'delete-question', class: 'btn btn-danger', method: :delete, style: 'margin-left:1em' + ' Delete Question + - cache [@subscription, policy(@question).subscribe?] do + - if policy(@question).subscribe? + .pull-right#subscribe-question-container + => button_to \ + @subscription ? unsubscribe_question_path(@question.id) : subscribe_question_path(@question.id), \ + method: @subscription ? :delete : :post, id: 'subscribe-question', class: 'btn btn-info' + ' #{@subscription ? 'Uns' : 'S' }ubscribe + #answers + - @question.answers.order(starred: :desc).each do |answer| + - if answer.persisted? + - cache answer do + = render 'answer', answer: answer, comet: false + + - if policy(@answer).create? + .row#afterAnswers + .col-sm-12 + = form_for [@question, @answer], remote: true do |f| + .form-group + = f.label :body, 'Your answer:', class: 'lead' + #answerErrors + - cache [:answer_form, @question] do + = f.text_area :body, class: 'form-control input new-answer-body', rows: 10, placeholder: 'Place your answer here' + h4 Answer attachments: + = f.fields_for :attachments, @answer.attachments.build do |attachment| + #attachments + = render 'attachment_fields', f: attachment + .links + = link_to_add_association 'Add attachment', f, :attachments, class: 'btn btn-sm btn-info' + button.btn.btn-primary.pull-right type="submit" Send an Answer = subscribe_to "/questions/#{@question.id}/answers" -= subscribe_to "/comments" += subscribe_to '/comments' coffee: ready = -> diff --git a/app/views/shared/_rating.slim b/app/views/shared/_rating.slim index 71c97f2..ac4ff71 100644 --- a/app/views/shared/_rating.slim +++ b/app/views/shared/_rating.slim @@ -1,10 +1,18 @@ -.rating - p.pull-right - span.small Rating: - |   - - if policy(rateable).rate? - => link_to glyph('plus-sign'), polymorphic_path([:rate_inc, rateable].flatten), id: "rate-inc-#{rateable.id}", method: :post, remote: true, class: 'rate ' << vh(!rateable.rated?(rateable, current_user)) - span id="rating-#{rateable.id}" = rateable.rating - - if policy(rateable).rate? - =< link_to glyph('minus-sign'), polymorphic_path([:rate_dec, rateable].flatten), id: "rate-dec-#{rateable.id}", method: :post, remote: true, class: 'rate ' << vh(!rateable.rated?(rateable, current_user)) - =< link_to glyph('remove-circle'), polymorphic_path([:rate_revoke, rateable].flatten), id: "rate-revoke-#{rateable.id}", method: :post, remote: true, class: 'rate ' << vh(rateable.rated?(rateable, current_user)) +- rated = rateable.rated?(rateable, current_user) +- cache [rateable, policy(rateable).rate?, rated] + .rating + p.pull-right + span.small Rating: + |   + - if policy(rateable).rate? + => link_to glyph('plus-sign'), polymorphic_path([:rate_inc, rateable].flatten), \ + id: "rate-inc-#{rateable.id}", method: :post, remote: true, \ + class: 'rate ' << vh(!rated) + span id="rating-#{rateable.id}" = rateable.rating + - if policy(rateable).rate? + =< link_to glyph('minus-sign'), polymorphic_path([:rate_dec, rateable].flatten), \ + id: "rate-dec-#{rateable.id}", method: :post, remote: true, \ + class: 'rate ' << vh(!rated) + =< link_to glyph('remove-circle'), polymorphic_path([:rate_revoke, rateable].flatten), \ + id: "rate-revoke-#{rateable.id}", method: :post, remote: true, \ + class: 'rate ' << vh(rated) diff --git a/circle.yml b/circle.yml index 46f7575..26cd429 100644 --- a/circle.yml +++ b/circle.yml @@ -9,3 +9,10 @@ dependencies: - gem install bundler --pre post: - bundle exec ./bin/comet + +database: + post: + - bundle exec rake ts:index + - sudo cp ./config/test.sphinx.conf /etc/sphinxsearch/sphinx.conf + - sudo sed -i -e 's,START=no,START=yes,' /etc/default/sphinxsearch + - service sphinxsearch start diff --git a/config/application.rb b/config/application.rb index 361e34f..4480c9d 100644 --- a/config/application.rb +++ b/config/application.rb @@ -41,6 +41,9 @@ class Application < Rails::Application ::Sass::Script::Value::Number.precision = [8, ::Sass::Script::Value::Number.precision].max config.active_job.queue_adapter = :sidekiq + config.cache_store = :redis_store, 'redis://localhost:6379/0/cache', { + expires_in: 3.hours + } config.generators do |g| g.helper false diff --git a/config/environments/development.rb b/config/environments/development.rb index c469ec5..2f9be19 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -11,7 +11,7 @@ # Show full error reports and disable caching. config.consider_all_requests_local = true - config.action_controller.perform_caching = false + config.action_controller.perform_caching = true config.action_mailer.perform_deliveries = true config.action_mailer.raise_delivery_errors = true diff --git a/config/environments/test.rb b/config/environments/test.rb index b9f32df..a174e41 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -42,4 +42,7 @@ # Raises error for missing translations # config.action_view.raise_on_missing_translations = true + + config.logger = Logger.new(STDOUT) + config.log_level = ENV.fetch('LEVEL', 'ERROR') end diff --git a/spec/controllers/answers_controller_spec.rb b/spec/controllers/answers_controller_spec.rb index 7a6b807..acc5ee9 100644 --- a/spec/controllers/answers_controller_spec.rb +++ b/spec/controllers/answers_controller_spec.rb @@ -202,9 +202,10 @@ let(:own_rating) { create(:answer_rating, user: user, rateable: answer) } let(:others_rating) { create(:answer_rating, user: other_user, rateable: others_answer) } + let(:own_rating_others_rateable) { create(:answer_rating, user: user, rateable: others_answer) } let(:post_rate_inc) { post :rate_inc, id: others_answer, question_id: question, js: true } let(:post_rate_dec) { post :rate_dec, id: others_answer, question_id: question, js: true } - let(:post_rate_revoke) { post :rate_revoke, id: answer, question_id: question, js: true } + let(:post_rate_revoke) { post :rate_revoke, id: others_answer, question_id: question, js: true } end end diff --git a/spec/controllers/questions_controller_spec.rb b/spec/controllers/questions_controller_spec.rb index c055178..f5e3658 100644 --- a/spec/controllers/questions_controller_spec.rb +++ b/spec/controllers/questions_controller_spec.rb @@ -197,9 +197,10 @@ let(:own_rating) { create(:question_rating, user: user, rateable: question) } let(:others_rating) { create(:question_rating, user: other_user, rateable: others_question) } + let(:own_rating_others_rateable) { create(:question_rating, user: user, rateable: others_question) } let(:post_rate_inc) { post :rate_inc, id: others_question, format: :js } let(:post_rate_dec) { post :rate_dec, id: others_question, format: :js } - let(:post_rate_revoke) { post :rate_revoke, id: question, format: :js } + let(:post_rate_revoke) { post :rate_revoke, id: others_question, format: :js } end end diff --git a/spec/features/answers/rate_answer_spec.rb b/spec/features/answers/rate_answer_spec.rb index 0ff92a4..47d189a 100644 --- a/spec/features/answers/rate_answer_spec.rb +++ b/spec/features/answers/rate_answer_spec.rb @@ -3,7 +3,7 @@ feature 'Rate the Answer', %q( To select the best Answer As a User - I want to rate other Users' Answers and revoke my votes + I want to rate other Users' Answers and revoke my ratings ) do given(:user) { create(:user) } given(:other_user) { create(:user) } @@ -67,7 +67,7 @@ end end - scenario 'can revoke own vote and rate other Answer', :js do + scenario 'can revoke own rating and rate other Answer', :js do alt_answer = create(:answer, question: question) visit question_path(question) diff --git a/spec/features/questions/rate_question_spec.rb b/spec/features/questions/rate_question_spec.rb index 613e4dd..b1e92b9 100644 --- a/spec/features/questions/rate_question_spec.rb +++ b/spec/features/questions/rate_question_spec.rb @@ -3,7 +3,7 @@ feature 'Rate the Question', %q( To select the best Question As a User - I want to rate other Users' Questions and revoke my votes + I want to rate other Users' Questions and revoke my rating ) do given(:user) { create(:user) } given(:other_user) { create(:user) } @@ -66,7 +66,7 @@ end end - scenario 'can revoke own vote and rate other Question', :js do + scenario 'can revoke own rating and rate other Question', :js do alt_question = create(:question) visit questions_path diff --git a/spec/policies/answer_policy_spec.rb b/spec/policies/answer_policy_spec.rb index 778da93..38735aa 100644 --- a/spec/policies/answer_policy_spec.rb +++ b/spec/policies/answer_policy_spec.rb @@ -15,19 +15,19 @@ it('allow user') { is_expected.to permit(user, any_answer) } end - permissions :show?, :create?, :update?, :destroy?, :rate?, :rate_revoke? do + permissions :show?, :create?, :update?, :destroy?, :rate? do it('allow admin') { is_expected.to permit(create(:user, admin: true), any_answer) } end - permissions :create?, :update?, :destroy?, :rate?, :rate_revoke?, :star? do + permissions :create?, :update?, :destroy?, :rate?, :star? do it('deny guest') { is_expected.not_to permit(nil, any_answer) } end - permissions :update?, :destroy?, :rate_revoke? do + permissions :update?, :destroy? do it('allow author') { is_expected.to permit(user, users_answer) } end - permissions :update?, :destroy?, :rate_revoke?, :star? do + permissions :update?, :destroy?, :star? do it('deny user') { is_expected.not_to permit(user, any_answer) } end diff --git a/spec/policies/question_policy_spec.rb b/spec/policies/question_policy_spec.rb index 2299958..03568d5 100644 --- a/spec/policies/question_policy_spec.rb +++ b/spec/policies/question_policy_spec.rb @@ -15,15 +15,15 @@ it('allow user') { is_expected.to permit(user, any_question) } end - permissions :show?, :create?, :update?, :destroy?, :rate?, :rate_revoke?, :subscribe? do + permissions :show?, :create?, :update?, :destroy?, :rate?, :subscribe? do it('allow admin') { is_expected.to permit(create(:user, admin: true), any_question) } end - permissions :create?, :update?, :destroy?, :rate?, :rate_revoke?, :subscribe? do + permissions :create?, :update?, :destroy?, :rate?, :subscribe? do it('deny guest') { is_expected.not_to permit(nil, any_question) } end - permissions :update?, :destroy?, :rate_revoke? do + permissions :update?, :destroy? do it('allow author') { is_expected.to permit(user, users_question) } it('deny user') { is_expected.not_to permit(user, any_question) } end diff --git a/spec/support/shared_examples/controllers/rated.rb b/spec/support/shared_examples/controllers/rated.rb index 4553192..11955c3 100644 --- a/spec/support/shared_examples/controllers/rated.rb +++ b/spec/support/shared_examples/controllers/rated.rb @@ -46,22 +46,22 @@ describe 'POST #rate_revoke' do before do own_rating - others_rating + own_rating_others_rateable end - it 'assigns @rateable to rateadle instance' do + it 'assigns @rateable to rateable instance' do post_rate_revoke - expect(assigns(:rateable)).to eq rateable + expect(assigns(:rateable)).to eq others_rateable end - it 'doesn\'t revoke rating for other users\' rateables' do + it 'doesn\'t revoke rating for own rateable' do expect { post_rate_revoke } - .not_to change { others_rateable.rating } + .not_to change { rateable.rating } end - it 'revokes rate for own rateables' do + it 'revokes rating for other users\' rateable' do expect { post_rate_revoke } - .to change { rateable.rating }.by(-1) + .to change { others_rateable.rating }.by(-1) end it 'responses with HTTP status 200' do