From cdf66dcea63fd9d0aeabc17653b088cf6a704caa Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Fri, 15 Feb 2019 18:07:33 -0600 Subject: [PATCH] page tags hooked up on content page forms --- app/controllers/content_controller.rb | 41 ++++++++++++++++++- app/models/content_types/building.rb | 2 +- app/services/page_tag_service.rb | 5 +++ .../content/display/_category_panel.html.erb | 15 +++++++ app/views/content/form/_panel.html.erb | 18 ++++---- config/routes.rb | 2 + 6 files changed, 70 insertions(+), 13 deletions(-) create mode 100644 app/services/page_tag_service.rb diff --git a/app/controllers/content_controller.rb b/app/controllers/content_controller.rb index cf7d9946..fff4273e 100644 --- a/app/controllers/content_controller.rb +++ b/app/controllers/content_controller.rb @@ -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 diff --git a/app/models/content_types/building.rb b/app/models/content_types/building.rb index f4087939..2d772103 100644 --- a/app/models/content_types/building.rb +++ b/app/models/content_types/building.rb @@ -8,7 +8,7 @@ class Building < ActiveRecord::Base include BelongsToUniverse include IsContentPage - + include Serendipitous::Concern include Authority::Abilities diff --git a/app/services/page_tag_service.rb b/app/services/page_tag_service.rb new file mode 100644 index 00000000..89f30053 --- /dev/null +++ b/app/services/page_tag_service.rb @@ -0,0 +1,5 @@ +class PageTagService < Service + def self.slug_for(text) + text.downcase.gsub(/[^0-9a-z ]/i, '').gsub(/ /, '-') + end +end diff --git a/app/views/content/display/_category_panel.html.erb b/app/views/content/display/_category_panel.html.erb index 803697fc..0780d4c1 100644 --- a/app/views/content/display/_category_panel.html.erb +++ b/app/views/content/display/_category_panel.html.erb @@ -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 %> + <% 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 + %> + + <% end %> + <% end %> + <% else # serialized_field[:type] == text %>
<%= diff --git a/app/views/content/form/_panel.html.erb b/app/views/content/form/_panel.html.erb index dec04b9d..03152c41 100644 --- a/app/views/content/form/_panel.html.erb +++ b/app/views/content/form/_panel.html.erb @@ -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 }); diff --git a/config/routes.rb b/config/routes.rb index c70c4aea..62e7b5c4 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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