Class: ActiveElement::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/active_element/component.rb

Overview

Exposed by ‘component` view helper, used as general entrypoint for component creation.

Instance Method Summary collapse

Constructor Details

#initialize(controller) ⇒ Component

Returns a new instance of Component.



6
7
8
# File 'lib/active_element/component.rb', line 6

def initialize(controller)
  @controller = controller
end

Instance Method Details

#button(title = nil, url = nil, type: 'primary', float: nil, **kwargs, &block) ⇒ Object



51
52
53
54
55
# File 'lib/active_element/component.rb', line 51

def button(title = nil, url = nil, type: 'primary', float: nil, **kwargs, &block)
  render Components::Button.new(
    controller, nil, { path: url, title: title }, type: type, float: float, **kwargs, &block
  )
end

#destroy_button(record = nil, flag_or_options = true, **kwargs) ⇒ Object

rubocop:disable Style/OptionalBooleanParameter



43
44
45
46
47
48
49
# File 'lib/active_element/component.rb', line 43

def destroy_button(record = nil, flag_or_options = true, **kwargs) # rubocop:disable Style/OptionalBooleanParameter
  confirm = kwargs.delete(:confirm) { true }

  render Components::Button.new(
    controller, record, flag_or_options, type: :destroy, confirm: confirm, **kwargs
  )
end

#edit_button(record = nil, flag_or_options = true, **kwargs) ⇒ Object

rubocop:disable Style/OptionalBooleanParameter



39
40
41
# File 'lib/active_element/component.rb', line 39

def edit_button(record = nil, flag_or_options = true, **kwargs) # rubocop:disable Style/OptionalBooleanParameter
  render Components::Button.new(controller, record, flag_or_options, type: :edit, **kwargs)
end

#form(fields: nil, submit: nil, item: nil, **kwargs) ⇒ Object



78
79
80
# File 'lib/active_element/component.rb', line 78

def form(fields: nil, submit: nil, item: nil, **kwargs)
  render Components::Form.new(controller, fields: fields, submit: submit, item: item, **kwargs)
end

#json(key, object) ⇒ Object



57
58
59
# File 'lib/active_element/component.rb', line 57

def json(key, object)
  render Components::Json.new(controller, object: object, key: key)
end


27
28
29
# File 'lib/active_element/component.rb', line 27

def navbar(**kwargs)
  render Components::Navbar.new(controller, **kwargs)
end

#new_button(record = nil, flag_or_options = true, **kwargs) ⇒ Object

rubocop:disable Style/OptionalBooleanParameter



35
36
37
# File 'lib/active_element/component.rb', line 35

def new_button(record = nil, flag_or_options = true, **kwargs) # rubocop:disable Style/OptionalBooleanParameter
  render Components::Button.new(controller, record, flag_or_options, type: :new, **kwargs)
end

#page_description(content) ⇒ Object



23
24
25
# File 'lib/active_element/component.rb', line 23

def page_description(content)
  render Components::PageDescription.new(controller, content: content)
end

#page_section_title(section_title, **kwargs) ⇒ Object



18
19
20
21
# File 'lib/active_element/component.rb', line 18

def page_section_title(section_title, **kwargs)
  kwargs[:class] ||= 'mt-3'
  controller.(:h4, section_title, **kwargs)
end

#page_subtitle(subtitle, **kwargs) ⇒ Object



14
15
16
# File 'lib/active_element/component.rb', line 14

def page_subtitle(subtitle, **kwargs)
  controller.(:h3, subtitle, **kwargs)
end

#page_title(title, **kwargs) ⇒ Object



10
11
12
# File 'lib/active_element/component.rb', line 10

def page_title(title, **kwargs)
  controller.(:h2, title, **kwargs)
end

#show_button(record = nil, flag_or_options = true, **kwargs) ⇒ Object

rubocop:disable Style/OptionalBooleanParameter



31
32
33
# File 'lib/active_element/component.rb', line 31

def show_button(record = nil, flag_or_options = true, **kwargs) # rubocop:disable Style/OptionalBooleanParameter
  render Components::Button.new(controller, record, flag_or_options, type: :show, **kwargs)
end

#table(**kwargs) ⇒ Object

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
74
75
76
# File 'lib/active_element/component.rb', line 66

def table(**kwargs)
  class_name = kwargs.delete(:class) { default_class(kwargs[:collection], kwargs[:item], kwargs[:model_name]) }

  return render item_table(controller, class_name: class_name, **kwargs) if kwargs.key?(:item)

  if kwargs.key?(:collection)
    return render collection_table(controller, class_name: class_name, params: params, **kwargs)
  end

  raise ArgumentError, 'Must provide one of `item` or `collection`.'
end

#tabs(class: nil, &block) ⇒ Object



61
62
63
64
# File 'lib/active_element/component.rb', line 61

def tabs(class: nil, &block)
  class_name = binding.local_variable_get(:class) || 'tabs'
  render Components::Tabs.new(controller, class_name: class_name, &block)
end