Issue 2169: Series with public works stuck as restricted #580

Merged
merged 1 commit into from Apr 15, 2012
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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