From 55affe50c31a7b2775721ca8d5df1a76e4e497ea Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Fri, 26 Mar 2021 13:04:30 -0700 Subject: [PATCH] hook up content#show again, add internal api for fetching page names on pageload --- app/assets/javascripts/content.js | 17 +++++++++++ .../api/internal/api_controller.rb | 26 +++++++++++++++++ app/controllers/content_controller.rb | 7 ----- .../content/display/_category_panel.html.erb | 17 ++++------- .../display/attribute_value/_link.html.erb | 28 ++++++++++++++----- .../attribute_value/_universe.html.erb | 2 +- .../content/display/sidebar/_more.html.erb | 2 +- config/routes.rb | 5 ++++ 8 files changed, 77 insertions(+), 27 deletions(-) create mode 100644 app/controllers/api/internal/api_controller.rb diff --git a/app/assets/javascripts/content.js b/app/assets/javascripts/content.js index 0fbf7f38..1cec4a92 100644 --- a/app/assets/javascripts/content.js +++ b/app/assets/javascripts/content.js @@ -100,5 +100,22 @@ $(document).ready(function () { }); }); + $('.js-load-page-name').each(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() { + console.log("OMG couldn't load that page: " + [tag.data('klass'), tag.data('id')]); + // set "unknown page" + }); + + console.log('loading page name'); + console.log(tag.data('klass') + '-' + tag.data('id')); + }); + }); diff --git a/app/controllers/api/internal/api_controller.rb b/app/controllers/api/internal/api_controller.rb new file mode 100644 index 00000000..3cb6c428 --- /dev/null +++ b/app/controllers/api/internal/api_controller.rb @@ -0,0 +1,26 @@ +module Api + module Internal + class ApiController < ApplicationController + before_action :sanitize_content_type + before_action :set_content + + def name_lookup + render body: @content.name, content_type: 'text/plain' + end + + private + + def sanitize_content_type + unless Rails.application.config.content_types[:all].map(&:name).include?(params[:page_type]) + raise "Invalid page type: #{params[:page_type]}" + end + + @sanitized_content_type = params[:page_type].constantize + end + + def set_content + @content = @sanitized_content_type.find_by(id: params[:page_id]) + end + end + end +end \ No newline at end of file diff --git a/app/controllers/content_controller.rb b/app/controllers/content_controller.rb index fe60723c..e486cf81 100644 --- a/app/controllers/content_controller.rb +++ b/app/controllers/content_controller.rb @@ -82,13 +82,6 @@ class ContentController < ApplicationController @serialized_content = ContentSerializer.new(@content) if (current_user || User.new).can_read?(@content) - if user_signed_in? - @navbar_actions.insert(1, { - label: @content.name, - href: main_app.polymorphic_path(@content) - }) - end - if user_signed_in? if @content.updated_at > 30.minutes.ago Mixpanel::Tracker.new(Rails.application.config.mixpanel_token).track(current_user.id, 'viewed content', { diff --git a/app/views/content/display/_category_panel.html.erb b/app/views/content/display/_category_panel.html.erb index 33a03657..0bbb7cf3 100644 --- a/app/views/content/display/_category_panel.html.erb +++ b/app/views/content/display/_category_panel.html.erb @@ -1,6 +1,6 @@
_panel" class="row panel">
- <% category[:fields].select { |field| !field[:hidden] }.each do |serialized_field| %> + <% category[:fields].reject { |field| field[:hidden] }.each do |serialized_field| %> <% if serialized_field[:label].start_with?("Private") %> <% next unless user_signed_in? && ( (content.raw_model.is_a?(Universe) && content.user == current_user) || @@ -21,14 +21,6 @@ when 'link' serialized_field[:value] - # Leaving this old code in just in case there's an issue with links after release; good to delete after a successful deploy + time - # if serialized_field[:old_column_source].present? && content.raw_model.respond_to?(serialized_field[:old_column_source].to_sym) - # serialized_field[:value] - # else - # # todo uhhhh is this just a safety guard or are there actual cases of bad data triggering here? - # nil - # end - when 'tags' content.page_tags @@ -36,7 +28,10 @@ raise "unknown field type = " + serialized_field[:type].inspect end %> - <% next if value.blank? %> + <% + # Don't show fields with a blank value + next if value.blank? + %>
@@ -51,7 +46,7 @@
- <%= + <%= render partial: "content/display/attribute_value/#{serialized_field[:type]}", locals: { value: value, content: content } %> diff --git a/app/views/content/display/attribute_value/_link.html.erb b/app/views/content/display/attribute_value/_link.html.erb index f7eac400..e19b6a77 100644 --- a/app/views/content/display/attribute_value/_link.html.erb +++ b/app/views/content/display/attribute_value/_link.html.erb @@ -1,13 +1,27 @@ -<% value.each do |li| %> -
- <%= link_to li do %> - + + +<% value.each do |link_code| %> + <% + klass, id = link_code.split('-') + %> +
+ <%= link_to content_class_from_name(klass) do %> + - <%= li.class.icon %> + <%= content_class_from_name(klass).icon %> - <%= li.name_field_value %> + + <%= id %> + <% end %>
<% end %> -

\ No newline at end of file +

+ +<%# + simple_format ContentFormatterService.show( + text: value.map { |link_code| "[[#{link_code}]]"}.join("\n"), + viewing_user: current_user + ) +%> diff --git a/app/views/content/display/attribute_value/_universe.html.erb b/app/views/content/display/attribute_value/_universe.html.erb index ecd60be6..d7b08180 100644 --- a/app/views/content/display/attribute_value/_universe.html.erb +++ b/app/views/content/display/attribute_value/_universe.html.erb @@ -1,5 +1,5 @@ <%# Treat universe values as if they're just link fields %> <%= render partial: "content/display/attribute_value/link", - locals: { value: value, content: content } + locals: { value: value.map { |universe| "Universe-#{universe.id}" }, content: content } %> \ No newline at end of file diff --git a/app/views/content/display/sidebar/_more.html.erb b/app/views/content/display/sidebar/_more.html.erb index cbfcd8e6..8dace646 100644 --- a/app/views/content/display/sidebar/_more.html.erb +++ b/app/views/content/display/sidebar/_more.html.erb @@ -38,7 +38,7 @@
  • <%= link_to send("#{raw_model.class.name.downcase.pluralize}_user_path", { id: raw_model.user_id }) do %> <%= raw_model.class.icon %> - <%= raw_model.class.name.pluralize %> by <%= editing ? current_user.display_name : @user.display_name %> + <%= raw_model.class.name.pluralize %> by <%= editing ? current_user.display_name : raw_model.user.display_name %> <% end %>
  • <% end %> diff --git a/config/routes.rb b/config/routes.rb index 6f4b47a5..e104cc2b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -303,6 +303,11 @@ Rails.application.routes.draw do get '/references', to: 'api_docs#references' end + # API requests we don't want to officially make public -- free to break! + namespace :internal do + get '/:page_type/:page_id/name', to: 'api#name_lookup' + end + namespace :v1 do scope '/categories' do get '/suggest/:entity_type', to: 'attribute_categories#suggest'