Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build out Hyrax::ResourceForm to (mostly) support create views #4307

Merged
merged 10 commits into from
May 14, 2020
2 changes: 1 addition & 1 deletion .regen
Original file line number Diff line number Diff line change
@@ -1 +1 @@
15
19
53 changes: 53 additions & 0 deletions app/forms/hyrax/forms/resource_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ module Forms
#
def self.ResourceForm(work_class)
Class.new(Hyrax::Forms::ResourceForm) do
self.model_class = work_class

(work_class.fields - work_class.reserved_attributes).each do |field|
property field, default: nil
end
Expand All @@ -24,6 +26,37 @@ def self.ResourceForm(work_class)
#
# This form wraps `Hyrax::ChangeSet` in the `HydraEditor::Form` interface.
class ResourceForm < Hyrax::ChangeSet
##
# Nested form for permissions.
#
# @note due to historical oddities with Hydra::AccessControls and Hydra
# Editor, Hyrax's views rely on `agent_name` and `access` as field
# names. we provide these as virtual fields andprepopulate these from
# `Hyrax::Permission`.
class Permission < Hyrax::ChangeSet
property :agent_name, virtual: true, prepopulator: ->(_opts) { self.agent_name = model.agent }
property :access, virtual: true, prepopulator: ->(_opts) { self.access = model.mode }
end

class_attribute :model_class

delegate :human_readable_type, to: :model

property :visibility # visibility has an accessor on the model

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

collection :permissions, virtual: true, default: [], form: Permission, prepopulator: ->(_opts) { self.permissions = Hyrax::AccessControl.for(resource: model).permissions }

# 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 }
property :visibility_during_embargo, virtual: true, prepopulator: ->(_opts) { self.visibility_during_embargo = embargo&.visibility_during_embargo }

property :lease_expiration_date, virtual: true, prepopulator: ->(_opts) { self.lease_expiration_date = lease&.lease_expiration_date }
property :visibility_after_lease, virtual: true, prepopulator: ->(_opts) { self.visibility_after_lease = lease&.visibility_after_lease }
property :visibility_during_lease, virtual: true, prepopulator: ->(_opts) { self.visibility_during_lease = lease&.visibility_during_lease }

class << self
##
# @api public
Expand Down Expand Up @@ -67,6 +100,26 @@ def required_fields=(fields)
def []=(attr, value)
public_send("#{attr}=".to_sym, value)
end

##
# @deprecated use model.class instead
#
# @return [Class]
def model_class # rubocop:disable Rails/Delegate
model.class
end

def primary_terms
[]
end

def secondary_terms
[]
end

def display_additional_fields?
secondary_terms.any?
end
end
end
end
11 changes: 10 additions & 1 deletion app/helpers/hyrax/embargo_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ def assets_with_deactivated_embargoes
# @return [Boolean] whether the resource has an embargo that is currently
# enforced (regardless of whether it has expired)
def embargo_enforced?(resource)
!resource.embargo_release_date.nil?
case resource
when Hydra::AccessControls::Embargoable
!resource.embargo_release_date.nil?
when HydraEditor::Form
embargo_enforced?(resource.model)
when Valkyrie::ChangeSet
Hyrax::EmbargoManager.new(resource: resource.model).enforced?
else
Hyrax::EmbargoManager.new(resource: resource).enforced?
end
end
end
end
11 changes: 10 additions & 1 deletion app/helpers/hyrax/lease_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,16 @@ def assets_with_deactivated_leases
# @return [Boolean] whether the resource has an lease that is currently
# enforced (regardless of whether it has expired)
def lease_enforced?(resource)
!resource.lease_expiration_date.nil?
case resource
when Hydra::AccessControls::Embargoable
!resource.lease_expiration_date.nil?
when HydraEditor::Form
lease_enforced?(resource.model)
when Valkyrie::ChangeSet
Hyrax::LeaseManager.new(resource: resource.model).enforced?
else
Hyrax::LeaseManager.new(resource: resource).enforced?
end
end
end
end
5 changes: 5 additions & 0 deletions app/presenters/hyrax/admin_set_options_presenter.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
module Hyrax
# Presents the options for the AdminSet widget on the create/edit form
class AdminSetOptionsPresenter
##
# @param [Hyrax::AdminSetService] service
def initialize(service)
@service = service
end

# Return AdminSet selectbox options based on access type
#
# @todo this hits the Solr from the view. it would be better to avoid this.
#
# @param [Symbol] access :deposit, :read, or :edit
def select_options(access = :deposit)
@service.search_results(access).map do |admin_set|
Expand Down
2 changes: 1 addition & 1 deletion app/search_builders/hyrax/filter_by_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def models
end

def models_to_solr_clause
models.map(&:to_rdf_representation).join(',')
models.map { |model| model.try(:to_rdf_representation) || model.name }.join(',')
end

def generic_type_field
Expand Down
15 changes: 15 additions & 0 deletions app/services/hyrax/lease_manager.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,25 @@ def apply_lease_for(resource:, query_service: Hyrax.query_service)
.apply
end

def apply_lease_for!(resource:, query_service: Hyrax.query_service)
new(resource: resource, query_service: query_service)
.apply!
end

def lease_for(resource:, query_service: Hyrax.query_service)
new(resource: resource, query_service: query_service)
.lease
end

def release_lease_for(resource:, query_service: Hyrax.query_service)
new(resource: resource, query_service: query_service)
.release
end

def release_lease_for!(resource:, query_service: Hyrax.query_service)
new(resource: resource, query_service: query_service)
.release!
end
end

##
Expand Down
2 changes: 1 addition & 1 deletion app/views/hyrax/base/_form_relationships.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% if Flipflop.assign_admin_set? %>
<%# TODO: consider `Hyrax::AdminSetOptionsPresenter.select_options_for(controller: controller)` instead %>
<%# TODO: avoid direct dependency on AdminSetService and AdminSetOptionsPresenter in the view!! make the controller provide the options! %>
<%= f.input :admin_set_id, as: :select,
include_blank: false,
collection: Hyrax::AdminSetOptionsPresenter.new(Hyrax::AdminSetService.new(controller)).select_options,
Expand Down
24 changes: 24 additions & 0 deletions lib/generators/hyrax/work_resource/work_resource_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,23 @@ def create_model_spec
rspec_installed?
end

# Inserts after the last registered work, or at the top of the config block
def register_work
config = 'config/initializers/hyrax.rb'
lastmatch = nil
in_root do
File.open(config).each_line do |line|
lastmatch = line if line.match?(/config.register_curation_concern :(?!#{file_name})/)
end
content = " # Injected via `rails g hyrax:work_resource #{class_name}`\n" \
" config.register_curation_concern #{registration_path_symbol}\n"
anchor = lastmatch || "Hyrax.config do |config|\n"
inject_into_file config, after: anchor do
content
end
end
end

def create_indexer
template('indexer.rb.erb', File.join('app/indexers/', class_path, "#{file_name}_indexer.rb"))
end
Expand Down Expand Up @@ -67,6 +84,13 @@ def rspec_installed?
defined?(RSpec) && defined?(RSpec::Rails)
end

def registration_path_symbol
return ":#{file_name}" if class_path.blank?
# creates a symbol with a path like "abc/scholarly_paper" where abc
# is the namespace and scholarly_paper is the resource name
":\"#{File.join(class_path, file_name)}\""
end

def revoking?
behavior == :revoke
end
Expand Down
29 changes: 29 additions & 0 deletions spec/controllers/concerns/hyrax/works_controller_behavior_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,35 @@
expect(response.status).to eq 401
end
end

context 'when the user has edit access' do
include_context 'with a logged in user'

before do
allow(controller.current_ability)
.to receive(:can?)
.with(any_args)
.and_return true
end

it 'is a success' do
get :edit, params: { id: work.id }

expect(response.status).to eq 200
end

it 'assigns a form with the current work as model' do
get :edit, params: { id: work.id }

expect(assigns[:form].model.id).to eq work.id
end

it 'renders the form' do
get :edit, params: { id: work.id }

expect(controller).to render_template('hyrax/base/edit')
end
end
end

describe '#new' do
Expand Down
Loading