Skip to content

Commit

Permalink
Use Rails' store_accessor for ingredients data
Browse files Browse the repository at this point in the history
No need to implement this ourselves, Rails has us backed.

Thanks @hmans 🎉
  • Loading branch information
tvdeyen committed Jun 29, 2021
1 parent 43c93fb commit 5d77542
Show file tree
Hide file tree
Showing 12 changed files with 48 additions and 85 deletions.
2 changes: 1 addition & 1 deletion app/controllers/alchemy/admin/ingredients_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def update
private

def ingredient_params
params.require(:ingredient).permit(@ingredient.class.ingredient_attributes)
params.require(:ingredient).permit(@ingredient.class.stored_attributes[:data])
end

def load_croppable_resource
Expand Down
16 changes: 0 additions & 16 deletions app/models/alchemy/ingredient.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,22 +59,6 @@ def create(attributes = {})
build(attributes).tap(&:save)
end

# Defines getters and setter methods for ingredient attributes
def ingredient_attributes=(attributes)
@ingredient_attributes = attributes
attributes.each do |name|
define_method name.to_sym do
data[name]
end
define_method "#{name}=" do |value|
data[name] = value
write_attribute(:data, data)
end
end
end

attr_reader :ingredient_attributes

# Defines getter and setter method aliases for related object
#
# @param [String|Symbol] The name of the alias
Expand Down
11 changes: 5 additions & 6 deletions app/models/alchemy/ingredients/audio.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ module Ingredients
# A audio attachment
#
class Audio < Alchemy::Ingredient
self.ingredient_attributes = %i[
autoplay
controls
loop
muted
]
store_accessor :data,
:autoplay,
:controls,
:muted,
:loop

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

Expand Down
9 changes: 4 additions & 5 deletions app/models/alchemy/ingredients/file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,10 @@ module Ingredients
# Attach Alchemy::Attachment into this ingredient
#
class File < Alchemy::Ingredient
self.ingredient_attributes = %i[
css_class
link_text
title
]
store_accessor :data,
:css_class,
:link_text,
:title

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

Expand Down
7 changes: 3 additions & 4 deletions app/models/alchemy/ingredients/headline.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ module Ingredients
# A text headline
#
class Headline < Alchemy::Ingredient
self.ingredient_attributes = %i[
level
size
]
store_accessor :data,
:level,
:size

before_create :set_level_and_size

Expand Down
9 changes: 4 additions & 5 deletions app/models/alchemy/ingredients/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ module Ingredients
# A URL
#
class Link < Alchemy::Ingredient
self.ingredient_attributes = %i[
link_class_name
link_target
link_title
]
store_accessor :data,
:link_class_name,
:link_target,
:link_title

alias_method :link, :value
end
Expand Down
25 changes: 12 additions & 13 deletions app/models/alchemy/ingredients/picture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,18 @@ module Ingredients
class Picture < Alchemy::Ingredient
include Alchemy::PictureThumbnails

self.ingredient_attributes = %i[
alt_tag
caption
crop_from
crop_size
css_class
link_class_name
link_target
link_title
link
render_size
title
]
store_accessor :data,
:alt_tag,
:caption,
:crop_from,
:crop_size,
:css_class,
:link_class_name,
:link_target,
:link_title,
:link,
:render_size,
:title

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

Expand Down
7 changes: 3 additions & 4 deletions app/models/alchemy/ingredients/richtext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@ module Ingredients
# A blob of richtext
#
class Richtext < Alchemy::Ingredient
self.ingredient_attributes = %i[
stripped_body
sanitized_body
]
store_accessor :data,
:stripped_body,
:sanitized_body

before_save :strip_content
before_save :sanitize_content
Expand Down
11 changes: 5 additions & 6 deletions app/models/alchemy/ingredients/text.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,11 @@ module Ingredients
# Optionally it can have a link
#
class Text < Alchemy::Ingredient
self.ingredient_attributes = %i[
link
link_target
link_title
link_class_name
]
store_accessor :data,
:link,
:link_target,
:link_title,
:link_class_name
end
end
end
19 changes: 9 additions & 10 deletions app/models/alchemy/ingredients/video.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ module Ingredients
# A video attachment
#
class Video < Alchemy::Ingredient
self.ingredient_attributes = %i[
allow_fullscreen
autoplay
controls
height
loop
muted
preload
width
]
store_accessor :data,
:allow_fullscreen,
:autoplay,
:controls,
:height,
:loop,
:muted,
:preload,
:width

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

Expand Down
5 changes: 2 additions & 3 deletions lib/generators/alchemy/ingredient/templates/model.rb.tt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
module Alchemy
module Ingredients
class <%= @class_name %> < Alchemy::Ingredient
# Set additional attributes that get stored in the data JSON column
# Alchemy will create attribute accessors for each of it.
# self.ingredient_attributes = %i[]
# Set additional attributes that get stored in the `data` JSON column
# store_accessor :data, :some, :attribute

# Set a related_object alias for convenience
# related_object_alias :some_association_name, class_name: "Some::Klass"
Expand Down
12 changes: 0 additions & 12 deletions spec/models/alchemy/ingredient_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,18 +179,6 @@
end
end

describe "ingredient_attributes=" do
let(:element) { create(:alchemy_element, :with_ingredients, name: "all_you_can_eat_ingredients") }
let(:ingredient) { element.ingredients.first }

it "persists the value in the data column" do
ingredient.value = "Welcome"
ingredient.level = "2"
ingredient.save!
expect(ingredient.reload.data[:level]).to eq("2")
end
end

describe "#partial_name" do
let(:ingredient) { Alchemy::Ingredients::Richtext.build(role: "text", element: element) }

Expand Down

0 comments on commit 5d77542

Please sign in to comment.