app/controllers/admin/resource_controller.rb in spree_essentials-0.1.0 vs app/controllers/admin/resource_controller.rb in spree_essentials-0.1.1

- old
+ new

@@ -1,119 +1,131 @@ require 'spree_core/action_callbacks' - class Admin::ResourceController < Admin::BaseController helper_method :new_object_url, :edit_object_url, :object_url, :collection_url before_filter :load_resource respond_to :html - + respond_to :js, :except => [:show, :index] + def new - render :layout => !request.xhr? + respond_with(@object) do |format| + format.html { render :layout => !request.xhr? } + format.js { render :layout => false } + end end - + def edit - render :layout => !request.xhr? + respond_with(@object) do |format| + format.html { render :layout => !request.xhr? } + format.js { render :layout => false } + end end - + def update invoke_callbacks(:update, :before) if @object.update_attributes(params[object_name]) invoke_callbacks(:update, :after) - resource_desc = I18n.t(object_name) + resource_desc = translated_object_name resource_desc += " \"#{@object.name}\"" if @object.respond_to?(:name) flash[:notice] = I18n.t(:successfully_updated, :resource => resource_desc) - respond_to do |format| + respond_with(@object) do |format| format.html { redirect_to location_after_save } - format.js { render :layout => false } + format.js { render :layout => false } end else invoke_callbacks(:update, :fails) - render :edit + respond_with(@object) end end def create invoke_callbacks(:create, :before) if @object.save invoke_callbacks(:create, :after) - resource_desc = I18n.t(object_name) + resource_desc = translated_object_name resource_desc += " \"#{@object.name}\"" if @object.respond_to?(:name) flash[:notice] = I18n.t(:successfully_created, :resource => resource_desc) - respond_to do |format| + respond_with(@object) do |format| format.html { redirect_to location_after_save } - format.js { render :layout => false } + format.js { render :layout => false } end else invoke_callbacks(:create, :fails) - render :new + respond_with(@object) end end - + def destroy invoke_callbacks(:destroy, :before) if @object.destroy invoke_callbacks(:destroy, :after) - resource_desc = I18n.t(object_name) + resource_desc = translated_object_name resource_desc += " \"#{@object.name}\"" if @object.respond_to?(:name) flash[:notice] = I18n.t(:successfully_removed, :resource => resource_desc) - respond_to do |format| + respond_with(@object) do |format| format.html { redirect_to collection_url } format.js { render :partial => "/admin/shared/destroy" } end else invoke_callbacks(:destroy, :fails) - redirect_to collection_url + respond_with(@object) do |format| + format.html { redirect_to collection_url } + end end end - + protected class << self attr_accessor :parent_data attr_accessor :callbacks - + def belongs_to(model_name, options = {}) @parent_data ||= {} @parent_data[:model_name] = model_name @parent_data[:model_class] = model_name.to_s.classify.constantize @parent_data[:find_by] = options[:find_by] || :id end - + def create @callbacks ||= {} @callbacks[:create] ||= Spree::ActionCallbacks.new end - + def update @callbacks ||= {} @callbacks[:update] ||= Spree::ActionCallbacks.new end - + def destroy @callbacks ||= {} @callbacks[:destroy] ||= Spree::ActionCallbacks.new end end def model_class controller_name.classify.constantize end - + + def translated_object_name + I18n.t(object_name) + end + def object_name controller_name.singularize end - + def load_resource if member_action? @object ||= load_resource_instance instance_variable_set("@#{object_name}", @object) else @collection ||= collection instance_variable_set("@#{controller_name}", @collection) end end - + def load_resource_instance if new_actions.include?(params[:action].to_sym) build_resource elsif params[:id] find_resource @@ -121,11 +133,11 @@ end def parent_data self.class.parent_data end - + def parent if parent_data.present? @parent ||= parent_data[:model_class].where(parent_data[:find_by] => params["#{parent_data[:model_name]}_id"]).first instance_variable_set("@#{parent_data[:model_name]}", @parent) else @@ -138,29 +150,29 @@ parent.send(controller_name).find(params[:id]) else model_class.find(params[:id]) end end - + def build_resource if parent_data.present? parent.send(controller_name).build(params[object_name]) else model_class.new(params[object_name]) end end - + def collection return parent.send(controller_name) if parent_data.present? - + if model_class.respond_to?(:accessible_by) && !current_ability.has_block?(params[:action], model_class) model_class.accessible_by(current_ability) else model_class.scoped end end - + def location_after_save collection_url end def invoke_callbacks(action, callback_type) @@ -180,36 +192,36 @@ new_polymorphic_url([:admin, parent, model_class], options) else new_polymorphic_url([:admin, model_class], options) end end - + def edit_object_url(object, options = {}) if parent_data.present? send "edit_admin_#{parent_data[:model_name]}_#{object_name}_url", parent, object, options else send "edit_admin_#{object_name}_url", object, options end end - + def object_url(object = nil, options = {}) target = object ? object : @object if parent_data.present? send "admin_#{parent_data[:model_name]}_#{object_name}_url", parent, target, options else send "admin_#{object_name}_url", target, options end end - + def collection_url(options = {}) if parent_data.present? polymorphic_url([:admin, parent, model_class], options) else polymorphic_url([:admin, model_class], options) end end - + def collection_actions [:index] end def member_action? @@ -217,6 +229,6 @@ end def new_actions [:new, :create] end -end \ No newline at end of file +end