page tags hooked up on content page forms

This commit is contained in:
Andrew Brown 2019-02-15 18:07:33 -06:00
parent b7427a6d37
commit cdf66dcea6
6 changed files with 70 additions and 13 deletions

View File

@ -36,7 +36,19 @@ class ContentController < ApplicationController
end
end
@content = @content.to_a.flatten.uniq.sort_by(&:name)
@content = @content.to_a.flatten.uniq
# Filters
if params.key?(:slug)
page_tags = PageTag.where(
slug: params[:slug],
page_type: @content_type_class.name,
page_id: @content.pluck(:id)
)
@content.select! { |content| page_tags.pluck(:page_id).include?(content.id) }
end
@content = @content.sort_by(&:name)
respond_to do |format|
format.html { render 'content/index' }
@ -336,7 +348,23 @@ class ContentController < ApplicationController
private
def update_page_tags
raise params.inspect
tag_list = page_tag_params.fetch(:page_tags, "").split(',,,|||,,,')
current_tags = @content.page_tags.pluck(:tag)
tags_to_add = tag_list - current_tags
tags_to_remove = current_tags - tag_list
tags_to_add.each do |tag|
@content.page_tags.find_or_create_by(
tag: tag,
slug: PageTagService.slug_for(tag),
user: @content.user
)
end
tags_to_remove.each do |tag|
@content.page_tags.find_by(tag: tag).destroy
end
end
def render_json(content)
@ -409,6 +437,15 @@ class ContentController < ApplicationController
params.require(content_class).permit(content_param_list + [:deleted_at])
end
def page_tag_params
content_class = content_type_from_controller(self.class)
.name
.downcase
.to_sym
params.require(content_class).permit(:page_tags)
end
def content_deletion_redirect_url
send("#{@content.class.name.underscore.pluralize}_path")
end

View File

@ -8,7 +8,7 @@ class Building < ActiveRecord::Base
include BelongsToUniverse
include IsContentPage
include Serendipitous::Concern
include Authority::Abilities

View File

@ -0,0 +1,5 @@
class PageTagService < Service
def self.slug_for(text)
text.downcase.gsub(/[^0-9a-z ]/i, '').gsub(/ /, '-')
end
end

View File

@ -28,6 +28,9 @@
nil
end
when 'tags'
content.page_tags
else
raise "unknown field type = " + serialized_field[:type].inspect
end
@ -61,6 +64,18 @@
<% end %>
</div>
<% elsif serialized_field[:type] == 'tags' %>
<% value.each do |tag| %>
<%=
link_to send(
"page_tag_#{content.raw_model.class.name.downcase.pluralize}_path",
slug: PageTagService.slug_for(tag)
) do
%>
<span class="new badge red left" data-badge-caption="<%= tag %>"></span>
<% end %>
<% end %>
<% else # serialized_field[:type] == text %>
<div class="col s9 m9 l9 markdownable">
<%=

View File

@ -56,20 +56,18 @@
secondaryPlaceholder: '+ Tag',
autocompleteOptions: {
data: {
'Apple': null,
'Microsoft': null,
'Google': null
<% @content.page_tags.pluck(:tag).each do |tag| %>
'<%= tag %>': null,
<% end %>
},
limit: 100,
minLength: 1
},
data: [{
tag: 'Apple',
}, {
tag: 'Microsoft',
}, {
tag: 'Google',
}],
data: [
<% @content.page_tags.pluck(:tag).each do |tag| %>
{tag: '<%= tag %>'},
<% end %>
],
onChipAdd: update_hidden_page_tag_value,
onChipDelete: update_hidden_page_tag_value
});

View File

@ -10,6 +10,7 @@ Rails.application.routes.draw do
# get :characters, on: :member <...etc...>
Rails.application.config.content_types[:all].each do |content_type|
get content_type.name.downcase.pluralize.to_sym, on: :member
# todo page tags here
end
end
scope '/my' do
@ -87,6 +88,7 @@ Rails.application.routes.draw do
# resources :characters do
resources content_type.name.downcase.pluralize.to_sym do
get :changelog, on: :member
get '/tagged/:slug', action: :index, on: :collection, as: :page_tag
end
end