module <

Public Instance Methods

build_sort(records, sort) click to toggle source
# File lib/generators/aginx/controller/templates/controller.rb, line 81
def build_sort(records, sort)
  records
end
index() click to toggle source
# File lib/generators/aginx/controller/templates/controller.rb, line 11
def index
  search = OpenStruct.new params[:search]
  sort = OpenStruct.new params[:sort]

  <%= pluralize_instance_name %> = <%= @model_name %>.all
  <%= pluralize_instance_name %> = build_search(<%= pluralize_instance_name %>, search)
  <%= pluralize_instance_name %> = build_sort(<%= pluralize_instance_name %>, sort)

  paginate json: <%= pluralize_instance_name %>, status: :ok, serializer: PaginationSerializer
end
multi_destroy() click to toggle source
# File lib/generators/aginx/controller/templates/controller.rb, line 52
def multi_destroy
  <%= pluralize_instance_name %> = <%= @model_name %>.where(id: params[:ids])
  results = <%= pluralize_instance_name %>.destroy_all
  render json: { success: true, ids: results.map(&:id), message: { type: 'success', cnt: I18n.t('flash.success', {action: I18n.t('buttons.multi_destroy')}) || '批量删除成功' }, status: :ok }
end

private

def set_<%=var_name %>
  <%= instance_name %> = <%= @model_name %>.find(params[:id])
end

def <%=var_name %>_params
  params.require(:<%=var_name %>).permit(<%= @model_columns.map{ |i| ":" + i.split(':').first }.join(", ") %>)
end
show() click to toggle source
# File lib/generators/aginx/controller/templates/controller.rb, line 22
def show
  render json: <%= instance_name %>, status: :ok, serializer: ApiSerializer
end

def create
  <%= instance_name %> = <%= @model_name %>.new(<%=var_name %>_params)

  if <%= instance_name %>.save
    render json: <%= instance_name %>, status: :created, location: [:v1, <%= instance_name %>], message: { type: 'success', cnt: I18n.t('flash.success', {action: I18n.t('buttons.create')}) || '创建成功' }, serializer: ApiSerializer
  else
    render json: <%= instance_name %>, serializer: ApiSerializer, status: :unprocessable_entity
  end
end
update() click to toggle source
# File lib/generators/aginx/controller/templates/controller.rb, line 36
def update
  if <%= instance_name %>.update(<%=var_name %>_params)
    render json: <%= instance_name %>, status: :ok, location: [:v1, <%= instance_name %>], message: { type: 'success', cnt: I18n.t('flash.success', {action: I18n.t('buttons.update')}) || '更新成功' }, serializer: ApiSerializer
  else
    render json: <%= instance_name %>, serializer: ApiSerializer, status: :unprocessable_entity
  end
end

def destroy
  if <%= instance_name %>.destroy
    render json: <%= instance_name %>, status: :ok, message: { type: 'success', cnt: I18n.t('flash.success', {action: I18n.t('buttons.destroy')}) || '删除成功' }, serializer: ApiSerializer
  else
    render json: <%= instance_name %>, serializer: ApiSerializer, status: :unprocessable_entity
  end
end