Skip to content

Commit

Permalink
Refactor CRUD for collections from hyrax/collections to hyrax/dashboa…
Browse files Browse the repository at this point in the history
…rd/collections.
  • Loading branch information
elrayle committed Aug 16, 2017
1 parent 0943ccd commit 18557e5
Show file tree
Hide file tree
Showing 66 changed files with 1,392 additions and 718 deletions.
4 changes: 1 addition & 3 deletions .rubocop_fixme.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Security/MarshalLoad:

Metrics/ClassLength:
Exclude:
- 'app/controllers/hyrax/dashboard/collections_controller.rb'
- 'app/controllers/hyrax/file_sets_controller.rb'
- 'app/forms/hyrax/forms/permission_template_form.rb'
- 'app/presenters/hyrax/work_show_presenter.rb'
Expand All @@ -23,7 +24,6 @@ Metrics/AbcSize:

Metrics/ModuleLength:
Exclude:
- 'app/controllers/concerns/hyrax/collections_controller_behavior.rb'
- 'app/controllers/concerns/hyrax/works_controller_behavior.rb'
- 'app/helpers/hyrax/hyrax_helper_behavior.rb'
- 'app/models/concerns/hyrax/ability.rb'
Expand Down Expand Up @@ -65,7 +65,6 @@ RSpec/ExampleLength:
- 'spec/actors/hyrax/actors/generic_work_actor_spec.rb'
- 'spec/controllers/hyrax/api/items_controller_spec.rb'
- 'spec/controllers/hyrax/batch_uploads_controller_spec.rb'
- 'spec/controllers/hyrax/collections_controller_spec.rb'
- 'spec/controllers/hyrax/file_sets_controller_spec.rb'
- 'spec/controllers/hyrax/generic_works_controller_spec.rb'
- 'spec/controllers/hyrax/my/highlights_controller_spec.rb'
Expand Down Expand Up @@ -129,7 +128,6 @@ RSpec/AnyInstance:
- 'spec/controllers/hyrax/api/items_controller_spec.rb'
- 'spec/controllers/hyrax/api/zotero_controller_spec.rb'
- 'spec/controllers/hyrax/batch_edits_controller_spec.rb'
- 'spec/controllers/hyrax/collections_controller_spec.rb'
- 'spec/controllers/hyrax/dashboard_controller_spec.rb'
- 'spec/controllers/hyrax/stats_controller_spec.rb'
- 'spec/jobs/file_set_attached_event_job_spec.rb'
Expand Down
167 changes: 0 additions & 167 deletions app/controllers/concerns/hyrax/collections_controller_behavior.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ module CollectionsControllerBehavior

included do
before_action :filter_docs_with_read_access!, except: :show
before_action :remove_select_something_first_flash, except: :show

include Hyrax::Collections::AcceptsBatches

Expand All @@ -18,12 +17,6 @@ module CollectionsControllerBehavior
# This is needed as of BL 3.7
copy_blacklight_config_from(::CatalogController)

# Catch permission errors
rescue_from Hydra::AccessDenied, CanCan::AccessDenied, with: :deny_collection_access

# actions: index, create, new, edit, show, update, destroy, permissions, citation
before_action :authenticate_user!, except: [:show, :index]

class_attribute :presenter_class,
:form_class,
:single_item_search_builder_class,
Expand All @@ -37,151 +30,23 @@ module CollectionsControllerBehavior

self.presenter_class = Hyrax::CollectionPresenter

self.form_class = Hyrax::Forms::CollectionForm

# The search builder to find the collection
self.single_item_search_builder_class = SingleCollectionSearchBuilder
# The search builder to find the collections' members
self.member_search_builder_class = Hyrax::CollectionMemberSearchBuilder
end

def deny_collection_access(exception)
if exception.action == :edit
redirect_to(url_for(action: 'show'), alert: 'You do not have sufficient privileges to edit this document')
elsif current_user && current_user.persisted?
redirect_to root_url, alert: exception.message
else
session['user_return_to'] = request.url
redirect_to main_app.new_user_session_url, alert: exception.message
end
end

