add filter field

This commit is contained in:
YingRui Lu 2019-04-30 11:59:49 +08:00
parent bd3db5e85e
commit e807339d29
4 changed files with 12 additions and 2 deletions

View File

@ -50,6 +50,10 @@ module Jsonapi
resource_klass._relationships
end
def filters
resource_klass.filters
end
def columns_with_comment
@columns_with_comment ||= {}.tap do |clos|
model_klass.columns.each do |col|
@ -60,6 +64,7 @@ module Jsonapi
def swagger_type(column)
return 'array' if column.array
case column.type
when :bigint, :integer then 'integer'
when :boolean then 'boolean'

View File

@ -12,6 +12,9 @@ RSpec.describe '<%= resouces_name %>', type: :request do
produces 'application/vnd.api+json'
parameter name: :'page[number]', in: :query, type: :string, description: '<%= t(:page_num) %>', required: false
parameter name: :include, in: :query, type: :string, description: '<%= t(:include_related_data) %>', required: false
<% filters.each do |filter_attr, filter_config| -%>
parameter name: :'filter[<%= filter_attr %>]', in: :query, type: :string, description: '<%= t(:filter_field) %>', <% if filter_config[:default] -%>default: '<%= safe_encode(filter_config[:default]) %>',<% end %>required: false
<% end -%>
parameter name: :'fields[<%= route_resouces %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false
<% relationships.each_value do |relation| -%>
parameter name: :'fields[<%= relation.class_name.tableize %>]', in: :query, type: :string, description: '<%= t(:display_field) %>', required: false
@ -36,7 +39,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do
type: :object,
properties: {
<% attributes.each_key.each do |attr| -%>
<%= attr %>: { type: :<%= columns_with_comment[attr][:type] %>, <%if columns_with_comment[attr][:is_array] -%> items: { type: :<%= columns_with_comment[attr][:items_type] %>},<% end -%> 'x-nullable': <%= columns_with_comment[attr][:nullable] %>, description: '<%= columns_with_comment[attr][:comment] %>'},
<%= attr %>: { type: :<%= columns_with_comment[attr][:type] %>, <%if columns_with_comment[attr][:is_array] -%> items: { type: :<%= columns_with_comment[attr][:items_type] %>},<% end -%>'x-nullable': <%= columns_with_comment[attr][:nullable] %>, description: '<%= columns_with_comment[attr][:comment] %>'},
<% end -%>
},
description: '<%= t(:attributes) %>'
@ -120,7 +123,7 @@ RSpec.describe '<%= resouces_name %>', type: :request do
type: :object,
properties: {
<% attributes.each_key.each do |attr| -%>
<%= attr %>: { type: :<%= columns_with_comment[attr][:type] %>, <%if columns_with_comment[attr][:is_array] -%> items: { type: :<%= columns_with_comment[attr][:items_type] %>},<% end -%> 'x-nullable': <%= columns_with_comment[attr][:nullable] %>, description: '<%= columns_with_comment[attr][:comment] %>'},
<%= attr %>: { type: :<%= columns_with_comment[attr][:type] %>, <%if columns_with_comment[attr][:is_array] -%> items: { type: :<%= columns_with_comment[attr][:items_type] %>},<% end -%>'x-nullable': <%= columns_with_comment[attr][:nullable] %>, description: '<%= columns_with_comment[attr][:comment] %>'},
<% end -%>
},
description: '<%= t(:attributes) %>'

View File

@ -3,6 +3,7 @@ en:
page_num: 'Page Number'
include_related_data: 'Include Related Data'
display_field: 'Display Field'
filter_field: 'Filter Field'
list: 'List'
get_list: 'Fetch List'
detail_link: 'Detail Link'

View File

@ -3,6 +3,7 @@ zh-CN:
page_num: '页码'
include_related_data: '包含关联数据'
display_field: '显示字段'
filter_field: '过滤字段'
list: '列表'
get_list: '获取列表'
detail_link: '详情链接'