Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix setting default value of ingredients #2257

Merged
merged 1 commit into from
Mar 10, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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