From f7be93172a314b27ea3d94c4882d0938dfebd8a0 Mon Sep 17 00:00:00 2001 From: Alessandro Rodi Date: Sun, 11 Dec 2016 16:00:02 +0100 Subject: [PATCH] drop support for InheritedResource --- CHANGELOG.rdoc | 2 + lib/cancan.rb | 1 - lib/cancan/controller_additions.rb | 6 +- lib/cancan/inherited_resource.rb | 20 ------- spec/cancan/controller_additions_spec.rb | 5 -- spec/cancan/inherited_resource_spec.rb | 71 ------------------------ 6 files changed, 3 insertions(+), 102 deletions(-) delete mode 100644 lib/cancan/inherited_resource.rb delete mode 100644 spec/cancan/inherited_resource_spec.rb diff --git a/CHANGELOG.rdoc b/CHANGELOG.rdoc index e7f8e964..2db0c23b 100644 --- a/CHANGELOG.rdoc +++ b/CHANGELOG.rdoc @@ -2,6 +2,8 @@ Develop Unreleased + * Drop support for InheritedResource (coorasse) + 1.15.0 (June 13th, 2016) * Add support for Rails 5 (craig1410) diff --git a/lib/cancan.rb b/lib/cancan.rb index bcaa3ce8..e2532544 100644 --- a/lib/cancan.rb +++ b/lib/cancan.rb @@ -5,7 +5,6 @@ require 'cancan/controller_additions' require 'cancan/model_additions' require 'cancan/exceptions' -require 'cancan/inherited_resource' require 'cancan/model_adapters/abstract_adapter' require 'cancan/model_adapters/default_adapter' diff --git a/lib/cancan/controller_additions.rb b/lib/cancan/controller_additions.rb index f3e0c2a8..f8bee16a 100644 --- a/lib/cancan/controller_additions.rb +++ b/lib/cancan/controller_additions.rb @@ -284,11 +284,7 @@ def skip_authorization(*args) end def cancan_resource_class - if ancestors.map(&:to_s).include? "InheritedResources::Actions" - InheritedResource - else - ControllerResource - end + ControllerResource end def cancan_skipper diff --git a/lib/cancan/inherited_resource.rb b/lib/cancan/inherited_resource.rb deleted file mode 100644 index 61bd3331..00000000 --- a/lib/cancan/inherited_resource.rb +++ /dev/null @@ -1,20 +0,0 @@ -module CanCan - # For use with Inherited Resources - class InheritedResource < ControllerResource # :nodoc: - def load_resource_instance - if parent? - @controller.send :association_chain - @controller.instance_variable_get("@#{instance_name}") - elsif new_actions.include? @params[:action].to_sym - resource = @controller.send :build_resource - assign_attributes(resource) - else - @controller.send :resource - end - end - - def resource_base - @controller.send :end_of_association_chain - end - end -end diff --git a/spec/cancan/controller_additions_spec.rb b/spec/cancan/controller_additions_spec.rb index 26223e15..f4c267d0 100644 --- a/spec/cancan/controller_additions_spec.rb +++ b/spec/cancan/controller_additions_spec.rb @@ -104,11 +104,6 @@ expect(@controller.class.cancan_resource_class).to eq(CanCan::ControllerResource) end - it "cancan_resource_class is InheritedResource when class includes InheritedResources::Actions" do - allow(@controller.class).to receive(:ancestors) { ["InheritedResources::Actions"] } - expect(@controller.class.cancan_resource_class).to eq(CanCan::InheritedResource) - end - it "cancan_skipper is an empty hash with :authorize and :load options and remember changes" do expect(@controller_class.cancan_skipper).to eq({:authorize => {}, :load => {}}) @controller_class.cancan_skipper[:load] = true diff --git a/spec/cancan/inherited_resource_spec.rb b/spec/cancan/inherited_resource_spec.rb deleted file mode 100644 index 5e74a9ac..00000000 --- a/spec/cancan/inherited_resource_spec.rb +++ /dev/null @@ -1,71 +0,0 @@ -require "spec_helper" - -describe CanCan::InheritedResource do - let(:ability) { Ability.new(nil) } - let(:params) { HashWithIndifferentAccess.new(:controller => "models") } - let(:controller_class) { Class.new } - let(:controller) { controller_class.new } - - before(:each) do - class Model - attr_accessor :name - - def initialize(attributes={}) - attributes.each do |attribute, value| - send("#{attribute}=", value) - end - end - end - - allow(controller).to receive(:params) { params } - allow(controller).to receive(:current_ability) { ability } - allow(controller_class).to receive(:cancan_skipper) { {:authorize => {}, :load => {}} } - end - - it "show loads resource through controller.resource" do - params.merge!(:action => "show", :id => 123) - allow(controller).to receive(:resource) { :model_resource } - CanCan::InheritedResource.new(controller).load_resource - expect(controller.instance_variable_get(:@model)).to eq(:model_resource) - end - - it "new loads through controller.build_resource" do - params[:action] = "new" - allow(controller).to receive(:build_resource) { :model_resource } - CanCan::InheritedResource.new(controller).load_resource - expect(controller.instance_variable_get(:@model)).to eq(:model_resource) - end - - it "index loads through controller.association_chain when parent" do - params[:action] = "index" - allow(controller).to receive(:association_chain) { controller.instance_variable_set(:@model, :model_resource) } - CanCan::InheritedResource.new(controller, :parent => true).load_resource - expect(controller.instance_variable_get(:@model)).to eq(:model_resource) - end - - it "index loads through controller.end_of_association_chain" do - params[:action] = "index" - allow(Model).to receive(:accessible_by).with(ability, :index) { :projects } - allow(controller).to receive(:end_of_association_chain) { Model } - CanCan::InheritedResource.new(controller).load_resource - expect(controller.instance_variable_get(:@models)).to eq(:projects) - end - - it "builds a new resource with attributes from current ability" do - params[:action] = "new" - ability.can(:create, Model, :name => "from conditions") - allow(controller).to receive(:build_resource) { Struct.new(:name).new } - resource = CanCan::InheritedResource.new(controller) - resource.load_resource - expect(controller.instance_variable_get(:@model).name).to eq("from conditions") - end - - it "overrides initial attributes with params" do - params.merge!(:action => "new", :model => {:name => "from params"}) - ability.can(:create, Model, :name => "from conditions") - allow(controller).to receive(:build_resource) { Struct.new(:name).new } - resource = CanCan::ControllerResource.new(controller) - resource.load_resource - expect(controller.instance_variable_get(:@model).name).to eq("from params") - end -end