Skip to content

Instantly share code, notes, and snippets.

@ricsdeol
Created October 1, 2020 01:14
Show Gist options
  • Save ricsdeol/1ea7f8384400235bdaf4afe10167d76e to your computer and use it in GitHub Desktop.
Save ricsdeol/1ea7f8384400235bdaf4afe10167d76e to your computer and use it in GitHub Desktop.
Resoursable
# frozen_string_literal: true
module Resourceable
extend ActiveSupport::Concern
included do
before_action :set_resource, only: %i[show edit update destroy]
before_action :authorize_resource
helper_method :resource
class_attribute :model, :model_attribute_name, :model_name_underscored,
:resource_actions, :relationships, :permitted_parameters,
:order
end
class_methods do
# Define the ActiveModel::Base served by this resource controller.
def serve(model, relationships: [], parameters: [], order: { id: :desc })
self.model = model
self.model_name_underscored = model.to_s.underscore.tr('/', '_')
self.model_attribute_name = "@#{model_name_underscored}"
self.relationships = relationships
self.permitted_parameters = parameters
self.order = order
end
end
def index
@search = policy_scope(model).
includes(relationships).
order(order).
ransack(params[:q])
@resources = @search.result.
distinct.
paginate page: params[:page],
per_page: params[:per_page]
respond_with @resources
end
def show
respond_with resource
end
def new
instance_variable_set model_attribute_name, resource_build
respond_with resource
end
def edit; end
def create
instance_variable_set model_attribute_name, resource_build
resource.assign_attributes resource_params
if resource.save
flash[:success] = I18n.t('flash.actions.create.notice')
else
flash[:error] = resource.errors.full_messages
end
respond_with resource
end
def update
if resource.update(resource_params)
flash[:success] = I18n.t('flash.actions.update.notice')
else
flash[:error] = resource.errors.full_messages
end
respond_with resource
end
def destroy
if resource.destroy
flash[:success] = I18n.t('flash.actions.destroy.notice')
else
flash[:error] = I18n.t('flash.actions.destroy.alert')
end
respond_with resource, location: resources_path
end
def destroy_all
if policy_scope(model).destroy_all
flash[:success] = I18n.t('flash.actions.destroy_all.notice')
else
flash[:error] = I18n.t('flash.actions.destroy_all.error')
end
redirect_to resources_path
end
def settings; end
private
def resources_path
polymorphic_path model
end
def resource_path
polymorphic_path resource
end
def resource_build
model.new
end
def resource
instance_variable_get(model_attribute_name)
end
def set_resource
instance_variable_set model_attribute_name,
policy_scope(model).
includes(relationships).
find(params[:id])
end
def authorize_resource
authorize(resource || model)
end
def resource_params
params.require(model_name_underscored.to_sym).permit permitted_parameters
end
end
class ProductItemsController < ApplicationController
include Resourceable
serve ProductItem,
relationships: %i[group_product product_type],
parameters: %i[part_number description base_price product_type_id remanufacturing_group_id],
order: { description: :asc }
end
def settings
@imports = Importer::ProductItems.order(created_at: :desc)
end
def import
importer = Importer::ProductItems.new document: params[:file],
created_by: current_user
if importer.save
ImporterProcessJob.perform_later importer
flash[:success] = "Importação #{importer.id} - agendada para processamento"
else
flash[:warn] = "Erro ao importar:\n#{importer.errors.full_messages.join("\n")}"
end
redirect_to settings_product_items_path
end
def export
@product_items = policy_scope(ProductItem).
includes(:product_type, :group_product, :remanufacturing_group).
ransack(params[:q]).result
render xlsx: "export", filename: "items_de_produto.xlsx"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment