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

Remove implicit Site and Language creation #1763

Merged
merged 29 commits into from
Mar 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c83f915
Remove implicit Site creation
mamhoff Mar 26, 2020
f132472
Make current site and current language ID optional in controllers
mamhoff Mar 26, 2020
3ac4a15
Fix factories to not rely on implicit site/language creation
mamhoff Mar 26, 2020
604c247
Fix Language spec to not rely on implicit language creation
mamhoff Mar 26, 2020
4622f9c
Add early return to `BaseHelper#page_or_find`
mamhoff Mar 27, 2020
94bf355
Fix Pages Helper spec to use German instead of English
mamhoff Mar 27, 2020
2dafd79
Fix Controller Actions Spec
mamhoff Mar 27, 2020
ac924e4
Fix Page Layout library spec
mamhoff Mar 27, 2020
d3e714b
Use safe navigation in needs_redirect_to_primary_host
mamhoff Mar 27, 2020
45e7c32
Create Language before Metadata view spec
mamhoff Mar 27, 2020
2e50a5f
Add Site and Language creation to seeder
mamhoff Mar 27, 2020
c6137c1
Do not raise in basecontroller
mamhoff Mar 27, 2020
971e103
Make sure test setup happens in node spec
mamhoff Mar 27, 2020
242e390
Fix page spec
mamhoff Mar 27, 2020
70671f6
Fix request specs
mamhoff Mar 27, 2020
014bf08
Fix prefix_locale and add a spec
mamhoff Mar 27, 2020
00ece06
Needs specs, don't fail if no current language
mamhoff Mar 27, 2020
b472a82
Refactor Admin Pages Controller to deal with no root pages
mamhoff Mar 27, 2020
be2862b
Fix Base Controller to rely on actual data rather than mocks
mamhoff Mar 27, 2020
a16ee99
Fix Alchemy Layoutpages Controller
mamhoff Mar 29, 2020
d2744ff
Fix ApiController#show to not break without a language
mamhoff Mar 29, 2020
3c744d3
Fix Pages Controller
mamhoff Mar 29, 2020
1c88f53
Redirect to sites if no sites present.
mamhoff Mar 29, 2020
93c10e0
Fix Languages Admin Feature Spec
mamhoff Mar 29, 2020
ca042df
Fix Page Redirects spec
mamhoff Mar 29, 2020
aff7903
Redirect to Sites if navigating to Menu Admin without Site
mamhoff Mar 29, 2020
f297dcb
Fix Page Navigation feature spec
mamhoff Mar 29, 2020
f96820c
Create language in pages feature spec
mamhoff Mar 29, 2020
669943d
Refactor Bootstrapping Redirects into concerns
mamhoff Mar 29, 2020
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
2 changes: 1 addition & 1 deletion app/controllers/alchemy/admin/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def current_alchemy_site
@current_alchemy_site ||= begin
site_id = params[:site_id] || session[:alchemy_site_id]
site = Site.find_by(id: site_id) || super
session[:alchemy_site_id] = site.id
session[:alchemy_site_id] = site&.id
site
end
end
Expand Down
5 changes: 5 additions & 0 deletions app/controllers/alchemy/admin/languages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
module Alchemy
module Admin
class LanguagesController < ResourcesController
before_action unless: -> { Alchemy::Site.any? } do
flash[:warning] = Alchemy.t('Please create a site first.')
redirect_to admin_sites_path
end

def index
@query = Language.on_current_site.ransack(search_filter_params[:q])
@query.sorts = default_sort_order if @query.sorts.empty?
Expand Down
3 changes: 3 additions & 0 deletions app/controllers/alchemy/admin/layoutpages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ module Alchemy
module Admin
class LayoutpagesController < Alchemy::Admin::BaseController
authorize_resource class: :alchemy_admin_layoutpages

include Alchemy::Admin::LanguageNeededRedirect

helper Alchemy::Admin::PagesHelper

def index
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/alchemy/admin/nodes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
module Alchemy
module Admin
class NodesController < Admin::ResourcesController
include Alchemy::Admin::LanguageNeededRedirect

def index
@root_nodes = Node.language_root_nodes
end
Expand Down
30 changes: 14 additions & 16 deletions app/controllers/alchemy/admin/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,27 @@ class PagesController < Alchemy::Admin::BaseController

helper 'alchemy/pages'

before_action :load_page, except: [:index, :flush, :new, :order, :create, :copy_language_tree, :link, :sort]

authorize_resource class: Alchemy::Page, except: [:index, :tree]

before_action only: [:index, :tree, :flush, :new, :order, :create, :copy_language_tree] do
authorize! :index, :alchemy_admin_pages
end

include Alchemy::Admin::LanguageNeededRedirect

before_action :set_translation,
except: [:show]

before_action :load_page,
only: [:show, :info, :unlock, :visit, :publish, :configure, :edit, :update, :destroy, :fold,
:tree]

