adds create and destroy to the tags CRUD

This commit is contained in:
David Gil
2015-08-15 20:01:52 +02:00
parent 664f656fe3
commit a4ec38d752
8 changed files with 74 additions and 11 deletions

View File

@@ -1,22 +1,37 @@
class Admin::TagsController < Admin::BaseController class Admin::TagsController < Admin::BaseController
layout 'admin' layout 'admin'
before_action :find_tag, only: [:update, :destroy]
respond_to :html, :js respond_to :html, :js
def index def index
@tags = ActsAsTaggableOn::Tag.order(featured: :desc) @tags = ActsAsTaggableOn::Tag.order(featured: :desc)
@tag = ActsAsTaggableOn::Tag.new
end
def create
ActsAsTaggableOn::Tag.create(tag_params)
redirect_to admin_tags_path
end end
def update def update
@tag = ActsAsTaggableOn::Tag.find(params[:id])
@tag.update(tag_params) @tag.update(tag_params)
redirect_to admin_tags_path redirect_to admin_tags_path
end end
def destroy
@tag.destroy
redirect_to admin_tags_path
end
private private
def tag_params def tag_params
params.require(:tag).permit(:featured) params.require(:tag).permit(:featured, :name)
end
def find_tag
@tag = ActsAsTaggableOn::Tag.find(params[:id])
end end
end end

View File

@@ -1,4 +1,14 @@
<div class="left"> <div class="left">
<h1><%= t("admin.tags.index.add_tag") %></h1>
<%= form_for(@tag, url: admin_tags_path, as: :tag) do |f| %>
<%= f.text_field :name, placeholder: t("admin.tags.name.placeholder") %>
<%= f.check_box :featured, label: false %>
<%= t("admin.tags.mark_as_featured") %>
<%= f.submit(class: "button radius small") %>
<% end %>
<h1><%= t("admin.tags.index.title") %></h1> <h1><%= t("admin.tags.index.title") %></h1>
<ul> <ul>
@@ -8,9 +18,11 @@
<%= form_for(tag, url: admin_tag_path(tag), as: :tag, html: { id: "edit_tag_#{tag.id}"}) do |f| %> <%= form_for(tag, url: admin_tag_path(tag), as: :tag, html: { id: "edit_tag_#{tag.id}"}) do |f| %>
<%= f.check_box :featured, label: false, id: "tag_featured_#{tag.id}" %> <%= f.check_box :featured, label: false, id: "tag_featured_#{tag.id}" %>
<%= t("admin.tags.index.mark_as_featured") %> <%= t("admin.tags.mark_as_featured") %>
<%= f.submit(class: "button radius tiny") %> <%= f.submit(class: "button radius tiny") %>
<%= link_to t("admin.tags.destroy"), admin_tag_path(tag), method: :delete, class: 'button tiny alert' %>
<% end %> <% end %>
</li> </li>
<% end %> <% end %>
</ul> </ul>

View File

@@ -3,6 +3,7 @@ en:
models: models:
comment: Comment comment: Comment
debate: Debate debate: Debate
tag: Tema
user: User user: User
vote: Vote vote: Vote
attributes: attributes:
@@ -19,4 +20,4 @@ en:
first_name: "First name" first_name: "First name"
last_name: "Last name" last_name: "Last name"
nickname: Nickname nickname: Nickname
password: Password password: Password

View File

@@ -3,6 +3,7 @@ es:
models: models:
comment: Comentario comment: Comentario
debate: Debate debate: Debate
tag: Topic
user: Usuario user: Usuario
vote: Voto vote: Voto
attributes: attributes:
@@ -19,4 +20,4 @@ es:
first_name: Nombre first_name: Nombre
last_name: Apellidos last_name: Apellidos
nickname: Pseudónimo nickname: Pseudónimo
password: Contraseña password: Contraseña

View File

@@ -6,4 +6,8 @@ en:
tags: tags:
index: index:
title: 'Debate topics' title: 'Debate topics'
mark_as_featured: 'Mark as featured' add_tag: 'Add a new debate topic'
mark_as_featured: 'Mark as featured'
name:
placeholder: 'Write a topic'
destroy: Delete Tag

View File

@@ -6,4 +6,8 @@ es:
tags: tags:
index: index:
title: 'Temas de debate' title: 'Temas de debate'
mark_as_featured: 'Marcar como destacado' add_tag: 'Añade un nuevo tema de debate'
mark_as_featured: 'Marcar como destacado'
name:
placeholder: 'Escribe el nombre del tema'
destroy: Elimina la etiqueta

View File

@@ -24,7 +24,7 @@ Rails.application.routes.draw do
namespace :admin do namespace :admin do
root to: "dashboard#index" root to: "dashboard#index"
resources :tags, only: [:index, :update] resources :tags, only: [:index, :create, :update, :destroy]
end end
namespace :moderation do namespace :moderation do

View File

@@ -74,14 +74,40 @@ feature 'Admin' do
end end
context 'Tags' do context 'Tags' do
scenario 'marking tags as featured / unfeatured' do let(:unfeatured_tag) { create :tag, :unfeatured, name: 'Mi barrio' }
unfeatured_tag = create :tag, :unfeatured, name: 'Mi barrio'
background do
login_as(administrator) login_as(administrator)
end
scenario 'adding a new tag' do
visit admin_tags_path
fill_in 'tag_name', with: 'Papeleras'
click_on 'Create Tag'
expect(page).to have_content 'Papeleras'
end
scenario 'deleting tag' do
unfeatured_tag
visit admin_tags_path
expect(page).to have_content 'Mi barrio'
click_link 'Delete Tag'
expect(page).not_to have_content 'Mi barrio'
end
scenario 'marking tags as featured / unfeatured' do
expect(unfeatured_tag).not_to be_featured
visit admin_tags_path visit admin_tags_path
expect(page).to have_content 'Mi barrio' expect(page).to have_content 'Mi barrio'
save_and_open_page
check "tag_featured_#{unfeatured_tag.id}" check "tag_featured_#{unfeatured_tag.id}"
click_button 'Update Tag' click_button 'Update Tag'