Skip to content

Commit

Permalink
Add has_one association for root page (AlchemyCMS#1820)
Browse files Browse the repository at this point in the history
This should allow us to preload better, and I believe Rails will also
cache the association if necessary.
  • Loading branch information
mamhoff authored May 8, 2020
1 parent 81ae05f commit 30c2aa8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 5 deletions.
7 changes: 2 additions & 5 deletions app/models/alchemy/language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class Language < BaseRecord

before_validation :set_locale, if: -> { locale.blank? }

has_one :root_page, -> { where(parent: nil) }, class_name: "Alchemy::Page"

validates :name, presence: true
validates :page_layout, presence: true
validates :frontpage_name, presence: true
Expand Down Expand Up @@ -105,11 +107,6 @@ def label(attrib)

include Alchemy::Language::Code

# Root page
def root_page
@root_page ||= pages.language_roots.first
end

# All available locales matching this language
#
# Matching either the code (+language_code+ + +country_code+) or the +language_code+
Expand Down
1 change: 1 addition & 0 deletions spec/models/alchemy/language_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
module Alchemy
describe Language do
it { is_expected.to have_many(:nodes) }
it { is_expected.to have_one(:root_page) }

let(:default_language) { create(:alchemy_language) }
let(:language) { create(:alchemy_language, :klingon) }
Expand Down

0 comments on commit 30c2aa8

Please sign in to comment.