Last active
January 23, 2020 19:53
-
-
Save mrjonesbot/1dc92d006aeb60580e8822262e9b4e8e 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
<% downcase_class_name = class_name.underscore %><% singularize_class_name = downcase_class_name.singularize %><% if namespaced? -%> | |
require_dependency "<%= namespaced_path %>/api_controller"<% end -%> | |
<% module_namespacing do -%> | |
module V1 | |
class <%= class_name %>Controller < ApiController | |
before_action :authenticate_request! | |
<% actions.each do |action| -%> | |
def <%= action %> | |
<% if action == "index" -%> <%= downcase_class_name %> = <%= class_name.singularize %>.all | |
render json: serializable_collection(<%= downcase_class_name %>, serializer) | |
<% end -%><% if action == "show" -%> render json: serializable(<%= singularize_class_name %>) | |
<% end -%><% if action == "create" -%> <%= singularize_class_name %> = <%= class_name.singularize %>.create!(<%= singularize_class_name %>_params) | |
render json: serializable(<%= singularize_class_name %>) | |
<% end -%><% if action == "update" -%> <%= singularize_class_name %>.update!(<%= singularize_class_name %>_params) | |
render json: serializable(<%= singularize_class_name %>) | |
<% end -%><% if action == "destroy" -%> <%= singularize_class_name %>.destroy | |
render json: {} | |
<% end -%>end | |
<%= "\n" unless action == actions.last -%> | |
<% end -%> | |
private | |
<% if actions.include?("show") %>def <%= singularize_class_name %> | |
<%= class_name.singularize %>.find(params[:id]) | |
end<% end %> | |
<% if actions.include?("index") %>def serializer | |
"<%= class_name.singularize %>" | |
end<% end %> | |
<% if actions.include?("create") %>def <%= singularize_class_name %>_params | |
params.require(:<%= singularize_class_name %>).permit(:id) | |
end<% end %> | |
end | |
end | |
<% end -%> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment