Skip to content

Commit

Permalink
Merge pull request #4822 from avalonmediasystem/cdl_conditional
Browse files Browse the repository at this point in the history
Add CDL conditional logic
  • Loading branch information
cjcolvar authored Aug 12, 2022
2 parents 2f9b58c + f2ec65f commit 9ab4b56
Show file tree
Hide file tree
Showing 17 changed files with 361 additions and 176 deletions.
14 changes: 8 additions & 6 deletions app/controllers/admin/collections_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,13 @@ def update_access(collection, params)

collection.default_visibility = params[:visibility] unless params[:visibility].blank?
collection.default_hidden = params[:hidden] == "1"
lending_period = build_default_lending_period(collection)
if lending_period.positive?
collection.default_lending_period = lending_period
elsif lending_period.zero?
flash[:error] = "Lending period must be greater than 0."
if Avalon::Configuration.controlled_digital_lending_enabled?
lending_period = build_default_lending_period(collection)
if lending_period.positive?
collection.default_lending_period = lending_period
elsif lending_period.zero?
flash[:error] = "Lending period must be greater than 0."
end
end
end

Expand All @@ -314,7 +316,7 @@ def build_default_lending_period(collection)
d.negative? ? collection.errors.add(:lending_period, "days needs to be a positive integer.") : lending_period += d.days
h.negative? ? collection.errors.add(:lending_period, "hours needs to be a positive integer.") : lending_period += h.hours

flash[:error] = collection.errors.full_messages.join if collection.errors.present?
flash[:error] = collection.errors.full_messages.join(' ') if collection.errors.present?
lending_period.to_i
rescue
0
Expand Down
10 changes: 6 additions & 4 deletions app/jobs/bulk_action_jobs.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Copyright 2011-2022, The Trustees of Indiana University and Northwestern
# University. Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
#
# You may obtain a copy of the License at
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
Expand Down Expand Up @@ -205,7 +205,9 @@ def perform(collection_id, overwrite = true)
media_object = MediaObject.find(id)
media_object.hidden = collection.default_hidden
media_object.visibility = collection.default_visibility
media_object.lending_period = collection.default_lending_period
if Avalon::Configuration.controlled_digital_lending_enabled?
media_object.lending_period = collection.default_lending_period
end

# Special access
if overwrite
Expand Down
14 changes: 8 additions & 6 deletions app/models/access_control_step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,13 @@ def execute context
unless limited_access_submit
media_object.visibility = context[:visibility] unless context[:visibility].blank?
media_object.hidden = context[:hidden] == "1"
lending_period = build_lending_period(context)
if lending_period.positive?
media_object.lending_period = lending_period
elsif lending_period.zero?
context[:error] = "Lending period must be greater than 0."
if Avalon::Configuration.controlled_digital_lending_enabled?
lending_period = build_lending_period(context)
if lending_period.positive?
media_object.lending_period = lending_period
elsif lending_period.zero?
context[:error] = "Lending period must be greater than 0."
end
end
end

Expand Down Expand Up @@ -134,7 +136,7 @@ def build_lending_period(context)
d.negative? ? errors.append("Lending period days needs to be a positive integer.") : lending_period += d.days
h.negative? ? errors.append("Lending period hours needs to be a positive integer.") : lending_period += h.hours

