Skip to content

Commit

Permalink
add permission support to ChangeSet forms
Browse files Browse the repository at this point in the history
adds support for permissions to change set style forms for use with valkyrie
objects.
  • Loading branch information
tom johnson committed May 12, 2020
1 parent bfad1fe commit 3d5984c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
5 changes: 5 additions & 0 deletions app/forms/hyrax/forms/resource_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ class ResourceForm < Hyrax::ChangeSet

property :agreement_accepted, virtual: true, default: false, prepopulator: ->(_opts) { self.agreement_accepted = !model.new_record }

collection :permissions, virtual: true, default: [], prepopulator: ->(_opts) { self.permissions = Hyrax::AccessControl.for(resource: model).permissions } do
property :agent_name
property :access
end

# virtual properties for embargo/lease;
property :embargo_release_date, virtual: true, prepopulator: ->(_opts) { self.embargo_release_date = embargo&.embargo_release_date }
property :visibility_after_embargo, virtual: true, prepopulator: ->(_opts) { self.visibility_after_embargo = embargo&.visibility_after_embargo }
Expand Down
22 changes: 22 additions & 0 deletions spec/forms/hyrax/forms/resource_form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,28 @@
end
end

describe '#permissions' do
it 'for a new object has empty permissions' do
expect(form.permissions).to be_empty
end

it 'for a new object prepopulates with empty permissions' do
expect { form.prepopulate! }
.not_to change { form.permissions }
.from(be_empty)
end

context 'with existing permissions' do
let(:work) { FactoryBot.valkyrie_create(:hyrax_work, :public) }

it 'prepopulates with the work permissions' do
expect { form.prepopulate! }
.to change { form.permissions }
.to contain_exactly(have_attributes(agent: 'group/public', mode: :read))
end
end
end

describe '#human_readable_type' do
it 'delegates to model' do
expect(form.human_readable_type).to eq work.human_readable_type
Expand Down

0 comments on commit 3d5984c

Please sign in to comment.