Skip to content

Commit

Permalink
Merge pull request #2084 from tf/conditional-packs
Browse files Browse the repository at this point in the history
Add if and unless options to additional packs
  • Loading branch information
tf authored Feb 26, 2024
2 parents f94f191 + 621179d commit f6f53f1
Show file tree
Hide file tree
Showing 3 changed files with 192 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,21 +37,25 @@ def scrolled_frontend_packs(entry, widget_scope:)

def scrolled_editor_packs(entry)
['pageflow-scrolled-editor'] +
Pageflow.config_for(entry).additional_editor_packs.paths +
Pageflow.config_for(entry).additional_editor_packs.paths(entry) +
scrolled_frontend_widget_type_packs(entry, :editor)
end

def scrolled_editor_stylesheet_packs(entry)
Pageflow.config_for(entry).additional_editor_packs.stylesheet_paths
Pageflow
.config_for(entry)
.additional_editor_packs
.stylesheet_paths(entry)
end

private

def scrolled_additional_frontend_packs(entry, widget_scope)
additional_packs = Pageflow.config_for(entry).additional_frontend_packs
return additional_packs.paths if widget_scope == :editor
return additional_packs.paths(entry) if widget_scope == :editor

additional_packs.paths_for_content_element_types(
entry,
ContentElement.select_used_type_names(
entry.revision,
additional_packs.content_element_type_names
Expand Down
32 changes: 23 additions & 9 deletions entry_types/scrolled/lib/pageflow_scrolled/additional_packs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ def initialize

# content_element_type_names option only takes effect for frontend
# packs.
def register(path, content_element_type_names: [], stylesheet: false)
@packs << AdditionalPack.new(path, content_element_type_names, stylesheet)
def register(path, content_element_type_names: [], stylesheet: false, if: nil, unless: nil)
@packs << AdditionalPack.new(
path,
content_element_type_names,
stylesheet,
binding.local_variable_get(:if),
binding.local_variable_get(:unless)
)
end

# @api private
Expand All @@ -20,24 +26,32 @@ def content_element_type_names
end

# @api private
def paths
@packs.map(&:path)
def paths(entry)
packs_matching_conditions(entry).map(&:path)
end

# @api private
def stylesheet_paths
@packs.select(&:stylesheet).map(&:path)
def stylesheet_paths(entry)
packs_matching_conditions(entry).select(&:stylesheet).map(&:path)
end

# @api private
def paths_for_content_element_types(type_names)
@packs.reject { |pack|
def paths_for_content_element_types(entry, type_names)
packs_matching_conditions(entry).reject { |pack|
pack.content_element_type_names.present? &&
(pack.content_element_type_names & type_names).empty?
}.map(&:path)
end

# @api private
AdditionalPack = Struct.new(:path, :content_element_type_names, :stylesheet)
AdditionalPack = Struct.new(:path, :content_element_type_names, :stylesheet, :if, :unless)

private

def packs_matching_conditions(entry)
@packs
.select { |pack| !pack.if || pack.if.call(entry:) }
.select { |pack| !pack.unless || !pack.unless.call(entry:) }
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,76 @@ module PageflowScrolled
expect(result).not_to include('pageflow-scrolled/contentElements/extra')
end

it 'supports if and unless options for additional packs in editor' do
pageflow_configure do |config|
config.for_entry_type(PageflowScrolled.entry_type) do |entry_type_config|
entry_type_config.additional_frontend_packs.register(
'some/script/if-true',
if: proc { true }
)

entry_type_config.additional_frontend_packs.register(
'some/script/if-false',
if: proc { false }
)

entry_type_config.additional_frontend_packs.register(
'some/script/unless-true',
unless: proc { true }
)

entry_type_config.additional_frontend_packs.register(
'some/script/unless-false',
unless: proc { false }
)
end
end

entry = create(:published_entry, type_name: 'scrolled')

result = helper.scrolled_frontend_packs(entry, widget_scope: :editor)

expect(result).to include('some/script/if-true')
expect(result).to include('some/script/unless-false')
expect(result).not_to include('some/script/if-false')
expect(result).not_to include('some/script/unless-true')
end

it 'supports if and unless options for additional packs outside of editor' do
pageflow_configure do |config|
config.for_entry_type(PageflowScrolled.entry_type) do |entry_type_config|
entry_type_config.additional_frontend_packs.register(
'some/script/if-true',
if: proc { true }
)

entry_type_config.additional_frontend_packs.register(
'some/script/if-false',
if: proc { false }
)

entry_type_config.additional_frontend_packs.register(
'some/script/unless-true',
unless: proc { true }
)

entry_type_config.additional_frontend_packs.register(
'some/script/unless-false',
unless: proc { false }
)
end
end

entry = create(:published_entry, type_name: 'scrolled')

result = helper.scrolled_frontend_packs(entry, widget_scope: :published)

expect(result).to include('some/script/if-true')
expect(result).to include('some/script/unless-false')
expect(result).not_to include('some/script/if-false')
expect(result).not_to include('some/script/unless-true')
end

it 'includes all react widget type packs in editor' do
pageflow_configure do |config|
config.widget_types.register(ReactWidgetType.new(name: 'customNavigation',
Expand Down Expand Up @@ -246,6 +316,59 @@ module PageflowScrolled

expect(result).to include('pageflow-scrolled/widgets/customNavigation')
end

it 'supports if and unless options for additional packs' do
pageflow_configure do |config|
config.for_entry_type(PageflowScrolled.entry_type) do |entry_type_config|
entry_type_config.additional_editor_packs.register(
'some/script/if-true',
if: proc { true }
)

entry_type_config.additional_editor_packs.register(
'some/script/if-false',
if: proc { false }
)

entry_type_config.additional_editor_packs.register(
'some/script/unless-true',
unless: proc { true }
)

entry_type_config.additional_editor_packs.register(
'some/script/unless-false',
unless: proc { false }
)
end
end

entry = create(:published_entry, type_name: 'scrolled')

result = helper.scrolled_editor_packs(entry)

expect(result).to include('some/script/if-true')
expect(result).to include('some/script/unless-false')
expect(result).not_to include('some/script/if-false')
expect(result).not_to include('some/script/unless-true')
end

it 'supports if and unless options for additional packs' do
condition = spy('callback', call: true)
pageflow_configure do |config|
config.for_entry_type(PageflowScrolled.entry_type) do |entry_type_config|
entry_type_config.additional_editor_packs.register(
'some/script',
if: condition
)
end
end

entry = create(:published_entry, type_name: 'scrolled')

helper.scrolled_editor_packs(entry)

expect(condition).to have_received(:call).with(entry:)
end
end

describe 'scrolled_editor_stylesheet_packs' do
Expand Down Expand Up @@ -277,6 +400,45 @@ module PageflowScrolled

expect(result).to eq(['pageflow-scrolled/extra-editor'])
end

it 'supports if and unless options for additional packs' do
pageflow_configure do |config|
config.for_entry_type(PageflowScrolled.entry_type) do |entry_type_config|
entry_type_config.additional_editor_packs.register(
'some/script/if-true',
stylesheet: true,
if: proc { true }
)

entry_type_config.additional_editor_packs.register(
'some/script/if-false',
stylesheet: true,
if: proc { false }
)

entry_type_config.additional_editor_packs.register(
'some/script/unless-true',
stylesheet: true,
unless: proc { true }
)

entry_type_config.additional_editor_packs.register(
'some/script/unless-false',
stylesheet: true,
unless: proc { false }
)
end
end

entry = create(:published_entry, type_name: 'scrolled')

result = helper.scrolled_editor_stylesheet_packs(entry)

expect(result).to include('some/script/if-true')
expect(result).to include('some/script/unless-false')
expect(result).not_to include('some/script/if-false')
expect(result).not_to include('some/script/unless-true')
end
end
end
end

0 comments on commit f6f53f1

Please sign in to comment.