Skip to content

Commit

Permalink
Merge pull request #2425 from tvdeyen/document-ingredient-settings
Browse files Browse the repository at this point in the history
Define allowed settings in ingredients
  • Loading branch information
tvdeyen authored Feb 1, 2023
2 parents 72e36a9 + d0753de commit 0e13375
Show file tree
Hide file tree
Showing 15 changed files with 71 additions and 1 deletion.
10 changes: 10 additions & 0 deletions app/models/alchemy/ingredient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,16 @@ def translated_label_for(role, element_name = nil)
default: Alchemy.t("ingredient_roles.#{role}", default: role.humanize),
)
end

# Allow to define settings on the ingredient definition
def allow_settings(settings)
@allowed_settings = Array(settings)
end

# Allowed settings on the ingredient
def allowed_settings
@allowed_settings ||= []
end
end

# The value or the related object if present
Expand Down
2 changes: 2 additions & 0 deletions app/models/alchemy/ingredients/audio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ class Audio < Alchemy::Ingredient
:muted,
:loop

allow_settings %i[except only]

related_object_alias :attachment, class_name: "Alchemy::Attachment"

delegate :name, to: :attachment, allow_nil: true
Expand Down
2 changes: 2 additions & 0 deletions app/models/alchemy/ingredients/datetime.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module Ingredients
# A datetime value
#
class Datetime < Alchemy::Ingredient
allow_settings %i[date_format]

def value
ActiveRecord::Type::DateTime.new.cast(self[:value])
end
Expand Down
7 changes: 7 additions & 0 deletions app/models/alchemy/ingredients/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ class File < Alchemy::Ingredient
:link_text,
:title

allow_settings %i[
css_classes
except
link_text
only
]

related_object_alias :attachment, class_name: "Alchemy::Attachment"

delegate :name, to: :attachment, allow_nil: true
Expand Down
6 changes: 6 additions & 0 deletions app/models/alchemy/ingredients/headline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ class Headline < Alchemy::Ingredient
:level,
:size

allow_settings %i[
anchor
levels
sizes
]

before_create :set_level_and_size

def preview_text(maxlength = 30)
Expand Down
2 changes: 2 additions & 0 deletions app/models/alchemy/ingredients/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ class Link < Alchemy::Ingredient
:link_target,
:link_title

allow_settings %i[text]

alias_method :link, :value
end
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/alchemy/ingredients/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Ingredients
class Node < Alchemy::Ingredient
related_object_alias :node, class_name: "Alchemy::Node"

allow_settings %i[query_params]

# The first 30 characters of node name
#
# Used by the Element#preview_text method.
Expand Down
2 changes: 2 additions & 0 deletions app/models/alchemy/ingredients/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Ingredients
class Page < Alchemy::Ingredient
related_object_alias :page, class_name: "Alchemy::Page"

allow_settings %i[query_params]

# The first 30 characters of page name
#
# Used by the Element#preview_text method.
Expand Down
11 changes: 11 additions & 0 deletions app/models/alchemy/ingredients/picture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ class Picture < Alchemy::Ingredient

related_object_alias :picture, class_name: "Alchemy::Picture"

allow_settings %i[
crop
css_classes
fixed_ratio
linkable
size
sizes
srcset
upsample
]

# The first 30 characters of the pictures name
#
# Used by the Element#preview_text method.
Expand Down
6 changes: 6 additions & 0 deletions app/models/alchemy/ingredients/richtext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ class Richtext < Alchemy::Ingredient
:stripped_body,
:sanitized_body

allow_settings %i[
plain_text
sanitizer
tinymce
]

before_save :strip_content
before_save :sanitize_content

Expand Down
1 change: 1 addition & 0 deletions app/models/alchemy/ingredients/select.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Ingredients
# A text value from a select box
#
class Select < Alchemy::Ingredient
allow_settings %i[display_inline select_values]
end
end
end
8 changes: 8 additions & 0 deletions app/models/alchemy/ingredients/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ class Text < Alchemy::Ingredient
:link_target,
:link_title,
:link_class_name

allow_settings %i[
anchor
disable_link
display_inline
input_type
linkable
]
end
end
end
2 changes: 2 additions & 0 deletions app/models/alchemy/ingredients/video.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Video < Alchemy::Ingredient
:preload,
:width

allow_settings %i[except only]

related_object_alias :attachment, class_name: "Alchemy::Attachment"

delegate :name, to: :attachment, allow_nil: true
Expand Down
9 changes: 9 additions & 0 deletions spec/models/alchemy/ingredient_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@
end
end

describe ".allow_settings" do
subject(:allow_settings) { described_class.allow_settings(:linkable) }

it "sets allowed_settings" do
allow_settings
expect(described_class.allowed_settings).to eq([:linkable])
end
end

describe "#settings" do
let(:ingredient) { Alchemy::Ingredients::Text.new(role: "headline", element: element) }

Expand Down
2 changes: 1 addition & 1 deletion spec/views/alchemy/ingredients/picture_editor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
rendered
end

context "with settings[:deletable] being nil" do
context "with settings[:linkable] being nil" do
it "should not render a button to link and unlink the picture" do
is_expected.to have_selector("a .icon.fa-link")
is_expected.to have_selector("a .icon.fa-unlink")
Expand Down

0 comments on commit 0e13375

Please sign in to comment.