From 27ece220f6e368727029ec3a4233964bab2e296b Mon Sep 17 00:00:00 2001 From: kikito Date: Mon, 31 Aug 2015 19:02:50 +0200 Subject: [PATCH 1/7] Transform Flag.flag! & unflag! into non-raising methods They now return false and do nothing instead --- app/controllers/comments_controller.rb | 4 ++-- app/controllers/debates_controller.rb | 4 ++-- app/models/flag.rb | 22 ++++------------------ spec/factories.rb | 4 ++-- spec/features/comments_spec.rb | 2 +- spec/features/debates_spec.rb | 8 ++++---- spec/models/ability_spec.rb | 4 ++-- spec/models/flag_spec.rb | 26 +++++++++++++------------- 8 files changed, 30 insertions(+), 44 deletions(-) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index c342b8d10..e792240f8 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -23,12 +23,12 @@ class CommentsController < ApplicationController end def flag - Flag.flag!(current_user, @comment) + Flag.flag(current_user, @comment) respond_with @comment, template: 'comments/_refresh_flag_actions' end def unflag - Flag.unflag!(current_user, @comment) + Flag.unflag(current_user, @comment) respond_with @comment, template: 'comments/_refresh_flag_actions' end diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index 9e5247134..2f660c1c7 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -55,12 +55,12 @@ class DebatesController < ApplicationController end def flag - Flag.flag!(current_user, @debate) + Flag.flag(current_user, @debate) respond_with @debate, template: 'debates/_refresh_flag_actions' end def unflag - Flag.unflag!(current_user, @debate) + Flag.unflag(current_user, @debate) respond_with @debate, template: 'debates/_refresh_flag_actions' end diff --git a/app/models/flag.rb b/app/models/flag.rb index d2f30d284..8d54c2dc7 100644 --- a/app/models/flag.rb +++ b/app/models/flag.rb @@ -9,28 +9,14 @@ class Flag < ActiveRecord::Base flaggable_id: flaggable.id) end) - - class AlreadyFlaggedError < StandardError - def initialize - super "The flaggable was already flagged by this user" - end - end - - class NotFlaggedError < StandardError - def initialize - super "The flaggable was not flagged by this user" - end - end - - - def self.flag!(user, flaggable) - raise AlreadyFlaggedError if flagged?(user, flaggable) + def self.flag(user, flaggable) + return false if flagged?(user, flaggable) create(user: user, flaggable: flaggable) end - def self.unflag!(user, flaggable) + def self.unflag(user, flaggable) flags = by_user_and_flaggable(user, flaggable) - raise NotFlaggedError if flags.empty? + return false if flags.empty? flags.destroy_all end diff --git a/spec/factories.rb b/spec/factories.rb index 7a85a0193..344b178a8 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -68,7 +68,7 @@ FactoryGirl.define do trait :flagged do after :create do |debate| - Flag.flag!(FactoryGirl.create(:user), debate) + Flag.flag(FactoryGirl.create(:user), debate) end end end @@ -101,7 +101,7 @@ FactoryGirl.define do trait :flagged do after :create do |debate| - Flag.flag!(FactoryGirl.create(:user), debate) + Flag.flag(FactoryGirl.create(:user), debate) end end end diff --git a/spec/features/comments_spec.rb b/spec/features/comments_spec.rb index 9814a4b33..c40863534 100644 --- a/spec/features/comments_spec.rb +++ b/spec/features/comments_spec.rb @@ -155,7 +155,7 @@ feature 'Comments' do user = create(:user) debate = create(:debate) comment = create(:comment, commentable: debate) - Flag.flag!(user, comment) + Flag.flag(user, comment) login_as(user) visit debate_path(debate) diff --git a/spec/features/debates_spec.rb b/spec/features/debates_spec.rb index da966f7a3..f6e07b844 100644 --- a/spec/features/debates_spec.rb +++ b/spec/features/debates_spec.rb @@ -335,7 +335,7 @@ feature 'Debates' do scenario "Unflagging", :js do user = create(:user) debate = create(:debate) - Flag.flag!(user, debate) + Flag.flag(user, debate) login_as(user) visit debate_path(debate) @@ -432,9 +432,9 @@ feature 'Debates' do expect(current_url).to include('tag=Deporte') expect(page).to have_selector('#debates .debate', count: 2) - expect(page).to_not have_content(debate3.title) - expect(page).to have_content(debate1.title) - expect(page).to have_content(debate2.title) + expect(page).to_not have_content(debate3.title) + expect(page).to have_content(debate1.title) + expect(page).to have_content(debate2.title) end end end diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 06e2a9a62..43dc06a23 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -52,14 +52,14 @@ describe Ability do end describe "already-flagged comments" do - before(:each) { Flag.flag!(user, comment) } + before(:each) { Flag.flag(user, comment) } it { should_not be_able_to(:flag, comment) } it { should be_able_to(:unflag, comment) } end describe "already-flagged debates" do - before(:each) { Flag.flag!(user, debate) } + before(:each) { Flag.flag(user, debate) } it { should_not be_able_to(:flag, debate) } it { should be_able_to(:unflag, debate) } diff --git a/spec/models/flag_spec.rb b/spec/models/flag_spec.rb index a5725e7c8..248bd04e1 100644 --- a/spec/models/flag_spec.rb +++ b/spec/models/flag_spec.rb @@ -5,41 +5,41 @@ describe Flag do let(:user) { create(:user) } let(:comment) { create(:comment) } - describe '.flag!' do + describe '.flag' do it 'creates a flag when there is none' do - expect { described_class.flag!(user, comment) }.to change{ Flag.count }.by(1) + expect { described_class.flag(user, comment) }.to change{ Flag.count }.by(1) expect(Flag.last.user).to eq(user) expect(Flag.last.flaggable).to eq(comment) end - it 'raises an error if the flag has already been created' do - described_class.flag!(user, comment) - expect { described_class.flag!(user, comment) }.to raise_error(Flag::AlreadyFlaggedError) + it 'does nothing if the flag already exists' do + described_class.flag(user, comment) + expect(described_class.flag(user, comment)).to eq(false) + expect(Flag.by_user_and_flaggable(user, comment).count).to eq(1) end it 'increases the flag count' do - expect { described_class.flag!(user, comment) }.to change{ comment.reload.flags_count }.by(1) + expect { described_class.flag(user, comment) }.to change{ comment.reload.flags_count }.by(1) end end - describe '.unflag!' do + describe '.unflag' do it 'raises an error if the flag does not exist' do - expect { described_class.unflag!(user, comment) }.to raise_error(Flag::NotFlaggedError) + expect(described_class.unflag(user, comment)).to eq(false) end describe 'when the flag already exists' do - before(:each) { described_class.flag!(user, comment) } + before(:each) { described_class.flag(user, comment) } it 'removes an existing flag' do - expect { described_class.unflag!(user, comment) }.to change{ Flag.count }.by(-1) + expect { described_class.unflag(user, comment) }.to change{ Flag.count }.by(-1) end it 'decreases the flag count' do - expect { described_class.unflag!(user, comment) }.to change{ comment.reload.flags_count }.by(-1) + expect { described_class.unflag(user, comment) }.to change{ comment.reload.flags_count }.by(-1) end end - end describe '.flagged?' do @@ -48,7 +48,7 @@ describe Flag do end it 'returns true when the user has flagged the comment' do - described_class.flag!(user, comment) + described_class.flag(user, comment) expect(described_class.flagged?(user, comment)).to be end end From 6902d2851b35fa0c875cfde6122fdbf4d3dfcd77 Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 1 Sep 2015 10:33:11 +0200 Subject: [PATCH 2/7] Takes already-flagged-content out of Ability's concerns --- app/models/ability.rb | 18 ++++-------------- spec/models/ability_spec.rb | 19 +++---------------- 2 files changed, 7 insertions(+), 30 deletions(-) diff --git a/app/models/ability.rb b/app/models/ability.rb index d8436a700..0d2ac3f8b 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -22,21 +22,11 @@ class Ability can :create, Comment can :create, Debate - can :flag, Comment do |comment| - comment.author_id != user.id && !Flag.flagged?(user, comment) - end + can [:flag, :unflag], Comment + cannot [:flag, :unflag], Comment, user_id: user.id - can :unflag, Comment do |comment| - comment.author_id != user.id && Flag.flagged?(user, comment) - end - - can :flag, Debate do |debate| - debate.author_id != user.id && !Flag.flagged?(user, debate) - end - - can :unflag, Debate do |debate| - debate.author_id != user.id && Flag.flagged?(user, debate) - end + can [:flag, :unflag], Debate + cannot [:flag, :unflag], Debate, author_id: user.id unless user.organization? can :vote, Debate diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index 43dc06a23..f106ef14e 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -33,9 +33,10 @@ describe Ability do describe 'flagging content' do it { should be_able_to(:flag, debate) } - it { should_not be_able_to(:unflag, debate) } + it { should be_able_to(:unflag, debate) } + it { should be_able_to(:flag, comment) } - it { should_not be_able_to(:unflag, comment) } + it { should be_able_to(:unflag, comment) } describe "own comments" do let(:own_comment) { create(:comment, author: user) } @@ -50,20 +51,6 @@ describe Ability do it { should_not be_able_to(:flag, own_debate) } it { should_not be_able_to(:unflag, own_debate) } end - - describe "already-flagged comments" do - before(:each) { Flag.flag(user, comment) } - - it { should_not be_able_to(:flag, comment) } - it { should be_able_to(:unflag, comment) } - end - - describe "already-flagged debates" do - before(:each) { Flag.flag(user, debate) } - - it { should_not be_able_to(:flag, debate) } - it { should be_able_to(:unflag, debate) } - end end describe "other users" do From 005fd4c09a05b72db760eb99d51c2debcfa468b6 Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 1 Sep 2015 10:34:46 +0200 Subject: [PATCH 3/7] Replaces can? :flag/:unflag by a helper function --- app/helpers/flags_helper.rb | 23 +++++++++++++++++++++++ app/views/comments/_flag_actions.html.erb | 4 ++-- app/views/debates/_flag_actions.html.erb | 4 ++-- 3 files changed, 27 insertions(+), 4 deletions(-) create mode 100644 app/helpers/flags_helper.rb diff --git a/app/helpers/flags_helper.rb b/app/helpers/flags_helper.rb new file mode 100644 index 000000000..3a8bfbc75 --- /dev/null +++ b/app/helpers/flags_helper.rb @@ -0,0 +1,23 @@ +module FlagsHelper + def show_flag_action?(flaggable) + current_user && !own_flaggable?(flaggable) && !flagged?(flaggable) + end + + def show_unflag_action?(flaggable) + current_user && !own_flaggable?(flaggable) && flagged?(flaggable) + end + + private + + def flagged?(flaggable) + Flag.flagged?(current_user, flaggable) + end + + def own_flaggable?(flaggable) + if flaggable.is_a? Comment + flaggable.user_id == current_user.id + else + flaggable.author_id == current_user.id + end + end +end diff --git a/app/views/comments/_flag_actions.html.erb b/app/views/comments/_flag_actions.html.erb index f1557668c..c056204e7 100644 --- a/app/views/comments/_flag_actions.html.erb +++ b/app/views/comments/_flag_actions.html.erb @@ -1,4 +1,4 @@ -<% if can? :flag, comment %> +<% if show_flag_action? comment %>  |  @@ -9,7 +9,7 @@ <% end %> -<% if can? :unflag, debate %> +<% if show_unflag_action? debate %> From 9a071d2ed7b3110dbbaaf15961b80b72e3893bd5 Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 1 Sep 2015 13:07:03 +0200 Subject: [PATCH 4/7] Renames @voted_values to @debate_votes in controllers --- app/controllers/application_controller.rb | 2 +- app/helpers/votes_helper.rb | 6 +++--- app/views/debates/_votes.html.erb | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 07876767b..0ddb59bb3 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -68,7 +68,7 @@ class ApplicationController < ActionController::Base end def set_debate_votes(debates) - @voted_values = current_user ? current_user.debate_votes(debates) : {} + @debate_votes = current_user ? current_user.debate_votes(debates) : {} end def ensure_signup_complete diff --git a/app/helpers/votes_helper.rb b/app/helpers/votes_helper.rb index 11d39e898..dfc1bbda0 100644 --- a/app/helpers/votes_helper.rb +++ b/app/helpers/votes_helper.rb @@ -1,7 +1,7 @@ module VotesHelper - def css_classes_for_debate_vote(voted_values, debate) - case voted_values[debate.id] + def css_classes_for_debate_vote(debate_votes, debate) + case debate_votes[debate.id] when true {in_favor: "voted", against: "no-voted"} when false @@ -11,4 +11,4 @@ module VotesHelper end end -end \ No newline at end of file +end diff --git a/app/views/debates/_votes.html.erb b/app/views/debates/_votes.html.erb index 4c252c8b5..462353ff9 100644 --- a/app/views/debates/_votes.html.erb +++ b/app/views/debates/_votes.html.erb @@ -1,4 +1,4 @@ -<% voted_classes = css_classes_for_debate_vote(@voted_values, debate) %> +<% voted_classes = css_classes_for_debate_vote(@debate_votes, debate) %>
<%= link_to vote_debate_path(debate, value: 'yes'), @@ -28,4 +28,4 @@ signup: link_to(t("votes.signup"), new_user_registration_path)).html_safe %>
<% end %> -
\ No newline at end of file + From c2faf0ef925594cdee42728efa194b2861ae4a30 Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 1 Sep 2015 13:08:32 +0200 Subject: [PATCH 5/7] Merge Vote.in with Vote.for_debates --- app/models/user.rb | 4 +-- config/initializers/vote_extensions.rb | 10 ++----- spec/models/vote_spec.rb | 41 ++++++++++++-------------- 3 files changed, 24 insertions(+), 31 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 6055772b4..67db79b5b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -77,8 +77,8 @@ class User < ActiveRecord::Base end def debate_votes(debates) - voted = votes.for_debates.in(debates) - voted.each_with_object({}) { |v, _| _[v.votable_id] = v.value } + voted = votes.for_debates(debates) + voted.each_with_object({}) { |v, h| h[v.votable_id] = v.value } end def administrator? diff --git a/config/initializers/vote_extensions.rb b/config/initializers/vote_extensions.rb index 4bbf48d20..c0a92ef40 100644 --- a/config/initializers/vote_extensions.rb +++ b/config/initializers/vote_extensions.rb @@ -1,13 +1,9 @@ ActsAsVotable::Vote.class_eval do - def self.for_debates - where(votable_type: 'Debate') - end - - def self.in(debates) - where(votable_id: debates) + def self.for_debates(debates) + where(votable_type: 'Debate', votable_id: debates) end def value vote_flag end -end \ No newline at end of file +end diff --git a/spec/models/vote_spec.rb b/spec/models/vote_spec.rb index 41fcf8471..1a44e7606 100644 --- a/spec/models/vote_spec.rb +++ b/spec/models/vote_spec.rb @@ -3,34 +3,31 @@ require 'rails_helper' describe 'Vote' do describe '#for_debates' do - it 'returns votes for debates' do - debate = create(:debate) - create(:vote, votable: debate) - - expect(Vote.for_debates.count).to eq(1) - end - it 'does not returns votes for other votables' do + debate = create(:debate) comment = create(:comment) create(:vote, votable: comment) - expect(Vote.for_debates.count).to eq(0) - end - end - - describe '#in' do - it 'returns debates send in parameters' do - debate = create(:debate) - create(:vote, votable: debate) - - expect(Vote.in(debate).count).to eq(1) + expect(Vote.for_debates(debate).count).to eq(0) end - it 'does not return debates not in parameters' do - debate = create(:debate) - create(:vote, votable: debate) + it 'returns votes only for debates in parameters' do + debate1 = create(:debate) + debate2 = create(:debate) + create(:vote, votable: debate1) - expect(Vote.in([]).count).to eq(0) + expect(Vote.for_debates(debate1).count).to eq(1) + expect(Vote.for_debates(debate2).count).to eq(0) + end + + it 'accepts more than 1 debate' do + debate1 = create(:debate) + debate2 = create(:debate) + debate3 = create(:debate) + create(:vote, votable: debate1) + create(:vote, votable: debate3) + + expect(Vote.for_debates([debate1, debate2]).count).to eq(1) end end @@ -43,4 +40,4 @@ describe 'Vote' do expect(vote.value).to eq(false) end end -end \ No newline at end of file +end From d0c7c7fe7935af0bb980b9c0225baa1d0b325ac7 Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 1 Sep 2015 13:09:35 +0200 Subject: [PATCH 6/7] Adds User.comment_flags --- app/models/flag.rb | 2 ++ app/models/user.rb | 5 +++++ spec/models/user_spec.rb | 22 ++++++++++++++++++++++ 3 files changed, 29 insertions(+) diff --git a/app/models/flag.rb b/app/models/flag.rb index 8d54c2dc7..a8be6da76 100644 --- a/app/models/flag.rb +++ b/app/models/flag.rb @@ -9,6 +9,8 @@ class Flag < ActiveRecord::Base flaggable_id: flaggable.id) end) + scope :for_comments, ->(comments) { where(flaggable_type: 'Comment', flaggable_id: comments) } + def self.flag(user, flaggable) return false if flagged?(user, flaggable) create(user: user, flaggable: flaggable) diff --git a/app/models/user.rb b/app/models/user.rb index 67db79b5b..d03cac6e4 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -81,6 +81,11 @@ class User < ActiveRecord::Base voted.each_with_object({}) { |v, h| h[v.votable_id] = v.value } end + def comment_flags(comments) + comment_flags = flags.for_comments(comments) + comment_flags.each_with_object({}){ |f, h| h[f.flaggable_id] = true } + end + def administrator? administrator.present? end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index fc26051b2..d00ed71ca 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -24,6 +24,28 @@ describe User do end end + describe "#comment_flags" do + let(:user) { create(:user) } + + it "returns {} if no comment" do + expect(user.comment_flags([])).to eq({}) + end + + it "returns a hash of flaggable_ids with 'true' if they were flagged by the user" do + comment1 = create(:comment) + comment2 = create(:comment) + comment3 = create(:comment) + Flag.flag(user, comment1) + Flag.flag(user, comment3) + + flagged = user.comment_flags([comment1, comment2, comment3]) + + expect(flagged[comment1.id]).to be + expect(flagged[comment2.id]).to_not be + expect(flagged[comment3.id]).to be + end + end + subject { build(:user) } it "is valid" do From 99fd0a917df4345812794b2165efb9aa591a6512 Mon Sep 17 00:00:00 2001 From: kikito Date: Tue, 1 Sep 2015 13:10:19 +0200 Subject: [PATCH 7/7] Uses @comment_flags in everything comment-related --- app/controllers/application_controller.rb | 4 ++++ app/controllers/comments_controller.rb | 2 ++ app/controllers/debates_controller.rb | 3 +++ app/helpers/flags_helper.rb | 6 +++++- 4 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 0ddb59bb3..9a84e9abd 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -71,6 +71,10 @@ class ApplicationController < ActionController::Base @debate_votes = current_user ? current_user.debate_votes(debates) : {} end + def set_comment_flags(comments) + @comment_flags = current_user ? current_user.comment_flags(comments) : {} + end + def ensure_signup_complete # Ensure we don't go into an infinite loop return if action_name.in? %w(finish_signup do_finish_signup) diff --git a/app/controllers/comments_controller.rb b/app/controllers/comments_controller.rb index e792240f8..076b33fce 100644 --- a/app/controllers/comments_controller.rb +++ b/app/controllers/comments_controller.rb @@ -24,11 +24,13 @@ class CommentsController < ApplicationController def flag Flag.flag(current_user, @comment) + set_comment_flags(@comment) respond_with @comment, template: 'comments/_refresh_flag_actions' end def unflag Flag.unflag(current_user, @comment) + set_comment_flags(@comment) respond_with @comment, template: 'comments/_refresh_flag_actions' end diff --git a/app/controllers/debates_controller.rb b/app/controllers/debates_controller.rb index 2f660c1c7..87df13d37 100644 --- a/app/controllers/debates_controller.rb +++ b/app/controllers/debates_controller.rb @@ -15,6 +15,9 @@ class DebatesController < ApplicationController def show set_debate_votes(@debate) @comments = @debate.root_comments.recent.page(params[:page]).for_render + # TODO limit this list to the paginated root comment's children once we have ancestry + all_visible_comments = @debate.comment_threads + set_comment_flags(all_visible_comments) end def new diff --git a/app/helpers/flags_helper.rb b/app/helpers/flags_helper.rb index 3a8bfbc75..b54c86b48 100644 --- a/app/helpers/flags_helper.rb +++ b/app/helpers/flags_helper.rb @@ -10,7 +10,11 @@ module FlagsHelper private def flagged?(flaggable) - Flag.flagged?(current_user, flaggable) + if flaggable.is_a? Comment + @comment_flags[flaggable.id] + else + Flag.flagged?(current_user, flaggable) + end end def own_flaggable?(flaggable)