More explicit initializer for rails 7 compatibility

This commit is contained in:
2022-09-11 08:47:20 -05:00
parent 3aa204aa30
commit 30e5a72dc0
4 changed files with 71 additions and 57 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ source "http://rubygems.org"
# development dependencies will be added by default to the :development group. # development dependencies will be added by default to the :development group.
gemspec gemspec
gem "rails", " ~> 4.2.3" gem "rails", " ~> 7.0.1"
group :assets do group :assets do
gem 'sass-rails' #, ' ~> 5.0.0' gem 'sass-rails' #, ' ~> 5.0.0'
gem 'bourbon', '~> 4.3.1' gem 'bourbon', '~> 4.3.1'
+1 -1
View File
@@ -120,7 +120,7 @@ If you add a method `cmtool_locale` to your application controller Cmtool will
take this value: take this value:
```ruby ```ruby
class ApplicationController class ApplicationController
before_filter :set_locale before_action :set_locale
private private
+15 -2
View File
@@ -13,15 +13,28 @@ require 'tinymce-rails'
module Cmtool module Cmtool
class Engine < ::Rails::Engine class Engine < ::Rails::Engine
isolate_namespace Cmtool isolate_namespace Cmtool
initializer "cmtool.build_menu" do initializer 'cmtool.build_menu', after: 'load_config_initializers' do |app|
require 'email_validator' require 'email_validator'
require 'bourbon' #require 'bourbon'
require 'slim-rails' require 'slim-rails'
require 'paperclip' require 'paperclip'
require 'devise' require 'devise'
require 'devise_simply_stored' require 'devise_simply_stored'
require 'jquery-rails' require 'jquery-rails'
require 'tinymce-rails' require 'tinymce-rails'
require 'page' # app/models/page.rb
require 'user' # app/models/user.rb
require 'cmtool/yml_file'
require 'cmtool/keyword'
require 'cmtool/news'
require 'cmtool/faq'
require 'cmtool/quote'
require 'cmtool/contact_form'
require 'cmtool/newsletter_subscription'
require 'cmtool/image'
require 'cmtool/directory'
Cmtool::Menu.register do Cmtool::Menu.register do
group label: :site do group label: :site do
title t('cmtool.menu.site.title') title t('cmtool.menu.site.title')
+54 -53
View File
@@ -1,67 +1,68 @@
module Cmtool module Cmtool
module Includes module Includes
module PagesController module PagesController
extend ActiveSupport::Concern extend ActiveSupport::Concern
def home
page_name = "home" def home
@page = find_page(page_name) page_name = 'home'
@sub_pages = @page.children.select{|child| child.in_menu.present? } @page = find_page(page_name)
render template: "pages/#{page_name}", layout: @page.layout.presence || ::Page.layouts.first.to_s, formats: [:html] @sub_pages = @page.children.select{|child| child.in_menu.present? }
render template: "pages/#{page_name}", layout: @page.layout.presence || ::Page.layouts.first.to_s, formats: [:html]
end
# General catcher for pages
def show
@page = ::Page.find_by_name_and_locale(params[:name], I18n.locale.to_s)
not_found and return unless @page
@sub_pages = [@page] + @page.children.select{|child| child.in_menu.present? }
template = "pages/#{@page.name}"
if template_exists?(template)
render template: template, formats: [:html], layout: @page.layout.presence || ::Page.layouts.first
return
else
render formats: [:html], layout: @page.layout.presence || ::Page.layouts.first
end end
end
# General catcher for pages def not_found
def show @page = ::Page.find_by_name_and_locale('404', I18n.locale.to_s) || ::Page.new(name: '404', body: "404 Page Not Found")
@page = ::Page.find_by_name_and_locale(params[:name], I18n.locale.to_s) @sub_pages = []
not_found and return unless @page page_name_extension = params[:name].to_s[/(?<=\.)\w{2,4}$/]
format = %w[html css js json].find{|f| f == request.format.symbol.to_s or f == page_name_extension }.try(:to_sym) || :html
@sub_pages = [@page] + @page.children.select{|child| child.in_menu.present? } case format
template = "pages/#{@page.name}" when :json
if template_exists?(template) render json: {}, status: 404
render template: template, formats: [:html], layout: @page.layout.presence || ::Page.layouts.first when :html
return render template: 'pages/404', formats: [:html], layout: @page.layout.presence || ::Page.layouts.first.to_s, status: 404
else else
render formats: [:html], layout: @page.layout.presence || ::Page.layouts.first render nothing: true, status: 404
end
end end
end
def not_found def sitemap
@page = ::Page.find_by_name_and_locale('404', I18n.locale.to_s) || ::Page.new(name: '404', body: "404 Page Not Found") respond_to do |format|
@sub_pages = [] format.xml do
page_name_extension = params[:name].to_s[/(?<=\.)\w{2,4}$/] pages_xml = ::Page.for_sitemap.map do |page|
format = %w[html css js json].find{|f| f == request.format.symbol.to_s or f == page_name_extension }.try(:to_sym) || :html uri = page_url(page.name, locale: page.locale)
case format "<url><loc>#{uri}</loc><lastmod>#{page.updated_at.strftime('%Y-%m-%d')}</lastmod></url>"
when :json end.join("\n")
render json: {}, status: 404 result = <<-XML
when :html
render template: 'pages/404', formats: [:html], layout: @page.layout.presence || ::Page.layouts.first.to_s, status: 404
else
render nothing: true, status: 404
end
end
def sitemap
respond_to do |format|
format.xml do
pages_xml = ::Page.for_sitemap.map do |page|
uri = page_url(page.name, locale: page.locale)
"<url><loc>#{uri}</loc><lastmod>#{page.updated_at.strftime('%Y-%m-%d')}</lastmod></url>"
end.join("\n")
result = <<-XML
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
#{pages_xml} #{pages_xml}
</urlset> </urlset>
XML XML
render xml: result render xml: result
end
end end
end end
end
private private
def find_page(name) def find_page(name)
::Page.find_by_name_and_locale(name, I18n.locale.to_s) || ::Page.new(name: name, locale: I18n.locale.to_s) ::Page.find_by_name_and_locale(name, I18n.locale.to_s) || ::Page.new(name: name, locale: I18n.locale.to_s)
end end
end end
end end
end end