app/controllers/administrate/application_controller.rb in administrate-0.0.4 vs app/controllers/administrate/application_controller.rb in administrate-0.0.6

- old
+ new

@@ -1,9 +1,10 @@ require "administrate/namespace" -require "administrate/pages/form" -require "administrate/pages/table" -require "administrate/pages/show" +require "administrate/page/form" +require "administrate/page/table" +require "administrate/page/show" +require "administrate/resource_resolver" module Administrate class ApplicationController < ActionController::Base def index @resources = resource_class.all @@ -30,11 +31,11 @@ set_resource(resource_class.new(resource_params)) if resource.save redirect_to( [Administrate::NAMESPACE, resource], - notice: notices[:created], + notice: translate("create.success"), ) else @page = Administrate::Page::Form.new(dashboard, resource) render :new end @@ -44,11 +45,11 @@ set_resource if resource.update(resource_params) redirect_to( [Administrate::NAMESPACE, resource], - notice: notices[:updated], + notice: translate("update.success"), ) else @page = Administrate::Page::Form.new(dashboard, resource) render :edit end @@ -56,11 +57,11 @@ def destroy set_resource resource.destroy - flash[:notice] = notices[:destroyed] + flash[:notice] = translate("destroy.success") redirect_to action: :index end private @@ -69,16 +70,12 @@ if resource_name.to_s.pluralize == resource.to_s :active end end - def resource_class - Object.const_get(resource_class_name) - end - def dashboard - @dashboard ||= dashboard_class.new + @dashboard ||= resource_resolver.dashboard_class.new end def set_resource(resource = nil) resource ||= resource_class.find(params[:id]) instance_variable_set(instance_variable, resource) @@ -87,41 +84,31 @@ def resource instance_variable_get(instance_variable) end def resource_params - params.require(:"#{resource_name}").permit(*permitted_attributes) + params.require(resource_name).permit(*permitted_attributes) end def permitted_attributes dashboard.permitted_attributes end - def dashboard_class - Object.const_get("#{resource_class_name}Dashboard") - end - - def resource_class_name - resource_name.to_s.camelcase - end - def instance_variable "@#{resource_name}" end - def resource_title - resource_class_name.titleize - end + delegate :resource_class, :resource_name, to: :resource_resolver - def notices - { - created: "#{resource_title} was successfully created.", - updated: "#{resource_title} was successfully updated.", - destroyed: "#{resource_title} was successfully destroyed.", - } + def resource_resolver + @resource_resolver ||= + Administrate::ResourceResolver.new(controller_path) end - def resource_name - controller_name.singularize.to_sym + def translate(key) + t( + "administrate.controller.#{key}", + resource: resource_resolver.resource_title, + ) end end end