Sha256: a99b0cb300f6321f810f6e6ce545f65b14113141fd94bbacbeda2b17ad6cc0e4

Contents?: true

Size: 1.4 KB

Versions: 1

Compression:

Stored size: 1.4 KB

Contents

module ActiveAdminAddons
  class ToggleBoolBuilder < CustomBuilder
    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

    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],
        'data-confirm_message' => options[:confirm_message],
        'data-confirm_message_trigger' => options[:confirm_message_trigger]
      )
    end

    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
  end
end

ActiveAdminAddons::ToggleBoolBuilder.create_view_methods

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activeadmin_addons-2.0.0.beta.4 lib/activeadmin_addons/addons/toggle_bool_builder.rb