Refactor topics controller. Add Common abilities. Add destroy action.

This commit is contained in:
taitus
2017-09-06 13:30:12 +02:00
parent 5ea16976f7
commit 64774b0d2d
10 changed files with 76 additions and 13 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -1,6 +1,4 @@
class Topic < ActiveRecord::Base
include Flaggable
acts_as_paranoid column: :hidden_at
include ActsAsParanoidAliases

View File

@@ -9,7 +9,7 @@
<% topics.each do |topic| %>
<div id="<%= dom_id(topic) %>" class="panel column">
<div class="small-10 column">
<div class="small-8 column">
<h3><%= link_to topic.title, community_topic_path(@community, topic) %></h3>
@@ -24,9 +24,10 @@
</div>
<div class="small-2 column text-right">
<div class="small-4 column text-right">
<% 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 %>
</div>

View File

@@ -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"

View File

@@ -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."

View File

@@ -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"

View File

@@ -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."

View File

@@ -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

View File

@@ -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