Module: Boxcab::ActionView::Helpers

Defined in:
lib/boxcab/action_view/helpers.rb

Defined Under Namespace

Classes: BoxcabViewListIterator

Constant Summary collapse

BOXCAB_CONTENT_TYPES =
%(string text markdown image list)

Instance Method Summary collapse

Instance Method Details

#_boxcab_append_definition(definition) ⇒ Object



84
85
86
87
88
89
# File 'lib/boxcab/action_view/helpers.rb', line 84

def _boxcab_append_definition(definition)
  return if @boxcab_no_schema

  schema = @boxcab_nested_schema || _boxcab_page.schema
  schema << definition
end

#_boxcab_list(value, definition, &block) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/boxcab/action_view/helpers.rb', line 29

def _boxcab_list(value, definition, &block)
  raise 'Too many nested lists' if @boxcab_nested_schema.present?
  raise 'You need to pass a template to your list type' unless block_given?

  # look for nested schema
  nested_schema = _boxcab_look_for_schema(&block)

  definition[:fields] = nested_schema

  # no need to look for definition for each item of the list
  @boxcab_no_schema = true

  # render list
  if value
    _boxcab_list_map(value.sort { |a, b| a['_position'] <=> b['_position'] }, &block)
  elsif definition[:default].is_a?(Integer)
    _boxcab_list_map(definition[:default].times, &block)
  else
    ''
  end.tap do
    @boxcab_no_schema = false
  end
end

#_boxcab_list_map(collection, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
# File 'lib/boxcab/action_view/helpers.rb', line 53

def _boxcab_list_map(collection, &block)
  iterator = BoxcabViewListIterator.new(collection.size)

  collection.each_with_index.map do |content, index|
    @boxcab_nested_content = content.is_a?(Hash) ? content : {}
    capture(iterator.change(index), &block)
  end.tap do
    @boxcab_nested_content = nil
  end.join("\n").html_safe
end

#_boxcab_look_for_schema(&block) ⇒ Object



64
65
66
67
68
# File 'lib/boxcab/action_view/helpers.rb', line 64

def _boxcab_look_for_schema(&block)
  @boxcab_nested_schema = []
  capture(BoxcabViewListIterator.new, &block) # don't want to display it
  @boxcab_nested_schema.tap { @boxcab_nested_schema = nil }
end

#_boxcab_pageObject



80
81
82
# File 'lib/boxcab/action_view/helpers.rb', line 80

def _boxcab_page
  @boxcab_page
end

#_boxcab_string(value, definition, &block) ⇒ Object Also known as: _boxcab_text, _boxcab_markdown, _boxcab_image



70
71
72
73
74
# File 'lib/boxcab/action_view/helpers.rb', line 70

def _boxcab_string(value, definition, &block)
  definition[:default] = capture(&block) if block_given?

  (value || definition[:default] || '').html_safe
end

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



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/boxcab/action_view/helpers.rb', line 12

def boxcab_content(name, options = {}, &block)
  definition = { name: name.to_s, type: 'string' }.with_indifferent_access.merge(options)

  # do not store symbols
  definition[:type] = definition[:type].to_s

  value = (@boxcab_nested_content || _boxcab_page.content)[name]

  if BOXCAB_CONTENT_TYPES.include?(definition[:type])
    send(:"_boxcab_#{definition[:type]}", value, definition, &block).tap do
      _boxcab_append_definition(definition)
    end
  else
    raise "[Boxcab helper] Unknown type: #{definition[:type]}"
  end
end

#boxcab_page(attributes) ⇒ Object



7
8
9
10
# File 'lib/boxcab/action_view/helpers.rb', line 7

def boxcab_page(attributes)
  _boxcab_page.attributes = attributes.slice(:title, :position)
  ''
end