Skip to content

Commit

Permalink
Datastream.content= should allow Rack::Test::Uploaded file. It behave…
Browse files Browse the repository at this point in the history
…s like an IO, but it doesn't decend from IO
  • Loading branch information
jcoyne committed Dec 20, 2012
1 parent f1e8d16 commit 963fdbe
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
10 changes: 8 additions & 2 deletions lib/rubydora/datastream.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ def content


@content ||= datastream_content @content ||= datastream_content


if @content.kind_of? IO if behaves_like_io?(@content)
begin begin
@content.rewind @content.rewind
@content.read @content.read
Expand Down Expand Up @@ -205,7 +205,7 @@ def has_content?


# return true if instance_variable_defined? :@content # return true if instance_variable_defined? :@content


@content.is_a?(IO) || !content.blank? behaves_like_io?(@content) || !content.blank?
end end


# Retrieve the datastream profile as a hash (and cache it) # Retrieve the datastream profile as a hash (and cache it)
Expand Down Expand Up @@ -367,6 +367,12 @@ def validate_dsLocation! val
end end


private private

# Rack::Test::UploadedFile is often set via content=, however it's not an IO, though it wraps an io object.
def behaves_like_io?(obj)
obj.is_a?(IO) || (defined?(Rack) && obj.is_a?(Rack::Test::UploadedFile))
end

def attribute_will_change! *args def attribute_will_change! *args
check_if_read_only check_if_read_only
super super
Expand Down
9 changes: 5 additions & 4 deletions spec/lib/datastream_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'spec_helper' require 'spec_helper'
require 'stringio'


describe Rubydora::Datastream do describe Rubydora::Datastream do
before do before do
Expand Down Expand Up @@ -187,26 +188,26 @@
@datastream.content @datastream.content
@datastream.content_changed?.should == false @datastream.content_changed?.should == false
end end
it "should be changed in the new content is different than the old content" do it "should be changed when the new content is different than the old content" do
@mock_repository.stub(:datastream_dissemination).with(hash_including(:pid => 'pid', :dsid => 'dsid')).and_return('asdf') @mock_repository.stub(:datastream_dissemination).with(hash_including(:pid => 'pid', :dsid => 'dsid')).and_return('asdf')
@datastream.content = "test" @datastream.content = "test"
@datastream.content_changed?.should == true @datastream.content_changed?.should == true
end end


it "should not be changed in the new content is the same as the existing content (when eager-loading is enabled)" do it "should not be changed when the new content is the same as the existing content (when eager-loading is enabled)" do
@mock_repository.stub(:datastream_dissemination).with(hash_including(:pid => 'pid', :dsid => 'dsid')).and_return('test') @mock_repository.stub(:datastream_dissemination).with(hash_including(:pid => 'pid', :dsid => 'dsid')).and_return('test')
@datastream.eager_load_datastream_content = true @datastream.eager_load_datastream_content = true
@datastream.content = "test" @datastream.content = "test"
@datastream.content_changed?.should == false @datastream.content_changed?.should == false
end end


it "should be changed in the new content is the same as the existing content (without eager loading, the default)" do it "should be changed when the new content is the same as the existing content (without eager loading, the default)" do
@mock_repository.stub(:datastream_dissemination).with(hash_including(:pid => 'pid', :dsid => 'dsid')).and_return('test') @mock_repository.stub(:datastream_dissemination).with(hash_including(:pid => 'pid', :dsid => 'dsid')).and_return('test')
@datastream.content = "test" @datastream.content = "test"
@datastream.content_changed?.should == true @datastream.content_changed?.should == true
end end


it "should not be changed in the new content is the same as the existing content (and we have accessed #content previously)" do it "should not be changed when the new content is the same as the existing content (and we have accessed #content previously)" do
@mock_repository.stub(:datastream_dissemination).with(hash_including(:pid => 'pid', :dsid => 'dsid')).and_return('test') @mock_repository.stub(:datastream_dissemination).with(hash_including(:pid => 'pid', :dsid => 'dsid')).and_return('test')
@datastream.content @datastream.content
@datastream.content = "test" @datastream.content = "test"
Expand Down

0 comments on commit 963fdbe

Please sign in to comment.