Class: CodengageViewComponents::ToggleComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/codengage_view_components/toggle_component.rb

Instance Method Summary collapse

Constructor Details

#initialize(form: nil, attribute: nil, url: nil, value: nil, options: {}) ⇒ ToggleComponent

Uso:

  • Form mode:

    render Form::ToggleComponent.new(form: f, attribute: :status, options: { id: "user-active-toggle" })

  • Remote mode:

    render Form::ToggleComponent.new(url: user_path(user), options: { method: :patch, enabled: user.active? })



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/components/codengage_view_components/toggle_component.rb', line 12

def initialize(form: nil, attribute: nil, url: nil, value: nil, options: {})
  @form = form
  @attribute = attribute
  @url = url
  @value = value

  # Defaults centralizados aqui
  @id = options.fetch(:id, nil)
  @disabled = options.fetch(:disabled, false)

  @method = options.fetch(:method, :patch)
  @enabled = options.fetch(:enabled, false)
  @params = options.fetch(:params, {})

  @options = options.except(:id, :value, :disabled, :method, :enabled, :params)

  validate_configuration!
end