Sha256: 43a19f9cbe8a82794b2d735b1448ac465d92a3a68734a03bad3d8cae59b4c67d

Contents?: true

Size: 1.12 KB

Versions: 15

Compression:

Stored size: 1.12 KB

Contents

# frozen_string_literal: true

module Effective
  module FormLogics
    class ShowIfAny < Effective::FormLogic

      def to_html(&block)
        disabled_was = @builder.disabled

        @builder.disabled = true unless show?

        content = content_tag(:div, options.merge(input_js_options), &block)

        @builder.disabled = disabled_was

        content
      end

      def options
        { style: ('display: none;' unless show?), class: 'effective-form-logic' }
      end

      def logic_options
        { name: tag_name(args.first), value: args.second.to_json, needDisable: !show? }.merge(input_logic_options)
      end

      def input_logic_options
        args.third.kind_of?(Hash) ? args.third : {}
      end

      def validate!(args)
        return if args.third.kind_of?(Hash) && args.third[:validate] == false
        raise "expected object to respond to #{args.first}" unless object.respond_to?(args.first)
      end

      def show?
        Array(args.second).any? do |value|
          selected = object.try(args.first)
          selected == value || selected.to_s == value.to_s
        end
      end

    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
effective_bootstrap-1.19.13 app/models/effective/form_logics/show_if_any.rb
effective_bootstrap-1.19.12 app/models/effective/form_logics/show_if_any.rb
effective_bootstrap-1.19.11 app/models/effective/form_logics/show_if_any.rb
effective_bootstrap-1.19.10 app/models/effective/form_logics/show_if_any.rb
effective_bootstrap-1.19.9 app/models/effective/form_logics/show_if_any.rb
effective_bootstrap-1.19.8 app/models/effective/form_logics/show_if_any.rb
effective_bootstrap-1.19.7 app/models/effective/form_logics/show_if_any.rb
effective_bootstrap-1.19.6 app/models/effective/form_logics/show_if_any.rb
effective_bootstrap-1.19.5 app/models/effective/form_logics/show_if_any.rb
effective_bootstrap-1.19.4 app/models/effective/form_logics/show_if_any.rb
effective_bootstrap-1.19.3 app/models/effective/form_logics/show_if_any.rb
effective_bootstrap-1.19.2 app/models/effective/form_logics/show_if_any.rb
effective_bootstrap-1.19.1 app/models/effective/form_logics/show_if_any.rb
effective_bootstrap-1.19.0 app/models/effective/form_logics/show_if_any.rb
effective_bootstrap-1.18.7 app/models/effective/form_logics/show_if_any.rb