From 8fe3abfd45fbf1f8c11fe30e3003e9abd26b48bc Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Wed, 27 Nov 2019 21:48:03 +0000 Subject: [PATCH 1/7] Add resource_controller from spree_backend so that we can now merge it with the OFN's decorator --- .../spree/admin/resource_controller.rb | 261 ++++++++++++++++++ 1 file changed, 261 insertions(+) create mode 100644 app/controllers/spree/admin/resource_controller.rb diff --git a/app/controllers/spree/admin/resource_controller.rb b/app/controllers/spree/admin/resource_controller.rb new file mode 100644 index 0000000000..2220db7d23 --- /dev/null +++ b/app/controllers/spree/admin/resource_controller.rb @@ -0,0 +1,261 @@ +require 'spree/backend/action_callbacks' + +class Spree::Admin::ResourceController < Spree::Admin::BaseController + helper_method :new_object_url, :edit_object_url, :object_url, :collection_url + before_filter :load_resource, :except => [:update_positions] + rescue_from ActiveRecord::RecordNotFound, :with => :resource_not_found + + respond_to :html + respond_to :js, :except => [:show, :index] + + def new + invoke_callbacks(:new_action, :before) + respond_with(@object) do |format| + format.html { render :layout => !request.xhr? } + format.js { render :layout => false } + end + end + + def edit + 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) + flash[:success] = flash_message_for(@object, :successfully_updated) + respond_with(@object) do |format| + format.html { redirect_to location_after_save } + format.js { render :layout => false } + end + else + invoke_callbacks(:update, :fails) + respond_with(@object) + end + end + + def create + invoke_callbacks(:create, :before) + @object.attributes = params[object_name] + if @object.save + invoke_callbacks(:create, :after) + flash[:success] = flash_message_for(@object, :successfully_created) + respond_with(@object) do |format| + format.html { redirect_to location_after_save } + format.js { render :layout => false } + end + else + invoke_callbacks(:create, :fails) + respond_with(@object) + end + end + + def update_positions + params[:positions].each do |id, index| + model_class.where(:id => id).update_all(:position => index) + end + + respond_to do |format| + format.js { render :text => 'Ok' } + end + end + + def destroy + invoke_callbacks(:destroy, :before) + if @object.destroy + invoke_callbacks(:destroy, :after) + flash[:success] = flash_message_for(@object, :successfully_removed) + respond_with(@object) do |format| + format.html { redirect_to collection_url } + format.js { render :partial => "spree/admin/shared/destroy" } + end + else + invoke_callbacks(:destroy, :fails) + respond_with(@object) do |format| + format.html { redirect_to collection_url } + end + end + end + + protected + + def resource_not_found + flash[:error] = flash_message_for(model_class.new, :not_found) + redirect_to collection_url + end + + 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 new_action + @callbacks ||= {} + @callbacks[:new_action] ||= Spree::ActionCallbacks.new + 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 + "Spree::#{controller_name.classify}".constantize + end + + def model_name + parent_data[:model_name].gsub('spree/', '') + end + + def object_name + controller_name.singularize + end + + def load_resource + if member_action? + @object ||= load_resource_instance + + # call authorize! a third time (called twice already in Admin::BaseController) + # this time we pass the actual instance so fine-grained abilities can control + # access to individual records, not just entire models. + authorize! action, @object + + instance_variable_set("@#{object_name}", @object) + else + @collection ||= collection + + # note: we don't call authorize here as the collection method should use + # CanCan's accessible_by method to restrict the actual records returned + + instance_variable_set("@#{controller_name}", @collection) + end + end + + def load_resource_instance + if new_actions.include?(action) + build_resource + elsif params[:id] + find_resource + end + end + + def parent_data + self.class.parent_data + end + + def parent + if parent_data.present? + @parent ||= parent_data[:model_class].send("find_by_#{parent_data[:find_by]}", params["#{model_name}_id"]) + instance_variable_set("@#{model_name}", @parent) + else + nil + end + end + + def find_resource + if parent_data.present? + 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 + else + model_class.new + 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, action) + else + model_class.scoped + end + end + + def location_after_save + collection_url + end + + def invoke_callbacks(action, callback_type) + callbacks = self.class.callbacks || {} + return if callbacks[action].nil? + case callback_type.to_sym + when :before then callbacks[action].before_methods.each {|method| send method } + when :after then callbacks[action].after_methods.each {|method| send method } + when :fails then callbacks[action].fails_methods.each {|method| send method } + end + end + + # URL helpers + + def new_object_url(options = {}) + if parent_data.present? + spree.new_polymorphic_url([:admin, parent, model_class], options) + else + spree.new_polymorphic_url([:admin, model_class], options) + end + end + + def edit_object_url(object, options = {}) + if parent_data.present? + spree.send "edit_admin_#{model_name}_#{object_name}_url", parent, object, options + else + spree.send "edit_admin_#{object_name}_url", object, options + end + end + + def object_url(object = nil, options = {}) + target = object ? object : @object + if parent_data.present? + spree.send "admin_#{model_name}_#{object_name}_url", parent, target, options + else + spree.send "admin_#{object_name}_url", target, options + end + end + + def collection_url(options = {}) + if parent_data.present? + spree.polymorphic_url([:admin, parent, model_class], options) + else + spree.polymorphic_url([:admin, model_class], options) + end + end + + def collection_actions + [:index] + end + + def member_action? + !collection_actions.include? action + end + + def new_actions + [:new, :create] + end +end From 486b5e9edc50c94403cd1581d9add6c8887f3dfc Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Mon, 11 Nov 2019 21:21:56 +0000 Subject: [PATCH 2/7] Merge resource_decorator into resource controller --- .../spree/admin/resource_controller.rb | 8 ++++++++ .../admin/resource_controller_decorator.rb | 20 ------------------- 2 files changed, 8 insertions(+), 20 deletions(-) delete mode 100644 app/controllers/spree/admin/resource_controller_decorator.rb diff --git a/app/controllers/spree/admin/resource_controller.rb b/app/controllers/spree/admin/resource_controller.rb index 2220db7d23..86f33a0640 100644 --- a/app/controllers/spree/admin/resource_controller.rb +++ b/app/controllers/spree/admin/resource_controller.rb @@ -4,6 +4,7 @@ class Spree::Admin::ResourceController < Spree::Admin::BaseController helper_method :new_object_url, :edit_object_url, :object_url, :collection_url before_filter :load_resource, :except => [:update_positions] rescue_from ActiveRecord::RecordNotFound, :with => :resource_not_found + rescue_from CanCan::AccessDenied, :with => :unauthorized respond_to :html respond_to :js, :except => [:show, :index] @@ -142,6 +143,13 @@ class Spree::Admin::ResourceController < Spree::Admin::BaseController authorize! action, @object instance_variable_set("@#{object_name}", @object) + + # If we don't have access, clear the object + unless can? action, @object + instance_variable_set("@#{object_name}", nil) + end + + authorize! action, @object else @collection ||= collection diff --git a/app/controllers/spree/admin/resource_controller_decorator.rb b/app/controllers/spree/admin/resource_controller_decorator.rb deleted file mode 100644 index b1dbc4a383..0000000000 --- a/app/controllers/spree/admin/resource_controller_decorator.rb +++ /dev/null @@ -1,20 +0,0 @@ -module AuthorizeOnLoadResource - def load_resource - super - - if member_action? - # If we don't have access, clear the object - unless can? action, @object - instance_variable_set("@#{object_name}", nil) - end - - authorize! action, @object - end - end -end - -Spree::Admin::ResourceController.prepend(AuthorizeOnLoadResource) - -Spree::Admin::ResourceController.class_eval do - rescue_from CanCan::AccessDenied, with: :unauthorized -end From 8cfd7c610b50c44d6cb42c370f1239fa4ece69d0 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Mon, 11 Nov 2019 21:23:49 +0000 Subject: [PATCH 3/7] Use nested module instead of class Spree::Admin:: --- .../spree/admin/resource_controller.rb | 494 +++++++++--------- 1 file changed, 249 insertions(+), 245 deletions(-) diff --git a/app/controllers/spree/admin/resource_controller.rb b/app/controllers/spree/admin/resource_controller.rb index 86f33a0640..a13559c35e 100644 --- a/app/controllers/spree/admin/resource_controller.rb +++ b/app/controllers/spree/admin/resource_controller.rb @@ -1,269 +1,273 @@ require 'spree/backend/action_callbacks' -class Spree::Admin::ResourceController < Spree::Admin::BaseController - helper_method :new_object_url, :edit_object_url, :object_url, :collection_url - before_filter :load_resource, :except => [:update_positions] - rescue_from ActiveRecord::RecordNotFound, :with => :resource_not_found - rescue_from CanCan::AccessDenied, :with => :unauthorized +module Spree + module Admin + class ResourceController < Spree::Admin::BaseController + helper_method :new_object_url, :edit_object_url, :object_url, :collection_url + before_filter :load_resource, :except => [:update_positions] + rescue_from ActiveRecord::RecordNotFound, :with => :resource_not_found + rescue_from CanCan::AccessDenied, :with => :unauthorized - respond_to :html - respond_to :js, :except => [:show, :index] + respond_to :html + respond_to :js, :except => [:show, :index] - def new - invoke_callbacks(:new_action, :before) - respond_with(@object) do |format| - format.html { render :layout => !request.xhr? } - format.js { render :layout => false } - end - end - - def edit - 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) - flash[:success] = flash_message_for(@object, :successfully_updated) - respond_with(@object) do |format| - format.html { redirect_to location_after_save } - format.js { render :layout => false } - end - else - invoke_callbacks(:update, :fails) - respond_with(@object) - end - end - - def create - invoke_callbacks(:create, :before) - @object.attributes = params[object_name] - if @object.save - invoke_callbacks(:create, :after) - flash[:success] = flash_message_for(@object, :successfully_created) - respond_with(@object) do |format| - format.html { redirect_to location_after_save } - format.js { render :layout => false } - end - else - invoke_callbacks(:create, :fails) - respond_with(@object) - end - end - - def update_positions - params[:positions].each do |id, index| - model_class.where(:id => id).update_all(:position => index) - end - - respond_to do |format| - format.js { render :text => 'Ok' } - end - end - - def destroy - invoke_callbacks(:destroy, :before) - if @object.destroy - invoke_callbacks(:destroy, :after) - flash[:success] = flash_message_for(@object, :successfully_removed) - respond_with(@object) do |format| - format.html { redirect_to collection_url } - format.js { render :partial => "spree/admin/shared/destroy" } - end - else - invoke_callbacks(:destroy, :fails) - respond_with(@object) do |format| - format.html { redirect_to collection_url } - end - end - end - - protected - - def resource_not_found - flash[:error] = flash_message_for(model_class.new, :not_found) - redirect_to collection_url - end - - 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 + def new + invoke_callbacks(:new_action, :before) + respond_with(@object) do |format| + format.html { render :layout => !request.xhr? } + format.js { render :layout => false } + end end - def new_action - @callbacks ||= {} - @callbacks[:new_action] ||= Spree::ActionCallbacks.new - end - - def create - @callbacks ||= {} - @callbacks[:create] ||= Spree::ActionCallbacks.new + def edit + respond_with(@object) do |format| + format.html { render :layout => !request.xhr? } + format.js { render :layout => false } + end end def update - @callbacks ||= {} - @callbacks[:update] ||= Spree::ActionCallbacks.new + invoke_callbacks(:update, :before) + if @object.update_attributes(params[object_name]) + invoke_callbacks(:update, :after) + flash[:success] = flash_message_for(@object, :successfully_updated) + respond_with(@object) do |format| + format.html { redirect_to location_after_save } + format.js { render :layout => false } + end + else + invoke_callbacks(:update, :fails) + respond_with(@object) + end + end + + def create + invoke_callbacks(:create, :before) + @object.attributes = params[object_name] + if @object.save + invoke_callbacks(:create, :after) + flash[:success] = flash_message_for(@object, :successfully_created) + respond_with(@object) do |format| + format.html { redirect_to location_after_save } + format.js { render :layout => false } + end + else + invoke_callbacks(:create, :fails) + respond_with(@object) + end + end + + def update_positions + params[:positions].each do |id, index| + model_class.where(:id => id).update_all(:position => index) + end + + respond_to do |format| + format.js { render :text => 'Ok' } + end end def destroy - @callbacks ||= {} - @callbacks[:destroy] ||= Spree::ActionCallbacks.new + invoke_callbacks(:destroy, :before) + if @object.destroy + invoke_callbacks(:destroy, :after) + flash[:success] = flash_message_for(@object, :successfully_removed) + respond_with(@object) do |format| + format.html { redirect_to collection_url } + format.js { render :partial => "spree/admin/shared/destroy" } + end + else + invoke_callbacks(:destroy, :fails) + respond_with(@object) do |format| + format.html { redirect_to collection_url } + end + end end - end - def model_class - "Spree::#{controller_name.classify}".constantize - end + protected - def model_name - parent_data[:model_name].gsub('spree/', '') - end + def resource_not_found + flash[:error] = flash_message_for(model_class.new, :not_found) + redirect_to collection_url + end - def object_name - controller_name.singularize - end + class << self + attr_accessor :parent_data + attr_accessor :callbacks - def load_resource - if member_action? - @object ||= load_resource_instance - - # call authorize! a third time (called twice already in Admin::BaseController) - # this time we pass the actual instance so fine-grained abilities can control - # access to individual records, not just entire models. - authorize! action, @object - - instance_variable_set("@#{object_name}", @object) - - # If we don't have access, clear the object - unless can? action, @object - instance_variable_set("@#{object_name}", nil) + 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 - authorize! action, @object - else - @collection ||= collection + def new_action + @callbacks ||= {} + @callbacks[:new_action] ||= Spree::ActionCallbacks.new + end - # note: we don't call authorize here as the collection method should use - # CanCan's accessible_by method to restrict the actual records returned + def create + @callbacks ||= {} + @callbacks[:create] ||= Spree::ActionCallbacks.new + end - instance_variable_set("@#{controller_name}", @collection) + def update + @callbacks ||= {} + @callbacks[:update] ||= Spree::ActionCallbacks.new + end + + def destroy + @callbacks ||= {} + @callbacks[:destroy] ||= Spree::ActionCallbacks.new + end + end + + def model_class + "Spree::#{controller_name.classify}".constantize + end + + def model_name + parent_data[:model_name].gsub('spree/', '') + end + + def object_name + controller_name.singularize + end + + def load_resource + if member_action? + @object ||= load_resource_instance + + # call authorize! a third time (called twice already in Admin::BaseController) + # this time we pass the actual instance so fine-grained abilities can control + # access to individual records, not just entire models. + authorize! action, @object + + instance_variable_set("@#{object_name}", @object) + + # If we don't have access, clear the object + unless can? action, @object + instance_variable_set("@#{object_name}", nil) + end + + authorize! action, @object + else + @collection ||= collection + + # note: we don't call authorize here as the collection method should use + # CanCan's accessible_by method to restrict the actual records returned + + instance_variable_set("@#{controller_name}", @collection) + end + end + + def load_resource_instance + if new_actions.include?(action) + build_resource + elsif params[:id] + find_resource + end + end + + def parent_data + self.class.parent_data + end + + def parent + if parent_data.present? + @parent ||= parent_data[:model_class].send("find_by_#{parent_data[:find_by]}", params["#{model_name}_id"]) + instance_variable_set("@#{model_name}", @parent) + else + nil + end + end + + def find_resource + if parent_data.present? + 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 + else + model_class.new + 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, action) + else + model_class.scoped + end + end + + def location_after_save + collection_url + end + + def invoke_callbacks(action, callback_type) + callbacks = self.class.callbacks || {} + return if callbacks[action].nil? + case callback_type.to_sym + when :before then callbacks[action].before_methods.each {|method| send method } + when :after then callbacks[action].after_methods.each {|method| send method } + when :fails then callbacks[action].fails_methods.each {|method| send method } + end + end + + # URL helpers + + def new_object_url(options = {}) + if parent_data.present? + spree.new_polymorphic_url([:admin, parent, model_class], options) + else + spree.new_polymorphic_url([:admin, model_class], options) + end + end + + def edit_object_url(object, options = {}) + if parent_data.present? + spree.send "edit_admin_#{model_name}_#{object_name}_url", parent, object, options + else + spree.send "edit_admin_#{object_name}_url", object, options + end + end + + def object_url(object = nil, options = {}) + target = object ? object : @object + if parent_data.present? + spree.send "admin_#{model_name}_#{object_name}_url", parent, target, options + else + spree.send "admin_#{object_name}_url", target, options + end + end + + def collection_url(options = {}) + if parent_data.present? + spree.polymorphic_url([:admin, parent, model_class], options) + else + spree.polymorphic_url([:admin, model_class], options) + end + end + + def collection_actions + [:index] + end + + def member_action? + !collection_actions.include? action + end + + def new_actions + [:new, :create] end end - - def load_resource_instance - if new_actions.include?(action) - build_resource - elsif params[:id] - find_resource - end - end - - def parent_data - self.class.parent_data - end - - def parent - if parent_data.present? - @parent ||= parent_data[:model_class].send("find_by_#{parent_data[:find_by]}", params["#{model_name}_id"]) - instance_variable_set("@#{model_name}", @parent) - else - nil - end - end - - def find_resource - if parent_data.present? - 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 - else - model_class.new - 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, action) - else - model_class.scoped - end - end - - def location_after_save - collection_url - end - - def invoke_callbacks(action, callback_type) - callbacks = self.class.callbacks || {} - return if callbacks[action].nil? - case callback_type.to_sym - when :before then callbacks[action].before_methods.each {|method| send method } - when :after then callbacks[action].after_methods.each {|method| send method } - when :fails then callbacks[action].fails_methods.each {|method| send method } - end - end - - # URL helpers - - def new_object_url(options = {}) - if parent_data.present? - spree.new_polymorphic_url([:admin, parent, model_class], options) - else - spree.new_polymorphic_url([:admin, model_class], options) - end - end - - def edit_object_url(object, options = {}) - if parent_data.present? - spree.send "edit_admin_#{model_name}_#{object_name}_url", parent, object, options - else - spree.send "edit_admin_#{object_name}_url", object, options - end - end - - def object_url(object = nil, options = {}) - target = object ? object : @object - if parent_data.present? - spree.send "admin_#{model_name}_#{object_name}_url", parent, target, options - else - spree.send "admin_#{object_name}_url", target, options - end - end - - def collection_url(options = {}) - if parent_data.present? - spree.polymorphic_url([:admin, parent, model_class], options) - else - spree.polymorphic_url([:admin, model_class], options) - end - end - - def collection_actions - [:index] - end - - def member_action? - !collection_actions.include? action - end - - def new_actions - [:new, :create] - end + end end From f79182253a7dd3e421de7fb7b8ddedce3f9357e2 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Mon, 11 Nov 2019 21:31:07 +0000 Subject: [PATCH 4/7] Fix some rubocop issues --- .../spree/admin/resource_controller.rb | 64 ++++++++++--------- 1 file changed, 33 insertions(+), 31 deletions(-) diff --git a/app/controllers/spree/admin/resource_controller.rb b/app/controllers/spree/admin/resource_controller.rb index a13559c35e..9f81568107 100644 --- a/app/controllers/spree/admin/resource_controller.rb +++ b/app/controllers/spree/admin/resource_controller.rb @@ -4,25 +4,25 @@ module Spree module Admin class ResourceController < Spree::Admin::BaseController helper_method :new_object_url, :edit_object_url, :object_url, :collection_url - before_filter :load_resource, :except => [:update_positions] - rescue_from ActiveRecord::RecordNotFound, :with => :resource_not_found - rescue_from CanCan::AccessDenied, :with => :unauthorized + before_filter :load_resource, except: [:update_positions] + rescue_from ActiveRecord::RecordNotFound, with: :resource_not_found + rescue_from CanCan::AccessDenied, with: :unauthorized respond_to :html - respond_to :js, :except => [:show, :index] + respond_to :js, except: [:show, :index] def new invoke_callbacks(:new_action, :before) respond_with(@object) do |format| - format.html { render :layout => !request.xhr? } - format.js { render :layout => false } + format.html { render layout: !request.xhr? } + format.js { render layout: false } end end def edit respond_with(@object) do |format| - format.html { render :layout => !request.xhr? } - format.js { render :layout => false } + format.html { render layout: !request.xhr? } + format.js { render layout: false } end end @@ -33,7 +33,7 @@ module Spree flash[:success] = flash_message_for(@object, :successfully_updated) 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) @@ -49,7 +49,7 @@ module Spree flash[:success] = flash_message_for(@object, :successfully_created) 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) @@ -59,11 +59,11 @@ module Spree def update_positions params[:positions].each do |id, index| - model_class.where(:id => id).update_all(:position => index) + model_class.where(id: id).update_all(position: index) end respond_to do |format| - format.js { render :text => 'Ok' } + format.js { render text: 'Ok' } end end @@ -74,7 +74,7 @@ module Spree flash[:success] = flash_message_for(@object, :successfully_removed) respond_with(@object) do |format| format.html { redirect_to collection_url } - format.js { render :partial => "spree/admin/shared/destroy" } + format.js { render partial: "spree/admin/shared/destroy" } end else invoke_callbacks(:destroy, :fails) @@ -175,17 +175,16 @@ module Spree end def parent - if parent_data.present? - @parent ||= parent_data[:model_class].send("find_by_#{parent_data[:find_by]}", params["#{model_name}_id"]) - instance_variable_set("@#{model_name}", @parent) - else - nil - end + return nil if parent_data.blank? + + @parent ||= parent_data[:model_class]. + public_send("find_by_#{parent_data[:find_by]}", params["#{model_name}_id"]) + instance_variable_set("@#{model_name}", @parent) end def find_resource if parent_data.present? - parent.send(controller_name).find(params[:id]) + parent.public_send(controller_name).find(params[:id]) else model_class.find(params[:id]) end @@ -193,15 +192,17 @@ module Spree def build_resource if parent_data.present? - parent.send(controller_name).build + parent.public_send(controller_name).build else model_class.new 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) + return parent.public_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, action) else model_class.scoped @@ -215,10 +216,11 @@ module Spree def invoke_callbacks(action, callback_type) callbacks = self.class.callbacks || {} return if callbacks[action].nil? + case callback_type.to_sym - when :before then callbacks[action].before_methods.each {|method| send method } - when :after then callbacks[action].after_methods.each {|method| send method } - when :fails then callbacks[action].fails_methods.each {|method| send method } + when :before then callbacks[action].before_methods.each { |method| __send__ method } + when :after then callbacks[action].after_methods.each { |method| __send__ method } + when :fails then callbacks[action].fails_methods.each { |method| __send__ method } end end @@ -234,18 +236,18 @@ module Spree def edit_object_url(object, options = {}) if parent_data.present? - spree.send "edit_admin_#{model_name}_#{object_name}_url", parent, object, options + spree.public_send "edit_admin_#{model_name}_#{object_name}_url", parent, object, options else - spree.send "edit_admin_#{object_name}_url", object, options + spree.public_send "edit_admin_#{object_name}_url", object, options end end def object_url(object = nil, options = {}) - target = object ? object : @object + target = object || @object if parent_data.present? - spree.send "admin_#{model_name}_#{object_name}_url", parent, target, options + spree.public_send "admin_#{model_name}_#{object_name}_url", parent, target, options else - spree.send "admin_#{object_name}_url", target, options + spree.public_send "admin_#{object_name}_url", target, options end end From 1a88549954846d555a541de3996b4edf01c131d7 Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Wed, 11 Dec 2019 12:30:03 +0000 Subject: [PATCH 5/7] Update rubocop todo lists --- .rubocop_manual_todo.yml | 3 +++ .rubocop_todo.yml | 1 - 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/.rubocop_manual_todo.yml b/.rubocop_manual_todo.yml index 61e2fda2a4..ffd69121d6 100644 --- a/.rubocop_manual_todo.yml +++ b/.rubocop_manual_todo.yml @@ -366,6 +366,7 @@ Metrics/AbcSize: - app/controllers/spree/admin/payments_controller_decorator.rb - app/controllers/spree/admin/products_controller_decorator.rb - app/controllers/spree/admin/reports_controller.rb + - app/controllers/spree/admin/resource_controller.rb - app/controllers/spree/admin/search_controller_decorator.rb - app/controllers/spree/admin/taxons_controller.rb - app/controllers/spree/admin/users_controller.rb @@ -565,6 +566,7 @@ Metrics/MethodLength: - app/controllers/spree/admin/payments_controller_decorator.rb - app/controllers/spree/admin/products_controller_decorator.rb - app/controllers/spree/admin/reports_controller.rb + - app/controllers/spree/admin/resource_controller.rb - app/controllers/spree/admin/search_controller_decorator.rb - app/controllers/spree/admin/tax_categories_controller.rb - app/controllers/spree/admin/taxons_controller.rb @@ -646,6 +648,7 @@ Metrics/ClassLength: - app/controllers/spree/admin/base_controller.rb - app/controllers/spree/admin/payment_methods_controller.rb - app/controllers/spree/admin/reports_controller.rb + - app/controllers/spree/admin/resource_controller.rb - app/controllers/spree/admin/users_controller.rb - app/controllers/spree/orders_controller.rb - app/models/enterprise.rb diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 2b7857d6ae..b777a46c7c 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -336,7 +336,6 @@ Style/GuardClause: - 'app/controllers/spree/admin/adjustments_controller_decorator.rb' - 'app/controllers/spree/admin/base_controller_decorator.rb' - 'app/controllers/spree/admin/orders_controller_decorator.rb' - - 'app/controllers/spree/admin/resource_controller_decorator.rb' - 'app/controllers/spree/admin/variants_controller_decorator.rb' - 'app/controllers/spree/checkout_controller.rb' - 'app/controllers/spree/orders_controller.rb' From f8451a2511acb8fc47d11424a6e25a1afe7bb75e Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Thu, 26 Dec 2019 12:11:45 +0000 Subject: [PATCH 6/7] Bring needed action_callbacks from spree_backend --- .../spree/admin/resource_controller.rb | 2 +- app/services/action_callbacks.rb | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 app/services/action_callbacks.rb diff --git a/app/controllers/spree/admin/resource_controller.rb b/app/controllers/spree/admin/resource_controller.rb index 9f81568107..6232fe2932 100644 --- a/app/controllers/spree/admin/resource_controller.rb +++ b/app/controllers/spree/admin/resource_controller.rb @@ -1,4 +1,4 @@ -require 'spree/backend/action_callbacks' +require 'action_callbacks' module Spree module Admin diff --git a/app/services/action_callbacks.rb b/app/services/action_callbacks.rb new file mode 100644 index 0000000000..232daba8d3 --- /dev/null +++ b/app/services/action_callbacks.rb @@ -0,0 +1,26 @@ +module Spree + class ActionCallbacks + attr_reader :before_methods + attr_reader :after_methods + attr_reader :fails_methods + + def initialize + @before_methods = [] + @after_methods = [] + @fails_methods = [] + end + + def before(method) + @before_methods << method + end + + def after(method) + @after_methods << method + end + + def fails(method) + @fails_methods << method + end + end + +end From d54850f097b8e1da38194502948d002e6549000e Mon Sep 17 00:00:00 2001 From: luisramos0 Date: Thu, 26 Dec 2019 12:13:45 +0000 Subject: [PATCH 7/7] Move ActionCallBacks out of the spree namespace --- .../spree/admin/resource_controller.rb | 8 ++-- app/services/action_callbacks.rb | 41 +++++++++---------- 2 files changed, 23 insertions(+), 26 deletions(-) diff --git a/app/controllers/spree/admin/resource_controller.rb b/app/controllers/spree/admin/resource_controller.rb index 6232fe2932..d1ce6e8b27 100644 --- a/app/controllers/spree/admin/resource_controller.rb +++ b/app/controllers/spree/admin/resource_controller.rb @@ -104,22 +104,22 @@ module Spree def new_action @callbacks ||= {} - @callbacks[:new_action] ||= Spree::ActionCallbacks.new + @callbacks[:new_action] ||= ActionCallbacks.new end def create @callbacks ||= {} - @callbacks[:create] ||= Spree::ActionCallbacks.new + @callbacks[:create] ||= ActionCallbacks.new end def update @callbacks ||= {} - @callbacks[:update] ||= Spree::ActionCallbacks.new + @callbacks[:update] ||= ActionCallbacks.new end def destroy @callbacks ||= {} - @callbacks[:destroy] ||= Spree::ActionCallbacks.new + @callbacks[:destroy] ||= ActionCallbacks.new end end diff --git a/app/services/action_callbacks.rb b/app/services/action_callbacks.rb index 232daba8d3..2cfecc961b 100644 --- a/app/services/action_callbacks.rb +++ b/app/services/action_callbacks.rb @@ -1,26 +1,23 @@ -module Spree - class ActionCallbacks - attr_reader :before_methods - attr_reader :after_methods - attr_reader :fails_methods +class ActionCallbacks + attr_reader :before_methods + attr_reader :after_methods + attr_reader :fails_methods - def initialize - @before_methods = [] - @after_methods = [] - @fails_methods = [] - end - - def before(method) - @before_methods << method - end - - def after(method) - @after_methods << method - end - - def fails(method) - @fails_methods << method - end + def initialize + @before_methods = [] + @after_methods = [] + @fails_methods = [] end + def before(method) + @before_methods << method + end + + def after(method) + @after_methods << method + end + + def fails(method) + @fails_methods << method + end end