radiant-images-extension 0.1.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,171 +0,0 @@
1
- module Images
2
- module Tags
3
- module Image
4
- include Radiant::Taggable
5
-
6
- class TagError < StandardError; end
7
-
8
- desc %{
9
- The namespace for referencing images. You may specify the title
10
- attribute for this tag to use only images with the specified title.
11
-
12
- *Usage:*
13
- <pre><code><r:images [title="image_title"]>...</r:images></code></pre>
14
- }
15
- tag 'images' do |tag|
16
- tag.locals.image = ::Image.find_by_title(tag.attr['title']) || ::Image.find(tag.attr['id']) unless tag.attr.empty?
17
- tag.expand
18
- end
19
-
20
- desc %{
21
- Goes through each of the available images.
22
- Use the limit and offset attributes to render a specific number of images.
23
- Use the by and order attributes to control the order of images.
24
-
25
- *Usage:*
26
- <pre><code><r:images:each [limit=0] [offset=0] [order="asc|desc"] [by="position|title|..."]>...</r:images:each></code></pre>
27
- }
28
- tag 'images:each' do |tag|
29
- options = tag.attr.dup
30
- result = []
31
- images = ::Image.find(:all, images_find_options(tag))
32
- tag.locals.images = images
33
- images.each do |image|
34
- tag.locals.image = image
35
- result << tag.expand
36
- end
37
- result
38
- end
39
-
40
- desc %{
41
- Renders the first image.
42
-
43
- *Usage:*
44
- <pre><code><r:images:first>...</r:images:first></code></pre>
45
- }
46
- tag 'images:first' do |tag|
47
- if first = ::Image.first
48
- tag.locals.image = first
49
- tag.expand
50
- end
51
- end
52
-
53
- desc %{
54
- Renders the contained elements only if the current image is the first.
55
-
56
- *Usage:*
57
- <pre><code><r:if_first>...</r:if_first></code></pre>
58
- }
59
- tag 'images:if_first' do |tag|
60
- images = tag.locals.images
61
- image = tag.locals.image
62
- if image == images.first
63
- tag.expand
64
- end
65
- end
66
-
67
- desc %{
68
- Renders the contained elements only if the current image is not the first.
69
-
70
- *Usage:*
71
- <pre><code><r:unless_first>...</r:unless_first></code></pre>
72
- }
73
- tag 'images:unless_first' do |tag|
74
- images = tag.locals.images
75
- image = tag.locals.image
76
- if image != images.first
77
- tag.expand
78
- end
79
- end
80
-
81
- desc %{
82
- Renders the contained elements only if the current contextual page has one or
83
- more image. The min_count attribute specifies the minimum number of required
84
- images.
85
-
86
- *Usage:*
87
- <pre><code><r:if_images [min_count="n"]>...</r:if_images></code></pre>
88
- }
89
- tag 'if_images' do |tag|
90
- count = tag.attr['min_count'] && tag.attr['min_count'].to_i || 1
91
- images = ::Image.count
92
- tag.expand if images >= count
93
- end
94
-
95
- desc %{
96
- Renders the contained elements only if the current contextual page does not
97
- have one or more image. The min_count attribute specifies the minimum number
98
- of required images.
99
-
100
- *Usage:*
101
- <pre><code><r:if_images [min_count="n"]>...</r:if_images></code></pre>
102
- }
103
- tag 'unless_images' do |tag|
104
- count = tag.attr['min_count'] && tag.attr['min_count'].to_i || 1
105
- images = ::Image.count
106
- tag.expand unless images >= count
107
- end
108
-
109
- desc %{
110
- Outputs the full URL of the image including the filename. Specify the style
111
- using the style option.
112
- }
113
- tag 'images:url' do |tag|
114
- style = tag.attr['style'] || :original
115
- image, options = image_and_options(tag)
116
- image.asset.url(style, false) rescue nil
117
- end
118
-
119
- desc %{
120
- Outputs the title of the current image
121
- }
122
- tag 'images:title' do |tag|
123
- image, option = image_and_options(tag)
124
- image.title rescue nil
125
- end
126
-
127
- desc %{
128
- Outputs the image tag for the current image. Use the size option to
129
- specify which size version of the image is to be used. Use alt to
130
- specify alt text.
131
-
132
- *Usage:*
133
- <pre><code><r:images:tag [title="image_title"] [size="icon|original"] [alt="alt_text"]></code></pre>
134
- }
135
- tag 'images:tag' do |tag|
136
- image, options = image_and_options(tag)
137
- size = options['size'] ? options.delete('size') : 'original'
138
- alt = " alt=\"#{image.title}\"" unless tag.attr['alt'] rescue nil
139
- attributes = options.inject('') { |s, (k, v)| s << %{#{k.downcase}="#{v}" } }.strip
140
- attributes << alt unless alt.nil?
141
- url = image.asset.url size
142
- %{<img src=\"#{url}\" #{attributes unless attributes.empty?} />} rescue nil
143
- end
144
-
145
-
146
- private
147
- def image_and_options(tag)
148
- options = tag.attr.dup
149
- [find_image(tag, options), options]
150
- end
151
-
152
- def find_image(tag, options)
153
- raise TagError, "'title' attribute required" unless title = options.delete('title') or id = options.delete('id') or tag.locals.image
154
- tag.locals.image || ::Image.find_by_title(title) || ::Image.find(id)
155
- end
156
-
157
- def images_find_options(tag)
158
- attr = tag.attr.symbolize_keys
159
- by = attr[:by] || 'position'
160
- order = attr[:order] || 'asc'
161
-
162
- options = {
163
- :order => "#{by} #{order}",
164
- :limit => attr[:limit] || nil,
165
- :offset => attr[:offset] || nil
166
- }
167
- end
168
-
169
- end
170
- end
171
- end
@@ -1,240 +0,0 @@
1
- require File.dirname(__FILE__) + '/../../../spec_helper'
2
-
3
- describe Images::Tags::Image do
4
- dataset :pages
5
- dataset :images
6
-
7
- before(:all) do
8
- @images = Image.all
9
- end
10
-
11
- before(:each) do
12
- stub(AWS::S3::Base).establish_connection!
13
- end
14
-
15
- describe '<r:images>' do
16
-
17
- it 'should render without errors' do
18
- content = '<r:images></r:images>'
19
- pages(:home).should render(content)
20
- end
21
-
22
- it 'should allow tags in the images namespace to be used without defining their namespace' do
23
- content = '<r:images><r:each></r:each></r:images>'
24
- pages(:home).should render(content)
25
- end
26
-
27
- it 'should reference an image given its title' do
28
- image = @images[2]
29
- content = '<r:images title="' + image.title + '"><r:images:url /></r:images>'
30
- pages(:home).should render(content).as(image.asset.url)
31
- end
32
-
33
- it 'should raise an exception if an image title is used that does not exist' do
34
- content = '<r:images title="doesntreallyexist"><r:images:url /></r:images>'
35
- lambda { pages(:home).render(content) }.should raise_error
36
- end
37
-
38
- end
39
-
40
- describe '<r:images:each>' do
41
-
42
- it 'should run through all of our images' do
43
- content = '<r:images:each>test!</r:images:each>'
44
- expected = ''
45
- @images.length.times { expected += 'test!' }
46
- pages(:home).should render(content).as(expected)
47
- end
48
-
49
- it 'should be running through the image objects' do
50
- content = '<r:images:each><r:images:url/></r:images:each>'
51
- expected = ''
52
- @images.each { |image| expected += image.asset.url }
53
- pages(:home).should render(content).as(expected)
54
- end
55
-
56
- it 'should only run through however many images we specify with limit' do
57
- content = '<r:images:each limit="2"><r:images:url/></r:images:each>'
58
- expected = ''
59
- @images[0..1].each { |image| expected += image.asset.url }
60
- pages(:home).should render(content).as(expected)
61
- end
62
-
63
- it 'should start at the image number we give it using offset' do
64
- content = '<r:images:each limit="2" offset="1"><r:images:url/></r:images:each>'
65
- expected = ''
66
- @images[1..2].each { |image| expected += image.asset.url }
67
- pages(:home).should render(content).as(expected)
68
- end
69
-
70
- it 'should display images in the order we give' do
71
- # asc
72
- content = '<r:images:each order="asc" by="position"><r:images:url/></r:images:each>'
73
- expected = ''
74
- @images.each { |image| expected += image.asset.url }
75
- pages(:home).should render(content).as(expected)
76
-
77
- #desc
78
- content = '<r:images:each order="desc" by="position"><r:images:url/></r:images:each>'
79
- expected = ''
80
- @images.reverse.each { |image| expected += image.asset.url }
81
- pages(:home).should render(content).as(expected)
82
- end
83
-
84
- it 'should allow us to order images by title' do
85
- content = '<r:images:each order="asc" by="title"><r:images:url/></r:images:each>'
86
- expected = ''
87
- @images.sort! { |a,b| a.title <=> b.title }
88
- @images.each { |image| expected += image.asset.url }
89
- pages(:home).should render(content).as(expected)
90
- end
91
-
92
- end
93
-
94
- describe '<r:images:first>' do
95
-
96
- it 'should render the first image' do
97
- content = '<r:images:first><r:images:url/></r:images:first>'
98
- expected = @images.first.asset.url
99
- pages(:home).should render(content).as(expected)
100
- end
101
-
102
- end
103
-
104
- describe '<r:images:if_first>' do
105
-
106
- it 'should expand the tag if the image is the first' do
107
- content = '<r:images><r:each><r:if_first><r:url /></r:if_first></r:each></r:images>'
108
- expected = @images.first.asset.url
109
- pages(:home).should render(content).as(expected)
110
- end
111
-
112
- end
113
-
114
- describe '<r:images:unless_first>' do
115
-
116
- it 'should expand the tag if the image is not the first' do
117
- content = '<r:images><r:each><r:unless_first><r:url /></r:unless_first></r:each></r:images>'
118
- expected = ''
119
-
120
- @images.each do |image|
121
- expected += image.asset.url unless image == @images.first
122
- end
123
-
124
- pages(:home).should render(content).as(expected)
125
- end
126
-
127
- end
128
-
129
- describe '<r:if_images>' do
130
-
131
- it 'should expand the contents if there are images' do
132
- content = '<r:images:if_images>test text</r:images:if_images>'
133
- expected = 'test text'
134
- pages(:home).should render(content).as(expected)
135
- end
136
-
137
- it 'should not expand the contents if there are no images' do
138
- Image.delete_all
139
- content = '<r:images:if_images>test text</r:images:if_images>'
140
- expected = ''
141
- pages(:home).should render(content).as(expected)
142
- end
143
-
144
- it 'should expand if the min count is equal to the image count' do
145
- min_count = Image.count
146
- content = '<r:images:if_images min_count="' + min_count.to_s + '">test text</r:images:if_images>'
147
- expected = 'test text'
148
- pages(:home).should render(content).as(expected)
149
- end
150
-
151
- it 'should not expand if the min count is greater than the image count' do
152
- min_count = Image.count + 1
153
- content = '<r:images:if_images min_count="' + min_count.to_s + '">test text</r:images:if_images>'
154
- expected = ''
155
- pages(:home).should render(content).as(expected)
156
- end
157
-
158
- end
159
-
160
- describe '<r:unless_images>' do
161
-
162
- it 'should not the contents if there are images' do
163
- content = '<r:images:unless_images>test text</r:images:unless_images>'
164
- expected = ''
165
- pages(:home).should render(content).as(expected)
166
- end
167
-
168
- it 'should expand the contents if there are no images' do
169
- Image.delete_all
170
- content = '<r:images:unless_images>test text</r:images:unless_images>'
171
- expected = 'test text'
172
- pages(:home).should render(content).as(expected)
173
- end
174
-
175
- it 'should not expand if the min count is equal to the image count' do
176
- min_count = Image.count
177
- content = '<r:images:unless_images min_count="' + min_count.to_s + '">test text</r:images:unless_images>'
178
- expected = ''
179
- pages(:home).should render(content).as(expected)
180
- end
181
-
182
- it 'should expand if the min count is greater than the image count' do
183
- min_count = Image.count + 1
184
- content = '<r:images:unless_images min_count="' + min_count.to_s + '">test text</r:images:unless_images>'
185
- expected = 'test text'
186
- pages(:home).should render(content).as(expected)
187
- end
188
-
189
- end
190
-
191
- describe '<r:images:url/>' do
192
-
193
- it 'should output the url for a valid image' do
194
- content = '<r:images title="' + @images.first.title + '"><r:images:url/></r:images>'
195
- expected = @images.first.asset.url
196
- pages(:home).should render(content).as(expected)
197
- end
198
-
199
- end
200
-
201
- describe '<r:images:title/>' do
202
-
203
- it 'should output the title for a valid image' do
204
- content = '<r:images title="' + @images.first.title + '"><r:images:title/></r:images>'
205
- expected = @images.first.title
206
- pages(:home).should render(content).as(expected)
207
- end
208
-
209
- end
210
-
211
- describe '<r:images:tag/>' do
212
-
213
- it 'should output a valid image tag when given a valid image' do
214
- content = '<r:images title="' + @images.first.title + '"><r:images:tag /></r:images>'
215
- expected = '<img src="' + @images.first.asset.url + '" alt="' + @images.first.title + '" />'
216
- pages(:home).should render(content).as(expected)
217
- end
218
-
219
- it 'should output a valid image tag when specifying an image by title' do
220
- content = '<r:images:tag title="' + @images.first.title + '" />'
221
- expected = '<img src="' + @images.first.asset.url + '" alt="' + @images.first.title + '" />'
222
- pages(:home).should render(content).as(expected)
223
- end
224
-
225
- it 'should output an image tag with the specified size' do
226
- content = '<r:images:tag title="' + @images.first.title + '" size="icon" />'
227
- expected = '<img src="' + @images.first.asset.url(:icon) + '" alt="' + @images.first.title + '" />'
228
- pages(:home).should render(content).as(expected)
229
- end
230
-
231
- it 'should use the given alt text specified' do
232
- content = '<r:images:tag title="' + @images.first.title + '" alt="new alt text" />'
233
- expected = '<img src="' + @images.first.asset.url + '" alt="new alt text" />'
234
- pages(:home).should render(content).as(expected)
235
- end
236
-
237
- end
238
-
239
-
240
- end