Sha256: 958a86ce0b428109c255c65446cd2a9a7976d6490d22ba6518cd2d08adae39b2

Contents?: true

Size: 1.21 KB

Versions: 25

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

# Creates a much prettier version of the file input field
class KepplerBooleanInput < SimpleForm::Inputs::Base
  def input(_wrapper_options)
    initializers
    template.content_tag(:label, '', class: 'keppler-switch') do
      input_switch + span_slider_round + label_switch
    end
  end

  def label_switch
    template.content_tag(
      :label,
      try_boolean.to_s,
      class: "inline-label #{'active' if try_boolean}"
    )
  end

  def input_switch
    template.tag(
      :input,
      class: ('active' if try_boolean),
      id: @input_id,
      name: @input_name,
      type: 'checkbox',
      checked: true,
      value: try_boolean.to_s
    )
  end

  def span_slider_round
    template.content_tag(:span, '', class: 'slider round')
  end

  private

  def try_boolean
    object.try(attribute_name) ? true : false
  end

  protected

  def initializers
    models = lookup_model_names
    other_models = lookup_model_names[1..-1]
    other_models_ar = "[#{other_models.join('][')}]" unless other_models.empty?
    attribute = reflection_or_attribute_name
    @input_id = "#{models.join('_')}_#{attribute}"
    @input_name = "#{models.first}#{other_models_ar}[#{attribute}]"
  end
end

Version data entries

25 entries across 24 versions & 1 rubygems

Version Path
keppler-2.1.22 installer/core/app/inputs/keppler_boolean_input.rb
keppler-2.1.21 installer/core/app/inputs/keppler_boolean_input.rb
keppler-2.1.20 installer/core/app/inputs/keppler_boolean_input.rb
keppler-2.1.19 installer/core/app/inputs/keppler_boolean_input.rb
keppler-2.1.18 testing-with-2.1.18/app/inputs/keppler_boolean_input.rb
keppler-2.1.18 installer/core/app/inputs/keppler_boolean_input.rb
keppler-2.1.17 installer/core/app/inputs/keppler_boolean_input.rb
keppler-2.1.16 installer/core/app/inputs/keppler_boolean_input.rb
keppler-2.1.15 installer/core/app/inputs/keppler_boolean_input.rb
keppler-2.1.14 installer/core/app/inputs/keppler_boolean_input.rb
keppler-2.1.13 installer/core/app/inputs/keppler_boolean_input.rb
keppler-2.1.12 installer/core/app/inputs/keppler_boolean_input.rb
keppler-2.1.11 installer/core/app/inputs/keppler_boolean_input.rb
keppler-2.1.10 installer/core/app/inputs/keppler_boolean_input.rb
keppler-2.1.9 installer/core/app/inputs/keppler_boolean_input.rb
keppler-2.1.8 installer/core/app/inputs/keppler_boolean_input.rb
keppler-2.1.7 installer/core/app/inputs/keppler_boolean_input.rb
keppler-2.1.6 installer/core/app/inputs/keppler_boolean_input.rb
keppler-2.1.5 installer/core/app/inputs/keppler_boolean_input.rb
keppler-2.1.4 installer/core/app/inputs/keppler_boolean_input.rb