radiant-clipped-extension 1.0.0 → 1.0.1

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.
@@ -25,11 +25,17 @@ class Admin::AssetsController < Admin::ResourceController
25
25
  end
26
26
 
27
27
  def create
28
- @asset.update_attributes!(params[:asset])
28
+ @assets, @page_attachments = [], []
29
+ params[:asset][:asset].to_a.each do |uploaded_asset|
30
+ @asset = Asset.create(:asset => uploaded_asset, :title => params[:asset][:title], :caption => params[:asset][:caption])
31
+ if params[:for_attachment]
32
+ @page = Page.find_by_id(params[:page_id]) || Page.new
33
+ @page_attachments << @page_attachment = @asset.page_attachments.build(:page => @page)
34
+ end
35
+ @assets << @asset
36
+ end
29
37
  if params[:for_attachment]
30
- @page = Page.find_by_id(params[:page_id]) || Page.new
31
- @page_attachment = @asset.page_attachments.build(:page => @page)
32
- render :partial => 'admin/page_attachments/attachment', :object => @page_attachment
38
+ render :partial => 'admin/page_attachments/attachment', :collection => @page_attachments
33
39
  else
34
40
  response_for :create
35
41
  end
data/app/models/asset.rb CHANGED
@@ -34,7 +34,7 @@ class Asset < ActiveRecord::Base
34
34
 
35
35
  has_attached_file :asset,
36
36
  :styles => lambda { |attachment|
37
- AssetType.from(attachment.instance_read(:content_type)).paperclip_styles
37
+ AssetType.for(attachment).paperclip_styles
38
38
  },
