Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Page URL and Path to Edit Page Dropdown and Add Slug to Search Options #920

Merged
merged 8 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
trusty-cms (7.0.19)
trusty-cms (7.0.20)
RedCloth (= 4.3.3)
activestorage-validator
acts_as_list (>= 0.9.5, < 1.3.0)
Expand Down Expand Up @@ -36,6 +36,7 @@ PATH
stringex (>= 2.7.1, < 2.9.0)
tzinfo (>= 1.2.3, < 2.1.0)
uglifier (>= 3.2, < 5.0)
uri
will_paginate (>= 3, < 5)

GEM
Expand Down Expand Up @@ -402,6 +403,7 @@ GEM
concurrent-ruby (~> 1.0)
uglifier (4.2.1)
execjs (>= 0.3.0, < 3)
uri (1.0.2)
warden (1.2.9)
rack (>= 2.0.9)
websocket-driver (0.7.6)
Expand Down
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,25 @@ Steps:

rspec

### Page Type Routes Setup
If your TrustyCMS project includes custom Page models, additional configuration is required to ensure proper URL formatting in the Admin interface. This setup allows TrustyCMS to determine the live URL of a page while editing, and to display it conveniently in the Edit Page dropdown menu.

To enable this, create the following initializer file: `config/initializers/page_type_routes.rb`

```ruby
PAGE_TYPE_ROUTES = {
BlogPage: 'blog',
DonationPage: 'donate',
ExhibitionPage: 'exhibit',
NonTicketedEventPage: 'event',
PackagePage: 'package',
PersonPage: 'biography',
ProductionPage: 'production',
VenuePage: 'venues',
}.freeze
```

This `PAGE_TYPE_ROUTES` constant maps custom Page model names to their corresponding route segments as defined in `config/routes.rb`. TrustyCMS will use these mappings to generate URLs for pages in the admin interface.

### Page Status Refresh Setup

Expand Down
15 changes: 10 additions & 5 deletions app/controllers/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ class Admin::PagesController < Admin::ResourceController
before_action :count_deleted_pages, only: [:destroy]
before_action :set_page, only: %i[edit restore]
rescue_from ActiveRecord::RecordInvalid, with: :validation_error
include Admin::NodeHelper
include Admin::PagesHelper
include Admin::UrlHelper

class PreviewStop < ActiveRecord::Rollback
def message
Expand Down Expand Up @@ -33,7 +35,7 @@ def search
@site_id = params[:site_id] || Page.current_site.id
@q = initialize_search

@pages = fetch_search_results if search_title_present?
@pages = fetch_search_results if search_query_present?
render
end

Expand All @@ -48,6 +50,8 @@ def new
def edit
verify_site_id
load_assets
@page_url = generate_page_url(request.url, @page)
@page_path = format_path(@page.path)
@versions = format_versions(@page.versions)
response_for :edit
end
Expand Down Expand Up @@ -100,12 +104,13 @@ def initialize_search
end

def fetch_search_results
@title = params.dig(:search, :title)
Page.ransack(title_cont: @title, site_id_eq: @site_id).result
@query = params.dig(:search, :query)
Page.ransack(title_cont: @query, site_id_eq: @site_id).result
.or(Page.ransack(slug_cont: @query, site_id_eq: @site_id).result)
end

def search_title_present?
params.dig(:search, :title).present?
def search_query_present?
params.dig(:search, :query).present?
end

def validation_error(e)
Expand Down
33 changes: 33 additions & 0 deletions app/helpers/admin/url_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module Admin::UrlHelper
require 'uri'

def build_url(base_url, page)
if page.class.name == 'Page'
"#{base_url}#{page.path}"
else
path = lookup_page_path(page)
path ? "#{base_url}/#{path}/#{page.slug}" : nil
end
end

def extract_base_url(url)
uri = URI.parse(url)
host = uri.host
scheme = uri.scheme
return "#{scheme}://#{host}:#{uri.port}" if host&.include?('localhost')

"#{scheme}://#{host}"
end

