Class: ActiveAdminAddons::ToggleBoolBuilder

Inherits:
CustomBuilder show all
Defined in:
lib/activeadmin_addons/addons/toggle_bool_builder.rb

Instance Attribute Summary

Attributes inherited from CustomBuilder

#args, #block, #context, #model

Instance Method Summary collapse

Methods inherited from CustomBuilder

builder_method_name, create_view_methods, #initialize

Constructor Details

This class inherits a constructor from ActiveAdminAddons::CustomBuilder

Instance Method Details

#conditional_eval_hide?Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
38
39
40
# File 'lib/activeadmin_addons/addons/toggle_bool_builder.rb', line 31

def conditional_eval_hide?
  [:if, :unless].any? do |cond|
    if options[cond]
      raise ArgumentError, "'#{cond}' option should be a proc" unless options[cond].is_a?(Proc)

      result = options[cond].call(model)
      cond == :if ? !result : result
    end
  end
end

#renderObject

Raises:

  • (ArgumentError)


3
4
5
6
7
8
9
10
# File 'lib/activeadmin_addons/addons/toggle_bool_builder.rb', line 3

def render
  raise ArgumentError, 'Block should not be used in toggle bool columns' if block
  return if conditional_eval_hide?

  context.div class: 'toggle-bool-switches-container' do
    context.span toggle
  end
end

#toggleObject



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/activeadmin_addons/addons/toggle_bool_builder.rb', line 12

def toggle
  toggle_classes = 'toggle-bool-switch'
  toggle_classes += ' on' if data
  toggle_classes += ' notify-success' if options[:success_message]
  return unless enabled_controller_action?(:update)

  context.span(
    '',
    id: "toggle-#{class_name}-#{model.id}-#{attribute}",
    class: toggle_classes,
    'data-model' => class_name,
    'data-object_id' => model.id,
    'data-field' => attribute,
    'data-value' => data,
    'data-url' => resource_url,
    'data-success_message' => options[:success_message]
  )
end