This repository has been archived by the owner on Jul 13, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Paperclip::InterpolatedString to store the interpolated string wh…
…ich has an escape flag
- Loading branch information
Showing
2 changed files
with
95 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
require 'uri' | ||
|
||
module Paperclip | ||
class InterpolatedString < String | ||
def escaped? | ||
!!@escaped | ||
end | ||
|
||
def escape | ||
if !escaped? | ||
escaped_string = self.class.new(URI.escape(self)) | ||
escaped_string.instance_variable_set(:@escaped, true) | ||
escaped_string | ||
else | ||
self | ||
end | ||
end | ||
|
||
def unescape | ||
if escaped? | ||
escaped_string = self.class.new(URI.unescape(self)) | ||
escaped_string.instance_variable_set(:@escaped, false) | ||
escaped_string | ||
else | ||
self | ||
end | ||
end | ||
|
||
def force_escape | ||
@escaped = true | ||
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,62 @@ | ||
require './test/helper' | ||
|
||
class InterpolatedStringTest < Test::Unit::TestCase | ||
context "inheritance" do | ||
should "inherited from String" do | ||
assert Paperclip::InterpolatedString.new("paperclip").is_a? String | ||
end | ||
end | ||
|
||
context "#escape" do | ||
subject { Paperclip::InterpolatedString.new("paperclip foo").escape } | ||
|
||
should "returns an InterpolatedString object" do | ||
assert subject.is_a? Paperclip::InterpolatedString | ||
end | ||
|
||
should "escape the output string" do | ||
assert_equal "paperclip%20foo", subject | ||
end | ||
|
||
should "not double escape output string" do | ||
assert_equal "paperclip%20foo", subject.escape | ||
end | ||
end | ||
|
||
context "#unescape" do | ||
subject { Paperclip::InterpolatedString.new("paperclip%20foo").escape.unescape } | ||
|
||
should "returns an InterpolatedString object" do | ||
assert subject.is_a? Paperclip::InterpolatedString | ||
end | ||
|
||
should "unescape the output string" do | ||
assert_equal "paperclip%20foo", subject | ||
end | ||
|
||
should "not double unescape output string" do | ||
assert_equal "paperclip%20foo", subject.unescape | ||
end | ||
end | ||
|
||
context "#escaped?" do | ||
subject { Paperclip::InterpolatedString.new("paperclip") } | ||
|
||
should "returns true if string was escaped" do | ||
assert subject.escape.escaped? | ||
end | ||
|
||
should "returns false if string wasn't escaped" do | ||
assert !subject.escaped? | ||
end | ||
end | ||
|
||
context "#force_escape" do | ||
subject { Paperclip::InterpolatedString.new("paperclip") } | ||
setup { subject.force_escape } | ||
|
||
should "sets escaped flag to true" do | ||
assert subject.escaped? | ||
end | ||
end | ||
end |