39
39
  :processors => lambda { |asset|
40
40
  asset.paperclip_processors
@@ -52,7 +52,8 @@ class Asset < ActiveRecord::Base
52
52
 
53
53
  before_save :assign_title
54
54
  before_save :assign_uuid
55
- after_post_process :get_dimensions
55
+
56
+ after_post_process :read_dimensions
56
57
 
57
58
  validates_attachment_presence :asset, :message => "You must choose a file to upload!"
58
59
  if Radiant.config["paperclip.skip_filetype_validation"] != "true" && Radiant.config['paperclip.content_types']
@@ -61,7 +62,7 @@ class Asset < ActiveRecord::Base
61
62
  validates_attachment_size :asset, :less_than => ( Radiant.config["assets.max_asset_size"] || 5 ).to_i.megabytes
62
63
 
63
64
  def asset_type
64
- AssetType.from(asset.content_type)
65
+ AssetType.for(asset)
65
66
  end
66
67
  delegate :paperclip_processors, :paperclip_styles, :style_dimensions, :style_format, :to => :asset_type
67
68
 
@@ -82,7 +83,7 @@ class Asset < ActiveRecord::Base
82
83
  def extension(style_name='original')
83
84
  if style_name == 'original'
84
85
  return original_extension
85
- elsif style = asset.styles[style_name.to_sym]
86
+ elsif style = paperclip_styles[style_name.to_sym]
86
87
  return style.format
87
88
  else
88
89
  return original_extension
@@ -104,8 +105,8 @@ class Asset < ActiveRecord::Base
104
105
  def geometry(style_name='original')
105
106
  if style_name == 'original'
106
107
  original_geometry
107
- elsif style = asset.styles[style_name.to_sym]
108
- original_geometry * style.geometry
108
+ elsif style = asset.styles[style_name.to_sym] # asset.styles is normalised, but self.paperclip_styles is not
109
+ original_geometry.transformed_by(style.geometry)
109
110
  end
110
111
  end
111
112
 
@@ -150,16 +151,20 @@ class Asset < ActiveRecord::Base
150
151
 
151
152
  private
152
153
 
154
+ # at this point the file queue will not have been written
155
+ # but the upload should be in place. We read dimensions from the
156
+ # original file and calculate thumbnail dimensions later, on demand.
157
+
153
158
  def read_dimensions
154
- if image? && !dimensions_known?
155
- geometry = Paperclip::Geometry.from_file(asset.path)
156
- self.update_attributes(
157
- :original_width => geometry.width,
158
- :original_height => geometry.height,
159
- :original_extension => extension
160
- )
161
- geometry
159
+ if image?
160
+ if file = asset.queued_for_write[:original]
161
+ geometry = Paperclip::Geometry.from_file(file)
162
+ self.original_width = geometry.width
163
+ self.original_height = geometry.height
164
+ self.original_extension = File.extname(file.path)
165
+ end
162
166
  end
167
+ true
163
168
  end
164
169
 
165
170
  def assign_title
@@ -75,20 +75,6 @@ module AssetTags
75
75
  end
76
76
  end
77
77
 
78
- desc %{
79
- References the last asset attached to the current page.
80
-
81
- *Usage:*
82
- <pre><code><r:assets:last>...</r:assets:last></code></pre>
83
- }
84
- tag 'assets:last' do |tag|
85
- p "tag.locals.page.assets is #{tag.locals.page.assets.join(',')} and tag.locals.page.assets.last is #{tag.locals.page.assets.last}"
86
-
87
- if tag.locals.asset = tag.locals.page.assets.last
88
- tag.expand
89
- end
90
- end
91
-
92
78
  desc %{
93
79
  Renders the contained elements only if the current contextual page has one or
94
80
  more assets. The @min_count@ attribute specifies the minimum number of required
@@ -12,6 +12,7 @@ class AssetType
12
12
 
13
13
  @@types = []
14
14
  @@type_lookup = {}
15
+ @@extension_lookup = {}
15
16
  @@mime_lookup = {}
16
17
  @@default_type = nil
17
18
  attr_reader :name, :processors, :styles, :icon_name, :catchall, :default_radius_tag
@@ -22,11 +23,12 @@ class AssetType
22
23
  @icon_name = options[:icon] || name
23
24
  @processors = options[:processors] || []
24
25
  @styles = options[:styles] || {}
25
- @mimes = options[:mime_types] || []
26
26
  @default_radius_tag = options[:default_radius_tag] || 'link'
27
- if @mimes.any?
28
- @mimes.each { |mimetype| @@mime_lookup[mimetype] ||= self }
29
- end
27
+ @extensions = options[:extensions] || []
28
+ @extensions.each { |ext| @@extension_lookup[ext] ||= self }
29
+ @mimes = options[:mime_types] || []
30
+ @mimes.each { |mimetype| @@mime_lookup[mimetype] ||= self }
31
+
30
32
  this = self
31
33
  Asset.send :define_method, "#{name}?".intern do this.mime_types.include?(asset_content_type) end
32
34
  Asset.send :define_class_method, "#{name}_condition".intern do this.condition; end
@@ -119,8 +121,17 @@ class AssetType
119
121
 
120
122
  # class methods
121
123
 
122
- def self.from(mimetype)
123
- @@mime_lookup[mimetype] || catchall
124
+ def self.for(attachment)
125
+ extension = File.extname(attachment.original_filename).sub(/^\.+/, "")
126
+ from_extension(extension) || from_mimetype(attachment.instance_read(:content_type)) || catchall
127
+ end
128
+
129
+ def self.from_extension(extension)
130
+ @@extension_lookup[extension]
131
+ end
132
+
133
+ def self.from_mimetype(mimetype)
134
+ @@mime_lookup[mimetype]
124
135
  end
125
136
 
126
137
  def self.catchall
@@ -138,6 +149,9 @@ class AssetType
138
149
  def self.find(type)
139
150
  @@type_lookup[type] if type
140
151
  end
152
+ def self.[](type)
153
+ find(type)
154
+ end
141
155
 
142
156
  def self.all
143
157
  @@types
@@ -12,7 +12,7 @@
12
12
 
13
13
  %p
14
14
  %label= t("clipped_extension.choose_file")
15
- = f.file_field :asset, :style => "width: 100%"
15
+ = f.file_field :asset, :style => "width: 100%", :multiple => true
16
16
 
17
17
  .buttons
18
18
  = save_model_button(@asset)
@@ -11,7 +11,7 @@
11
11
 
12
12
  %p
13
13
  = f.label :asset, t("clipped_extension.choose_file")
14
- = f.file_field :asset, :class => 'file'
14
+ = f.file_field :asset, :class => 'file', :multiple => true
15
15
 
16
16
  %p
17
17
  = f.label :title, t("clipped_extension.alt_text_or_title")
data/clipped_extension.rb CHANGED
@@ -22,11 +22,12 @@ class ClippedExtension < Radiant::Extension
22
22
  Page.send :include, AssetTags # radius tags for selecting sets of assets and presenting each one
23
23
  UserActionObserver.instance.send :add_observer!, Asset # the usual creator- and updater-stamping
24
24
 
25
- AssetType.new :image, :icon => 'image', :default_radius_tag => 'image', :processors => [:thumbnail], :styles => {:icon => ['42x42#', :png], :thumbnail => ['100x100#', :png]}, :mime_types => %w[image/png image/x-png image/jpeg image/pjpeg image/jpg image/gif]
25
+ AssetType.new :image, :icon => 'image', :default_radius_tag => 'image', :processors => [:thumbnail], :styles => {:icon => ['42x42#', :png], :thumbnail => ['100x100#', :png]}, :extensions => %w[jpg jpeg png gif], :mime_types => %w[image/png image/x-png image/jpeg image/pjpeg image/jpg image/gif]
26
26
  AssetType.new :video, :icon => 'video', :processors => [:frame_grab], :styles => {:native => ['', :jpg], :icon => ['42x42#', :png], :thumbnail => ['100x100#', :png]}, :mime_types => %w[application/x-mp4 video/mpeg video/quicktime video/x-la-asf video/x-ms-asf video/x-msvideo video/x-sgi-movie video/x-flv flv-application/octet-stream video/3gpp video/3gpp2 video/3gpp-tt video/BMPEG video/BT656 video/CelB video/DV video/H261 video/H263 video/H263-1998 video/H263-2000 video/H264 video/JPEG video/MJ2 video/MP1S video/MP2P video/MP2T video/mp4 video/MP4V-ES video/MPV video/mpeg4 video/mpeg4-generic video/nv video/parityfec video/pointer video/raw video/rtx video/ogg video/webm]
27
27
  AssetType.new :audio, :icon => 'audio', :mime_types => %w[audio/mpeg audio/mpg audio/ogg application/ogg audio/x-ms-wma audio/vnd.rn-realaudio audio/x-wav]
28
- AssetType.new :flash, :icon => 'flash', :default_radius_tag => 'flash', :mime_types => %w[application/x-shockwave-flash]
29
- AssetType.new :pdf, :icon => 'pdf', :processors => [:thumbnail], :mime_types => %w[application/pdf application/x-pdf], :styles => {:icon => ['42x42#', :png], :thumbnail => ['100x100#', :png]}
28
+ AssetType.new :font, :icon => 'font', :extensions => %w[ttf otf eot woff]
29
+ AssetType.new :flash, :icon => 'flash', :default_radius_tag => 'flash', :extensions => %w{swf}, :mime_types => %w[application/x-shockwave-flash]
30
+ AssetType.new :pdf, :icon => 'pdf', :processors => [:thumbnail], :extensions => %w{pdf}, :mime_types => %w[application/pdf application/x-pdf], :styles => {:icon => ['42x42#', :png], :thumbnail => ['100x100#', :png]}
30
31
  AssetType.new :document, :icon => 'document', :mime_types => %w[application/msword application/rtf application/vnd.ms-excel application/vnd.ms-powerpoint application/vnd.ms-project application/vnd.ms-works text/plain text/html]
31
32
  AssetType.new :other, :icon => 'unknown'
32
33
 
@@ -1,3 +1,3 @@
1
1
  module RadiantClippedExtension
2
- VERSION = '1.0.0'
2
+ VERSION = '1.0.1'
3
3
  end
Binary file
Binary file
@@ -33,10 +33,6 @@ describe AssetTags do
33
33
  page.should render('<r:assets:first><r:asset:id /></r:assets:first>').as( "#{asset_id(:test2)}" )
34
34
  end
35
35
 
36
- it "assets:last" do
37
- page.should render('<r:assets:last><r:asset:id /></r:assets:last>').as( "#{asset_id(:test1)}" )
38
- end
39
-
40
36
  it "should retreive an asset by name" do
41
37
  page.should render('<r:asset:id name="video" />').as( "#{asset_id(:video)}" )
42
38
  end
@@ -23,7 +23,7 @@ describe Asset do
23
23
  it 'should be valid when saved' do
24
24
  create_asset.should be_valid
25
25
  end
26
-
26
+
27
27
  describe '#thumbnail' do
28
28
  describe 'without argument' do
29
29
  it 'should return paperclip asset url for image' do
@@ -63,7 +63,7 @@ describe Asset do
63
63
  end
64
64
 
65
65
  it 'should return icon for non-image with a given size' do
66
- document = new_asset :asset_content_type => 'application/msword'
66
+ document = new_asset :asset_content_type => 'application/msword', :asset_file_name => "document.doc"
67
67
  document.thumbnail('icon').should == "/images/admin/assets/document_icon.png"
68
68
  document.thumbnail('anything_but_icon').should == "/images/admin/assets/document_icon.png"
69
69
  end
@@ -15,9 +15,9 @@ end
15
15
  AssetType.new :simple, :mime_types => %w[test/this test/that]
16
16
  AssetType.new :complex, :processors => [:dummy], :styles => {:something => "99x99>"}, :mime_types => %w[test/complex], :icon => 'document'
17
17
  AssetType.new :configured, :processors => [:dummy], :mime_types => %w[test/configured]
18
-
18
+ AssetType.new :unstandard, :extensions => %w[unstandard nomimetype]
19
+
19
20
  describe AssetType do
20
-
21
21
  context 'without thumbnails' do
22
22
  subject{ AssetType.find(:simple) }
23
23
  its(:plural) { should == "simples" }
@@ -54,10 +54,13 @@ describe AssetType do
54
54
  AssetType.slice('simple', 'complex').should =~ [AssetType.find(:simple), AssetType.find(:complex)]
55
55
  end
56
56
 
57
- describe '.from' do
58
- AssetType.from('test/this').should == AssetType.find(:simple)
59
- AssetType.from('test/complex').should == AssetType.find(:complex)
57
+ describe '.from_extension' do
58
+ AssetType.from_extension('nomimetype').should == AssetType.find(:unstandard)
59
+ end
60
+
61
+ describe '.from_mimetype' do
62
+ AssetType.from_mimetype('test/this').should == AssetType.find(:simple)
60
63
  end
61
64
  end
62
-
65
+
63
66
  end
@@ -0,0 +1,61 @@
1
+ require File.dirname(__FILE__) + '/../spec_helper'
2
+
3
+ describe Asset do
4
+ dataset :assets
5
+
6
+ # these are here to check that paperclip and our various add-ons are all working together.
7
+ # most of the components are also tested individually but in more abstract ways.
8
+
9
+ let(:asset) {
10
+ asset = assets(:test1)
11
+ asset.asset = File.new(File.join(File.dirname(__FILE__), "..", "fixtures", "5k.png"))
12
+ asset
13
+ }
14
+
15
+ describe "on assigning a file to an asset" do
16
+ before do
17
+ Radiant.config["assets.create_image_thumbnails?"] = true
18
+ end
19
+
20
+ it "should have saved the asset" do
21
+ asset.new_record?.should be_false
22
+ end
23
+
24
+ it "should have calculated asset type" do
25
+ asset.asset_type.should == AssetType[:image]
26
+ end
27
+
28
+ it "should have recorded width and height and original extension" do
29
+ asset.original_width.should == 434
30
+ asset.original_height.should == 66
31
+ asset.original_extension.should == 'png'
32
+ end
33
+
34
+ it "should respond to original geometry" do
35
+ asset.original_geometry.should == Paperclip::Geometry.new(434,66)
36
+ end
37
+
38
+ it "should calculate thumbnail geometry" do
39
+ original_geometry = Paperclip::Geometry.new(434,66)
40
+ asset.geometry.should == original_geometry
41
+ asset.geometry(:icon).should == original_geometry * Paperclip::Geometry.parse("42x42#")
42
+ end
43
+
44
+ it "should respond to image dimension methods" do
45
+ asset.width.should == 434
46
+ asset.height.should == 66
47
+ asset.width(:icon).should == 42
48
+ asset.height(:icon).should == 42
49
+ end
50
+
51
+ it "should respond to image shape methods" do
52
+ asset.horizontal?.should be_true
53
+ asset.vertical?.should be_false
54
+ asset.square?.should be_false
55
+ asset.square?(:icon).should be_true
56
+ asset.orientation.should == 'horizontal'
57
+ asset.aspect.should == 434.0/66.0
58
+ end
59
+
60
+ end
61
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: radiant-clipped-extension
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
4
+ hash: 21
5
5
  prerelease:
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 0
10
- version: 1.0.0
9
+ - 1
10
+ version: 1.0.1
11
11
  platform: ruby
12
12
  authors:
13
13
  - Keith Bingman
@@ -18,7 +18,7 @@ autorequire:
18
18
  bindir: bin
19
19
  cert_chain: []
20
20
 
21
- date: 2011-06-09 00:00:00 Z
21
+ date: 2011-06-22 00:00:00 Z
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency
24
24
  name: acts_as_list
@@ -178,18 +178,21 @@ files:
178
178
  - public/images/admin/assets/zip_icon.png
179
179
  - public/javascripts/admin/assets.js
180
180
  - public/stylesheets/sass/admin/assets.sass
181
+ - radiant-clipped-extension-1.0.0.gem
181
182
  - radiant-clipped-extension.gemspec
182
183
  - Rakefile
183
184
  - README.md
184
185
  - spec/controllers/admin/assets_controller_spec.rb
185
186
  - spec/controllers/admin/page_attachments_controller_spec.rb
186
187
  - spec/datasets/assets_dataset.rb
188
+ - spec/fixtures/5k.png
187
189
  - spec/fixtures/test.flv
188
190
  - spec/lib/asset_tags_spec.rb
189
191
  - spec/lib/frame_grab_spec.rb
190
192
  - spec/lib/geometry_transformation_spec.rb
191
193
  - spec/models/asset_spec.rb
192
194
  - spec/models/asset_type_spec.rb
195
+ - spec/models/post_processing_spec.rb
193
196
  - spec/spec.opts
194
197
  - spec/spec_helper.rb
195
198
  - wireframes/edit-page-assets-2.bmml
@@ -203,7 +206,7 @@ files:
203
206
  homepage: http://radiantcms.org
204
207
  licenses: []
205
208
 
206
- post_install_message: "\n Add this to your radiant project with:\n config.gem 'radiant-clipped-extension', :version => '~>1.0.0'\n "
209
+ post_install_message: "\n Add this to your radiant project with:\n config.gem 'radiant-clipped-extension', :version => '~>1.0.1'\n "
207
210
  rdoc_options: []
208
211
 
209
212
  require_paths:
@@ -237,12 +240,14 @@ test_files:
237
240
  - spec/controllers/admin/assets_controller_spec.rb
238
241
  - spec/controllers/admin/page_attachments_controller_spec.rb
239
242
  - spec/datasets/assets_dataset.rb
243
+ - spec/fixtures/5k.png
240
244
  - spec/fixtures/test.flv
241
245
  - spec/lib/asset_tags_spec.rb
242
246
  - spec/lib/frame_grab_spec.rb
243
247
  - spec/lib/geometry_transformation_spec.rb
244
248
  - spec/models/asset_spec.rb
245
249
  - spec/models/asset_type_spec.rb
250
+ - spec/models/post_processing_spec.rb
246
251
  - spec/spec.opts
247
252
  - spec/spec_helper.rb
248
253
  - features/support/env.rb