Feature: Label APIs (#931)
This commit is contained in:
@@ -1,10 +1,38 @@
|
||||
class Api::V1::Accounts::LabelsController < Api::BaseController
|
||||
# list all labels in account
|
||||
before_action :current_account
|
||||
before_action :fetch_label, except: [:index, :create]
|
||||
before_action :check_authorization
|
||||
|
||||
def index
|
||||
@labels = current_account.all_conversation_tags
|
||||
@labels = policy_scope(current_account.labels)
|
||||
end
|
||||
|
||||
def most_used
|
||||
@labels = ActsAsTaggableOn::Tag.most_used(params[:count] || 10)
|
||||
def show; end
|
||||
|
||||
def create
|
||||
@label = current_account.labels.create!(permitted_params)
|
||||
end
|
||||
|
||||
def update
|
||||
@label.update(permitted_params)
|
||||
end
|
||||
|
||||
def destroy
|
||||
@label.destroy
|
||||
head :ok
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def fetch_label
|
||||
@label = current_account.labels.find(params[:id])
|
||||
end
|
||||
|
||||
def check_authorization
|
||||
authorize(Label)
|
||||
end
|
||||
|
||||
def permitted_params
|
||||
params.require(:label).permit(:title, :description, :color, :show_on_sidebar)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -45,6 +45,7 @@ class Account < ApplicationRecord
|
||||
has_many :web_widgets, dependent: :destroy, class_name: '::Channel::WebWidget'
|
||||
has_many :canned_responses, dependent: :destroy
|
||||
has_many :webhooks, dependent: :destroy
|
||||
has_many :labels, dependent: :destroy
|
||||
has_one :subscription, dependent: :destroy
|
||||
has_many :notification_settings, dependent: :destroy
|
||||
has_flags ACCOUNT_SETTINGS_FLAGS.merge(column: 'settings_flags').merge(DEFAULT_QUERY_SETTING)
|
||||
|
||||
20
app/models/label.rb
Normal file
20
app/models/label.rb
Normal file
@@ -0,0 +1,20 @@
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: labels
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# color :string
|
||||
# description :text
|
||||
# show_on_sidebar :boolean
|
||||
# title :string
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
# account_id :bigint
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_labels_on_account_id (account_id)
|
||||
#
|
||||
class Label < ApplicationRecord
|
||||
belongs_to :account
|
||||
end
|
||||
17
app/policies/label_policy.rb
Normal file
17
app/policies/label_policy.rb
Normal file
@@ -0,0 +1,17 @@
|
||||
class LabelPolicy < ApplicationPolicy
|
||||
def index?
|
||||
@account_user.administrator?
|
||||
end
|
||||
|
||||
def update?
|
||||
@account_user.administrator?
|
||||
end
|
||||
|
||||
def show?
|
||||
@account_user.administrator?
|
||||
end
|
||||
|
||||
def create?
|
||||
@account_user.administrator?
|
||||
end
|
||||
end
|
||||
5
app/views/api/v1/accounts/labels/create.json.jbuilder
Normal file
5
app/views/api/v1/accounts/labels/create.json.jbuilder
Normal file
@@ -0,0 +1,5 @@
|
||||
json.id @label.id
|
||||
json.title @label.title
|
||||
json.description @label.description
|
||||
json.color @label.color
|
||||
json.show_on_sidebar @label.show_on_sidebar
|
||||
@@ -1,8 +1,9 @@
|
||||
json.data do
|
||||
json.meta do
|
||||
end
|
||||
|
||||
json.payload do
|
||||
json.labels @labels
|
||||
json.payload do
|
||||
json.array! @labels do |label|
|
||||
json.id label.id
|
||||
json.title label.title
|
||||
json.description label.description
|
||||
json.color label.color
|
||||
json.show_on_sidebar label.show_on_sidebar
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
json.payload do
|
||||
json.labels @labels
|
||||
end
|
||||
5
app/views/api/v1/accounts/labels/show.json.jbuilder
Normal file
5
app/views/api/v1/accounts/labels/show.json.jbuilder
Normal file
@@ -0,0 +1,5 @@
|
||||
json.id @label.id
|
||||
json.title @label.title
|
||||
json.description @label.description
|
||||
json.color @label.color
|
||||
json.show_on_sidebar @label.show_on_sidebar
|
||||
5
app/views/api/v1/accounts/labels/update.json.jbuilder
Normal file
5
app/views/api/v1/accounts/labels/update.json.jbuilder
Normal file
@@ -0,0 +1,5 @@
|
||||
json.id @label.id
|
||||
json.title @label.title
|
||||
json.description @label.description
|
||||
json.color @label.color
|
||||
json.show_on_sidebar @label.show_on_sidebar
|
||||
Reference in New Issue
Block a user