Skip to content
Browse files

not_type finders, new conditional radius tags and a few small fixes

  • Loading branch information...
1 parent c4f2d35 commit 12df1de818e324f638b9c934edf5552d341507cb @will-r will-r committed Aug 3, 2009
Showing with 50 additions and 18 deletions.
  1. +12 −5 app/models/asset.rb
  2. +28 −4 app/models/asset_tags.rb
  3. +10 −9 paperclipped_extension.rb
View
17 app/models/asset.rb
@@ -4,11 +4,12 @@ class Asset < ActiveRecord::Base
@@known_types = []
cattr_accessor :known_types
- # type declarations are consolidated here so that other extensions can add more
- # see register_type() calls in the main class definition below
+ # type declarations are consolidated here so that other extensions can add more types
+ # for example: Asset.register_type(:gps, %w{application/gpx+xml application/tcx+xml})
+ # the main Asset register_type() calls are in the class definition below after validation
def self.register_type(type, mimes)
- Mime::Type.register mimes.shift, type, mimes
+ Mime::Type.register mimes.shift, type, mimes # Mime::Type.register 'image/png', :image, %w[image/x-png image/jpeg image/pjpeg image/jpg image/gif]
self.class.send :define_method, "#{type}?" do |asset_content_type|
Mime::Type.lookup_by_extension(type.to_s) == asset_content_type.to_s
@@ -19,8 +20,14 @@ def self.register_type(type, mimes)
# use #send due to a ruby 1.8.2 issue
send(:sanitize_sql, ['asset_content_type IN (?)', types])
end
+
+ self.class.send :define_method, "not_#{type}_condition" do
+ types = Mime::Type.lookup_by_extension(type.to_s).all_types
+ send(:sanitize_sql, ['NOT asset_content_type IN (?)', types])
+ end
named_scope type.to_s.pluralize.intern, :conditions => self.send("#{type}_condition".intern)
+ named_scope "not_#{type.to_s.pluralize}".intern, :conditions => self.send("not_#{type}_condition".intern)
known_types.push(type)
end
@@ -33,7 +40,7 @@ def self.other_condition
send(:sanitize_sql, ['asset_content_type NOT IN (?)', self.mime_types_not_considered_other])
end
- # this is made separate so that it can be overridden or alias_chained
+ # this is made separate for consistency and so that it can be overridden or alias_chained
def self.mime_types_not_considered_other
Mime::IMAGE.all_types + Mime::AUDIO.all_types + Mime::MOVIE.all_types
end
@@ -153,7 +160,7 @@ def additional_thumbnails
register_type :movie, Mime::SWF.all_types + Mime::VIDEO.all_types
# 'others' are anything that is not image, video, audio or swf
- # the lambda is just a precaution: it delays interpolation.
+ # the lambda delays interpolation, allowing extensions to change the 'other' conduition
named_scope :others, lambda {{:conditions => self.other_condition}}
View
32 app/models/asset_tags.rb
@@ -146,6 +146,29 @@ class TagError < StandardError; end
tag.expand unless asset_content_type.match(regexp).nil?
end
+ Asset.known_types.each do |type|
+ desc %{
+ Renders the contained elements only if the asset is of the specified type.
+
+ *Usage:*
+ <pre><code><r:assets:if_#{type}>...</r:assets:if_#{type}></code></pre>
+ }
+ tag "assets:if_#{type}" do |tag|
+ tag.expand if tag.locals.asset.send("#{type}?".intern)
+ end
+
+ desc %{
+ Renders the contained elements only if the asset is not of the specified type.
+
+ *Usage:*
+ <pre><code><r:assets:unless_#{type}>...</r:assets:unless_#{type}></code></pre>
+ }
+ tag "assets:unless_#{type}" do |tag|
+ tag.expand unless tag.locals.asset.send("#{type}?".intern)
+ end
+
+ end
+
[:title, :caption, :asset_file_name, :asset_content_type, :asset_file_size, :id].each do |method|
desc %{
Renders the `#{method.to_s}' attribute of the asset.
@@ -230,10 +253,11 @@ class TagError < StandardError; end
end
tag 'assets:thumbnail' do |tag|
- options = tag.attr.dup
- asset = find_asset(tag, options)
- asset.generate_thumbnail('test', ['24x24#',nil])
- asset.save
+ tag.render('assets:url', tag.attr.dup.merge('size' => 'thumbnail'))
+ end
+
+ tag 'assets:icon' do |tag|
+ tag.render('assets:url', tag.attr.dup.merge('size' => 'icon'))
end
desc %{
View
19 paperclipped_extension.rb
@@ -27,16 +27,17 @@ class PaperclippedExtension < Radiant::Extension
end
def activate
-
- Radiant::AdminUI.send :include, AssetsAdminUI unless defined? admin.asset # UI is a singleton and already loaded
- admin.asset = Radiant::AdminUI.load_default_asset_regions
+ unless defined? admin.asset # UI is a singleton and already loaded
+ Radiant::AdminUI.send :include, AssetsAdminUI
+ admin.asset = Radiant::AdminUI.load_default_asset_regions
- %w{page}.each do |view|
- admin.send(view).edit.add :main, "/admin/assets/show_bucket_link", :before => "edit_header"
- admin.send(view).edit.add :main, "/admin/assets/assets_bucket", :after => "edit_buttons"
- admin.send(view).edit.asset_tabs.concat %w{attachment_tab upload_tab bucket_tab search_tab}
- admin.send(view).edit.bucket_pane.concat %w{bucket_notes bucket bucket_bottom}
- admin.send(view).edit.asset_panes.concat %w{page_attachments upload search}
+ %w{page}.each do |view|
+ admin.send(view).edit.add :main, "/admin/assets/show_bucket_link", :before => "edit_header"
+ admin.send(view).edit.add :main, "/admin/assets/assets_bucket", :after => "edit_buttons"
+ admin.send(view).edit.asset_tabs.concat %w{attachment_tab upload_tab bucket_tab search_tab}
+ admin.send(view).edit.bucket_pane.concat %w{bucket_notes bucket bucket_bottom}
+ admin.send(view).edit.asset_panes.concat %w{page_attachments upload search}
+ end
end
Page.class_eval {

0 comments on commit 12df1de

Please sign in to comment.
Something went wrong with that request. Please try again.