def new
add_breadcrumb t(:'hyrax.controls.home'), root_path
add_breadcrumb t(:'hyrax.dashboard.breadcrumbs.admin'), hyrax.dashboard_path
add_breadcrumb t(:'hyrax.collections.new.header'), hyrax.new_collection_path
@collection.apply_depositor_metadata(current_user.user_key)
form
end

def show
presenter
query_collection_members
end

def edit
query_collection_members
# this is used to populate the "add to a collection" action for the members
@user_collections = find_collections_for_form
form
end

def after_create
form
respond_to do |format|
ActiveFedora::SolrService.instance.conn.commit
format.html { redirect_to collection_path(@collection), notice: 'Collection was successfully created.' }
format.json { render json: @collection, status: :created, location: @collection }
end
end

def after_create_error
form
respond_to do |format|
format.html { render action: 'new' }
format.json { render json: @collection.errors, status: :unprocessable_entity }
end
end

def create
# Manual load and authorize necessary because Cancan will pass in all
# form attributes. When `permissions_attributes` are present the
# collection is saved without a value for `has_model.`
@collection = ::Collection.new
authorize! :create, @collection

@collection.attributes = collection_params.except(:members)
@collection.apply_depositor_metadata(current_user.user_key)
add_members_to_collection unless batch.empty?
if @collection.save
after_create
else
after_create_error
end
end

def after_update
if flash[:notice].nil?
flash[:notice] = 'Collection was successfully updated.'
end
respond_to do |format|
format.html { redirect_to collection_path(@collection) }
format.json { render json: @collection, status: :updated, location: @collection }
end
end

def after_update_error
form
query_collection_members
respond_to do |format|
format.html { render action: 'edit' }
format.json { render json: @collection.errors, status: :unprocessable_entity }
end
end

def update
process_member_changes
if @collection.update(collection_params.except(:members))
after_update
else
after_update_error
end
end

def after_destroy(id)
respond_to do |format|
format.html do
redirect_to my_collections_path,
notice: "Collection #{id} was successfully deleted"
end
format.json { head :no_content, location: @collection }
end
end

def after_destroy_error(id)
respond_to do |format|
format.html do
flash[:notice] = "Collection #{id} could not be deleted"
render :edit, status: :unprocessable_entity
end
format.json { render json: { id: id }, status: :unprocessable_entity, location: @collection }
end
end

def destroy
if @collection.destroy
after_destroy(params[:id])
else
after_destroy_error(params[:id])
end
end

def collection
action_name == 'show' ? @presenter : @collection
end

private

# run a solr query to get the collections the user has access to edit
# @return [Array] a list of the user's collections
def find_collections_for_form
Hyrax::CollectionsService.new(self).search_results(:edit)
end

def remove_select_something_first_flash
flash.delete(:notice) if flash.notice == 'Select something first'
end

def presenter
@presenter ||= begin
# Query Solr for the collection.
Expand Down Expand Up @@ -236,38 +101,6 @@ def params_for_members_query
params.merge(q: params[:cq])
end

def process_member_changes
case params[:collection][:members]
when 'add' then add_members_to_collection
when 'remove' then remove_members_from_collection
when 'move' then move_members_between_collections
end
end

def add_members_to_collection(collection = nil)
collection ||= @collection
collection.add_member_objects batch
end

def remove_members_from_collection
batch.each do |pid|
work = ActiveFedora::Base.find(pid)
work.member_of_collections.delete @collection
work.save!
end
end

def move_members_between_collections
destination_collection = ::Collection.find(params[:destination_collection_id])
remove_members_from_collection
add_members_to_collection(destination_collection)
if destination_collection.save
flash[:notice] = "Successfully moved #{batch.count} files to #{destination_collection.title} Collection."
else
flash[:error] = "An error occured. Files were not moved to #{destination_collection.title} Collection."
end
end

# Include 'catalog' and 'hyrax/base' in the search path for views, while prefering
# our local paths. Thus we are unable to just override `self.local_prefixes`
def _prefixes
Expand Down
Loading

0 comments on commit 18557e5

Please sign in to comment.