Class: Coco::Component

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
AlpineHelper, ComponentsHelper, Coco::Concerns::AcceptsTagAttributes, Coco::Concerns::HasName, Coco::Concerns::Translatable
Defined in:
app/components/coco/component.rb

Constant Summary

Constants included from Coco::Concerns::AcceptsTagAttributes

Coco::Concerns::AcceptsTagAttributes::TAG_ATTRIBUTE_NAMES

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AlpineHelper

#x_attrs, #x_data

Methods included from TagHelper

#prefix_attr_keys, #random_id, #style_str

Methods included from ComponentsHelper

#coco_avatar, #coco_badge, #coco_button, #coco_button_group, #coco_button_to, #coco_color_picker_button, #coco_component, #coco_confirm_button, #coco_dropdown, #coco_embed, #coco_fields, #coco_form_for, #coco_form_with, #coco_icon, #coco_image, #coco_image_picker_button, #coco_layout_picker_button, #coco_link, #coco_link_to, #coco_menu, #coco_menu_button, #coco_menu_item, #coco_modal, #coco_modal_canvas, #coco_modal_dialog, #coco_modal_lightbox, #coco_notice, #coco_option_bar, #coco_page, #coco_pager_button, #coco_panel, #coco_placeholder, #coco_popover, #coco_prose, #coco_seamless_textarea, #coco_snackbar, #coco_spacer, #coco_stack, #coco_stamp, #coco_svg, #coco_system_banner, #coco_tabs, #coco_tag, #coco_toast, #coco_toolbar, #coco_tooltip, #resolve_component

Methods included from Coco::Concerns::Translatable

#tt

Methods included from Coco::Concerns::HasName

#component_name

Methods included from Coco::Concerns::AcceptsTagAttributes

#tag_attrs

Class Method Details

.after_initialize(method_name = nil, &callback) ⇒ Object



84
85
86
# File 'app/components/coco/component.rb', line 84

def after_initialize(method_name = nil, &callback)
  callbacks[:after_initialize].push(method_name || callback)
end

.before_initialize(method_name = nil, &callback) ⇒ Object



88
89
90
# File 'app/components/coco/component.rb', line 88

def before_initialize(method_name = nil, &callback)
  callbacks[:before_initialize].push(method_name || callback)
end

.before_render(method_name = nil, &callback) ⇒ Object



80
81
82
# File 'app/components/coco/component.rb', line 80

def before_render(method_name = nil, &callback)
  callbacks[:before_render].push(method_name || callback)
end

.new(**kwargs) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/components/coco/component.rb', line 62

def new(**kwargs)
  # If a `before_initialize` callback returns a hash then
  # that is used as the new kwargs value which will eventually
  # be used to initialize the component.
  kwargs = run_callbacks(:before_initialize, kwargs, reduce: true)

  obj = super

  if obj.instance_of?(Coco::Component)
    raise "`Coco::Component` must be subclassed before use"
  end

  obj.instance_variable_set(:@_args, kwargs)
  obj.send(:process_tag_attrs, kwargs)
  obj.send(:merge_option_values, kwargs) if obj.accepts_options?
  obj.class.send(:run_callbacks, :after_initialize, {}, context: obj) && obj
end

Instance Method Details

#accepts_options?Boolean

Returns:

  • (Boolean)


9
10
11
# File 'app/components/coco/component.rb', line 9

def accepts_options?
  self.class.respond_to?(:accepts_option)
end