Skip to content

Commit

Permalink
Merge pull request #2257 from tvdeyen/fix-ingredient-default-value
Browse files Browse the repository at this point in the history
Fix setting default value of ingredients
  • Loading branch information
tvdeyen authored Mar 10, 2022
2 parents 3a02faa + 4d72863 commit 7e7fe68
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 16 deletions.
7 changes: 6 additions & 1 deletion app/models/alchemy/ingredient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ class DefinitionError < StandardError; end
belongs_to :element, touch: true, class_name: "Alchemy::Element", inverse_of: :ingredients
belongs_to :related_object, polymorphic: true, optional: true

before_validation(on: :create) { self.value ||= default_value }
after_initialize :set_default_value,
if: -> { definition.key?(:default) && value.nil? }

validates :type, presence: true
validates :role, presence: true
Expand Down Expand Up @@ -161,6 +162,10 @@ def hint_translation_attribute
role
end

def set_default_value
self.value = default_value
end

# Returns the default value from ingredient definition
#
# If the value is a symbol it gets passed through i18n
Expand Down
6 changes: 4 additions & 2 deletions lib/alchemy/test_support/shared_ingredient_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

context "with element" do
before do
expect(element).to receive(:ingredient_definition_for) do
expect(element).to receive(:ingredient_definition_for).at_least(:once) do
{
settings: {
linkable: true,
Expand Down Expand Up @@ -63,7 +63,9 @@
end

before do
expect(element).to receive(:ingredient_definition_for) { definition }
expect(element).to receive(:ingredient_definition_for).at_least(:once) do
definition
end
end

it "returns ingredient definition" do
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/config/alchemy/elements.yml
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@
- role: boolean
type: Boolean
hint: true
default: true
- role: datetime
type: Datetime
hint: true
Expand Down
4 changes: 3 additions & 1 deletion spec/models/alchemy/element_ingredients_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,9 @@
end

describe "#has_value_for?" do
let!(:element) { create(:alchemy_element, :with_ingredients) }
let!(:element) do
create(:alchemy_element, :with_ingredients, name: "all_you_can_eat_ingredients")
end

context "with role existing" do
let(:ingredient) { element.ingredient_by_role(:headline) }
Expand Down
17 changes: 5 additions & 12 deletions spec/views/alchemy/ingredients/boolean_editor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,14 @@
end

context "with default value given in ingredient settings" do
let(:element) { create(:alchemy_element, name: "all_you_can_eat_ingredients") }

let(:ingredient) do
allow_any_instance_of(Alchemy::Ingredients::Boolean).to receive(:definition) do
{
role: "boolean",
type: "Boolean",
default: true,
}.with_indifferent_access
end
Alchemy::Ingredients::Boolean.create!(role: "boolean", element: element)
let(:element) do
create(:alchemy_element, :with_ingredients, name: "all_you_can_eat_ingredients")
end

it "checks the checkbox" do
is_expected.to have_selector('input[type="checkbox"][checked="checked"]')
within ".ingredient-editor boolean" do
is_expected.to have_selector('input[type="checkbox"][checked="checked"]')
end
end
end
end

0 comments on commit 7e7fe68

Please sign in to comment.