diff --git a/app/assets/javascripts/content.js b/app/assets/javascripts/content.js index 6eac7528..8de06125 100644 --- a/app/assets/javascripts/content.js +++ b/app/assets/javascripts/content.js @@ -104,13 +104,35 @@ $(document).ready(function () { // Replace this element's content with the name of the page var tag = $(this); - $.get( - '/api/internal/' + tag.data('klass') + '/' + tag.data('id') + '/name' - ).done(function (response) { - tag.find('.name-container').text(response); - }).fail(function() { - tag.find('.name-conainer').text("Unknown " + tag.data('klass')); - }); + // Instantiate a cache for all page lookup queries (if not already created) + window.load_page_name_cache = window.load_page_name_cache || {}; + var page_name_key = tag.data('klass') + '/' + tag.data('id'); + + if (page_name_key in window.load_page_name_cache) { + // If we've already made a request for this klass+id, we can just insta-load the + // cached result instead of requesting it again. + tag.find('.name-container').text(window.load_page_name_cache[page_name_key]); + + } else { + // If we haven't made a request for this klass+id, look it up and cache it + $.get( + '/api/internal/' + page_name_key + '/name' + ).done(function (response) { + tag.find('.name-container').text(response); + window.load_page_name_cache[page_name_key] = response; + + // Go ahead and pre-fill all tags on the page for this klass+id, too + $('.js-load-page-name[data-klass=' + tag.data('klass') + '][data-id=' + tag.data('id') + ']') + .find('.name-container') + .text(response); + + }).fail(function() { + tag.find('.name-container').text("Unknown " + tag.data('klass')); + }); + } + + + }); }); diff --git a/app/assets/javascripts/page_tags.coffee b/app/assets/javascripts/page_tags.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/page_tags.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 6e5b9a9f..cd3e10b2 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -159,7 +159,7 @@ class ApplicationController < ActionController::Base cache_current_user_content @contributable_universe_ids ||= if user_signed_in? - current_user.contributable_universe_ids + @current_user_content.fetch('Universe', []).map(&:id) + current_user.contributable_universe_ids else [] end diff --git a/app/controllers/data_controller.rb b/app/controllers/data_controller.rb index f42eef7e..b6897575 100644 --- a/app/controllers/data_controller.rb +++ b/app/controllers/data_controller.rb @@ -115,6 +115,10 @@ class DataController < ApplicationController @content = current_user.content end + def tags + @tags = current_user.page_tags + end + def discussions @topics = Thredded::Topic.where(user_id: current_user.id) @posts = Thredded::Post.where(user_id: current_user.id) diff --git a/app/controllers/page_tags_controller.rb b/app/controllers/page_tags_controller.rb new file mode 100644 index 00000000..55653d39 --- /dev/null +++ b/app/controllers/page_tags_controller.rb @@ -0,0 +1,23 @@ +class PageTagsController < ApplicationController + # Remove a tag and all of its links to a page + def remove + # Params + # {"page_type"=>"Location", "slug"=>"mountains", "controller"=>"page_tags", "action"=>"remove" + return unless params.key?(:page_type) && params.key?(:slug) + + PageTag.where( + page_type: params[:page_type], + slug: params[:slug], + user_id: current_user.id + ).destroy_all + + return redirect_back fallback_location: root_path, notice: 'Tag(s) deleted successfully.' + end + + # Destroy a specific tag by ID + def destroy + PageTag.find_by(id: params[:id], user_id: current_user.id).destroy! + + return redirect_back fallback_location: root_path, notice: 'Tag(s) deleted successfully.' + end +end diff --git a/app/helpers/page_tags_helper.rb b/app/helpers/page_tags_helper.rb new file mode 100644 index 00000000..96c4808b --- /dev/null +++ b/app/helpers/page_tags_helper.rb @@ -0,0 +1,2 @@ +module PageTagsHelper +end diff --git a/app/views/content/display/_changelog.html.erb b/app/views/content/display/_changelog.html.erb index 396aa2b0..8f1aecee 100644 --- a/app/views/content/display/_changelog.html.erb +++ b/app/views/content/display/_changelog.html.erb @@ -5,7 +5,6 @@ changed_fields = AttributeField.where(id: changed_attributes.pluck(:attribute_field_id)).includes([:attribute_category]) %> -
+ Manage the tags you've used for your worldbuilding. +
+