Skip to content

Commit

Permalink
Merge pull request #421 from mbarnett/multi_comm_coll
Browse files Browse the repository at this point in the history
 Working multi-community/collection selection
  • Loading branch information
mbarnett authored Jan 31, 2018
2 parents 8762341 + 8611cbb commit 5cb10a2
Show file tree
Hide file tree
Showing 20 changed files with 180 additions and 314 deletions.
49 changes: 45 additions & 4 deletions app/assets/javascripts/jupiter/item_draft.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ var unsavedChanges = false;
$(document).on('turbolinks:load', function() {
unsavedChanges = false;

toggle_remove_visibility();

$('#js-license-accordion .card').on('hidden.bs.collapse', toggleIcon);
$('#js-license-accordion .card').on('shown.bs.collapse', toggleIcon);

Expand All @@ -23,10 +25,18 @@ $(document).on('turbolinks:load', function() {
e.preventDefault();
});

// bring over community/collection select from items (tweaked a bit)
// (we only need to handle one pairing for time being)
$('form.js-deposit-item .js-community-select').change(function() {
var $collectionSelect = $('.js-collection-select');
$('form.js-deposit-item .js-add-community-collection').click(function(e) {
e.preventDefault();
add_community_collection_input();
});

$('form.js-deposit-item').on('click', '.js-remove-community-collection', function() {
event.preventDefault();
remove_community_collection_input($(this));
});

$('form.js-deposit-item').on('change', '.js-community-select', function() {
var $collectionSelect = collection_select($(this));
var id = $(this).find('option:selected').val();
if (!id) {
$collectionSelect.prop('disabled', true).empty();
Expand Down Expand Up @@ -72,6 +82,37 @@ $(window).bind('beforeunload', function(event) {
}
});

// Find collection select
function collection_select($element) {
var $root = $element.hasClass('.js-community-collection') ? $element : $element.closest('.js-community-collection');
return $root.find('.js-collection-select');
}

function add_community_collection_input() {
var $new_input = $("div.js-community-collection").first().clone();
// Clear selections and disable collection select
$new_input.find('.js-community-select').val(null);
collection_select($new_input).attr('disabled', true).val(null);

$new_input.appendTo('.js-communities-collections-list');
toggle_remove_visibility();
}

function remove_community_collection_input($link) {
if ($('div.js-community-collection').length > 1) {
$link.closest('div.js-community-collection').remove();
toggle_remove_visibility();
}
}

function toggle_remove_visibility() {
if ($('div.js-community-collection').length > 1) {
$('.js-remove-community-collection').show();
} else {
$('.js-remove-community-collection').hide();
}
}

function toggleIcon(e) {
$(e.target).prev('.card-header')
.find('.js-more-less')
Expand Down
60 changes: 0 additions & 60 deletions app/assets/javascripts/jupiter/items.js

This file was deleted.

1 change: 0 additions & 1 deletion app/controllers/admin/items_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
class Admin::ItemsController < Admin::AdminController

# TODO: Implement me
def index
@items = Item.limit(10).sort(:record_created_at, :desc)
end
Expand Down
19 changes: 15 additions & 4 deletions app/controllers/items/draft_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Items::DraftController < ApplicationController

include Wicked::Wizard

before_action :initialize_communities, only: [:show, :edit]

steps(*DraftItem.wizard_steps.keys.map(&:to_sym))

def show
Expand Down Expand Up @@ -36,10 +38,15 @@ def update
when :describe_item
params[:draft_item][:status] = DraftItem.statuses[:active]

# TODO: Need to handle multiple community/collection pairs down the road
community_id = params[:draft_item].delete :community_id
collection_id = params[:draft_item].delete :collection_id
@draft_item.member_of_paths = { 'community_id': community_id, 'collection_id': collection_id }
@draft_item.member_of_paths = { community_id: [], collection_id: [] }
communities = params[:draft_item].delete :community_id
communities.each_with_index do |community_id, idx|
next if community_id.blank?
@draft_item.member_of_paths['community_id'] << community_id
collection_id = params[:draft_item]['collection_id'][idx]
@draft_item.member_of_paths['collection_id'] << collection_id
end
params[:draft_item].delete :collection_id

@draft_item.update_attributes(permitted_attributes(DraftItem))

Expand Down Expand Up @@ -83,4 +90,8 @@ def destroy
redirect_back(fallback_location: root_path, notice: t('.successful_deletion'))
end

def initialize_communities
@communities = Community.all
end

end
80 changes: 3 additions & 77 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
@@ -1,85 +1,11 @@
class ItemsController < ApplicationController

before_action :load_item, only: [:show, :edit, :update]
before_action :initialize_communities_and_collections, only: [:new, :edit]

def show; end

def new
# TODO: Remove for Draft Item Controller
@item = Item.new_locked_ldp_object
authorize @item
end

def create
# TODO: Remove for Draft Item Controller
communities = params[:item].delete :community
collections = params[:item].delete :collection

@item = Item.new_locked_ldp_object(permitted_attributes(Item))
authorize @item

# TODO: add validations?
@item.unlock_and_fetch_ldp_object do |unlocked_item|
unlocked_item.owner = current_user.id
unlocked_item.add_communities_and_collections(communities, collections)
unlocked_item.add_files(params[:item][:file])
unlocked_item.save!

if unlocked_item.save
redirect_to @item, notice: t('.created')
else
initialize_communities_and_collections
render :new, status: :bad_request
end
end
end

def edit
# TODO: Remove for Draft Item Controller
end

def update
# TODO: Remove for Draft Item Controller
authorize @item

communities = params[:item].delete :community
collections = params[:item].delete :collection

@item.unlock_and_fetch_ldp_object do |unlocked_item|
unlocked_item.update_attributes(permitted_attributes(@item))
unlocked_item.add_communities_and_collections(communities, collections)
unlocked_item.add_files(params[:item][:file])

if unlocked_item.save
redirect_to @item, notice: t('.updated')
else
initialize_communities_and_collections
render :edit, status: :bad_request
end
end
end

private

def load_item
def show
@item = JupiterCore::LockedLdpObject.find(params[:id], types: [Item, Thesis])
authorize @item
end

def initialize_communities_and_collections
@communities = Community.all

@item_communities = []
@item_collections = []
@collection_choices = []

return if @item.nil?
@item.each_community_collection do |community, collection|
@item_communities << community
@item_collections << collection
@collection_choices << community.member_collections
end
end
# TODO: this is just a temp hack to give edit_item_path calls in templates somewhere to point until draft editing lands
def edit; end

end
28 changes: 25 additions & 3 deletions app/models/draft_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,21 @@ class DraftItem < ApplicationRecord

validates :files, presence: true, if: :validate_upload_files?

def communities
return unless member_of_paths.present? && member_of_paths['community_id']
member_of_paths['community_id'].map do |cid|
Community.find(cid)
end
end

def each_community_collection
return unless member_of_paths && member_of_paths['community_id'].present?
member_of_paths['community_id'].each_with_index do |community_id, idx|
collection_id = member_of_paths['collection_id'][idx]
yield Community.find(community_id), collection_id.present? ? Collection.find(collection_id) : nil
end
end

def thumbnail
if thumbnail_id.present?
# TODO: Weird bug with activestorage when walking the assocation to all the attachments
Expand Down Expand Up @@ -124,7 +139,11 @@ def ingest_into_fedora
source: source,
related_link: related_item
).unlock_and_fetch_ldp_object do |unlocked_obj|
unlocked_obj.add_to_path(member_of_paths['community_id'], member_of_paths['collection_id'])

each_community_collection do |community, collection|
unlocked_obj.add_to_path(community.id, collection.id)
end

unlocked_obj.add_files(map_activestorage_files_as_file_objects)
unlocked_obj.save!
end
Expand All @@ -137,10 +156,13 @@ def ingest_into_fedora
# TODO: validate if community/collection ID's are actually in Fedora?
def communities_and_collections_validations
return if member_of_paths.blank? # caught by presence check
# member_of_paths.each do |path| # TODO eventually this will be an array of hashes
errors.add(:member_of_paths, :community_not_found) if member_of_paths['community_id'].blank?
errors.add(:member_of_paths, :collection_not_found) if member_of_paths['collection_id'].blank?
# end
return unless member_of_paths['community_id'].present? && member_of_paths['collection_id'].present?
member_of_paths['community_id'].each_with_index do |community_id, idx|
errors.add(:member_of_paths, :community_not_found) if community_id.blank?
errors.add(:member_of_paths, :collection_not_found) if member_of_paths['collection_id'][idx].blank?
end
end

def validate_describe_item?
Expand Down
27 changes: 0 additions & 27 deletions app/views/items/_community_collection_input.html.erb

This file was deleted.

Loading

0 comments on commit 5cb10a2

Please sign in to comment.