Sha256: 9a9bc2433c6d11592939e0f5d6958028313aa3fc59b77ba2e510b456f03a8d16

Contents?: true

Size: 1.82 KB

Versions: 93

Compression:

Stored size: 1.82 KB

Contents

module Avo
  module Fields
    class TagsField < BaseField
      attr_reader :acts_as_taggable_on
      attr_reader :close_on_select
      attr_reader :delimiters
      attr_reader :enforce_suggestions

      def initialize(id, **args, &block)
        super(id, **args, &block)

        add_boolean_prop args, :close_on_select
        add_boolean_prop args, :enforce_suggestions
        add_string_prop args, :acts_as_taggable_on
        add_array_prop args, :disallowed
        add_array_prop args, :delimiters, [","]
        add_array_prop args, :suggestions
      end

      def field_value
        return json_value if acts_as_taggable_on.present?

        value || []
      end

      def json_value
        value.map do |item|
          {
            value: item.name
          }
        end.as_json
      end

      def fill_field(model, key, value, params)
        if acts_as_taggable_on.present?
          model.send(act_as_taggable_attribute(key), parsed_value(value))
        else
          model.send("#{key}=", parsed_value(value))
        end

        model
      end

      def suggestions
        return @suggestions if @suggestions.is_a? Array

        if @suggestions.respond_to? :call
          return Avo::Hosts::RecordHost.new(block: @suggestions, record: model).handle
        end

        []
      end

      def disallowed
        return @disallowed if @disallowed.is_a? Array

        if @disallowed.respond_to? :call
          return Avo::Hosts::RecordHost.new(block: @disallowed, record: model).handle
        end

        []
      end

      private

      def act_as_taggable_attribute(key)
        "#{key.singularize}_list="
      end

      def parsed_value(value)
        JSON.parse(value).pluck("value")
      rescue
        []
      end

      private

      def parse_suggestions_from_args(args)
      end
    end
  end
end

Version data entries

93 entries across 93 versions & 1 rubygems

Version Path
avo-2.18.1 lib/avo/fields/tags_field.rb
avo-2.18.1.pre.1.eagerloaddirs lib/avo/fields/tags_field.rb
avo-2.18.0 lib/avo/fields/tags_field.rb
avo-2.17.1.pre.5.stackedlayout lib/avo/fields/tags_field.rb
avo-2.17.1.pre.4.issue.1342 lib/avo/fields/tags_field.rb
avo-2.17.1.pre.3 lib/avo/fields/tags_field.rb
avo-2.17.1.pre.2.customauthorizationclients lib/avo/fields/tags_field.rb
avo-2.17.1.pre.1.zeitwerk.eager.load.dir lib/avo/fields/tags_field.rb
avo-2.17.0 lib/avo/fields/tags_field.rb
avo-2.16.1.pre.1.nativefields lib/avo/fields/tags_field.rb
avo-2.16.0 lib/avo/fields/tags_field.rb
avo-2.15.3 lib/avo/fields/tags_field.rb
avo-2.15.3.pre.1.data.attrs.to.sidebar.items lib/avo/fields/tags_field.rb
avo-2.15.2 lib/avo/fields/tags_field.rb
avo-2.15.2.pre.1 lib/avo/fields/tags_field.rb
avo-2.15.1 lib/avo/fields/tags_field.rb
avo-2.15.0 lib/avo/fields/tags_field.rb
avo-2.14.3.pre.7.polytranslations1 lib/avo/fields/tags_field.rb
avo-2.14.3.pre.6.nosprockets lib/avo/fields/tags_field.rb
avo-2.14.3.pre.5.nosprockets lib/avo/fields/tags_field.rb