Skip to content

Commit

Permalink
Issue 2169: Series with public works stuck as restricted
Browse files Browse the repository at this point in the history
  • Loading branch information
elzj committed Apr 15, 2012
1 parent 70b8ec5 commit 1c5436e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/models/serial_work.rb
Expand Up @@ -4,7 +4,7 @@ class SerialWork < ActiveRecord::Base
validates_uniqueness_of :work_id, :scope => [:series_id]
acts_as_list :scope => :series

before_create :adjust_series_visibility
after_create :adjust_series_visibility
after_destroy :adjust_series_visibility
after_destroy :delete_empty_series

Expand Down
7 changes: 5 additions & 2 deletions app/models/series.rb
Expand Up @@ -31,6 +31,8 @@ class Series < ActiveRecord::Base
:allow_blank => true,
:maximum => ArchiveConfig.NOTES_MAX,
:too_long => t('notes_too_long', :default => "must be less than %{max} letters long.", :max => ArchiveConfig.NOTES_MAX)

after_save :adjust_restricted

attr_accessor :authors
attr_accessor :authors_to_remove
Expand Down Expand Up @@ -104,8 +106,9 @@ def unrevealed?
# if the series includes an unrestricted work, restricted should be false
# if the series includes no unrestricted works, restricted should be true
def adjust_restricted
unless self.restricted == !self.works.collect(&:restricted).include?(false)
self.toggle!(:restricted)
unless self.restricted? == !(self.works.where(:restricted => false).count > 0)
self.restricted = !(self.works.where(:restricted => false).count > 0)
self.save(:validate => false)
end
end

Expand Down
4 changes: 4 additions & 0 deletions features/support/factories.rb
Expand Up @@ -69,6 +69,10 @@
end
end

Factory.define :series do |f|
f.sequence(:title) { |n| "Awesome Series #{n}" }
end

# Factory.define :collection_participant do |f|
# f.association :pseud
# f.association :collection
Expand Down
31 changes: 31 additions & 0 deletions spec/models/series_spec.rb
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
require 'spec_helper'

describe Series do

let(:unrestricted_work) { Factory(:work, :restricted => false) }
let(:restricted_work) { Factory(:work, :restricted => true) }

before(:each) do
@series = Factory.create(:series)
end

it "should be unrestricted when it has unrestricted works" do
@series.works = [unrestricted_work]
@series.reload
@series.restricted.should_not be_true
end

it "should be restricted when it has no unrestricted works" do
@series.works = [restricted_work]
@series.reload
@series.restricted.should be_true
end

it "should be unrestricted when it has both restricted and unrestricted works" do
@series.works = [restricted_work, unrestricted_work]
@series.reload
@series.restricted.should_not be_true
end

end

0 comments on commit 1c5436e

Please sign in to comment.