before_action :set_root_page,
only: [:index, :show, :sort, :order]

authorize_resource class: Alchemy::Page, except: [:index, :tree]

before_action :run_on_page_layout_callbacks,
if: :run_on_page_layout_callbacks?,
only: [:show]

def index
authorize! :index, :alchemy_admin_pages

if !@page_root
@language = Language.current
@languages_with_page_tree = Language.on_current_site.with_root_page
Expand All @@ -36,8 +38,6 @@ def index
# Returns all pages as a tree from the root given by the id parameter
#
def tree
authorize! :tree, :alchemy_admin_pages

render json: serialized_page_tree
end

Expand All @@ -56,10 +56,10 @@ def info
end

def new
@page = Page.new(layoutpage: params[:layoutpage] == 'true', parent_id: params[:parent_id])
@page_layouts = PageLayout.layouts_for_select(Language.current.id, @page.layoutpage?)
@page ||= Page.new(layoutpage: params[:layoutpage] == 'true', parent_id: params[:parent_id])
@page_layouts = PageLayout.layouts_for_select(Language.current&.id, @page.layoutpage?)
@clipboard = get_clipboard('pages')
@clipboard_items = Page.all_from_clipboard_for_select(@clipboard, Language.current.id, @page.layoutpage?)
@clipboard_items = Page.all_from_clipboard_for_select(@clipboard, Language.current&.id, @page.layoutpage?)
end

def create
Expand All @@ -68,9 +68,7 @@ def create
flash[:notice] = Alchemy.t("Page created", name: @page.name)
do_redirect_to(redirect_path_after_create_page)
else
@page_layouts = PageLayout.layouts_for_select(Language.current.id, @page.layoutpage?)
@clipboard = get_clipboard('pages')
@clipboard_items = Page.all_from_clipboard_for_select(@clipboard, Language.current.id, @page.layoutpage?)
new
render :new
end
end
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/alchemy/api/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ def load_page_by_id
end

def load_page_by_urlname
return unless Language.current

