Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from benissimo/master
Quality and extension for convert image + fixes and specs
- Loading branch information
Showing
4 changed files
with
225 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
require_relative './spec_helper' | ||
|
||
describe "Attachment on the fly mixin" do | ||
|
||
subject { Paperclip::Attachment.new } | ||
|
||
context "#respond_to?" do | ||
method_names = %w{s125 cls125 s_125_250 cls_125_250 | ||
s_125_width cls_125_width s_125_height cls_125_height | ||
s_125_both cls_125_both | ||
} | ||
|
||
method_names.each do |method_name| | ||
it { should respond_to(method_name.to_sym) } | ||
end | ||
|
||
it { should_not respond_to(:x_125_250) } | ||
it { should_not respond_to(:s_125_250foo) } | ||
it { should_not respond_to(:S_125_250) } | ||
end | ||
|
||
context "#method_missing" do | ||
|
||
context "translates method into a generate image call" do | ||
method_name_to_generate_image_call = { | ||
:s_125_225 => ["both", 125, 225, {}], | ||
:s125 => ["width", 125, 125, {}], | ||
:s_125_height => ["height", 125, 125, {}], | ||
:s_125_width => ["width", 125, 125, {}], | ||
:s_125_both => ["both", 125, 125, {}] | ||
} | ||
|
||
method_name_to_generate_image_call.each do |method_name, generate_image_args| | ||
it "#{method_name}" do | ||
subject.should_receive(:generate_image).with(*generate_image_args) | ||
subject.send(method_name) | ||
end | ||
end | ||
|
||
it "passes parameters through as well" do | ||
subject.should_receive(:generate_image).with("width", 125, 125, {:quality => 90, :extension => "jpeg"}) | ||
subject.s_125_width :quality => 90, :extension => "jpeg" | ||
end | ||
end | ||
end | ||
|
||
context "#generate_image" do | ||
|
||
context "it should generate a new image" do | ||
method_name_to_expectations = { | ||
:s_125_width => { | ||
:new => "/S_125_WIDTH__q_100__path.png", | ||
:regex => /-geometry 125 / | ||
}, | ||
:s_125_height => { | ||
:new => "/S_125_HEIGHT__q_100__path.png", | ||
:regex => /-geometry x125 / | ||
}, | ||
:s_125_both => { | ||
:new => "/S_125_125__q_100__path.png", | ||
:regex => /-geometry 125x125 / | ||
} | ||
} | ||
method_name_to_expectations.each do |method_name, expected| | ||
it "for #{method_name}" do | ||
File.should_receive(:exist?).with(expected[:new]).and_return(false) | ||
File.should_receive(:exist?).with("//file.png").and_return(true) | ||
subject.should_receive(:convert_command).with(expected[:regex]) | ||
subject.send(method_name) | ||
end | ||
end | ||
|
||
it "passes in parameters for quality" do | ||
File.should_receive(:exist?).with("/S_125_WIDTH__q_75__path.png").and_return(false) | ||
File.should_receive(:exist?).with("//file.png").and_return(true) | ||
subject.should_receive(:convert_command).with(/-quality 75/) | ||
subject.s_125_width :quality => 75 | ||
end | ||
|
||
it "passes in parameters for extension" do | ||
File.should_receive(:exist?).with("/S_125_WIDTH_extension_jpeg_q_75__path.jpeg").and_return(false) | ||
File.should_receive(:exist?).with("//file.png").and_return(true) | ||
subject.should_receive(:convert_command).with(/-quality 75/) | ||
subject.should_receive(:has_alpha?).with("//file.png").and_return(false) | ||
subject.s_125_width(:quality => 75, :extension => "jpeg").should == "/S_125_WIDTH_extension_jpeg_q_75__path.jpeg" | ||
end | ||
|
||
it "preserves original extension if file has alpha channel" do | ||
File.should_receive(:exist?).with("/S_125_WIDTH_extension_jpeg_q_75__path.jpeg").and_return(false) | ||
File.should_receive(:exist?).with("//file.png").and_return(true) | ||
subject.should_receive(:convert_command).with(/-quality 75/) | ||
subject.should_receive(:has_alpha?).with("//file.png").and_return(true) | ||
subject.s_125_width(:quality => 75, :extension => "jpeg").should == "/S_125_WIDTH_extension_jpeg_q_75__path.png" | ||
end | ||
|
||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
require 'rubygems' | ||
require 'rspec' | ||
|
||
class Hash | ||
def symbolize_keys! | ||
keys.each do |key| | ||
self[(key.to_sym rescue key) || key] = delete(key) | ||
end | ||
self | ||
end | ||
end | ||
|
||
class Paperclip | ||
def self.options | ||
{} | ||
end | ||
def self.log msg | ||
end | ||
class Attachment | ||
def initialize | ||
end | ||
def path | ||
"/path.png" | ||
end | ||
def url | ||
"/url" | ||
end | ||
def original_filename | ||
"/file.png" | ||
end | ||
end | ||
end | ||
|
||
require_relative '../lib/attachment_on_the_fly' |