radiant-images-extension 0.5.0 → 0.6.0

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.5.0
1
+ 0.6.0
data/app/models/image.rb CHANGED
@@ -3,6 +3,8 @@ class Image < ActiveRecord::Base
3
3
  belongs_to :created_by, :class_name => 'User'
4
4
  belongs_to :updated_by, :class_name => 'User'
5
5
 
6
+ has_many :attachments, :dependent => :destroy
7
+
6
8
  before_save :assign_title
7
9
  validates_uniqueness_of :asset_file_name, :message => 'This file already exists', :allow_nil => true
8
10
  validates_uniqueness_of :title
data/images_extension.rb CHANGED
@@ -5,7 +5,7 @@ require 'will_paginate'
5
5
 
6
6
  class ImagesExtension < Radiant::Extension
7
7
  version YAML::load_file(File.join(File.dirname(__FILE__), 'VERSION'))
8
- description "Images stores images on s3"
8
+ description "Image management tool, meant only to be useful to pages and extensions that require images."
9
9
  url "http://github.com/squaretalent/radiant-images-extension"
10
10
 
11
11
  extension_config do |config|
@@ -40,12 +40,10 @@ module Images
40
40
  <pre><code><r:images:each [limit=0] [offset=0] [order="asc|desc"] [by="position|title|..."]>...</r:images:each></code></pre>
41
41
  }
42
42
  tag 'images:each' do |tag|
43
- context = ''
44
- Helpers.all_images_with_options(tag).each do |image|
43
+ content = tag.locals.images.map { |image|
45
44
  tag.locals.image = image
46
- context << tag.expand
47
- end
48
- context
45
+ tag.expand
46
+ }
49
47
  end
50
48
 
51
49
  desc %{
@@ -57,7 +55,8 @@ module Images
57
55
  }
58
56
  tag 'image' do |tag|
59
57
  tag.locals.image = Helpers.current_image(tag)
60
- tag.expand
58
+
59
+ tag.expand if tag.locals.image.present?
61
60
  end
62
61
 
63
62
  desc %{
@@ -68,12 +67,8 @@ module Images
68
67
  <pre><code><r:image title='image'><r:url [style="preview|original"] /></r:image></code></pre>
69
68
  }
70
69
  tag 'image:url' do |tag|
71
- result = nil
72
- if tag.locals.image ||= Helpers.current_image(tag)
73
- style = tag.attr['style'] || Radiant::Config['images.default'].to_sym
74
- result = tag.locals.image.url(style, false)
75
- end
76
- result
70
+ style = tag.attr['style'] || Radiant::Config['images.default']
71
+ Helpers.current_image(tag).url(style.to_sym, false)
77
72
  end
78
73
 
79
74
  desc %{
@@ -81,30 +76,22 @@ module Images
81
76
  The style of the image can be specified by passing the style attribute.
82
77
 
83
78
  *Usage:*
84
- <pre><code><r:image title='image'><r:tag [style="preview|original"] /></r:image></code></pre>
79
+ <pre><code><r:image title='image' style='preview'><r:tag [style="original"] /></r:image></code></pre>
85
80
  }
86
81
  tag 'image:tag' do |tag|
87
- result = nil
88
- if tag.locals.image ||= Helpers.current_image(tag)
89
- style = tag.attr['style'] || Radiant::Config['images.default'].to_sym
90
- result = %{<img src="#{tag.locals.image.url(style, false)}" />}
91
- end
92
- result
82
+ style = tag.attr['style'] || Radiant::Config['images.default']
83
+ %{<img src="#{Helpers.current_image(tag).url(style.to_sym, false)}" />}
93
84
  end
94
85
 
95
- [:id, :title, :position].each do |method|
86
+ [:id, :title, :position].each do |symbol|
96
87
  desc %{
97
- Outputs the #{method} of the current image
88
+ Outputs the #{symbol} of the current image
98
89
 
99
90
  *Usage:*
100
- <pre><code><r:image title='image'><r:#{method} /></code></pre>
91
+ <pre><code><r:image title='image'><r:#{symbol} /></code></pre>
101
92
  }
102
- tag "image:#{method}" do |tag|
103
- result = nil
104
- if tag.locals.image ||= Helpers.current_image(tag)
105
- result = tag.locals.image.send(method)
106
- end
107
- result
93
+ tag "image:#{symbol}" do |tag|
94
+ Helpers.current_image(tag).send(symbol)
108
95
  end
109
96
  end
110
97
 
@@ -1,64 +1,77 @@
1
1
  module Images
2
2
  module Tags
3
3
  class Helpers
