diff --git a/app/admin/homepage.rb b/app/admin/homepage.rb index 1f7f36090..ec6c43f60 100644 --- a/app/admin/homepage.rb +++ b/app/admin/homepage.rb @@ -27,10 +27,13 @@ config.sort_order = 'published_desc' # Customizing the action items (buttons) on the show page - action_item :publish, priority: 0, only: :show do + action_item :publish, priority: 1, only: :show do publish_action_str = resource.published? ? 'Unpublish' : 'Publish' link_to publish_action_str, publish_admin_homepage_path(resource), method: :post end + action_item :preview, priority: 0, only: :show do + link_to "Preview", preview_admin_homepage_path(resource), method: :get + end member_action :publish, priority: 0, method: :post do title = resource.internal_title.present? ? resource.internal_title : "Homepage #{resource.id}" @@ -51,6 +54,11 @@ redirect_back fallback_location: root_path, notice: message end + member_action :preview, method: :get do + homepage_id = resource.id + redirect_to "/homepages/#{homepage_id}/preview" + end + index do id_column column :internal_title @@ -59,6 +67,7 @@ actions do |homepage| publish_action_str = homepage.published ? "Unpublish" : "Publish" item publish_action_str, publish_admin_homepage_path(homepage), method: :post + item "Preview", preview_admin_homepage_path(homepage), method: :get end end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 7c6aed734..00256b5a4 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -10,9 +10,35 @@ def index @homepage = Homepage.where(published: true)&.first if @homepage current_features = @homepage&.homepage_features - @section_one_features = current_features&.where(section_id: 1).first(3) - @section_two_features = current_features&.where(section_id: 2).first(3) - @section_three_features = current_features&.where(section_id: 3).first(3) + @section_one_features = current_features&.where(section_id: 1)&.first(3) + @section_two_features = current_features&.where(section_id: 2)&.first(3) + @section_three_features = current_features&.where(section_id: 3)&.first(3) + end + end + + def preview + if current_user&.has_role?(:admin) + @@dropdown_categories = nil + @dropdown_communities = nil + @homepage = nil + begin + @homepage = Homepage.find(params[:id]) + rescue ActiveRecord::RecordNotFound + warning = "That homepage does not exist" + flash[:warning] = warning + redirect_to admin_homepages_path, warning: warning + return + end + current_features = @homepage&.homepage_features + if current_features + @section_one_features = current_features&.where(section_id: 1)&.first(3) + @section_two_features = current_features&.where(section_id: 2)&.first(3) + @section_three_features = current_features&.where(section_id: 3)&.first(3) + end + flash.now[:notice] = "This is a preview of unpublished content" + render 'index' + else + redirect_to root_path end end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 6e73962af..8623da356 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -217,6 +217,7 @@ def private_path? # https://github.com/digital-analytics-program/gov-wide-code#a params["controller"] == "admin" || # all admin pages (params["controller"] == "page" && !@page&.published?) || # unpublished PageBuilder pages (params["controller"] == "practices" && !["show", "index", "search"].include?(params["action"])) || # practice editor pages - (params["controller"] == "practices" && params["action"] == "show" && !@practice.is_public) # internal or unpublished practices + (params["controller"] == "practices" && params["action"] == "show" && !@practice.is_public) || # internal or unpublished practices + params["controller"] == "home" && params["action"] == "preview" # homepage previews end end diff --git a/app/models/homepage_feature.rb b/app/models/homepage_feature.rb index 4b92f7e14..f0bd42cfc 100644 --- a/app/models/homepage_feature.rb +++ b/app/models/homepage_feature.rb @@ -19,7 +19,7 @@ def image_s3_presigned_url(style = nil) end def self.column_size(item_count = 3) - if item_count >= 3 + if item_count == nil || item_count >= 3 'three-column-layout' else # update this later to deal with incoming designs 'two-column-layout' diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index 3607f3248..0fda4cfc7 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -44,6 +44,7 @@ style="display: none;" > + <% if @dropdown_practices.present? && @dropdown_categories.present? && @dropdown_communities.present? %>