context[:error] = errors.join if errors.present?
context[:error] = errors.join(' ') if errors.present?
lending_period.to_i
rescue
0
Expand Down
14 changes: 8 additions & 6 deletions app/views/_user_util_links.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ Unless required by applicable law or agreed to in writing, software distributed
<%= link_to 'Timelines', main_app.timelines_path, id:'timelines_nav', class: 'nav-link' %>
</li>
<% end %>
<% if current_ability.can? :create, Checkout %>
<li class="nav-item <%= active_for_controller('checkouts') %>">
<%= link_to main_app.checkouts_path, id:'checkouts_nav', class: 'nav-link' do %>
Checkouts
(<span data-role='checkout-counter'><%= Checkout.active_for_user(current_or_guest_user.id).count %></span>)
<% if Avalon::Configuration.controlled_digital_lending_enabled? %>
<% if current_ability.can? :create, Checkout %>
<li class="nav-item <%= active_for_controller('checkouts') %>">
<%= link_to main_app.checkouts_path, id:'checkouts_nav', class: 'nav-link' do %>
Checkouts
(<span data-role='checkout-counter'><%= Checkout.active_for_user(current_or_guest_user.id).count %></span>)
<% end %>
</li>
<% end %>
</li>
<% end %>
<% if render_bookmarks_control? %>
<li class="nav-item <%= active_for_controller('bookmarks') %>">
Expand Down
6 changes: 3 additions & 3 deletions app/views/media_objects/_item_view.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ Unless required by applicable law or agreed to in writing, software distributed
<% f_start, f_end = parse_media_fragment params['t'] %>
<div class="row">
<div class="col-sm-8" id="left_column">
<% if can_stream && @currentStream %>
<% if Avalon::Configuration.controlled_digital_lending_enabled? && !can_stream %>
<%= render 'embed_checkout' %>
<% elsif @currentStream %>
<%= render partial: "modules/player/section", locals: {section: @currentStream, section_info: @currentStreamInfo, f_start: @f_start, f_end: @f_end} %>
<%= render file: '_track_scrubber.html.erb' if is_mejs_2? %>
<%= render file: '_add_to_playlist.html.erb' if current_user.present? && is_mejs_2? %>
Expand All @@ -41,8 +43,6 @@ Unless required by applicable law or agreed to in writing, software distributed
locals: { mediaobject: @media_object,
sections: @masterFiles,
activeStream: @currentStream } %>
<% else %>
<%= render 'embed_checkout' %>
<% end %>
</div>
<div class="col-sm-4" id="right-column">
Expand Down
4 changes: 3 additions & 1 deletion app/views/media_objects/_metadata_display.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ Unless required by applicable law or agreed to in writing, software distributed
</div>
<div class="card-body">
<%= @media_object.access_text %> <br />
Lending Period: <%= ActiveSupport::Duration.build(@media_object.lending_period).to_day_hour_s %>
<% if Avalon::Configuration.controlled_digital_lending_enabled? %>
Lending Period: <%= ActiveSupport::Duration.build(@media_object.lending_period).to_day_hour_s %>
<% end %>
</div>
</div>

Expand Down
64 changes: 34 additions & 30 deletions app/views/modules/_access_control.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -64,31 +64,33 @@ Unless required by applicable law or agreed to in writing, software distributed
</div>
</div>

<div class="card item-lending-period">
<div class="card-header">
<h3 class="card-title">Item lending period</h3>
</div>
<div class="card-body">
<div class="form-group">
<%= render partial: "modules/tooltip", locals: { form: vid, field: :lending_period, tooltip: t("access_control.#{:lending_period}"), options: {display_label: (t("access_control.#{:lending_period}label")+'*').html_safe} } %><br />
<% d, h = (lending_period/3600).divmod(24) %>
<div class='form-row'>
<div class="col-1">
<label class="col-form-label" for="add_lending_period_days">
<span data-title="Days">Days</span>
</label>
<%= text_field_tag "add_lending_period_days", d ? d : 0, class: 'form-control' %>
</div>
<div class="col-1">
<label class="col-form-label" for="add_lending_period_hours">
<span data-title="Hours">Hours</span>
</label>
<%= text_field_tag "add_lending_period_hours", h ? h : 0, class: 'form-control' %>
<% if Avalon::Configuration.controlled_digital_lending_enabled? %>
<div class="card item-lending-period">
<div class="card-header">
<h3 class="card-title">Item lending period</h3>
</div>
<div class="card-body">
<div class="form-group">
<%= render partial: "modules/tooltip", locals: { form: vid, field: :lending_period, tooltip: t("access_control.#{:lending_period}"), options: {display_label: (t("access_control.#{:lending_period}label")+'*').html_safe} } %><br />
<% d, h = (lending_period/3600).divmod(24) %>
<div class='form-row'>
<div class="col-1">
<label class="col-form-label" for="add_lending_period_days">
<span data-title="Days">Days</span>
</label>
<%= text_field_tag "add_lending_period_days", d ? d : 0, class: 'form-control' %>
</div>
<div class="col-1">
<label class="col-form-label" for="add_lending_period_hours">
<span data-title="Hours">Hours</span>
</label>
<%= text_field_tag "add_lending_period_hours", h ? h : 0, class: 'form-control' %>
</div>
</div>
</div>
</div>
</div>
</div>
<% end %>