4
-
4
+
5
+ CONDITIONS = ['images.position','images.title','images.id']
6
+
5
7
  class TagError < StandardError; end
6
8
 
7
9
  class << self
8
10
 
9
- def all_images_with_options(tag)
10
- Image.all image_options(tag)
11
- end
12
-
13
11
  def current_images(tag)
14
- result = nil
12
+ @conditions = CONDITIONS.dup
15
13
 
16
- if tag.locals.images.present?
17
- result = tag.locals.images
18
- elsif tag.attr['key'] and tag.attr['value']
19
- result = Image.find(:all, :conditions => { tag.attr['key'].to_sym => tag.attr['value'].to_s }) rescue nil
20
- else
21
- result = Image.all
14
+ if tag.locals.images.nil?
15
+ return Image.all image_conditions(tag).merge(image_options(tag))
16
+ end
17
+
18
+ if tag.locals.images.empty?
19
+ return tag.locals.images
20
+ end
21
+
22
+ images = tag.locals.images
23
+ if images.first.is_a?(Image)
24
+ images.all image_conditions(tag).merge(image_options(tag))
25
+ else
26
+ # We're looking based on attachment positions, not image positions
27
+ @conditions.map! { |term| term.gsub('images.position','attachments.position') }
28
+ images.all image_conditions(tag).merge(image_options(tag)).merge(:joins => 'JOIN images ON images.id = attachments.image_id')
22
29
  end
23
-
24
- result
25
30
  end
26
31
 
27
32
  def current_image(tag)
28
- result = nil
33
+ @conditions = CONDITIONS.dup
29
34
 
30
- if tag.locals.image.present?
31
- result = tag.locals.image
32
- elsif tag.attr['id']
33
- result = Image.find(tag.attr['id'])
34
- elsif tag.attr['title']
35
- result = Image.find_by_title(tag.attr['title'])
36
- elsif tag.attr['position']
37
- begin
38
- result = tag.locals.images[(tag.attr['position']-1).to_i]
39
- rescue
40
- result = Image.find_by_position(tag.attr['position'].to_i)
41
- end
35
+ # Images exist, and we're not looking to change the scope
36
+ if tag.locals.image.present? and image_conditions(tag).empty?
37
+ return tag.locals.image
42
38
  end
43
39
 
44
- result
40
+ unless tag.locals.images.nil?
41
+ images = tag.locals.images
42
+ if images.first.is_a?(Image)
43
+ query = Image.all image_conditions(tag).merge(image_options(tag))
44
+ else
45
+ @conditions.map! { |term| term.gsub('images.position','attachments.position') }
46
+ query = Attachment.all image_conditions(tag).merge(image_options(tag)).merge(:joins => 'JOIN images ON images.id = attachments.image_id')
47
+ query = query.map { |a| a.image }
48
+ end
49
+ return (query && images).first
50
+ else
51
+ return Image.first image_conditions(tag).merge(image_options(tag))
52
+ end
45
53
  end
46
54
 
55
+ private
56
+
47
57
  def image_options(tag)
48
58
  attr = tag.attr.symbolize_keys
49
- by = attr[:by] || 'position'
50
- order = attr[:order] || 'asc'
51
59
 
52
60
  options = {
53
- :order => "#{by} #{order}",
54
- :limit => attr[:limit] || nil,
61
+ :order => "#{attr[:by] || 'position'} #{attr[:order] || 'asc'}",
62
+ :limit => attr[:limit] || nil,
55
63
  :offset => attr[:offset] || nil
56
64
  }
57
65
  end
58
66
 
