From a4ec38d752e64412e0af939892c976f6f479b1a4 Mon Sep 17 00:00:00 2001 From: David Gil Date: Sat, 15 Aug 2015 20:01:52 +0200 Subject: [PATCH] adds create and destroy to the tags CRUD --- app/controllers/admin/tags_controller.rb | 19 ++++++++++++-- app/views/admin/tags/index.html.erb | 14 ++++++++++- config/locales/activerecord.en.yml | 3 ++- config/locales/activerecord.es.yml | 3 ++- config/locales/admin.en.yml | 6 ++++- config/locales/admin.es.yml | 6 ++++- config/routes.rb | 2 +- spec/features/admin_spec.rb | 32 +++++++++++++++++++++--- 8 files changed, 74 insertions(+), 11 deletions(-) diff --git a/app/controllers/admin/tags_controller.rb b/app/controllers/admin/tags_controller.rb index 61df7968e..fc9f1e1a5 100644 --- a/app/controllers/admin/tags_controller.rb +++ b/app/controllers/admin/tags_controller.rb @@ -1,22 +1,37 @@ class Admin::TagsController < Admin::BaseController layout 'admin' + before_action :find_tag, only: [:update, :destroy] respond_to :html, :js def index @tags = ActsAsTaggableOn::Tag.order(featured: :desc) + @tag = ActsAsTaggableOn::Tag.new + end + + def create + ActsAsTaggableOn::Tag.create(tag_params) + redirect_to admin_tags_path end def update - @tag = ActsAsTaggableOn::Tag.find(params[:id]) @tag.update(tag_params) redirect_to admin_tags_path end + def destroy + @tag.destroy + redirect_to admin_tags_path + end + private 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 diff --git a/app/views/admin/tags/index.html.erb b/app/views/admin/tags/index.html.erb index 12003c0a5..4dbbed167 100644 --- a/app/views/admin/tags/index.html.erb +++ b/app/views/admin/tags/index.html.erb @@ -1,4 +1,14 @@
+

<%= t("admin.tags.index.add_tag") %>

+ + <%= 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 %> + +

<%= t("admin.tags.index.title") %>

diff --git a/config/locales/activerecord.en.yml b/config/locales/activerecord.en.yml index 3edd65527..54d2a923a 100644 --- a/config/locales/activerecord.en.yml +++ b/config/locales/activerecord.en.yml @@ -3,6 +3,7 @@ en: models: comment: Comment debate: Debate + tag: Tema user: User vote: Vote attributes: @@ -19,4 +20,4 @@ en: first_name: "First name" last_name: "Last name" nickname: Nickname - password: Password \ No newline at end of file + password: Password diff --git a/config/locales/activerecord.es.yml b/config/locales/activerecord.es.yml index 6bab3edb6..0db8c4f83 100644 --- a/config/locales/activerecord.es.yml +++ b/config/locales/activerecord.es.yml @@ -3,6 +3,7 @@ es: models: comment: Comentario debate: Debate + tag: Topic user: Usuario vote: Voto attributes: @@ -19,4 +20,4 @@ es: first_name: Nombre last_name: Apellidos nickname: Pseudónimo - password: Contraseña \ No newline at end of file + password: Contraseña diff --git a/config/locales/admin.en.yml b/config/locales/admin.en.yml index 2cd19f755..a5b469e1b 100644 --- a/config/locales/admin.en.yml +++ b/config/locales/admin.en.yml @@ -6,4 +6,8 @@ en: tags: index: 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 diff --git a/config/locales/admin.es.yml b/config/locales/admin.es.yml index bfafea00d..fecb7c5f4 100644 --- a/config/locales/admin.es.yml +++ b/config/locales/admin.es.yml @@ -6,4 +6,8 @@ es: tags: index: 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 diff --git a/config/routes.rb b/config/routes.rb index a0b9a97d9..5470223e8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -24,7 +24,7 @@ Rails.application.routes.draw do namespace :admin do root to: "dashboard#index" - resources :tags, only: [:index, :update] + resources :tags, only: [:index, :create, :update, :destroy] end namespace :moderation do diff --git a/spec/features/admin_spec.rb b/spec/features/admin_spec.rb index ca5074772..6c19b03f7 100644 --- a/spec/features/admin_spec.rb +++ b/spec/features/admin_spec.rb @@ -74,14 +74,40 @@ feature 'Admin' do end context 'Tags' do - scenario 'marking tags as featured / unfeatured' do - unfeatured_tag = create :tag, :unfeatured, name: 'Mi barrio' + let(:unfeatured_tag) { create :tag, :unfeatured, name: 'Mi barrio' } + background do 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 expect(page).to have_content 'Mi barrio' - save_and_open_page check "tag_featured_#{unfeatured_tag.id}" click_button 'Update Tag'