<%= render modal[:partial], modal_title: modal[:title] if defined? modal %>

Expand Down Expand Up @@ -130,17 +132,19 @@ Unless required by applicable law or agreed to in writing, software distributed
</div>
</div>

<div class="card item-access">
<div class="card-header">
<h3 class="card-title">Item lending period</h3>
</div>
<div class="card-body">
<div class="form-group">
Item is available to be checked out for
<%= ActiveSupport::Duration.build(lending_period).to_day_hour_s %>
<% if Avalon::Configuration.controlled_digital_lending_enabled? %>
<div class="card item-access">
<div class="card-header">
<h3 class="card-title">Item lending period</h3>
</div>
<div class="card-body">
<div class="form-group">
Item is available to be checked out for
<%= ActiveSupport::Duration.build(lending_period).to_day_hour_s %>
</div>
</div>
</div>
</div>
<% end %>

<% end #form_for %>
Expand Down
3 changes: 2 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,8 @@ en:
or ffaa:aaff:bbcc:ddee:1122:3344:5566:7777/ffff:ffff:ffff:ffff:ffff:ffff:ffff:ff00
Start and end dates are not required; if included, access for the specified IP Address(es) will start at the beginning of the start date and end at the beginning of the end date. Otherwise, access will be open ended for the specified IP Address(es).
lending_period: |
Lending Period is the length of time that an item may be checked out for.
Lending Period is the length of time that an item may be checked out for.
Day and Hour values must be positive integers.
contact:
title: 'Contact Us - %{application_name}'
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
end
end

resources :checkouts, only: [:index, :create, :show, :update, :destroy] do
resources :checkouts, only: [:index, :create, :show, :update, :destroy], :constraints => lambda { |request| Avalon::Configuration.controlled_digital_lending_enabled? } do
collection do
patch :return_all
end
Expand Down
2 changes: 1 addition & 1 deletion config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,6 @@ active_storage:
service: local
#bucket: supplementalfiles
controlled_digital_lending:
enable: true
enable: false
default_lending_period: 'P14D' # ISO8601 duration format: P14D == 14.days, PT8H == 8.hours, etc.
max_checkouts_per_user: 25
11 changes: 8 additions & 3 deletions lib/avalon/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Copyright 2011-2022, The Trustees of Indiana University and Northwestern
# University. Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
#
#
# You may obtain a copy of the License at
#
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#
# Unless required by applicable law or agreed to in writing, software distributed
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
Expand Down Expand Up @@ -35,6 +35,11 @@ def sanitize_filename
end
end

# To be called as Avalon::Configuration.controlled_digital_lending_enabled?
def controlled_digital_lending_enabled?
!!Settings.controlled_digital_lending&.enable
end

private
class << self
def coerce(value, method)
Expand Down
7 changes: 7 additions & 0 deletions spec/controllers/admin_collections_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@
end

context "changing lending period" do
before { allow(Settings.controlled_digital_lending).to receive(:enable).and_return(true) }
it "sets a custom lending period" do
expect { put 'update', params: { id: collection.id, save_access: "Save Access Settings", add_lending_period_days: 7, add_lending_period_hours: 8 } }.to change { collection.reload.default_lending_period }.to(633600)
end
Expand Down Expand Up @@ -484,6 +485,12 @@
expect { put 'update', params: { id: collection.id, save_access: "Save Access Settings", add_lending_period_days: 1, add_lending_period_hours: 0 } }.to change { collection.reload.default_lending_period }.to(86400)
expect(flash[:error]).not_to be_present
end

it "returns error if both day and hour are 0" do
expect { put 'update', params: { id: collection.id, save_access: "Save Access Settings", add_lending_period_days: 0, add_lending_period_hours: 0 } }.not_to change { collection.reload.default_lending_period }
expect(response).to redirect_to(admin_collection_path(collection))
expect(flash[:error]).to be_present
end
end
end

Expand Down
Loading

0 comments on commit 9ab4b56

Please sign in to comment.