Skip to content

Commit

Permalink
Fix samvera/hyku#971. Remove client side date calculation for "no del…
Browse files Browse the repository at this point in the history
…ay" releases
  • Loading branch information
tdonohue committed Apr 21, 2017
1 parent 41b2695 commit ae8f464
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 49 deletions.
21 changes: 8 additions & 13 deletions app/assets/javascripts/hyrax/save_work/visibility_component.es6
Expand Up @@ -55,29 +55,31 @@ export default class VisibilityComponent {
restrictToVisibility(data) {
// visibility requirement is in HTML5 'data-visibility' attr
let visibility = data['visibility']
// if immediate release required, then 'data-release-no-delay' attr will be true
let release_no_delay = data['releaseNoDelay']
// release date requirement is in HTML5 'data-release-date' attr
let release_date = data['releaseDate']
// if release_date is flexible (i.e. before date), then 'data-release-before-date' attr will be true
let release_before = data['releaseBeforeDate']

// Restrictions require either a visibility requirement or a release_date requirement (or both)
if(visibility || release_date) {
this.applyRestrictions(visibility, release_date, release_before)
// Restrictions require either a visibility requirement or a date requirement (or both)
if(visibility || release_no_delay || release_date) {
this.applyRestrictions(visibility, release_no_delay, release_date, release_before)
}
else {
this.enableAllOptions()
}
}

// Apply visibility/release restrictions based on selected AdminSet
applyRestrictions(visibility, release_date, release_before)
applyRestrictions(visibility, release_no_delay, release_date, release_before)
{
// If immediate release required and visibility specified
if(this.isToday(release_date) && visibility) {
if(release_no_delay && visibility) {
// Select required visibility
this.selectVisibility(visibility)
}
else if(this.isToday(release_date)) {
else if(release_no_delay) {
// No visibility required, but must be released today. Disable embargo & lease.
this.disableEmbargoAndLease();
}
Expand Down Expand Up @@ -267,11 +269,4 @@ export default class VisibilityComponent {
}
return yyyy + '-' + mm + '-' + dd
}

// Return true if datestring represents today's date
isToday(dateString) {
let today = new Date(this.getToday())
let date = new Date(dateString)
return date.getTime() === today.getTime()
}
}
7 changes: 6 additions & 1 deletion app/presenters/hyrax/admin_set_options_presenter.rb
Expand Up @@ -30,7 +30,12 @@ def data_attributes(admin_set)
def attributes_for(permission_template:)
{}.tap do |attrs|
attrs['data-sharing'] = sharing?(permission_template: permission_template)
attrs['data-release-date'] = permission_template.release_date if permission_template.release_date.present?
# Either add "no-delay" (if immediate release) or a specific release date, but not both.
if permission_template.release_no_delay?
attrs['data-release-no-delay'] = true
elsif permission_template.release_date.present?
attrs['data-release-date'] = permission_template.release_date
end
attrs['data-release-before-date'] = true if permission_template.release_before_date?
attrs['data-visibility'] = permission_template.visibility if permission_template.visibility.present?
end
Expand Down
49 changes: 15 additions & 34 deletions spec/javascripts/visibility_component_spec.js
Expand Up @@ -45,33 +45,33 @@ describe("VisibilityComponent", function() {
});
it("calls applyRestrictions with specified visibility", function() {
target.limitByAdminSet();
expect(target.applyRestrictions).toHaveBeenCalledWith('authenticated', undefined, undefined);
expect(target.applyRestrictions).toHaveBeenCalledWith('authenticated', undefined, undefined, undefined);
});
});
describe("with selected admin set having release immediately restrictions (no visibility)", function() {
beforeEach(function() {
var fixture = setFixtures(visibilityForm('<option data-release-date="' + target.getToday() + '" data-release-before-date="false" selected="selected">Release Immediately AdminSet</option>'));
var fixture = setFixtures(visibilityForm('<option data-release-no-delay="true" selected="selected">Release Immediately AdminSet</option>'));
element = fixture.find('.visibility');
admin_set = new AdminSetWidget(fixture.find('select'))
target = new VisibilityComponent(element, admin_set);
spyOn(target, 'applyRestrictions');
});
it("calls applyRestrictions with specified date requirement", function() {
it("calls applyRestrictions with release_no_delay=true", function() {
target.limitByAdminSet();
expect(target.applyRestrictions).toHaveBeenCalledWith(undefined, target.getToday(), false);
expect(target.applyRestrictions).toHaveBeenCalledWith(undefined, true, undefined, undefined);
});
});
describe("with selected admin set having release publicly immediately restrictions", function() {
beforeEach(function() {
var fixture = setFixtures(visibilityForm('<option data-visibility="open" data-release-date="' + target.getToday() + '" data-release-before-date="false" selected="selected">Release Publicly Immediately AdminSet</option>'));
var fixture = setFixtures(visibilityForm('<option data-visibility="open" data-release-no-delay="true" selected="selected">Release Publicly Immediately AdminSet</option>'));
element = fixture.find('.visibility');
admin_set = new AdminSetWidget(fixture.find('select'))
target = new VisibilityComponent(element, admin_set);
spyOn(target, 'applyRestrictions');
});
it("calls applyRestrictions with specified date and visibility requirement", function() {
it("calls applyRestrictions with release_no_delay=true and visibility requirement", function() {
target.limitByAdminSet();
expect(target.applyRestrictions).toHaveBeenCalledWith("open", target.getToday(), false);
expect(target.applyRestrictions).toHaveBeenCalledWith("open", true, undefined, undefined);
});
});
describe("with selected admin set having release on future date set", function() {
Expand All @@ -84,7 +84,7 @@ describe("VisibilityComponent", function() {
});
it("calls applyRestrictions with specified date requirement", function() {
target.limitByAdminSet();
expect(target.applyRestrictions).toHaveBeenCalledWith(undefined, getOneYearFromToday(), false);
expect(target.applyRestrictions).toHaveBeenCalledWith(undefined, undefined, getOneYearFromToday(), false);
});
});
describe("with selected admin set having release to institution before one year set", function() {
Expand All @@ -97,7 +97,7 @@ describe("VisibilityComponent", function() {
});
it("calls applyRestrictions with specified date and visibility requirement", function() {
target.limitByAdminSet();
expect(target.applyRestrictions).toHaveBeenCalledWith("authenticated", getOneYearFromToday(), true);
expect(target.applyRestrictions).toHaveBeenCalledWith("authenticated", undefined, getOneYearFromToday(), true);
});
});
});
Expand All @@ -109,7 +109,7 @@ describe("VisibilityComponent", function() {
spyOn(target, 'enableReleaseNowOrEmbargo');
});
it("enable that visibility option OR embargo, and limit embargo to any future date", function() {
target.applyRestrictions("authenticated", undefined, undefined);
target.applyRestrictions("authenticated", undefined, undefined, undefined);
expect(target.enableReleaseNowOrEmbargo).toHaveBeenCalledWith("authenticated", undefined, undefined);
});
});
Expand All @@ -118,7 +118,7 @@ describe("VisibilityComponent", function() {
spyOn(target, 'disableEmbargoAndLease');
});
it("disables embargo and lease options", function() {
target.applyRestrictions(undefined, target.getToday(), false);
target.applyRestrictions(undefined, true, undefined, undefined);
expect(target.disableEmbargoAndLease).toHaveBeenCalled();
});
});
Expand All @@ -127,7 +127,7 @@ describe("VisibilityComponent", function() {
spyOn(target, 'selectVisibility');
});
it("selects that visibility (disabling other options)", function() {
target.applyRestrictions("open", target.getToday(), false);
target.applyRestrictions("open", true, undefined, undefined);
expect(target.selectVisibility).toHaveBeenCalledWith("open");
});
});
Expand All @@ -137,7 +137,7 @@ describe("VisibilityComponent", function() {
});
it("allows any date between now and future date", function() {
var futureDate = getOneYearFromToday();
target.applyRestrictions("open", futureDate, true);
target.applyRestrictions("open", undefined, futureDate, true);
expect(target.enableReleaseNowOrEmbargo).toHaveBeenCalledWith("open", futureDate, true);
});
});
Expand All @@ -147,7 +147,7 @@ describe("VisibilityComponent", function() {
});
it("require embargo until release_date and don't restrict visibility", function() {
var futureDate = getOneYearFromToday();
target.applyRestrictions(undefined, futureDate, false);
target.applyRestrictions(undefined, undefined, futureDate, false);
expect(target.requireEmbargo).toHaveBeenCalledWith(undefined, futureDate);
});
});
Expand All @@ -157,7 +157,7 @@ describe("VisibilityComponent", function() {
});
it("require embargo until release_date and require visibility", function() {
var futureDate = getOneYearFromToday();
target.applyRestrictions("authenticated", futureDate, false);
target.applyRestrictions("authenticated", undefined, futureDate, false);
expect(target.requireEmbargo).toHaveBeenCalledWith("authenticated", futureDate);
});
});
Expand Down Expand Up @@ -408,25 +408,6 @@ describe("VisibilityComponent", function() {
expect(target.getVisibilityAfterEmbargoInput()).toHaveProp("name", "generic_work[visibility_after_embargo]");
});
});

