Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactored how assets are previewed.

  • Loading branch information...
commit cec77be97a09069a3dcd60d1c91048a313b7306b 1 parent 685832b
Francesc Esplugas authored
View
33 app/helpers/admin/form_helper.rb
@@ -380,6 +380,39 @@ def attribute_disabled?(attribute)
return accessible.nil? ? false : !accessible.include?(attribute)
end
+ def typus_preview(item, attribute)
+
+ # typus_preview(attribute).html_safe! unless @item.send(attribute).blank?
+
+ attachment = attribute.split("_file_name").first
+ file_preview = Typus::Configuration.options[:file_preview]
+ file_thumbnail = Typus::Configuration.options[:file_thumbnail]
+
+ has_file_preview = item.send(attachment).styles.member?(file_preview)
+ has_file_thumbnail = item.send(attachment).styles.member?(file_thumbnail)
+ file_preview_is_image = item.send("#{attachment}_content_type") =~ /^image\/.+/
+
+ href = if has_file_preview
+ item.send(attachment).url(file_preview)
+ else
+ item.send(attachment).url
+ end
+
+ content = if has_file_thumbnail
+ image_tag item.send(attachment).url(file_thumbnail)
+ else
+ item.send(attribute)
+ end
+
+ render "admin/helpers/preview",
+ :attribute => attribute,
+ :content => content,
+ :has_file_preview => has_file_preview,
+ :href => href,
+ :item => item
+
+ end
+
##
# Tree builder when model +acts_as_tree+
#
View
13 app/views/admin/helpers/_preview.html.erb
@@ -0,0 +1,13 @@
+<%
+ dom = item.to_dom(:suffix => "#{attribute}_preview" )
+%>
+
+<% if has_file_preview %>
+<script type="text/javascript" charset="utf-8">
+ $(document).ready(function() { $("#<%= dom %>").fancybox(); });
+</script>
+<% end %>
+
+<a id="<%= dom %>"
+ href="<%= href %>"
+ title="<%= item.to_label %>"><%= content %></a>
View
4 app/views/admin/templates/_file.html.erb
@@ -16,5 +16,5 @@
<li>
<%= form.label attribute, label_text %>
<%= form.file_field attachment, options %>
- <%= @item.typus_preview(attribute).html_safe! unless @item.send(attribute).blank? %>
-</li>
+ <%= typus_preview(@item, attribute) %>
+</li>
View
1  lib/typus.rb
@@ -109,7 +109,6 @@ def boot!
# Typus mixins.
require "typus/authentication"
require "typus/format"
- require "typus/preview"
require "typus/preferences"
require "typus/reloader"
require "typus/quick_edit"
View
57 lib/typus/preview.rb
@@ -1,57 +0,0 @@
-module Typus
-
- module InstanceMethods
-
- include ActionView::Helpers::UrlHelper
-
- # OPTIMIZE: Move html code to helper.
- def typus_preview(attribute)
-
- attachment = attribute.split('_file_name').first
-
- case send("#{attachment}_content_type")
- when /^image\/.+/
- file_preview = Typus::Configuration.options[:file_preview]
- file_thumbnail = Typus::Configuration.options[:file_thumbnail]
- if send(attachment).styles.member?(file_preview) && send(attachment).styles.member?(file_thumbnail)
- <<-HTML
-<script type="text/javascript" charset="utf-8">
- $(document).ready(function() { $("##{to_dom}_#{attribute}_preview").fancybox(); });
-</script>
-<a id="#{to_dom}_#{attribute}_preview" href="#{send(attachment).url(file_preview)}" title="#{to_label}">
-<img src="#{send(attachment).url(file_thumbnail)}" />
- </a>
- HTML
- elsif send(attachment).styles.member?(file_thumbnail)
- <<-HTML
-<a href="#{send(attachment).url}" title="#{to_label}">
-<img src="#{send(attachment).url(file_thumbnail)}" />
- </a>
- HTML
- elsif send(attachment).styles.member?(file_preview)
- <<-HTML
-<script type="text/javascript" charset="utf-8">
- $(document).ready(function() { $("##{to_dom}_#{attribute}_preview").fancybox(); });
-</script>
-<a id="#{to_dom}_#{attribute}_preview" href="#{send(attachment).url(file_preview)}" title="#{to_label}">
-#{send(attribute)}
- </a>
- HTML
- else
- <<-HTML
-<a href="#{send(attachment).url}">#{send(attribute)}</a>
- HTML
- end
- else
- <<-HTML
-<a href="#{send(attachment).url}">#{send(attribute)}</a>
- HTML
- end
-
- end
-
- end
-
-end
-
-ActiveRecord::Base.send :include, Typus::InstanceMethods
Please sign in to comment.
Something went wrong with that request. Please try again.