def generate_page_url(url, page)
base_url = extract_base_url(url)
build_url(base_url, page)
end

def lookup_page_path(page)
# Use the globally defined PAGE_TYPE_ROUTES from the parent application
return nil unless defined?(PAGE_TYPE_ROUTES) && PAGE_TYPE_ROUTES.is_a?(Hash)

PAGE_TYPE_ROUTES[page.class.name.to_sym]
end
end
2 changes: 1 addition & 1 deletion app/models/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ def self.save_order(new_position)
end

def self.ransackable_attributes(auth_object = nil)
['site_id', 'title']
['site_id', 'title', 'slug']
end

def self.parent_pages(homepage_id)
Expand Down
4 changes: 4 additions & 0 deletions app/views/admin/pages/_fields.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
.drawer
.drawer_contents#attributes
%table.fieldset
- if @page_url.present?
= render :partial => 'url_row', :locals => {:page_url => @page_url}
- if @page_path.present?
= render :partial => 'path_row', :locals => {:page_path => @page_path}
= render :partial => 'meta_row', :collection => @meta, :locals => {:f => fields}
= render :partial => 'admin/page_fields/page_field', :collection => @page.fields
= render_region :extended_metadata, :locals => {:f => fields}
Expand Down
5 changes: 5 additions & 0 deletions app/views/admin/pages/_path_row.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%tr
%th.label
%label{ :for => "page_path" }= t('path')
%td.field
= text_field_tag "page[path]", page_path, :id => "page_path_content", :class => 'textbox', :maxlength => 200, disabled: true
2 changes: 1 addition & 1 deletion app/views/admin/pages/_search_form.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
= hidden_field_tag :site_id, @site_id
.page-search
%i.fas.fa-search
= f.search_field :title, value: title, placeholder: 'Search by Page Title', id: 'search-input'
= f.search_field :query, value: @query, placeholder: 'Search by Page Title or Slug', id: 'search-input'
%input.button{ type: 'submit', value: 'Search' }
- if show_view_all_button
= link_to t("view_all_pages"), admin_pages_path, class: 'button'
5 changes: 5 additions & 0 deletions app/views/admin/pages/_url_row.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
%tr
%th.label
%label{ :for => "page_url" }= t('url')
%td.field
= text_field_tag "page[url]", page_url, :id => "page_url_content", :class => 'textbox', :maxlength => 200, disabled: true
4 changes: 2 additions & 2 deletions app/views/admin/pages/search.html.haml
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
- @page_title = t('pages') + ' - ' + default_page_title
- @page_title = t('search') + ' - ' + default_page_title

.outset
= render_region :top
= render 'search_form', title: @title, show_view_all_button: true
= render 'search_form', query: @query, show_view_all_button: true
%table.index.tablesaw#pages{ :summary => t('page_hierarchy') }
%thead
%tr
Expand Down
2 changes: 2 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ en:
saving_changes: Saving Changes
saving_preferences: Saving preferences
scheduled: "Scheduled"
search: 'Search'
search_tags: 'Search Tags:'
select:
default: '<default>'
Expand Down Expand Up @@ -322,6 +323,7 @@ en:
days: "days"
weeks: "weeks"
months: "months"
url: 'URL'
user: 'User'
username: 'Username'
username_or_email: 'Username or E-mail Address'
Expand Down
2 changes: 1 addition & 1 deletion lib/trusty_cms/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module TrustyCms
VERSION = '7.0.19'.freeze
VERSION = '7.0.20'.freeze
end
1 change: 1 addition & 0 deletions trusty_cms.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,6 @@ a general purpose content management system--not merely a blogging engine.'
s.add_dependency 'stringex', '>= 2.7.1', '< 2.9.0'
s.add_dependency 'tzinfo', '>= 1.2.3', '< 2.1.0'
s.add_dependency 'uglifier', '>= 3.2', '< 5.0'
s.add_dependency 'uri'
s.add_dependency 'will_paginate', '>= 3', '< 5'
end
Loading