Skip to content

Commit

Permalink
Actors now only take Ability
Browse files Browse the repository at this point in the history
Passing a User was previously deprecated
  • Loading branch information
jcoyne committed Mar 24, 2017
1 parent 2b6e273 commit 321bad8
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 53 deletions.
21 changes: 5 additions & 16 deletions app/actors/hyrax/actors/actor_stack.rb
@@ -1,10 +1,10 @@
module Hyrax
module Actors
class ActorStack
attr_reader :curation_concern, :user, :first_actor_class, :more_actors
def initialize(curation_concern, user_or_ability, more_actors)
attr_reader :curation_concern, :ability, :first_actor_class, :more_actors
def initialize(curation_concern, ability, more_actors)
@curation_concern = curation_concern
self.user = user_or_ability
@ability = ability
@more_actors = more_actors
@first_actor_class = @more_actors.shift || RootActor
end
Expand Down Expand Up @@ -37,23 +37,12 @@ def destroy
curation_concern.destroy
end

def ability
@ability ||= ::Ability.new(user)
def user
ability.current_user
end

private

def user=(user_or_ability)
if user_or_ability.respond_to?(:current_user)
@user = user_or_ability.current_user
@ability = user_or_ability
else
Deprecation.warn(self, "Passing a user as an argument to Hyrax::Actors::ActorStack is deprecated, pass an Ability instead")
@user = user_or_ability
@ability = ::Ability.new(user)
end
end

# @param [ActionController::Parameters,Hash,NilClass] new_attributes
def cast_to_indifferent_hash(new_attributes)
new_attributes ||= {}
Expand Down
4 changes: 0 additions & 4 deletions app/actors/hyrax/actors/add_to_work_actor.rb
Expand Up @@ -13,10 +13,6 @@ def update(attributes)

private

def ability
::Ability.new(user)
end

def can_edit_both_works?(work)
ability.can?(:edit, work) && ability.can?(:edit, curation_concern)
end
Expand Down
4 changes: 0 additions & 4 deletions app/actors/hyrax/actors/apply_order_actor.rb
Expand Up @@ -8,10 +8,6 @@ def update(attributes)

private

def ability
::Ability.new(user)
end

def can_edit_both_works?(work)
ability.can?(:edit, work) && ability.can?(:edit, curation_concern)
end
Expand Down
4 changes: 0 additions & 4 deletions app/actors/hyrax/actors/attach_members_actor.rb
Expand Up @@ -40,10 +40,6 @@ def assign_nested_attributes_for_collection(attributes_collection)
end
end

def ability
@ability ||= ::Ability.new(user)
end

# Adds the item to the ordered members so that it displays in the items
# along side the FileSets on the show page
def add(id)
Expand Down
30 changes: 8 additions & 22 deletions spec/actors/hyrax/actors/actor_stack_spec.rb
@@ -1,35 +1,21 @@
require 'spec_helper'
describe Hyrax::Actors::ActorStack do
let(:user_or_ability) { create(:user) }
let(:user) { create(:user) }
let(:ability) { Ability.new(user) }

let(:curation_concern) { GenericWork.new }
let(:attributes) { {} }
subject do
described_class.new(curation_concern,
user_or_ability,
ability,
[])
end

context "when an ability is passed as the second argument" do
let(:user_or_ability) { Ability.new(create(:user)) }
it "assigns user to the ability's user" do
expect(subject.user).to eq user_or_ability.current_user
end
it "delegates user to the ability" do
expect(subject.user).to eq user
end

## Remove these specs when user support is removed.
context "when a user is passed as the second argument" do
let(:user_or_ability) { create(:user) }
before do
allow(Deprecation).to receive(:warn)
end
it "assigns user the user" do
expect(subject.user).to eq user_or_ability
end
it "builds an ability" do
expect(subject.ability.current_user).to eq user_or_ability
end
it "throws a deprecation warning" do
expect(Deprecation).to have_received(:warn).with(subject, "Passing a user as an argument to Hyrax::Actors::ActorStack is deprecated, pass an Ability instead")
end
it "has ability" do
expect(subject.ability).to eq ability
end
end
5 changes: 2 additions & 3 deletions spec/actors/hyrax/actors/attach_members_actor_spec.rb
Expand Up @@ -8,8 +8,9 @@
user: depositor)
end
let(:actor) do
Hyrax::Actors::ActorStack.new(work, depositor, [described_class])
Hyrax::Actors::ActorStack.new(work, ability, [described_class])
end
let(:ability) { ::Ability.new(depositor) }
let(:depositor) { create(:user) }
let(:work) { create(:work) }
let(:attributes) { { work_members_attributes: { '0' => { id: id } } } }
Expand Down Expand Up @@ -47,9 +48,7 @@
let(:another_work) { create(:work) }
let(:id) { another_work.id }
context "and I can edit that object" do
let(:ability) { instance_double(Ability) }
before do
allow(Ability).to receive(:new).and_return(ability)
allow(ability).to receive(:can?).with(:edit, GenericWork).and_return(true)
end
it "is added to the ordered members" do
Expand Down

0 comments on commit 321bad8

Please sign in to comment.