Created
July 28, 2016 02:35
-
-
Save GiaoGiaoCat/5c0375644d10d61dad20c895ac46ca90 to your computer and use it in GitHub Desktop.
RestFul Resource
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
class ImagesController < ApplicationController | |
before_action :load_parent_resource | |
include RestfulResources | |
restful_resources resource_name: :image | |
private | |
def load_resources | |
@images = resource_scope.all.group_by(&:kind) | |
end | |
def load_parent_resource | |
@product ||= Product.find(params[:product_id]) | |
end | |
def build_resource | |
@image ||= Image::ImageBuilder.new | |
@image.attributes = resource_params | |
end | |
def resource_params | |
resource_params = params.fetch(:image_image_builder, {}) | |
resource_params.permit!.merge(viewable_type: 'Product::Variant') | |
end | |
def resource_scope | |
@product.variant_images | |
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 RestfulResources | |
extend ActiveSupport::Concern | |
included do | |
before_action :load_resource, only: %i(show edit update destroy) | |
end | |
module ClassMethods | |
def restful_resources(options = {}) | |
cattr_accessor :resource_name | |
self.resource_name = options[:resource_name] | |
end | |
end | |
def index | |
load_resources | |
end | |
def new | |
build_resource | |
end | |
def create | |
build_resource | |
save_resource or render 'new' | |
end | |
def update | |
build_resource | |
save_resource or render 'edit' | |
end | |
def destroy | |
destroy_resource | |
end | |
private | |
# Use callbacks to share common setup or constraints between actions. | |
def load_resources | |
resources = instance_variable_get("@#{self.class.resource_name.to_s.pluralize}") | |
resources ||= resource_scope.page(params[:page]) | |
instance_variable_set("@#{self.class.resource_name.to_s.pluralize}", resources) | |
end | |
def load_resource | |
resource = instance_variable_get("@#{self.class.resource_name}") | |
resource ||= resource_scope.find(params[:id]) | |
instance_variable_set("@#{self.class.resource_name}", resource) | |
end | |
def build_resource | |
resource = instance_variable_get("@#{self.class.resource_name}") | |
resource ||= resource_scope.new | |
resource.attributes = resource_params | |
instance_variable_set("@#{self.class.resource_name}", resource) | |
end | |
def save_resource | |
resource = instance_variable_get("@#{self.class.resource_name}") | |
resource.save | |
end | |
def destroy_resource | |
resource = instance_variable_get("@#{self.class.resource_name}") | |
resource.destroy | |
end | |
def resource_scope | |
self.class.resource_name.to_s.classify.constantize | |
end | |
# Never trust parameters from the scary internet, only allow the white list through. | |
def resource_params | |
resource_params = params.fetch(self.class.resource_name.to_sym, {}) | |
resource_params.permit! | |
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
class SpacesController < ApplicationController | |
include RestfulResources | |
restful_resources resource_name: :space | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment