diff --git a/app/controllers/content_controller.rb b/app/controllers/content_controller.rb index 66f7f6b9..1d83cca7 100644 --- a/app/controllers/content_controller.rb +++ b/app/controllers/content_controller.rb @@ -89,7 +89,7 @@ class ContentController < ApplicationController }) end - if current_user + 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', { 'content_type': content_type.name, @@ -105,6 +105,14 @@ class ContentController < ApplicationController end end + if user_signed_in? && current_user.can_create?(content_type) + @navbar_actions << { + label: "New #{content_type.name.downcase}", + href: main_app.new_polymorphic_path(content_type), + class: 'right' + } + end + respond_to do |format| format.html { render 'content/show', locals: { content: @content } } format.json { render json: @serialized_content.data } @@ -168,6 +176,14 @@ class ContentController < ApplicationController return redirect_to @content, notice: t(:no_do_permission) end + if user_signed_in? && current_user.can_create?(content_type_class) + @navbar_actions << { + label: "New #{content_type_class.name.downcase}", + href: main_app.new_polymorphic_path(content_type_class), + class: 'right' + } + end + respond_to do |format| format.html { render 'content/edit', locals: { content: @content } } format.json { render json: @content } @@ -614,12 +630,6 @@ class ContentController < ApplicationController href: main_app.polymorphic_path(content_type) } end - - @navbar_actions << { - label: "New #{content_type.name.downcase}", - href: main_app.new_polymorphic_path(content_type), - class: 'right' - } end discussions_link = ForumsLinkbuilderService.worldbuilding_url(content_type) diff --git a/app/controllers/timelines_controller.rb b/app/controllers/timelines_controller.rb index 3a3473fd..257d4053 100644 --- a/app/controllers/timelines_controller.rb +++ b/app/controllers/timelines_controller.rb @@ -13,6 +13,20 @@ class TimelinesController < ApplicationController if @universe_scope @timelines = @timelines.where(universe: @universe_scope) end + + @page_tags = PageTag.where( + page_type: Timeline.name, + page_id: @timelines.pluck(:id) + ).order(:tag) + if params.key?(:slug) + @filtered_page_tags = @page_tags.where(slug: params[:slug]) + @timelines = @timelines.select { |timeline| @filtered_page_tags.pluck(:page_id).include?(timeline.id) } + end + + # if params.key?(:favorite_only) + # @content.select!(&:favorite?) + # end + end def show @@ -32,6 +46,8 @@ class TimelinesController < ApplicationController # GET /timelines/1/edit def edit @page_title = "Editing " + @timeline.name + + @suggested_page_tags = [] raise "No Access" unless user_signed_in? && current_user == @timeline.user end @@ -55,6 +71,8 @@ class TimelinesController < ApplicationController return unless user_signed_in? && current_user == @timeline.user if @timeline.update(timeline_params) + update_page_tags + render status: 200, json: @timeline.reload else render status: 501, json: @timeline.errors @@ -69,6 +87,27 @@ class TimelinesController < ApplicationController private + # TODO: move this (and the copy in ContentController) into the has_page_tags concern? + def update_page_tags + tag_list = page_tag_params.split(PageTag::SUBMISSION_DELIMITER) + current_tags = @timeline.page_tags.pluck(:tag) + + tags_to_add = tag_list - current_tags + tags_to_remove = current_tags - tag_list + + tags_to_add.each do |tag| + @timeline.page_tags.find_or_create_by( + tag: tag, + slug: PageTagService.slug_for(tag), + user: @timeline.user + ) + end + + tags_to_remove.each do |tag| + @timeline.page_tags.find_by(tag: tag).destroy + end + end + # Use callbacks to share common setup or constraints between actions. def set_timeline @timeline = Timeline.find(params[:id]) @@ -76,7 +115,11 @@ class TimelinesController < ApplicationController # Only allow a trusted parameter "white list" through. def timeline_params - params.require(:timeline).permit(:name, :subtitle, :description, :notes, :private_notes, :universe_id, :deleted_at, :archived_at, :privacy) + params.require(:timeline).except(:page_tags).permit(:name, :subtitle, :description, :notes, :private_notes, :universe_id, :deleted_at, :archived_at, :privacy) + end + + def page_tag_params + params.require(:timeline).fetch(:page_tags, "") end def set_navbar_color diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index fbe61078..cfc59dfa 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -26,4 +26,9 @@ module ApplicationHelper def title(*parts) content_for(:title) { (parts << 'Notebook').join(' - ') } unless parts.empty? end + + def clean_links html + html.gsub!(/\(.*?)\<\/a\>/mi, '\2') + html.html_safe + end end diff --git a/app/models/concerns/has_image_uploads.rb b/app/models/concerns/has_image_uploads.rb index ddb86f1f..8ec198ec 100644 --- a/app/models/concerns/has_image_uploads.rb +++ b/app/models/concerns/has_image_uploads.rb @@ -20,6 +20,10 @@ module HasImageUploads image_uploads.sample.try(:src, format).presence || "card-headers/#{self.class.name.downcase.pluralize}.jpg" end + def first_public_image(format: :medium) + public_image_uploads.first.try(:src, format).presence || "card-headers/#{self.class.name.downcase.pluralize}.jpg" + end + def random_public_image(format: :medium) public_image_uploads.sample.try(:src, format).presence || "card-headers/#{self.class.name.downcase.pluralize}.jpg" end diff --git a/app/models/page_collections/page_collection.rb b/app/models/page_collections/page_collection.rb index 75ecfa5c..4eb982e9 100644 --- a/app/models/page_collections/page_collection.rb +++ b/app/models/page_collections/page_collection.rb @@ -51,6 +51,11 @@ class PageCollection < ApplicationRecord # If all else fails, fall back on default header "card-headers/#{self.class.name.downcase.pluralize}.jpg" end + + def first_public_image + random_public_image + end + def name title end diff --git a/app/models/page_types/universe.rb b/app/models/page_types/universe.rb index ca6d4884..9529f886 100644 --- a/app/models/page_types/universe.rb +++ b/app/models/page_types/universe.rb @@ -55,6 +55,7 @@ class Universe < ApplicationRecord has_many :contributors, dependent: :destroy has_many :documents + has_many :timelines scope :in_universe, ->(universe = nil) { where(id: universe.try(:id)) unless universe.nil? } diff --git a/app/models/timelines/timeline.rb b/app/models/timelines/timeline.rb index e84f2887..c78e8ff5 100644 --- a/app/models/timelines/timeline.rb +++ b/app/models/timelines/timeline.rb @@ -2,6 +2,7 @@ class Timeline < ApplicationRecord acts_as_paranoid include IsContentPage + include HasPageTags include Authority::Abilities self.authorizer_name = 'ExtendedContentAuthorizer' diff --git a/app/views/content/components/_collection_header.html.erb b/app/views/content/components/_collection_header.html.erb index fc9de293..d4b0e01e 100644 --- a/app/views/content/components/_collection_header.html.erb +++ b/app/views/content/components/_collection_header.html.erb @@ -1,7 +1,7 @@