Sha256: 0ddabea6975b656cf3de17300d8d127074a32f24344544a42445b93de88c9073

Contents?: true

Size: 1.03 KB

Versions: 5

Compression:

Stored size: 1.03 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.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

5 entries across 5 versions & 1 rubygems

Version Path
udongo-2.0.4 app/controllers/backend/tagbox_controller.rb
udongo-2.0.3 app/controllers/backend/tagbox_controller.rb
udongo-2.0.2 app/controllers/backend/tagbox_controller.rb
udongo-2.0.1 app/controllers/backend/tagbox_controller.rb
udongo-2.0.0 app/controllers/backend/tagbox_controller.rb