59
- def image_and_options(tag)
60
- options = tag.attr.dup
61
- [find_image(tag, options), options]
67
+ def image_conditions(tag)
68
+ attr = tag.attr.symbolize_keys
69
+
70
+ @conditions.reject! { |term| attr[term.split('.').last.to_sym].nil? }
71
+
72
+ query = @conditions.map { |term| %{#{term} = ?} }.join(' AND ')
73
+ values = @conditions.map { |term| %{#{attr[term.split('.').last.to_sym]}} }
74
+ query.blank? ? {} : { :conditions => [query,*values] }
62
75
  end
63
76
 
64
77
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{radiant-images-extension}
8
- s.version = "0.5.0"
8
+ s.version = "0.6.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Dirk Kelly", "Mario Visic"]
12
- s.date = %q{2010-12-09}
12
+ s.date = %q{2010-12-13}
13
13
  s.description = %q{Image Radiant Extension management tool, meant only to be useful to pages and extensions that need to require images.}
14
14
  s.email = %q{info@squaretalent.com}
15
15
  s.extra_rdoc_files = [
@@ -5,8 +5,6 @@ describe Images::Tags::Core do
5
5
  dataset :pages, :images
6
6
 
7
7
  before(:all) do
8
- @images = [ images(:first), images(:second), images(:third),
9
- images(:fourth), images(:fifth), images(:sixth) ]
10
8
  Radiant::Config['images.default'] = "original"
11
9
  Radiant::Config['images.path'] = ":rails_root/public/:class/:basename-:style.:extension"
12
10
  Radiant::Config['images.storage'] = "local"
@@ -42,7 +40,7 @@ describe Images::Tags::Core do
42
40
  end
43
41
 
44
42
  it 'should not render its contents when there are no images' do
45
- mock(Image).all { [] }
43
+ mock(Images::Tags::Helpers).current_images(anything) { [] }
46
44
  input = '<r:images><r:if_images>failure</r:if_images></r:images>'
47
45
  expected = ''
48
46
  pages(:home).should render(input).as(expected)
@@ -53,7 +51,7 @@ describe Images::Tags::Core do
53
51
  describe '<r:images:unless_images>' do
54
52
 
55
53
  it 'should render its contents when there are no images' do
56
- mock(Image).all { [] }
54
+ mock(Images::Tags::Helpers).current_images(anything) { [] }
57
55
  input = '<r:images><r:unless_images>success</r:unless_images></r:images>'
58
56
  expected = 'success'
59
57
  pages(:home).should render(input).as(expected)
@@ -76,39 +74,38 @@ describe Images::Tags::Core do
76
74
  end
77
75
 
78
76
  it 'should not expand its contents if there are no images available' do
79
- mock(Image).all() { [] }
80
- mock(Image).all(anything) { [] }
77
+ mock(Images::Tags::Helpers).current_images(anything) { [] }
81
78
  input = '<r:images:each>test </r:images:each>'
82
79
  expected = ''
83
80
  pages(:home).should render(input).as(expected)
84
81
  end
85
82
 
86
83
  it 'should limit the number of images based on the limit parameter passed' do
87
- input = '<r:images:each limit="3"><r:image:title /> </r:images:each>'
84
+ input = '<r:images limit="3"><r:each><r:image:title /> </r:each></r:images>'
88
85
  expected = 'first second third '
89
86
  pages(:home).should render(input).as(expected)
90
87
  end
91
88
 
92
89
  it 'should use the offset parameter to ignore results before the offset' do
93
- input = '<r:images:each limit="3" offset="2"><r:image:title /> </r:images:each>'
90
+ input = '<r:images limit="3" offset="2"><r:each><r:image:title /> </r:each></r:images>'
94
91
  expected = 'third fourth fifth '
95
92
  pages(:home).should render(input).as(expected)
96
93
  end
97
94
 
98
95
  it 'should order the results based by the key passed' do
99
- input = '<r:images:each by="title"><r:image:title /> </r:images:each>'
96
+ input = '<r:images by="title"><r:each><r:image:title /> </r:each></r:images>'
100
97
  expected = 'fifth first fourth second sixth third '
101
98
  pages(:home).should render(input).as(expected)
102
99
  end
103
100
 
104
101
  it 'should order the results by ascending order when asc is passed for the order' do
105
- input = '<r:images:each by="position" order="asc" ><r:image:title /> </r:images:each>'
102
+ input = '<r:images by="position" order="asc"><r:each><r:image:title /> </r:each></r:images>'
106
103
  expected = 'first second third fourth fifth sixth '
107
104
  pages(:home).should render(input).as(expected)
108
105
  end
109
106
 
110
107
  it 'should order the results by descending order when desc is passed for the order' do
111
- input = '<r:images:each by="position" order="desc" ><r:image:title /> </r:images:each>'
108
+ input = '<r:images by="position" order="desc"><r:each><r:image:title /> </r:each></r:images>'
112
109
  expected = 'sixth fifth fourth third second first '
113
110
  pages(:home).should render(input).as(expected)
114
111
  end
@@ -117,54 +114,34 @@ describe Images::Tags::Core do
117
114
 
118
115
  describe '<r:image>' do
119
116
 
120
- it 'should add the image namespace to nested radius tags' do
121
- input = '<r:image title="first"><r:title /></r:image>'
122
- expected = 'first'
123
- pages(:home).should render(input).as(expected)
124
- end
125
-
126
- it 'should render its contents if there is a current image' do
117
+ it 'should render overide the above context' do
127
118
  input = '<r:images:each><r:image title="first"><r:title /> </r:image></r:images:each>'
128
- expected = 'first second third fourth fifth sixth '
119
+ expected = 'first first first first first first '
129
120
  pages(:home).should render(input).as(expected)
130
121
  end
131
122
 
132
- it 'should not render its contents if there is no current image' do
133
- input = '<r:image title="invalid"><r:title /></r:image>'
134
- expected = ''
123
+ it 'should allow images to be looked up by their title attribute' do
124
+ input = %{<r:image title="fifth"><r:title /></r:image>}
125
+ expected = 'fifth'
135
126
  pages(:home).should render(input).as(expected)
136
127
  end
137
128
 
138
129
  it 'should allow images to be looked up by their id attribute' do
139
- mock(Image).find('5') { @images[4] }
140
- input = '<r:image id="5"><r:title /></r:image>'
141
- expected = 'fifth'
130
+ input = %{<r:image id="#{images(:second).id}"><r:title /></r:image>}
131
+ expected = 'second'
142
132
  pages(:home).should render(input).as(expected)
143
133
  end
144
134
 
145
135
  it 'should allow images to be looked up by their position attribute' do
146
- input = '<r:image position="3"><r:title /></r:image>'
136
+ input = %{<r:image:title position="3" />}
147
137
  expected = 'third'
148
138
  pages(:home).should render(input).as(expected)
149
139
  end
150
-
151
- it 'should allow images to be looked up by their title attribute' do
152
- input = '<r:image title="sixth"><r:title /></r:image>'
153
- expected = 'sixth'
154
- pages(:home).should render(input).as(expected)
155
- end
156
-
140
+
157
141
  end
158
142
 
159
143
  describe '<r:image:url>' do
160
144
 
161
- before :each do
162
- asset = Paperclip::Attachment.new('asset', @images[0], { :url => Radiant::Config['images.url'] })
163
- stub(Image).find_by_title('first') { @images[0] }
164
- stub.proxy(Image).find_by_title('invalid')
165
- stub(@images[0]).asset { asset }
166
- end
167
-
168
145
  it 'should not render a valid url if there is no current image' do
169
146
  input = '<r:image title="invalid"><r:url /></r:image>'
170
147
  expected = ''
@@ -172,8 +149,8 @@ describe Images::Tags::Core do
172
149
  end
173
150
 
174
151
  it 'should render the url with the default style if not specified' do
175
- input = '<r:image title="first"><r:url /></r:image>'
176
- expected = '/images/first-original.png'
152
+ input = '<r:image title="fourth"><r:url /></r:image>'
153
+ expected = '/images/fourth-original.png'
177
154
  pages(:home).should render(input).as(expected)
178
155
  end
179
156
 
@@ -187,13 +164,6 @@ describe Images::Tags::Core do
187
164
 
188
165
  describe '<r:image:tag>' do
189
166
 
190
- before :each do
191
- asset = Paperclip::Attachment.new('asset', @images[0], { :url => Radiant::Config['images.url'] })
192
- stub(Image).find_by_title('first') { @images[0] }
193
- stub.proxy(Image).find_by_title('invalid')
194
- stub(@images[0]).asset { asset }
195
- end
196
-
197
167
  it 'should not render a valid img tag if there is no current image' do
198
168
  input = '<r:image title="invalid"><r:tag /></r:image>'
199
169
  expected = ''
@@ -207,18 +177,18 @@ describe Images::Tags::Core do
207
177
  end
208
178
 
209
179
  it 'should render the img tag with the style specified by the user' do
210
- input = '<r:image title="first"><r:tag style="icon"/></r:image>'
211
- expected = '<img src="/images/first-icon.png" />'
180
+ input = '<r:image title="second"><r:tag style="icon"/></r:image>'
181
+ expected = '<img src="/images/second-icon.png" />'
212
182
  pages(:home).should render(input).as(expected)
213
183
  end
214
-
184
+
215
185
  end
216
186
 
217
187
  describe '<r:image:id>' do
218
188
 
219
189
  it 'should render the id of the current image context' do
220
190
  input = '<r:image title="sixth"><r:id /></r:image>'
221
- expected = @images[5].id.to_s
191
+ expected = images(:sixth).id.to_s
222
192
  pages(:home).should render(input).as(expected)
223
193
  end
224
194
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: radiant-images-extension
3
3
  version: !ruby/object:Gem::Version
4
- hash: 11
4
+ hash: 7
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 5
8
+ - 6
9
9
  - 0
10
- version: 0.5.0
10
+ version: 0.6.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Dirk Kelly
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-12-09 00:00:00 +08:00
19
+ date: 2010-12-13 00:00:00 +08:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency