Skip to content

Commit

Permalink
Collections role sharing language (#139)
Browse files Browse the repository at this point in the history
* Updates collections sharing language

* Add roles select option to participants and sharing collections

* Removes unused Twitter Typeahead
  • Loading branch information
norm0 committed Feb 15, 2022
1 parent 6e1bbe6 commit 983bb9c
Show file tree
Hide file tree
Showing 4 changed files with 397 additions and 1 deletion.
215 changes: 215 additions & 0 deletions app/helpers/hyrax/collections_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
# frozen_string_literal: true
module Hyrax
module CollectionsHelper # rubocop:disable Metrics/ModuleLength
##
# @since 3.1.0
# @return [Array<SolrDocument>]
def available_child_collections(collection:)
Hyrax::Collections::NestedCollectionQueryService
.available_child_collections(parent: collection, scope: controller, limit_to_id: nil)
end

##
# @since 3.1.0
#
# @note provides data for handleAddToCollection javascript
#
# @return [String] JSON document containing id/title pairs for eligible
# parent collections to be displayed in an "Add to Collection" dropdown
def available_parent_collections_data(collection:)
Hyrax::Collections::NestedCollectionQueryService
.available_parent_collections(child: collection, scope: controller, limit_to_id: nil)
.map do |result|
{ "id" => result.id, "title_first" => result.title.first }
end.to_json
end

##
# @since 3.0.0
# @return [#to_s]
def collection_metadata_label(collection, field)
Hyrax::PresenterRenderer.new(collection, self).label(field)
end

##
# @since 3.0.0
# @return [#to_s]
def collection_metadata_value(collection, field)
Hyrax::PresenterRenderer.new(collection, self).value(field)
end

##
# @deprecated Use #collection_metadata_label and #collection_metadata_value instead.
#
# @param presenter [Hyrax::CollectionPresenter]
# @param terms [Array<Symbol>,:all] the list of terms to draw
def present_terms(presenter, terms = :all, &block)
Deprecation.warn("the .present_terms is deprecated for removal in Hyrax 4.0.0; " \
"use #collection_metadata_label/value instead")

terms = presenter.terms if terms == :all
Hyrax::PresenterRenderer.new(presenter, self).fields(terms, &block)
end

##
# @since 3.0.0
#
# @see Blacklight::ConfigurationHelperBehavior#active_sort_fields
def collection_member_sort_fields
active_sort_fields
end

def render_collection_links(solr_doc)
collection_list = Hyrax::CollectionMemberService.run(solr_doc, controller.current_ability)
return if collection_list.empty?
links = collection_list.map { |collection| link_to collection.title_or_label, hyrax.collection_path(collection.id) }
collection_links = []
links.each_with_index do |link, n|
collection_links << link
collection_links << ', ' unless links[n + 1].nil?
end
tag.span safe_join([t('hyrax.collection.is_part_of'), ': '] + collection_links)
end

def render_other_collection_links(solr_doc, collection_id)
collection_list = Hyrax::CollectionMemberService.run(solr_doc, controller.current_ability)
return if collection_list.empty?
links = collection_list.select { |collection| collection.id != collection_id }.map { |collection| link_to collection.title_or_label, hyrax.collection_path(collection.id) }
return if links.empty?
collection_links = []
links.each_with_index do |link, n|
collection_links << link
collection_links << ', ' unless links[n + 1].nil?
end
tag.span safe_join([t('hyrax.collection.also_belongs_to'), ': '] + collection_links)
end

##
# Append a collection_type_id to the existing querystring (whether or not it has pre-existing params)
# @return [String] the original url with and added collection_type_id param
def append_collection_type_url(url, collection_type_id)
uri = URI.parse(url)
uri.query = [uri.query, "collection_type_id=#{collection_type_id}"].compact.join('&')
uri.to_s
end

##
# @return [Boolean]
def collection_search_parameters?
params[:cq].present?
end

##
# @deprecated
# @return [Boolean]
def has_collection_search_parameters? # rubocop:disable Naming/PredicateName:
Deprecation.warn('use #collection_search_parameters? helper instead')
collection_search_parameters?
end

def button_for_remove_from_collection(collection, document, label: 'Remove From Collection', btn_class: 'btn-primary')
render 'hyrax/dashboard/collections/button_remove_from_collection', collection: collection, label: label, document: document, btn_class: btn_class
end

def button_for_remove_selected_from_collection(collection, label = 'Remove From Collection')
render 'hyrax/dashboard/collections/button_for_remove_selected_from_collection', collection: collection, label: label
end

# add hidden fields to a form for removing a single document from a collection
def single_item_action_remove_form_fields(form, document)
single_item_action_form_fields(form, document, 'remove')
end

##
# @param collection [Object]
def collection_type_label_for(collection:)
case collection
when Valkyrie::Resource
CollectionType
.find_by_gid!(collection.collection_type_gid)
.title
else
collection.collection_type.title
end
end

##
# @param collection [Object]
#
# @return [Boolean]
def collection_brandable?(collection:)
case collection
when Valkyrie::Resource
CollectionType
.find_by_gid!(collection.collection_type_gid)
.brandable?
else
collection.try(:brandable?)
end
end

##
# @param collection [Object]
#
# @return [Boolean]
def collection_discoverable?(collection:)
case collection
when Valkyrie::Resource
CollectionType
.find_by_gid!(collection.collection_type_gid)
.discoverable?
else
collection.try(:discoverable?)
end
end

##
# @param collection [Object]
#
# @return [Boolean]
def collection_sharable?(collection:)
case collection
when Valkyrie::Resource
CollectionType
.find_by_gid!(collection.collection_type_gid)
.sharable?
else
collection.try(:sharable?)
end
end

##
# @note this helper is primarily intended for use with blacklight facet
# fields. it assumes we index a `collection_type_gid` and the helper
# can be passed as as a `helper_method:` to `add_facet_field`.
#
# @param collection_type_gid [String] The gid of the CollectionType to be looked up
# @return [String] The CollectionType's title if found, else the gid
def collection_type_label(collection_type_gid)
CollectionType.find_by_gid!(collection_type_gid).title
rescue ActiveRecord::RecordNotFound, URI::InvalidURIError, URI::BadURIError
CollectionType.find_or_create_default_collection_type.title
end

##
# @param collection [Object]
#
# @return [PermissionTemplateForm]
def collection_permission_template_form_for(form:)
case form
when Valkyrie::ChangeSet
template_model = Hyrax::PermissionTemplate.find_or_create_by(source_id: form.id.to_s)
Hyrax::Forms::PermissionTemplateForm.new(template_model)
else
form.permission_template
end
end

private

# add hidden fields to a form for performing an action on a single document on a collection
def single_item_action_form_fields(form, document, action)
render 'hyrax/dashboard/collections/single_item_action_fields', form: form, document: document, action: action
end
end
end # rubocop:enable Metrics/ModuleLength
76 changes: 76 additions & 0 deletions app/views/hyrax/admin/admin_sets/_form_participants.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<div id="participants" class="tab-pane">
<div class="panel panel-default labels edit-sharing-tab">
<div class="panel-body">
<section class="section-add-sharing clearfix">
<h3><%= t('.add_participants') %></h3>
<% access_options = options_for_select([['Manage', 'manage'], ['Deposit', 'deposit'], ['View', 'view']]) %>
<div class="sharing-row-form">
<%= simple_form_for collection_permission_template_form_for(form: @form),
url: [hyrax, :admin, @form, :permission_template],
html: { id: 'group-participants-form', class: 'form-inline' } do |f| %>
<%= f.fields_for 'access_grants_attributes',
f.object.access_grants.build(agent_type: 'group'),
index: 0 do |builder| %>
<div class="form-group">
<label><%= t('.add_group') %></label>
<%= builder.hidden_field :agent_type %>
<%= builder.select :agent_id,
Role.all.collect { |p| [ p.name, p.name ] },
{ prompt: "Select a role …"},
class: 'form-control'
%>
</div>
<div class="form-group">
<label>can</label>
<%= builder.select :access,
access_options,
{ prompt: "Add a responsiblity..." },
class: 'form-control' %>
</div>
<% end %>
<%= f.submit t('helpers.submit.hyrax_permission_template_access.create'), class: 'btn btn-sm btn-info' %>
<% end %>
</div>
<hr>
<div class="sharing-row-form">
<%= simple_form_for collection_permission_template_form_for(form: @form),
url: [hyrax, :admin, @form, :permission_template],
html: { id: 'user-participants-form', class: 'form-inline add-users' } do |f| %>
<%= f.fields_for 'access_grants_attributes',
f.object.access_grants.build(agent_type: 'user'),
index: 0 do |builder| %>
<div class="form-group">
<label><%= t('.add_user') %></label>
<%= builder.hidden_field :agent_type %>
<%= builder.text_field :agent_id,
placeholder: "Search for a user..." %>
</div>
<div class="form-group">
<label>can</label>
<%= builder.select :access,
access_options,
{ prompt: "Add a responsiblity..." },
class: 'form-control' %>
</div>
<% end %>
<%= f.submit t('helpers.submit.hyrax_permission_template_access.create'), class: 'btn btn-sm btn-info' %>
<% end %>
</div>

<p class="help-block"><%= t('hyrax.admin.admin_sets.form.note') %></p>
</section>

<fieldset class="admin-set-participants section-collection-sharing">
<legend><%= t(".current_participants") %></legend>
<%= render 'form_participant_table', access: 'managers', filter: :manage? %>
<%= render 'form_participant_table', access: 'depositors', filter: :deposit? %>
<%= render 'form_participant_table', access: 'viewers', filter: :view? %>
</fieldset>

</div>
<!-- /.panel-body -->
</div>
</div>
87 changes: 87 additions & 0 deletions app/views/hyrax/dashboard/collections/_form_share.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<% access_options = options_for_select([[t('.manager'), 'manage'], [t('.depositor'), 'deposit'], [t('.viewer'), 'view']]) %>

<div id="participants" class="tab-pane">
<div class="edit-sharing-tab">
<section class="section-add-sharing">
<p><%= t('.note') %></p>
<h3><%= t('.add_sharing') %></h3>

<!-- Add group form -->
<div class="form-add-sharing-wrapper" data-id="<%= @form.id %>">
<%= simple_form_for collection_permission_template_form_for(form: @form),
url: [hyrax, :dashboard, @form, :permission_template],
html: { id: 'group-participants-form' } do |f| %>

<div class="form-inline add-sharing-form sharing-row-form">
<%= f.fields_for 'access_grants_attributes',
f.object.access_grants.build(agent_type: 'group'),
index: 0 do |builder| %>

<div class="form-group">
<label><%= t('.add_group') %>:</label>
<%= builder.hidden_field :agent_type %>
<%= builder.select :agent_id,
Role.all.collect { |p| [ p.name, p.name ] },
{ prompt: t('.search_for_a_group')},
class: 'form-control'
%>
</div>
<div class="form-group">
<label>can</label>
<%= builder.select :access,
access_options,
{ prompt: t('.select_a_role') },
class: 'form-control' %>
</div>
<% end %>
<%= f.submit t('helpers.submit.hyrax_permission_template_access.create'), class: 'btn btn-sm btn-info edit-collection-add-sharing-button', :disabled => true %>

</div>
<!-- /.form-inline -->
<% end %>
</div>
<hr>
<!-- Add user form -->
<div class="form-add-sharing-wrapper" data-id="<%= @form.id %>">
<%= simple_form_for collection_permission_template_form_for(form: @form),
url: [hyrax, :dashboard, @form, :permission_template],
html: { id: 'user-participants-form' } do |f| %>

<div class="form-inline sharing-row-form add-users">
<%= f.fields_for 'access_grants_attributes',
f.object.access_grants.build(agent_type: 'user'),
index: 0 do |builder| %>

<div class="form-group">
<label><%= t('.add_user') %>:</label>
<%= builder.hidden_field :agent_type %>
<%= builder.text_field :agent_id,
placeholder: t('.search_for_a_user') %>
</div>
<div class="form-group">
<label>can</label>
<%= builder.select :access,
access_options,
{ prompt: t('.select_a_role') },
class: 'form-control' %>
</div>
<% end %>
<%= f.submit t('helpers.submit.hyrax_permission_template_access.create'), class: 'btn btn-sm btn-info edit-collection-add-sharing-button', :disabled => true %>

</div>
<!-- /.form-inline -->
<% end %>
</div>

<p class="help-block"><%= t('hyrax.admin.admin_sets.form.note') %></p>
</section>

<section class="section-collection-sharing">
<legend><%= t(".current_shared") %></legend>
<%= render 'form_share_table', access: 'managers', filter: :manage? %>
<%= render 'form_share_table', access: 'depositors', filter: :deposit? %>
<%= render 'form_share_table', access: 'viewers', filter: :view? %>
</section>
</div>
</div>

0 comments on commit 983bb9c

Please sign in to comment.