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.26.1.pr1584.pre.1 lib/avo/fields/tags_field.rb
avo-2.25.1.pre.1.pr1579 lib/avo/fields/tags_field.rb
avo-2.26.0 lib/avo/fields/tags_field.rb
avo-2.25.0 lib/avo/fields/tags_field.rb
avo-2.24.1 lib/avo/fields/tags_field.rb
avo-2.24.0 lib/avo/fields/tags_field.rb
avo-2.23.3.pre.1.pr1529 lib/avo/fields/tags_field.rb
avo-2.23.2 lib/avo/fields/tags_field.rb
avo-2.23.1 lib/avo/fields/tags_field.rb
avo-2.23.0 lib/avo/fields/tags_field.rb
avo-2.22.0 lib/avo/fields/tags_field.rb
avo-2.21.3.pre.pr1489 lib/avo/fields/tags_field.rb
avo-2.21.2.pre.pr1486 lib/avo/fields/tags_field.rb
avo-2.21.1.pre.pr1484 lib/avo/fields/tags_field.rb
avo-2.21.0 lib/avo/fields/tags_field.rb
avo-2.21.1.pre.pr1476 lib/avo/fields/tags_field.rb
avo-2.21.1.pre.issue1450 lib/avo/fields/tags_field.rb
avo-2.21.1.pre.issue1444 lib/avo/fields/tags_field.rb
avo-2.20.0 lib/avo/fields/tags_field.rb
avo-2.19.0 lib/avo/fields/tags_field.rb