Sha256: 5ea1a3db312aadfaef9f97bb6ab4d3a9a2a9928284da1a2411d844a60d4ab3f2

Contents?: true

Size: 1.04 KB

Versions: 19

Compression:

Stored size: 1.04 KB

Contents

class Backend::TagboxController < Backend::BaseController
  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.tags.allow_new?
  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

19 entries across 19 versions & 1 rubygems

Version Path
udongo-6.2.1 app/controllers/backend/tagbox_controller.rb
udongo-6.2.0 app/controllers/backend/tagbox_controller.rb
udongo-6.1.0 app/controllers/backend/tagbox_controller.rb
udongo-6.0.0 app/controllers/backend/tagbox_controller.rb
udongo-5.9.0 app/controllers/backend/tagbox_controller.rb
udongo-5.8.0 app/controllers/backend/tagbox_controller.rb
udongo-5.7.0 app/controllers/backend/tagbox_controller.rb
udongo-5.6.0 app/controllers/backend/tagbox_controller.rb
udongo-5.5.0 app/controllers/backend/tagbox_controller.rb
udongo-5.4.0 app/controllers/backend/tagbox_controller.rb
udongo-5.3.1 app/controllers/backend/tagbox_controller.rb
udongo-5.3.0 app/controllers/backend/tagbox_controller.rb
udongo-5.2.0 app/controllers/backend/tagbox_controller.rb
udongo-5.1.0 app/controllers/backend/tagbox_controller.rb
udongo-5.0.2 app/controllers/backend/tagbox_controller.rb
udongo-5.0.1 app/controllers/backend/tagbox_controller.rb
udongo-5.0.0 app/controllers/backend/tagbox_controller.rb
udongo-4.0.0 app/controllers/backend/tagbox_controller.rb
udongo-3.0.0 app/controllers/backend/tagbox_controller.rb