From 64774b0d2db7a3d1403442ebc083a0f0fa51fbce Mon Sep 17 00:00:00 2001 From: taitus Date: Wed, 6 Sep 2017 13:30:12 +0200 Subject: [PATCH] Refactor topics controller. Add Common abilities. Add destroy action. --- app/controllers/topics_controller.rb | 11 +++-- app/models/abilities/common.rb | 3 ++ app/models/topic.rb | 2 - app/views/topics/_topics.html.erb | 5 ++- config/locales/en/community.yml | 1 + config/locales/en/responders.yml | 1 + config/locales/es/community.yml | 1 + config/locales/es/responders.yml | 1 + db/seeds.rb | 2 +- spec/features/topics_specs.rb | 62 +++++++++++++++++++++++++--- 10 files changed, 76 insertions(+), 13 deletions(-) diff --git a/app/controllers/topics_controller.rb b/app/controllers/topics_controller.rb index f0e57a383..0e63685bd 100644 --- a/app/controllers/topics_controller.rb +++ b/app/controllers/topics_controller.rb @@ -1,13 +1,13 @@ class TopicsController < ApplicationController include CommentableActions - include FlagActions before_action :load_community - before_action :load_topic, only: [:show, :edit, :update] + before_action :load_topic, only: [:show, :edit, :update, :destroy] has_orders %w{most_voted newest oldest}, only: :show - skip_authorization_check + skip_authorization_check only: :show + load_and_authorize_resource except: :show def new @topic = Topic.new @@ -39,6 +39,11 @@ class TopicsController < ApplicationController end end + def destroy + @topic.destroy + redirect_to community_path(@community), notice: I18n.t('flash.actions.destroy.topic') + end + private def topic_params diff --git a/app/models/abilities/common.rb b/app/models/abilities/common.rb index 311b0dade..620cfb212 100644 --- a/app/models/abilities/common.rb +++ b/app/models/abilities/common.rb @@ -71,6 +71,9 @@ module Abilities can :create, Annotation can [:update, :destroy], Annotation, user_id: user.id + + can [:create], Topic + can [:update, :destroy], Topic, author_id: user.id end end end diff --git a/app/models/topic.rb b/app/models/topic.rb index 8c8f3f6c9..34abed0e3 100644 --- a/app/models/topic.rb +++ b/app/models/topic.rb @@ -1,6 +1,4 @@ class Topic < ActiveRecord::Base - include Flaggable - acts_as_paranoid column: :hidden_at include ActsAsParanoidAliases diff --git a/app/views/topics/_topics.html.erb b/app/views/topics/_topics.html.erb index f74fa573f..b5080a61f 100644 --- a/app/views/topics/_topics.html.erb +++ b/app/views/topics/_topics.html.erb @@ -9,7 +9,7 @@ <% topics.each do |topic| %>
-
+

<%= link_to topic.title, community_topic_path(@community, topic) %>

