Sha256: 615ccc9a60d890387d14b03f6d43bd0a1f17954bf7dbb16214ec15677cd25243

Contents?: true

Size: 1.04 KB

Versions: 5

Compression:

Stored size: 1.04 KB

Contents

class Backend::TagboxController < BackendController
  def index
    tags = ::Tag.by_locale(locale).pluck(:name).map do |name|
      { label: name, value: name }
    end

    render json: tags
  end

  def create
    create_tag if tag_creatable?
    render json: { tag: params[:tag], valid: item_tagged?(find_tag) }
  end

  def destroy
    tag = find_tag
    find_model.tagged_items.where(tag_id: tag.id).destroy_all if tag.present?
    render json: { success: true }
  end

  private

  def find_model
    @model ||= params[:taggable_type].to_s.constantize.find params[:taggable_id]
  end

  def find_tag
    ::Tag.find_by locale: params[:locale], name: params[:tag]
  end

  def tag_creatable?
    !::Tag.exists?(tag_params) && Udongo.config.allow_new_tags?
  end

  def create_tag
    ::Tag.create! tag_params
  end

  def tag_params
    {
      locale: params[:locale],
      name: params[:tag],
      slug: params[:tag].parameterize
    }
  end

  def item_tagged?(tag)
    return false unless tag
    find_model.tagged_items.create tag: tag
    true
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
udongo-1.0.3 app/controllers/backend/tagbox_controller.rb
udongo-1.0.2 app/controllers/backend/tagbox_controller.rb
udongo-1.0.1 app/controllers/backend/tagbox_controller.rb
udongo-1.0.0 app/controllers/backend/tagbox_controller.rb
udongo-0.1.0 app/controllers/backend/tagbox_controller.rb