Module: StyleusHelper

Defined in:
app/helpers/styleus_helper.rb

Instance Method Summary collapse

Instance Method Details

#_coderay_highlight_wrap(note = nil, options = { type: :html }, &block) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'app/helpers/styleus_helper.rb', line 86

def _coderay_highlight_wrap(note = nil, options = { type: :html }, &block)
  captured_block = capture(&block)
  code_block     = CodeRay.scan(captured_block.to_s, options[:type])

  note_tag = note ? ('p', note, class: '__code_note') : safe_empty

  highlighted_code = "#{note_tag}#{code_block.div(:css => :class, line_numbers: :table)}"
  ('div', data: { subject: "#{options[:type]}-representation" }) do
    highlighted_code.html_safe
  end
end

#_helper_representation(&block) ⇒ Object



82
83
84
# File 'app/helpers/styleus_helper.rb', line 82

def _helper_representation(&block)
  _coderay_highlight_wrap('Rails Helper', type: :ruby, &block)
end

#_html_representation(note = nil, &block) ⇒ Object



78
79
80
# File 'app/helpers/styleus_helper.rb', line 78

def _html_representation(note = nil, &block)
  _coderay_highlight_wrap(note, type: :html, &block)
end

#_joined_component_listObject



98
99
100
# File 'app/helpers/styleus_helper.rb', line 98

def _joined_component_list
  @component_list.join.html_safe
end

#_styleus_article_wrap(options = { }, &block) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
# File 'app/helpers/styleus_helper.rb', line 54

def _styleus_article_wrap(options = { }, &block)
  captured_block = capture(&block)

  ('article', class: '__sg_article', id: options[:id]) do
    content  = safe_empty
    headline = options[:headline]
    content.concat(('h3', headline)) if headline
    content.concat(captured_block)
    content
  end
end

#_styleus_representation_wrap(options = { }, &block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
# File 'app/helpers/styleus_helper.rb', line 66

def _styleus_representation_wrap(options = { }, &block)
  captured_block = capture(&block)

  classes = '__sg_component'.concat(" #{options[:class].to_s}")
  ('section', class: classes) do
    render layout: 'layouts/styleus_context' do
      captured_block.to_s.html_safe
    end
  end
end

#component_index(headline) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/helpers/styleus_helper.rb', line 41

def component_index(headline)
  return if @components.empty?
   'nav', class: "__component_index" do
    menu_entries =  'ul' do
      (:li, @components) do |component|
        link_to component.headline, anchor: component.id
      end
    end
    ('h3', headline).concat menu_entries
  end

end

#option_bar(component) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'app/helpers/styleus_helper.rb', line 29

def option_bar(component)
   'nav', class: '__option_bar' do
     'ul' do
      html_area = ('li') { link_to t('icons.html'), component_path(component), title: t('links.titles.html'), class: 'icon', data: { toggle: "##{component.id} [data-subject=html-representation]" } }
      helper_area = ('li') { link_to t('icons.helper'), component_path(component), title: t('links.titles.helper'), class: 'icon', data: { toggle: "##{component.id} [data-subject=ruby-representation]" } } if component.helper?
      presentation_area = ('li') { link_to t('icons.expand_all'), component_path(component), title: t('links.titles.expand_all'), class: 'icon', data: { toggle: "##{component.id} [data-subject*=representation]" } }

      html_area.concat(helper_area || safe_empty).concat(presentation_area)
    end
  end
end

#safe_emptyObject



102
103
104
# File 'app/helpers/styleus_helper.rb', line 102

def safe_empty
  ''.html_safe
end

#styleus(comp_list = []) ⇒ Object



2
3
4
5
6
7
8
9
10
# File 'app/helpers/styleus_helper.rb', line 2

def styleus(comp_list = [])
  @components = Styleus::ViewComponent.from_hashes(comp_list)

  @component_list = @components.map do |component|
    wrap_component component
  end

  component_index(components_category).concat(_joined_component_list)
end

#styleus_partials(partial_path, options = { }) ⇒ Object



19
20
21
22
23
24
25
26
27
# File 'app/helpers/styleus_helper.rb', line 19

def styleus_partials(partial_path, options = { })
  sample_template = _styleus_representation_wrap(class: '__boxed') { render partial: "#{partial_path}_sample" }

  plain_template = _html_representation("#{partial_path}.html.erb") { render partial: "#{partial_path}" }

  helper_template = _helper_representation { render partial: "#{partial_path}_helper" } if options[:helper]

  sample_template.concat(plain_template).concat(helper_template || safe_empty)
end

#wrap_component(component) ⇒ Object



12
13
14
15
16
17
# File 'app/helpers/styleus_helper.rb', line 12

def wrap_component(component)
  article = _styleus_article_wrap(headline: component.headline, id: component.id) do
    styleus_partials(component.partial_path, helper: component.helper?)
  end
  option_bar(component).concat article
end