@@ -24,9 +24,10 @@
-
+
<% if topic.author == current_user %> <%= link_to t("community.show.topic.edit_button"), edit_community_topic_path(@community.id, topic), class: 'button small hollow' %> + <%= link_to t("community.show.topic.destroy_button"), community_topic_path(@community.id, topic), method: :delete, class: 'button hollow alert small' %> <% end %>
diff --git a/config/locales/en/community.yml b/config/locales/en/community.yml index 37b7ea74f..068c98998 100644 --- a/config/locales/en/community.yml +++ b/config/locales/en/community.yml @@ -26,6 +26,7 @@ en: disabled_info_title: You need to be logged to create a new topic topic: edit_button: Edit + destroy_button: Destroy comments: one: 1 comment other: "%{count} comments" diff --git a/config/locales/en/responders.yml b/config/locales/en/responders.yml index 17de27b74..203d5700e 100644 --- a/config/locales/en/responders.yml +++ b/config/locales/en/responders.yml @@ -29,3 +29,4 @@ en: spending_proposal: "Spending proposal deleted succesfully." budget_investment: "Investment project deleted succesfully." error: "Could not delete" + topic: "Topic deleted successfully." diff --git a/config/locales/es/community.yml b/config/locales/es/community.yml index 196cd4c50..8013c116c 100644 --- a/config/locales/es/community.yml +++ b/config/locales/es/community.yml @@ -26,6 +26,7 @@ es: disabled_info_title: Necesitas estar logueado para crear un nuevo tema topic: edit_button: Editar + destroy_button: Eliminar comments: one: 1 Comentario other: "%{count} Comentarios" diff --git a/config/locales/es/responders.yml b/config/locales/es/responders.yml index d3d4ff163..74e3ea20a 100644 --- a/config/locales/es/responders.yml +++ b/config/locales/es/responders.yml @@ -29,3 +29,4 @@ es: spending_proposal: "Propuesta de inversión eliminada." budget_investment: "Propuesta de inversión eliminada." error: "No se pudo borrar" + topic: "Tema eliminado." diff --git a/db/seeds.rb b/db/seeds.rb index 90665fdb8..41b995f8a 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -79,7 +79,7 @@ Setting['feature.public_stats'] = true Setting['feature.budgets'] = true Setting['feature.signature_sheets'] = true Setting['feature.legislation'] = true -Setting['feature.community'] = nil +Setting['feature.community'] = true # Spending proposals feature flags Setting['feature.spending_proposal_features.voting_allowed'] = nil diff --git a/spec/features/topics_specs.rb b/spec/features/topics_specs.rb index 0d668d44b..9970e2339 100644 --- a/spec/features/topics_specs.rb +++ b/spec/features/topics_specs.rb @@ -13,7 +13,7 @@ feature 'Topics' do expect(page).to have_selector(".button.expanded.disabled") end - scenario 'Should can access to new topic page with user logged', :js do + scenario 'Can access to new topic page with user logged', :js do proposal = create(:proposal) community = proposal.community user = create(:user) @@ -47,10 +47,10 @@ feature 'Topics' do context 'Create' do - scenario 'Should can create a new topic', :js do + scenario 'Can create a new topic', :js do proposal = create(:proposal) community = proposal.community - user = create(:user) + user = create(:user) login_as(user) visit new_community_topic_path(community) @@ -62,11 +62,20 @@ feature 'Topics' do expect(current_path).to eq(community_path(community)) end + scenario 'Can not create a new topic when user not logged', :js do + proposal = create(:proposal) + community = proposal.community + + visit new_community_topic_path(community) + + expect(page).to have_content "You do not have permission to carry out the action 'new' on topic." + end + end context 'Edit' do - scenario 'Should can edit a topic' do + scenario 'Can edit a topic' do proposal = create(:proposal) community = proposal.community user = create(:user) @@ -82,11 +91,23 @@ feature 'Topics' do expect(current_path).to eq(community_path(community)) end + scenario 'Can not edit a topic when user logged is not an author' do + proposal = create(:proposal) + community = proposal.community + topic = create(:topic, community: community) + user = create(:user) + login_as(user) + + visit edit_community_topic_path(community, topic) + + expect(page).to have_content "You do not have permission to carry out the action 'edit' on topic." + end + end context 'Show' do - scenario 'Should can show topic' do + scenario 'Can show topic' do proposal = create(:proposal) community = proposal.community topic = create(:topic, community: community) @@ -99,4 +120,35 @@ feature 'Topics' do end + context 'Destroy' do + + scenario 'Can destroy a topic' do + proposal = create(:proposal) + community = proposal.community + user = create(:user) + topic = create(:topic, community: community, author: user) + login_as(user) + visit community_path(community) + + click_link "Destroy" + + expect(page).to have_content "Topic deleted successfully." + expect(page).not_to have_content topic.title + expect(current_path).to eq(community_path(community)) + end + + scenario 'Can not destroy a topic when user logged is not an author' do + proposal = create(:proposal) + community = proposal.community + topic = create(:topic, community: community) + user = create(:user) + login_as(user) + + visit community_path(community) + + expect(page).not_to have_link "Destroy" + end + + end + end