Sha256: f882c23432c6951c294398805210fbb57f9493c4bc7cd593237e4bc3d0448a47

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

require 'rails'
require 'administrate/field/text'
require 'administrate/engine'

module Administrate
  module Field
    class ActsAsTaggable < Administrate::Field::Text
      class Engine < ::Rails::Engine
        if defined?(Administrate::Engine)
          Administrate::Engine.add_javascript 'administrate-field-taggable/application'
          Administrate::Engine.add_stylesheet 'administrate-field-taggable/application'
        end
      end

      def context
        options.fetch(:context, @attribute)
      end

      def attribute
        context = super.to_s.singularize
        "#{context}_list"
      end

      def self.permitted_attribute(attr, **opts)
        context = super.to_s.singularize
        "#{context}_list"
      end

      def tags
        data
      end

      def name
        context.to_s
      end

      def delimitted
        tags.join(', ').to_s
      end

      def truncate
        delimitted.to_s[0...truncation_length]
      end

      def tag_options
        return [] unless defined? ActsAsTaggableOn::Tag

        ActsAsTaggableOn::Tag.for_context(context).order(:name).map do |t|
          { text: t.name, value: t.name }
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
administrate-field-acts_as_taggable-0.0.4 lib/administrate/field/acts_as_taggable.rb