Module: StoryblokHelper

Includes:
Rubyblok::Mixins::ModelCacheClass
Defined in:
lib/rubyblok/helpers/storyblok_helper.rb

Instance Method Summary collapse

Methods included from Rubyblok::Mixins::ModelCacheClass

#model_cache_class

Instance Method Details

#get_story_content(slug) ⇒ Object



61
62
63
# File 'lib/rubyblok/helpers/storyblok_helper.rb', line 61

def get_story_content(slug)
  get_story(slug)['content'].to_dot
end

#invisible_on_production?(blok) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/rubyblok/helpers/storyblok_helper.rb', line 38

def invisible_on_production?(blok)
  Rails.env.production? && blok.fetch('invisible_on_production', false)
end

#rubyblok_blocks_tag(bloks) ⇒ Object

rubocop:enable Rails/OutputSafety



52
53
54
55
56
57
58
59
# File 'lib/rubyblok/helpers/storyblok_helper.rb', line 52

def rubyblok_blocks_tag(bloks)
  template =
    %{<% bloks.each do |blok| %>
          <%= rubyblok_component_tag(blok:) %>
      <% end %>}

  render_inline_partial template:, locals: { bloks: }
end

#rubyblok_component_tag(blok:, partial: blok.component) ⇒ Object



32
33
34
35
36
# File 'lib/rubyblok/helpers/storyblok_helper.rb', line 32

def rubyblok_component_tag(blok:, partial: blok.component)
  return if invisible_on_production?(blok)

  render_partial(partial:, locals: { blok: }).prepend(rubyblok_editable_tag(blok).to_s)
end

#rubyblok_content_tag(content) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/rubyblok/helpers/storyblok_helper.rb', line 8

def (content)
  return if content.blank?

  if content.is_a?(String)
    rubyblok_markdown_tag(content)
  else
    rubyblok_richtext_tag(content)
  end
end

#rubyblok_markdown_tag(content) ⇒ Object

rubocop:disable Rails/OutputSafety



43
44
45
# File 'lib/rubyblok/helpers/storyblok_helper.rb', line 43

def rubyblok_markdown_tag(content)
  markdown_renderer.render(content).html_safe
end

#rubyblok_richtext_tag(content) ⇒ Object



47
48
49
# File 'lib/rubyblok/helpers/storyblok_helper.rb', line 47

def rubyblok_richtext_tag(content)
  rich_text_renderer.render(content).html_safe
end

#rubyblok_story_tag(slug) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rubyblok/helpers/storyblok_helper.rb', line 18

def (slug)
  story = get_story(slug)
  content = story['content'].to_dot

  template = <<-ERB
    <% cache_key = "rubyblok/#{story['id']}-#{Digest::SHA1.hexdigest(story.to_json)}" %>
    <% cache_if use_cache, cache_key do %>
      <%= rubyblok_component_tag(partial: content.component, blok: content) %>
    <% end %>
  ERB

  render_inline_partial template:, locals: { content:, use_cache: Rubyblok.configuration.cache_views }
end