Language.current.pages.where(
urlname: params[:urlname],
language_code: params[:locale] || Language.current.code
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/alchemy/base_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ class BaseController < ApplicationController
# Sets +I18n.locale+ to current Alchemy language.
#
def set_locale
::I18n.locale = Language.current.locale
return unless Language.current
::I18n.locale = Language.current&.locale
end

def not_found_error!(msg = "Not found \"#{request.fullpath}\"")
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/alchemy/pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ def load_index_page
# @return NilClass
#
def load_page
page_not_found! unless Language.current

@page ||= Language.current.pages.contentpages.find_by(
urlname: params[:urlname],
language_code: params[:locale] || Language.current.code
Expand Down
16 changes: 16 additions & 0 deletions app/controllers/concerns/alchemy/admin/language_needed_redirect.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# frozen_string_literal: true

module Alchemy
module Admin
module LanguageNeededRedirect
extend ActiveSupport::Concern

included do
before_action unless: -> { Alchemy::Language.current }, only: :index do
flash[:warning] = Alchemy.t('Please create a language first.')
redirect_to admin_languages_path
end
end
end
end
end
2 changes: 1 addition & 1 deletion app/controllers/concerns/alchemy/site_redirects.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def enforce_primary_host_for_site
end

def needs_redirect_to_primary_host?
current_alchemy_site.redirect_to_primary_host? &&
current_alchemy_site&.redirect_to_primary_host? &&
current_alchemy_site.host != '*' &&
current_alchemy_site.host != request.host
end
Expand Down
5 changes: 5 additions & 0 deletions app/helpers/alchemy/base_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ def render_flash_notice(notice, style = :notice)
# If a String is given, it tries to find the page via page_layout
# Logs a warning if no page is given.
def page_or_find(page)
unless Language.current
warning("No default language set up")
return nil
end

if page.is_a?(String)
page = Language.current.pages.find_by(page_layout: page)
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/alchemy/language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ def current

# The root page of the current language.
def current_root_page
return unless current

current.pages.language_roots.first
end

Expand Down
37 changes: 1 addition & 36 deletions app/models/alchemy/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ class Site < BaseRecord

scope :published, -> { where(public: true) }

# Callbacks
before_create :create_default_language!, unless: -> { languages.any? }

# concerns
include Alchemy::Site::Layout

Expand Down Expand Up @@ -64,7 +61,7 @@ def current
end

def default
Site.first || create_default_site!
Site.first
end

def find_for_host(host)
Expand All @@ -81,38 +78,6 @@ def find_in_aliases(host)
site.aliases.split.include?(host) if site.aliases.present?
end
end

private

def create_default_site!
default_site = Alchemy::Config.get(:default_site)
if default_site
create!(name: default_site['name'], host: default_site['host'])
else
raise DefaultSiteNotFoundError
end
end
end

private

# If no languages are present, create a default language based
# on the host app's Alchemy configuration.
def create_default_language!
default_language = Alchemy::Config.get(:default_language)
if default_language
languages.build(
name: default_language['name'],
language_code: default_language['code'],
locale: default_language['code'],
frontpage_name: default_language['frontpage_name'],
page_layout: default_language['page_layout'],
public: true,
default: true
)
else
raise DefaultLanguageNotFoundError
end
end
end
end
2 changes: 1 addition & 1 deletion config/alchemy/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ default_site:
name: Default Site
host: '*'

# This is the default language when a new site gets created.
# This is the default language when seeding.
default_language:
code: en
name: English
Expand Down
3 changes: 2 additions & 1 deletion lib/alchemy/configuration_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def multi_language?
# matches the current I18n.locale then the prefix os omitted.
# Also, if only one published language exists.
#
def prefix_locale?(locale = Language.current.code)
def prefix_locale?(locale = Language.current&.code)
return false unless locale
multi_language? && locale != ::I18n.default_locale.to_s
end

Expand Down
1 change: 1 addition & 0 deletions lib/alchemy/page_layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ def page_with_layout_existing?(layout)
# page_layouts: [default_intro]
#
def available_on_site?(layout)
return false unless Alchemy::Site.current
Alchemy::Site.current.definition.blank? ||
Alchemy::Site.current.definition.fetch('page_layouts', []).include?(layout['name'])
end
Expand Down
31 changes: 31 additions & 0 deletions lib/alchemy/seeder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def try_seed_pages
log "There are already pages present in your database. " \
"Please use `rake db:reset' if you want to rebuild your database.", :skip
else
create_default_site! unless Alchemy::Site.default
create_default_language! unless Alchemy::Language.default
seed_pages if contentpages.present?
seed_layoutpages if layoutpages.present?
end
Expand Down Expand Up @@ -108,6 +110,35 @@ def create_page(draft, attributes = {})
create_page(child, parent: page)
end
end

# If no languages are present, create a default language based
# on the host app's Alchemy configuration.
def create_default_language!
default_language = Alchemy::Config.get(:default_language)
if default_language
Alchemy::Language.create!(
name: default_language['name'],
language_code: default_language['code'],
locale: default_language['code'],
frontpage_name: default_language['frontpage_name'],
page_layout: default_language['page_layout'],
public: true,
default: true,
site: Alchemy::Site.default
)
else
raise DefaultLanguageNotFoundError
end
end

def create_default_site!
default_site = Alchemy::Config.get(:default_site)
if default_site
Alchemy::Site.create!(name: default_site['name'], host: default_site['host'])
else
raise DefaultSiteNotFoundError
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/alchemy/test_support/factories/language_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

public { true }

site { Alchemy::Site.default }
site { Alchemy::Site.default || create(:alchemy_site, :default) }

trait :klingon do
name { 'Klingon' }
Expand Down
4 changes: 2 additions & 2 deletions lib/alchemy/test_support/factories/node_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

FactoryBot.define do
factory :alchemy_node, class: 'Alchemy::Node' do
site { Alchemy::Site.default }
language { Alchemy::Language.default }
site { Alchemy::Site.default || create(:alchemy_site) }
language { Alchemy::Language.default || create(:alchemy_language) }
name { 'A Node' }

trait :with_page do
Expand Down
2 changes: 1 addition & 1 deletion lib/alchemy/test_support/factories/page_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

parent_id do
(Alchemy::Page.find_by(language_root: true) ||
FactoryBot.create(:alchemy_page, :language_root)).id
FactoryBot.create(:alchemy_page, :language_root, language: language)).id
end

# This speeds up creating of pages dramatically.
Expand Down
28 changes: 24 additions & 4 deletions spec/controllers/alchemy/admin/languages_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
end

describe "#index" do
context 'without a site' do
it "redirects to the sites admin" do
get :index
expect(response).to redirect_to(admin_sites_path)
end
end

context "with multiple sites" do
let!(:default_site_language) do
create(:alchemy_language)
Expand All @@ -31,6 +38,8 @@
end

context "editor users" do
let!(:site) { create(:alchemy_site) }

before do
authorize_user(:as_editor)
end
Expand All @@ -43,10 +52,21 @@
end

describe "#new" do
it "has default language's page_layout set" do
get :new
expect(assigns(:language).page_layout).
to eq(Alchemy::Config.get(:default_language)['page_layout'])
context 'without a site' do
it "redirects to the sites admin" do
get :index
expect(response).to redirect_to(admin_sites_path)
end
end

context 'with a site' do
let!(:site) { create(:alchemy_site) }

it "has default language's page_layout set" do
get :new
expect(assigns(:language).page_layout).
to eq(Alchemy::Config.get(:default_language)['page_layout'])
end
end
end

Expand Down
Loading