JSON API Resource Controller для абстрактного ресурса в Rails
Я использую Jsonapi-ресурс для моего Rails API. у меня есть User
модель, которая имеет логическое значение account_manager
как атрибут Я хочу иметь отдельный контроллер / конечные точки для менеджеров по работе с клиентами.
Действие index должно вернуть всех пользователей, которые являются менеджерами учетных записей, и очистить данные, чтобы вернуть только id
, first_name
а также last_name
,
у меня есть AccountManagerResource
а также AccountManagerController
, Но ошибки действия индекса из-за @model
быть ноль в моем ресурсе. Кто-нибудь знает, если я что-то упустил?
class JsonApiBaseResource < JSONAPI::Resource
abstract
relationship :customer, to: :one
before_create do
@model.customer_id = context[:current_customer].id
end
filter :updated_since,
verify: -> (value, _context) {
value.map{ |value| value.to_i }
},
apply: -> (records, value, _options) {
records.updated_since(Time.at(value[0]))
}
def self.records(options = {})
current_customer(options).public_send(@model.to_s.tableize).ordered
end
def self.current_customer(options)
options[:context][:current_customer]
end
end
UserResource
class UserResource < JsonApiBaseResource
attributes :title, :first_name, :last_name, :email, :company_name,
:job_title, :telephone_number, :user_type
relationship :account_manager, to: :one
relationship :company, to: :one
relationship :roles, to: :many
relationship :asset_permissions, to: :many
paginator :paged_with_all
before_create do
if current_user = context[:current_user]
@model.created_by_admin = current_user.super_admin?
end
end
filter :search,
apply: -> (records, value, _options) {
records.search(value[0])
}
filter :user_type
filter :roles
filter :company
filter :account_manager
end
Ресурс менеджера аккаунта
class AccountManagerResource < JsonApiBaseResource
abstract
attributes :id, :first_name, :last_name
model_name 'User'
end
Диспетчер аккаунтов
class AccountManagersController < JsonApiBaseController
apipie_concern_subst(resource: 'Account Manger')
include CrudActions
skip_before_action :authenticate_user, only: [:index]
resource_description do
description <<-EOS
- **type** = users
### Attributes
- **id**: *number*
- **first-name**: *string*
- **last-name**: *string*
EOS
end
api! "Returns all Account Manager names"
def index
super
end
end
Ошибка, которую я получаю, уникальна для моего конкретного проекта, но это результат @model
быть ноль, когда вырос в JsonApiBaseResource
на призыв к действию индекса AccountManagerController
,
Любая помощь приветствуется.