bulldog 0.0.3 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/CHANGELOG +4 -0
- data/VERSION +1 -1
- data/bulldog.gemspec +1 -1
- data/lib/bulldog/attachment/maybe.rb +0 -2
- data/lib/bulldog/interpolation.rb +6 -4
- data/spec/unit/interpolation_spec.rb +88 -53
- metadata +1 -1
data/CHANGELOG
CHANGED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.0.
|
1
|
+
0.0.4
|
data/bulldog.gemspec
CHANGED
@@ -120,7 +120,6 @@ module Bulldog
|
|
120
120
|
def interpolate_path(style_name, params={})
|
121
121
|
template = reflection.path_template
|
122
122
|
style = reflection.styles[style_name]
|
123
|
-
params[:extension] ||= style[:format]
|
124
123
|
Interpolation.interpolate(template, record, name, style, params)
|
125
124
|
end
|
126
125
|
|
@@ -135,7 +134,6 @@ module Bulldog
|
|
135
134
|
def interpolate_url(style_name, params={})
|
136
135
|
template = reflection.url_template
|
137
136
|
style = reflection.styles[style_name]
|
138
|
-
params[:extension] ||= style[:format]
|
139
137
|
Interpolation.interpolate(template, record, name, style, params)
|
140
138
|
end
|
141
139
|
|
@@ -3,10 +3,12 @@ module Bulldog
|
|
3
3
|
Error = Class.new(Bulldog::Error)
|
4
4
|
|
5
5
|
def self.interpolate(template, record, name, style, overrides={})
|
6
|
-
|
7
|
-
|
8
|
-
extension
|
9
|
-
overrides[:
|
6
|
+
unless overrides[:extension]
|
7
|
+
overrides = overrides.dup
|
8
|
+
overrides[:extension] ||= style[:format]
|
9
|
+
if overrides[:basename]
|
10
|
+
overrides[:extension] ||= File.extname(overrides[:basename]).sub(/\A./, '')
|
11
|
+
end
|
10
12
|
end
|
11
13
|
template.gsub(/:(?:(\w+)|\{(\w+?)\})/) do
|
12
14
|
key = ($1 || $2).to_sym
|
@@ -3,8 +3,8 @@ require 'spec_helper'
|
|
3
3
|
describe Interpolation do
|
4
4
|
use_model_class(:Thing, :photo_file_name => :string)
|
5
5
|
|
6
|
-
def interpolate(template)
|
7
|
-
Interpolation.interpolate(template, @thing, :photo, @style)
|
6
|
+
def interpolate(template, overrides={})
|
7
|
+
Interpolation.interpolate(template, @thing, :photo, @style, overrides)
|
8
8
|
end
|
9
9
|
|
10
10
|
describe ".to_interpolate" do
|
@@ -36,73 +36,108 @@ describe Interpolation do
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
-
describe "
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
39
|
+
describe ".interpolate" do
|
40
|
+
describe "when the file name is not being stored" do
|
41
|
+
before do
|
42
|
+
Thing.has_attachment :photo do
|
43
|
+
style :small
|
44
|
+
store_attributes :file_name => nil
|
45
|
+
end
|
46
|
+
@thing = Thing.new(:photo => test_image_file('test.jpg'))
|
47
|
+
@style = Thing.attachment_reflections[:photo].styles[:small]
|
44
48
|
end
|
45
|
-
@thing = Thing.new(:photo => test_image_file('test.jpg'))
|
46
|
-
@style = Thing.attachment_reflections[:photo].styles[:small]
|
47
|
-
end
|
48
49
|
|
49
|
-
|
50
|
-
|
51
|
-
|
50
|
+
it "should interpolate :class as the plural class name" do
|
51
|
+
interpolate("a/:class/b").should == "a/things/b"
|
52
|
+
end
|
52
53
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
54
|
+
it "should interpolate :id as the record ID" do
|
55
|
+
@thing.stubs(:id).returns(123)
|
56
|
+
interpolate("a/:id/b").should == "a/123/b"
|
57
|
+
end
|
57
58
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
59
|
+
it "should interpolate :id_partition as the record ID split into 3 3-digit partitions, 0-padded" do
|
60
|
+
@thing.stubs(:id).returns(12345)
|
61
|
+
interpolate("a/:id_partition/b").should == "a/000/012/345/b"
|
62
|
+
end
|
62
63
|
|
63
|
-
|
64
|
-
|
65
|
-
|
64
|
+
it "should interpolate :attachment as the attachment name" do
|
65
|
+
interpolate("a/:attachment/b").should == "a/photo/b"
|
66
|
+
end
|
66
67
|
|
67
|
-
|
68
|
-
|
69
|
-
|
68
|
+
it "should interpolate :style as the style name" do
|
69
|
+
interpolate("a/:style/b").should == "a/small/b"
|
70
|
+
end
|
70
71
|
|
71
|
-
|
72
|
-
|
73
|
-
|
72
|
+
it "should raise an error for :basename by default" do
|
73
|
+
lambda{interpolate("a/:basename/b")}.should raise_error(Interpolation::Error)
|
74
|
+
end
|
74
75
|
|
75
|
-
|
76
|
-
|
77
|
-
|
76
|
+
it "should raise an error for :extension by default" do
|
77
|
+
lambda{interpolate("a/:extension/b")}.should raise_error(Interpolation::Error)
|
78
|
+
end
|
78
79
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
end
|
80
|
+
it "should allow overriding the basename to use to avoid an error" do
|
81
|
+
interpolate("a/:basename/b", :basename => 'BASENAME').should == 'a/BASENAME/b'
|
82
|
+
end
|
83
83
|
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
end
|
84
|
+
it "should allow overriding the extension to use to avoid an error" do
|
85
|
+
interpolate("a/:extension/b", :extension => 'EXT').should == 'a/EXT/b'
|
86
|
+
end
|
88
87
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
style :small, {}
|
93
|
-
store_attributes :file_name => :photo_file_name
|
88
|
+
it "should take the extension from the style format, if given" do
|
89
|
+
@style[:format] = 'FMT'
|
90
|
+
Interpolation.interpolate("a/:extension/b", @thing, :photo, @style).should == 'a/FMT/b'
|
94
91
|
end
|
95
92
|
|
96
|
-
|
97
|
-
|
98
|
-
|
93
|
+
it "should take the extension from the overridden basename, if given" do
|
94
|
+
interpolate("a/:extension/b", :basename => 'BASENAME.EXT').should == 'a/EXT/b'
|
95
|
+
end
|
96
|
+
|
97
|
+
it "should use an extension override over the style format if both are present" do
|
98
|
+
@style[:format] = 'FMT'
|
99
|
+
interpolate("a/:extension/b", :extension => 'EXT').should == 'a/EXT/b'
|
100
|
+
end
|
99
101
|
|
100
|
-
|
101
|
-
|
102
|
+
it "should use the style format over the basename override if both are present" do
|
103
|
+
@style[:format] = 'FMT'
|
104
|
+
interpolate("a/:extension/b", :basename => 'BASENAME.EXT').should == 'a/FMT/b'
|
105
|
+
end
|
106
|
+
|
107
|
+
it "should not modify the hash of overrides, if given" do
|
108
|
+
overrides = {:basename => 'BASENAME.EXT'}
|
109
|
+
interpolate("a/:extension/b", overrides).should == 'a/EXT/b'
|
110
|
+
overrides.should == {:basename => 'BASENAME.EXT'}
|
111
|
+
end
|
112
|
+
|
113
|
+
it "should allow using braces for interpolating between symbol characters" do
|
114
|
+
@thing.stubs(:id).returns(5)
|
115
|
+
interpolate("a/x:{id}x/b").should == "a/x5x/b"
|
116
|
+
end
|
117
|
+
|
118
|
+
it "should raise an error for an unrecognized interpolation key" do
|
119
|
+
lambda{interpolate(":invalid")}.should raise_error(Interpolation::Error)
|
120
|
+
end
|
102
121
|
end
|
103
122
|
|
104
|
-
|
105
|
-
|
123
|
+
describe "when the file name is being stored" do
|
124
|
+
before do
|
125
|
+
Thing.has_attachment :photo do
|
126
|
+
style :small, {}
|
127
|
+
store_attributes :file_name => :photo_file_name
|
128
|
+
end
|
129
|
+
|
130
|
+
@thing = Thing.new(:photo => test_image_file('test.jpg'))
|
131
|
+
@style = Thing.attachment_reflections[:photo].styles[:small]
|
132
|
+
end
|
133
|
+
|
134
|
+
it "should interpolate :basename as the basename of the uploaded file" do
|
135
|
+
interpolate("a/:basename/b").should == "a/test.jpg/b"
|
136
|
+
end
|
137
|
+
|
138
|
+
it "should interpolate :extension as the extension of the uploaded file" do
|
139
|
+
interpolate("a/:extension/b").should == "a/jpg/b"
|
140
|
+
end
|
106
141
|
end
|
107
142
|
end
|
108
143
|
end
|