Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't define .name on Joint module #16

Merged
merged 1 commit into from Mar 11, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
22 changes: 1 addition & 21 deletions lib/joint.rb
Expand Up @@ -11,27 +11,6 @@ module Joint
include attachment_accessor_module
end

def self.name(file)
if file.respond_to?(:original_filename)
file.original_filename
else
File.basename(file.path)
end
end

def self.size(file)
if file.respond_to?(:size)
file.size
else
File.size(file)
end
end

def self.type(file)
return file.type if file.is_a?(Joint::IO)
Wand.wave(file.path, :original_filename => Joint.name(file))
end

private
def self.blank?(str)
str.nil? || str !~ /\S/
Expand All @@ -42,3 +21,4 @@ def self.blank?(str)
require 'joint/instance_methods'
require 'joint/attachment_proxy'
require 'joint/io'
require 'joint/file_helpers'
6 changes: 3 additions & 3 deletions lib/joint/class_methods.rb
Expand Up @@ -37,9 +37,9 @@ def #{name}=(file)
assigned_attachments.delete(:#{name})
else
send("#{name}_id=", BSON::ObjectId.new) if send("#{name}_id").nil?
send("#{name}_name=", Joint.name(file))
send("#{name}_size=", Joint.size(file))
send("#{name}_type=", Joint.type(file))
send("#{name}_name=", Joint::FileHelpers.name(file))
send("#{name}_size=", Joint::FileHelpers.size(file))
send("#{name}_type=", Joint::FileHelpers.type(file))
assigned_attachments[:#{name}] = file
nil_attachments.delete(:#{name})
end
Expand Down
24 changes: 24 additions & 0 deletions lib/joint/file_helpers.rb
@@ -0,0 +1,24 @@
module Joint
module FileHelpers
def self.name(file)
if file.respond_to?(:original_filename)
file.original_filename
else
File.basename(file.path)
end
end

def self.size(file)
if file.respond_to?(:size)
file.size
else
File.size(file)
end
end

def self.type(file)
return file.type if file.is_a?(Joint::IO)
Wand.wave(file.path, :original_filename => Joint::FileHelpers.name(file))
end
end
end
52 changes: 52 additions & 0 deletions test/joint/test_file_helpers.rb
@@ -0,0 +1,52 @@
require 'helper'

class FileHelpersTest < Test::Unit::TestCase
include JointTestHelpers

def setup
super
@image = open_file('mr_t.jpg')
end

def teardown
@image.close
end

context ".name" do
should "return original_filename" do
def @image.original_filename
'frank.jpg'
end
Joint::FileHelpers.name(@image).should == 'frank.jpg'
end

should "fall back to File.basename" do
Joint::FileHelpers.name(@image).should == 'mr_t.jpg'
end
end

context ".size" do
should "return size" do
def @image.size
25
end
Joint::FileHelpers.size(@image).should == 25
end

should "fall back to File.size" do
Joint::FileHelpers.size(@image).should == 13661
end
end

context ".type" do
should "return type if Joint::Io instance" do
file = Joint::IO.new(:type => 'image/jpeg')
Joint::FileHelpers.type(@image).should == 'image/jpeg'
end

should "fall back to Wand" do
Joint::FileHelpers.type(@image).should == 'image/jpeg'
end
end

end
37 changes: 0 additions & 37 deletions test/test_joint.rb
Expand Up @@ -16,43 +16,6 @@ def teardown
all_files.each { |file| file.close }
end

context ".name" do
should "return original_filename" do
def @image.original_filename
'frank.jpg'
end
Joint.name(@image).should == 'frank.jpg'
end

should "fall back to File.basename" do
Joint.name(@image).should == 'mr_t.jpg'
end
end

context ".size" do
should "return size" do
def @image.size
25
end
Joint.size(@image).should == 25
end

should "fall back to File.size" do
Joint.size(@image).should == 13661
end
end

context ".type" do
should "return type if Joint::Io instance" do
file = Joint::IO.new(:type => 'image/jpeg')
Joint.type(@image).should == 'image/jpeg'
end

should "fall back to Wand" do
Joint.type(@image).should == 'image/jpeg'
end
end

context "Using Joint plugin" do
should "add each attachment to attachment_names" do
Asset.attachment_names.should == Set.new([:image, :file])
Expand Down