Sha256: a9b9e47d477c5f5e80dd219c16b931bb5b0b661ae0d275f45fb8ae590348e62c

Contents?: true

Size: 1.95 KB

Versions: 19

Compression:

Stored size: 1.95 KB

Contents

# require "active_support/inflector"

module Avo
  module FieldsCollector
    def field(field_name, as:, **args, &block)
      self.fields ||= []

      field_instance = parse_field(field_name, as: as, **args, &block)

      if field_instance.present?
        self.fields << field_instance
      else
        message = "[Avo] The #{field_name} field, as: #{as} from #{self.name} has an invalid configuration."
        ::Rails.logger.warn message
      end
    end

    def parse_field(field_name, as:, **args, &block)
      # The field is passed as a symbol eg: :text, :color_picker, :trix
      if as.is_a? Symbol
        field_class = field_class_from_symbol(as)

        if field_class.present?
          # The field has been registered before.
          instantiate_field(field_name, klass: field_class, **args, &block)
        else
          # The symbol can be transformed to a class and found.
          class_name = as.to_s.classify
          field_class = "#{class_name}Field"

          # Discover & load custom field classes
          if Object.const_defined? field_class
            instantiate_field(field_name, klass: field_class.safe_constantize, **args, &block)
          end
        end
      elsif as.is_a? Class
        # The field has been passed as a class.
        if Object.const_defined? as.to_s
          instantiate_field(field_name, klass: as, **args, &block)
        end
      end
    end

    def heading(body, **args)
      self.fields << Avo::Fields::HeadingField.new(body, **args)
    end

    private

    def instantiate_field(field_name, klass:, **args, &block)
      if block
        klass.new(field_name, **args || {}, &block)
      else
        klass.new(field_name, **args || {})
      end
    end

    def field_class_from_symbol(symbol)
      matched_field = Avo::App.fields.find do |field|
        field[:name].to_s == symbol.to_s
      end

      return matched_field[:class] if matched_field.present? && matched_field[:class].present?
    end
  end
end

Version data entries

19 entries across 19 versions & 1 rubygems

Version Path
avo-1.6.4.pre.1 lib/avo/fields_collector.rb
avo-1.6.3.pre.2 lib/avo/fields_collector.rb
avo-1.6.3.pre.1 lib/avo/fields_collector.rb
avo-1.6.2.pre.1 lib/avo/fields_collector.rb
avo-1.6.1 lib/avo/fields_collector.rb
avo-1.6.0 lib/avo/fields_collector.rb
avo-1.5.5 lib/avo/fields_collector.rb
avo-1.5.4 lib/avo/fields_collector.rb
avo-1.5.3 lib/avo/fields_collector.rb
avo-1.5.2 lib/avo/fields_collector.rb
avo-1.5.1 lib/avo/fields_collector.rb
avo-1.5.0 lib/avo/fields_collector.rb
avo-1.4.5.pre.1 lib/avo/fields_collector.rb
avo-1.4.4.pre.1 lib/avo/fields_collector.rb
avo-1.4.4 lib/avo/fields_collector.rb
avo-1.4.3 lib/avo/fields_collector.rb
avo-1.4.2 lib/avo/fields_collector.rb
avo-1.4.1 lib/avo/fields_collector.rb
avo-1.4.0 lib/avo/fields_collector.rb