Last active
August 16, 2021 01:52
-
-
Save dudo/e24b01a12f7e1e52bef884d4928ef144 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Pundit | |
# Friendly ID | |
# FastJSON | |
module ActsAsResource | |
extend ActiveSupport::Concern | |
included do | |
include Pundit | |
before_action :set_resource, only: %i[show update destroy] | |
end | |
# GET /resources | |
def index | |
authorize(resource_class) | |
render json: serializer_class.new(resource_scope, serializer_options) | |
end | |
# GET /resources/1 | |
def show | |
authorize(@resource) | |
render json: serialized_resource | |
end | |
# POST /resources | |
def create | |
@resource = resource_class.new | |
@resource.assign_attributes(resource_params) | |
authorize(@resource) | |
if @resource.save | |
render json: serialized_resource, status: :created, location: @resource | |
else | |
render json: @resource.errors, status: :unprocessable_entity | |
end | |
end | |
# PATCH/PUT /resources/1 | |
def update | |
authorize(@resource) | |
if @resource.update(resource_params) | |
render json: serialized_resource | |
else | |
render json: @resource.errors, status: :unprocessable_entity | |
end | |
end | |
# DELETE /resources/1 | |
def destroy | |
authorize(@resource) | |
@resource.destroy | |
end | |
private | |
def resource_class | |
@resource_class ||= controller_name.classify.constantize | |
end | |
def serializer_class | |
@serializer_class ||= "#{controller_name.classify}Serializer".constantize | |
end | |
def set_resource | |
@resource = resource_scope.friendly.find(params[:id]) | |
end | |
def resource_scope | |
policy_scope(resource_class) | |
end | |
def resource_params | |
params.require(resource_class.model_name.param_key) | |
.permit(policy(@resource).permitted_attributes) | |
end | |
def serialized_resource | |
serializer_class.new(@resource, serializer_options) | |
end | |
def serializer_options | |
@serializer_options ||= {} | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# FastJSON | |
module ActsAsResource | |
extend ActiveSupport::Concern | |
included do | |
before_action :set_resource, only: %i[show update destroy] | |
end | |
# GET /resources | |
def index | |
render json: serializer_class.new(resource_scope.all, serializer_options) | |
end | |
# GET /resources/1 | |
def show | |
render json: serialized_resource | |
end | |
# POST /resources | |
def create | |
@resource = resource_class.new | |
@resource.assign_attributes(resource_params) | |
if @resource.save | |
render json: serialized_resource, status: :created, location: @resource | |
else | |
render json: @resource.errors, status: :unprocessable_entity | |
end | |
end | |
# PATCH/PUT /resources/1 | |
def update | |
if @resource.update(resource_params) | |
render json: serialized_resource | |
else | |
render json: @resource.errors, status: :unprocessable_entity | |
end | |
end | |
# DELETE /resources/1 | |
def destroy | |
@resource.destroy | |
end | |
private | |
def resource_class | |
@resource_class ||= controller_name.classify.constantize | |
end | |
def serializer_class | |
@serializer_class ||= "#{controller_name.classify}Serializer".constantize | |
end | |
def set_resource | |
@resource = resource_scope.find_by(id: params[:id]) | |
end | |
def resource_scope | |
@resource_scope ||= resource_class | |
end | |
def resource_params | |
params.require(resource_class.model_name.param_key) | |
.permit(resource_class.column_names) | |
end | |
def serialized_resource | |
serializer_class.new(@resource, serializer_options) | |
end | |
def serializer_options | |
@serializer_options ||= {} | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module ActsAsResource | |
extend ActiveSupport::Concern | |
included do | |
before_action :set_resource, only: %i[show update destroy] | |
end | |
# GET /resources | |
def index | |
render json: serialized_resources | |
end | |
# GET /resources/1 | |
def show | |
render json: serialized_resource | |
end | |
# POST /resources | |
def create | |
@resource = resource_class.new | |
@resource.assign_attributes(resource_params) | |
if @resource.save | |
render json: serialized_resource, status: :created, location: @resource | |
else | |
render json: @resource.errors, status: :unprocessable_entity | |
end | |
end | |
# PATCH/PUT /resources/1 | |
def update | |
if @resource.update(resource_params) | |
render json: serialized_resource | |
else | |
render json: @resource.errors, status: :unprocessable_entity | |
end | |
end | |
# DELETE /resources/1 | |
def destroy | |
@resource.destroy | |
end | |
private | |
def resource_class | |
@resource_class ||= controller_name.classify.constantize | |
end | |
def set_resource | |
@resource = resource_scope.find_by(id: params[:id]) | |
end | |
def resource_scope | |
@resource_scope ||= resource_class | |
end | |
def resource_params | |
params.require(resource_class.model_name.param_key) | |
.permit(resource_class.column_names) | |
end | |
def serialized_resource | |
@resource.as_json | |
end | |
def serialized_resources | |
resource_scope.all.as_json | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment