Sha256: 6aa0d0ea9edefd285b3429e01abee32262f43eeeae846e4bea97b5c29bd67e0c

Contents?: true

Size: 1.74 KB

Versions: 6

Compression:

Stored size: 1.74 KB

Contents

# -*- encoding: utf-8 -*-
# -*- frozen_string_literal: true -*-
# -*- warn_indent: true -*-

module RailsBootstrapForm
  module Helpers
    module Labels
      extend ActiveSupport::Concern

      def self.included(base_class)
        def draw_label(attribute, options, bootstrap)
          unless bootstrap.skip_label? && !bootstrap.floating?
            label_options = {
              class: label_classes(attribute, options, bootstrap)
            }
            label_options[:for] = options[:id] if options[:id].present?
            label_text = label_text(attribute, bootstrap)

            label(attribute, label_text, label_options)
          end
        end

        def label_classes(attribute, options, bootstrap)
          classes = []
          classes << label_layout_classes(bootstrap)
          classes << bootstrap.additional_label_class
          classes << bootstrap.hide_class if hide_class_required?(bootstrap)
          classes << "required" if is_field_required?(attribute, options)
          classes << "is-invalid" if is_invalid?(attribute)
          classes.flatten.compact
        end

        def label_layout_classes(bootstrap)
          if bootstrap.layout_horizontal?
            [bootstrap.label_col_class, bootstrap.label_col_wrapper_class]
          else
            bootstrap.label_class
          end
        end

        def label_text(attribute, bootstrap)
          bootstrap.label_text || object&.class.try(:human_attribute_name, attribute)
        end

        def hide_class_required?(bootstrap)
          bootstrap.hide_label? || (bootstrap.layout_inline? && !bootstrap.floating?)
        end

        private :draw_label, :label_classes, :label_text, :label_layout_classes,
                :hide_class_required?
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rails_bootstrap_form-0.9.7 lib/rails_bootstrap_form/helpers/labels.rb
rails_bootstrap_form-0.9.6 lib/rails_bootstrap_form/helpers/labels.rb
rails_bootstrap_form-0.9.5 lib/rails_bootstrap_form/helpers/labels.rb
rails_bootstrap_form-0.9.4 lib/rails_bootstrap_form/helpers/labels.rb
rails_bootstrap_form-0.9.3 lib/rails_bootstrap_form/helpers/labels.rb
rails_bootstrap_form-0.9.2 lib/rails_bootstrap_form/helpers/labels.rb