Skip to content

Commit

Permalink
Add ability to create tags for mods
Browse files Browse the repository at this point in the history
  • Loading branch information
Taeir committed Sep 12, 2022
1 parent af31ded commit 7d6575e
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 47 deletions.
21 changes: 18 additions & 3 deletions app/controllers/tags_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
class TagsController < ApplicationController
before_action :authenticate_user!, only: [:edit, :update, :rename, :merge, :select_merge]
before_action :authenticate_user!, only: [:new, :create, :edit, :update, :rename, :merge, :select_merge]
before_action :set_category, except: [:index]
before_action :set_tag, only: [:show, :edit, :update, :children, :rename, :merge, :select_merge, :nuke, :nuke_warning]
before_action :verify_moderator, only: [:rename, :merge, :select_merge]
before_action :verify_moderator, only: [:new, :create, :rename, :merge, :select_merge]
before_action :verify_admin, only: [:nuke, :nuke_warning]

def index
Expand Down Expand Up @@ -59,6 +59,21 @@ def show
end
end

def new
@tag = Tag.new
end

def create
@tag = Tag.new(tag_params.merge(tag_set_id: @category.tag_set.id))
if @tag.save
flash[:danger] = nil
redirect_to tag_path(id: @category.id, tag_id: @tag.id)
else
flash[:danger] = @tag.errors.full_messages.join(', ')
render :new, status: :bad_request
end
end

def edit
check_your_privilege('edit_tags', nil, true)
end
Expand Down Expand Up @@ -177,7 +192,7 @@ def set_category
end

def tag_params
params.require(:tag).permit(:excerpt, :wiki_markdown, :parent_id)
params.require(:tag).permit(:excerpt, :wiki_markdown, :parent_id, :name)
end

def exec(sql_array)
Expand Down
54 changes: 54 additions & 0 deletions app/views/tags/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<%= render 'posts/markdown_script' %>

<% if @tag.errors.any? %>
<div class="notice is-danger">
There were some errors while saving this tag:

<ul>
<% @tag.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<%= form_for @tag, url: submit_path do |f| %>
<% if submit_path == create_tag_path %>
<div class="form-group">
<%= f.label :name, 'Name', class: 'form-element' %>
<span class="form-caption">
Name of the tag
</span>
<%= f.text_field :name, class: 'form-element' %>
</div>
<% end %>

<div class="form-group">
<%= f.label :parent_id, 'Parent tag', class: 'form-element' %>
<span class="form-caption">
Optional. Select a parent tag to make this part of a tag hierarchy.
</span>
<%= f.select :parent_id, options_for_select(@tag.parent.present? ? [[@tag.parent.name, @tag.parent_id]] : [],
selected: @tag.parent.present? ? @tag.parent_id : nil),
{ include_blank: true }, class: "form-element js-tag-select",
data: { tag_set: @category.tag_set_id, use_ids: true, placeholder: "None" } %>
</div>

<div class="form-group">
<%= f.label :excerpt, 'Usage guidance', class: 'form-element' %>
<span class="form-caption">
Short usage guidance for this tag. Will be cut off at 120 characters in the tags list, but displayed in full on
the tag page.
</span>
<%= f.text_area :excerpt, class: 'form-element js-tag-excerpt', rows: 3 %>
<span class="has-float-right has-font-size-caption js-character-count"
data-target=".js-tag-excerpt" data-max="600">0 / 600</span>
</div>

<%= render 'shared/body_field', f: f, field_name: :wiki_markdown, field_label: 'Wiki', post: @tag do %>
Full usage guidance and any other information you want people to know about this tag.
<% end %>
<div class="post-preview"></div>

<%= f.submit 'Save', class: 'button is-filled' %>
<% end %>
4 changes: 4 additions & 0 deletions app/views/tags/category.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

<h1>Tags used in <%= @category.name %></h1>

<% if current_user&.is_moderator %>
<%= link_to 'New', new_tag_path(id: @category.id), class: 'button is-muted is-outlined' %>
<% end %>

<%= form_tag category_tags_path(@category), method: :get, class: 'form-inline' do %>
<div class="form-group-horizontal">
<div class="form-group">
Expand Down
45 changes: 1 addition & 44 deletions app/views/tags/edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,50 +1,7 @@
<% content_for :title, 'Edit tag' %>

<%= render 'posts/markdown_script' %>

<h1>
Edit <span class="<%= @classes %> is-large"><%= @tag.name %></span>
</h1>

<% if @tag.errors.any? %>
<div class="notice is-danger">
There were some errors while saving this tag:

<ul>
<% @tag.errors.full_messages.each do |msg| %>
<li><%= msg %></li>
<% end %>
</ul>
</div>
<% end %>

<%= form_for @tag, url: update_tag_path(id: @category.id, tag_id: @tag.id) do |f| %>
<div class="form-group">
<%= f.label :parent_id, 'Parent tag', class: 'form-element' %>
<span class="form-caption">
Optional. Select a parent tag to make this part of a tag hierarchy.
</span>
<%= f.select :parent_id, options_for_select(@tag.parent.present? ? [[@tag.parent.name, @tag.parent_id]] : [],
selected: @tag.parent.present? ? @tag.parent_id : nil),
{ include_blank: true }, class: "form-element js-tag-select",
data: { tag_set: @category.tag_set_id, use_ids: true, placeholder: "None" } %>
</div>

<div class="form-group">
<%= f.label :excerpt, 'Usage guidance', class: 'form-element' %>
<span class="form-caption">
Short usage guidance for this tag. Will be cut off at 120 characters in the tags list, but displayed in full on
the tag page.
</span>
<%= f.text_area :excerpt, class: 'form-element js-tag-excerpt', rows: 3 %>
<span class="has-float-right has-font-size-caption js-character-count"
data-target=".js-tag-excerpt" data-max="600">0 / 600</span>
</div>

<%= render 'shared/body_field', f: f, field_name: :wiki_markdown, field_label: 'Wiki', post: @tag do %>
Full usage guidance and any other information you want people to know about this tag.
<% end %>
<div class="post-preview"></div>

<%= f.submit 'Save', class: 'button is-filled' %>
<% end %>
<%= render 'form', submit_path: update_tag_path(id: @category.id, tag_id: @tag.id) %>
7 changes: 7 additions & 0 deletions app/views/tags/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<% content_for :title, 'Create tag' %>

<h1>
Create <span class="<%= @classes %> is-large">Tag</span>
</h1>

<%= render 'form', submit_path: create_tag_path %>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,8 @@
get ':id/types', to: 'categories#post_types', as: :category_post_types
get ':id/feed', to: 'categories#rss_feed', as: :category_feed
get ':id/tags', to: 'tags#category', as: :category_tags
get ':id/tags/new', to: 'tags#new', as: :new_tag
post ':id/tags/new', to: 'tags#create', as: :create_tag
get ':id/tags/:tag_id', to: 'tags#show', as: :tag
get ':id/tags/:tag_id/children', to: 'tags#children', as: :tag_children
get ':id/tags/:tag_id/edit', to: 'tags#edit', as: :edit_tag
Expand Down

0 comments on commit 7d6575e

Please sign in to comment.