// isToday(dateString)
describe("isToday", function() {
describe("with today's date", function() {
it("is true", function() {
expect(target.isToday(target.getToday())).toEqual(true);
});
});
describe("with past date", function() {
it("is false", function() {
expect(target.isToday("2016-12-31")).toEqual(false);
});
});
describe("with future date", function() {
it("is false", function() {
expect(target.isToday(getOneYearFromToday())).toEqual(false);
});
});
});
});

// Generate a form that includes AdminSet selectbox (with a passed in option)
Expand Down
2 changes: 1 addition & 1 deletion spec/presenters/hyrax/admin_set_options_presenter_spec.rb
Expand Up @@ -46,7 +46,7 @@

it do
is_expected.to eq [['Fixed Release Date Set', '123', { 'data-sharing' => false, 'data-release-date' => today + 2.days }],
['No Delay Set', '345', { 'data-sharing' => false, 'data-release-date' => today }],
['No Delay Set', '345', { 'data-sharing' => false, 'data-release-no-delay' => true }],
['One Year Max Embargo Set', '567', { 'data-sharing' => false, 'data-release-date' => today + 1.year, 'data-release-before-date' => true }],
['Release Before Date Set', '789', { 'data-sharing' => false, 'data-release-date' => today + 1.month, 'data-release-before-date' => true }]]
end
Expand Down

0 comments on commit ae8f464

Please sign in to comment.