Skip to content

Commit

Permalink
Add Ingredients::AudioView component
Browse files Browse the repository at this point in the history
  • Loading branch information
tvdeyen committed May 4, 2023
1 parent 2766040 commit 3cb34e2
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 14 deletions.
37 changes: 37 additions & 0 deletions app/components/alchemy/ingredients/audio_view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Alchemy
module Ingredients
class AudioView < BaseView
def call
content_tag(:audio, **html_options) do
tag(:source, src: src, type: type)
end
end

def render?
!!ingredient.attachment
end

private

def src
alchemy.show_attachment_path(
ingredient.attachment,
format: ingredient.attachment.suffix
)
end

def type
ingredient.attachment.file_mime_type
end

def html_options
{
controls: ingredient.controls,
autoplay: ingredient.autoplay,
loop: ingredient.loop,
muted: ingredient.muted
}
end
end
end
end
14 changes: 14 additions & 0 deletions app/components/alchemy/ingredients/base_view.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Alchemy
module Ingredients
class BaseView < ViewComponent::Base
attr_reader :ingredient, :html_options

delegate :alchemy, to: :helpers

def initialize(ingredient, html_options: {})
@ingredient = ingredient
@html_options = html_options
end
end
end
end
15 changes: 1 addition & 14 deletions app/views/alchemy/ingredients/_audio_view.html.erb
Original file line number Diff line number Diff line change
@@ -1,14 +1 @@
<%- if audio_view.attachment -%>
<%= content_tag :audio,
controls: audio_view.controls,
autoplay: audio_view.autoplay,
loop: audio_view.loop,
muted: audio_view.muted do %>
<%= tag :source,
src: alchemy.show_attachment_path(
audio_view.attachment,
format: audio_view.attachment.suffix
),
type: audio_view.attachment.file_mime_type %>
<% end %>
<%- end -%>
<%= render Alchemy::Ingredients::AudioView.new(audio_view) -%>

0 comments on commit 3cb34e2

Please sign in to comment.