Skip to content

Commit

Permalink
Set inverse of draft and published_revision associations on entry
Browse files Browse the repository at this point in the history
This enables an edge-case where the account of an example entry is
changed transiently before creating a theme customization
preview. Make sure feature flags of the account are taken into account
correctly.

Generally, this should be an improvement since draft and published
revisions no longer need to look up there entries in a separate query.

REDMINE-20474
  • Loading branch information
tf committed Nov 16, 2023
1 parent fa8858e commit 00393d4
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/models/pageflow/entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ class PasswordMissingError < StandardError

has_many :imports, class_name: 'Pageflow::FileImport', dependent: :destroy

has_one :draft, -> { editable }, :class_name => 'Revision'
has_one :published_revision, -> { published }, :class_name => 'Revision'
has_one :draft, -> { editable }, class_name: 'Revision', inverse_of: :entry
has_one :published_revision, -> { published }, class_name: 'Revision', inverse_of: :entry

has_one :edit_lock, :dependent => :destroy

Expand Down
16 changes: 16 additions & 0 deletions spec/models/pageflow/entry_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ module Pageflow
end
end

describe '#draft' do
it 'sets inverse of association' do
entry = create(:entry)

expect(entry.draft.entry).to be(entry)
end
end

describe '#published_revision' do
it 'sets inverse of association' do
entry = create(:entry, :published)

expect(entry.published_revision.entry).to be(entry)
end
end

describe '#entry_type' do
it 'returns entry type' do
pageflow_configure do |config|
Expand Down
21 changes: 19 additions & 2 deletions spec/models/pageflow/revision_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -632,9 +632,26 @@ module Pageflow
config.themes.register(:named_theme)
end

site = build(:revision, theme_name: 'named_theme')
revision = build(:revision, theme_name: 'named_theme')

expect(site.theme.name).to eq('named_theme')
expect(revision.theme.name).to eq('named_theme')
end

it 'honors feature flags of transiently set account' do
pageflow_configure do |config|
config.themes.register(:some_theme, some: :option)

config.features.register('override_theme') do |feature_config|
feature_config.themes.register(:some_theme, some: :override)
end
end

entry = create(:entry, draft_attributes: {theme_name: 'some_theme'})
account_with_feature_flag = create(:account, with_feature: 'override_theme')

entry = Entry.find(entry.id)
entry.account = account_with_feature_flag
expect(entry.draft.theme.options[:some]).to eq(:override)
end
end

Expand Down

0 comments on commit 00393d4

Please sign in to comment.