Skip to content

Commit

Permalink
fixed preview and preview_online methods in AttachmentsController; so…
Browse files Browse the repository at this point in the history
…me refactoring (added ActiveSupport::Concern)
  • Loading branch information
nodecarter committed Mar 26, 2013
1 parent 54a4446 commit 2a0394d
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 153 deletions.
20 changes: 11 additions & 9 deletions init.rb
@@ -1,16 +1,18 @@
require 'redmine'

require_dependency 'redmine_lightbox/hooks/view_layouts_base_html_head_hook'
require_dependency 'redmine_lightbox/services/document_converter'
require_dependency 'redmine_lightbox/patches/attachment_patch'
require_dependency 'redmine_lightbox/patches/attachments_controller_patch'
require_dependency 'redmine_lightbox/patches/issues_helper_patch'
require 'redmine_lightbox/hooks/view_layouts_base_html_head_hook'
require 'redmine_lightbox/services/document_converter'
require 'redmine_lightbox/patches/attachment_patch'
require 'redmine_lightbox/patches/attachments_controller_patch'
require 'redmine_lightbox/patches/application_helper_patch'
require 'redmine_lightbox/patches/issues_helper_patch'
require 'redmine_lightbox/patches/wiki_helper_patch'

Redmine::Plugin.register :redmine_lightbox do
name 'Redmine Light Box plugin'
author 'G.K.'
author 'G.K., Undev.ru'
description 'Lightbox for attachments'
version '0.0.1'
url 'https://github.com/zipme/redmine_lightbox'
author_url 'https://github.com/zipme/redmine_lightbox'
version '0.0.2'
url 'https://github.com/Undev/redmine_lightbox'
author_url 'https://github.com/Undev/redmine_lightbox'
end
102 changes: 102 additions & 0 deletions lib/redmine_lightbox/patches/application_helper_patch.rb
@@ -0,0 +1,102 @@
require_dependency 'application_helper'

module RedmineLightbox
module Patches
module ApplicationHelperPatch
extend ActiveSupport::Concern

included do
alias_method_chain :link_to_attachment, :preview
end

def link_to_attachment_with_preview(attachment, options = {})
preview_icon = absolute_asset_url('images/preview.png')
icon_style = "width: 18px; margin: 0px 4px"
preview_button = image_tag(preview_icon, :style => icon_style)

unless preview_available?(attachment)
return link_to_attachment_without_preview(attachment, options)
end

download_link = link_to_attachment_without_preview(attachment, :only_path => false)
raw("#{download_link} #{preview_link_with(attachment, preview_button)}")
end

def thumbnail_with_preview_tag(attachment)
preview_link_with(attachment,
image_tag(url_for(:controller => 'attachments', :action => 'thumbnail', :id => attachment)))
end

private

def absolute_asset_url(asset_url, plugin_asset = true)
if plugin_asset
paths = [
'plugin_assets',
'redmine_lightbox',
asset_url
]
relative_url = File.join(*paths)
else
relative_url = asset_url
end

"#{home_url}#{relative_url}"
end

def preview_available?(attachment)
image = attachment.image?
pdf_or_swf = attachment.filename =~ /.(pdf|swf)$/i
attachment_preview = attachment.attachment_preview
text = attachment.is_text?

image || text || pdf_or_swf || attachment_preview
end

def preview_link_with(attachment, preview_button)
if attachment.attachment_preview
link_class = "attachment_preview"
attachment_action = "preview"
else
if attachment.filename =~ /.(pdf|swf)$/i
link_class = $1
else
link_class = "image"
end
attachment_action = ($1 === 'swf' || attachment.image?) ? 'download_inline' : 'show'
end

if attachment.description.present?
attachment_title = "#{attachment.filename}#{ ('-' + attachment.description)}"
else
attachment_title = attachment.filename
end

if attachment.is_text?
link_to(image_tag(absolute_asset_url('images/magnifier.png', false)),
:controller => 'attachments',
:action => 'show',
:id => attachment,
:filename => attachment.filename,
:only_path => false)
else
link_to(preview_button, {
:controller => 'attachments',
:action => attachment_action,
:id => attachment,
:filename => attachment.filename,
:only_path => false },
:class => link_class,
:rel => 'attachments',
:title => attachment_title
)
end
end

end
end
end

unless ApplicationHelper.included_modules.include?(RedmineLightbox::Patches::ApplicationHelperPatch)
ApplicationHelper.send(:include, RedmineLightbox::Patches::ApplicationHelperPatch)
end
49 changes: 25 additions & 24 deletions lib/redmine_lightbox/patches/attachment_patch.rb
Expand Up @@ -3,22 +3,20 @@
module RedmineLightbox
module Patches
module AttachmentPatch
PREVIEW_TRANSFORMATIONS = {
'doc' => 'pdf',
'docx' => 'pdf',
'rtf' => 'pdf'
}

class << self
def included(base)
base.class_eval do
has_one :attachment_preview, :dependent => :destroy

after_save :generate_preview
end
end
extend ActiveSupport::Concern

included do
PREVIEW_TRANSFORMATIONS = {
'doc' => 'pdf',
'docx' => 'pdf',
'rtf' => 'pdf'
}

has_one :attachment_preview, :dependent => :destroy

after_save :generate_preview
end

def try_to_generate_preview
format = preview_format
if format && !attachment_preview
Expand All @@ -29,17 +27,20 @@ def try_to_generate_preview
end

private
def preview_format
attachment_format = filename.rpartition(".")[2].downcase
preview_format = PREVIEW_TRANSFORMATIONS[attachment_format]
end

def generate_preview
try_to_generate_preview
true
end
def preview_format
attachment_format = filename.rpartition(".")[2].downcase
preview_format = PREVIEW_TRANSFORMATIONS[attachment_format]
end

def generate_preview
try_to_generate_preview
true
end
end
end
end

Attachment.send(:include, RedmineLightbox::Patches::AttachmentPatch)
unless Attachment.included_modules.include?(RedmineLightbox::Patches::AttachmentPatch)
Attachment.send(:include, RedmineLightbox::Patches::AttachmentPatch)
end
61 changes: 30 additions & 31 deletions lib/redmine_lightbox/patches/attachments_controller_patch.rb
Expand Up @@ -3,39 +3,38 @@
module RedmineLightbox
module Patches
module AttachmentsControllerPatch
class << self
def included(base)
base.class_eval do
unloadable

before_filter :file_readable, :read_authorize, :only => [:show, :download, :download_inline, :preview, :preview_inline]

def preview
send_preview 'attachment'
end

def preview_inline
send_preview 'inline'
end

def download_inline
send_file @attachment.diskfile, :filename => filename_for_content_disposition(@attachment.filename),
:type => detect_content_type(@attachment),
:disposition => 'inline'
end

private
def send_preview(disposition)
preview = @attachment.attachment_preview
send_file preview.diskfile, :filename => filename_for_content_disposition(preview.filename),
:type => detect_content_type(preview),
:disposition => disposition
end
end
end
extend ActiveSupport::Concern

included do
unloadable

before_filter :file_readable, :read_authorize, :only => [:show, :download, :download_inline, :preview, :preview_inline]
end

def preview
send_preview 'attachment'
end

def preview_inline
send_preview 'inline'
end

def download_inline
send_preview 'inline'
end

private

def send_preview(disposition)
send_file @attachment.diskfile,
:filename => filename_for_content_disposition(@attachment.filename),
:type => detect_content_type(@attachment),
:disposition => disposition
end
end
end
end

AttachmentsController.send(:include, RedmineLightbox::Patches::AttachmentsControllerPatch)
unless AttachmentsController.included_modules.include?(RedmineLightbox::Patches::AttachmentsControllerPatch)
AttachmentsController.send(:include, RedmineLightbox::Patches::AttachmentsControllerPatch)
end
95 changes: 6 additions & 89 deletions lib/redmine_lightbox/patches/issues_helper_patch.rb
Expand Up @@ -3,103 +3,20 @@
module RedmineLightbox
module Patches
module IssuesHelperPatch
def link_to_attachment_with_preview(attachment, options = {})
preview_icon = absolute_asset_url('images/preview.png')
icon_style = "width: 18px; margin: 0px 4px"
preview_button = image_tag(preview_icon, :style => icon_style)
extend ActiveSupport::Concern

unless preview_available?(attachment)
return link_to_attachment_without_preview(attachment, options)
end

download_link = link_to_attachment_without_preview(attachment, :only_path => false)
raw("#{download_link} #{preview_link_with(attachment, preview_button)}")
end

def thumbnail_with_preview_tag(attachment)
preview_link_with(attachment,
image_tag(url_for(:controller => 'attachments', :action => 'thumbnail', :id => attachment)))
included do
alias_method_chain :show_detail, :only_path_disabled
end

def show_detail_with_only_path_disabled(detail, no_html = false, options={})
show_detail_without_only_path_disabled(detail, no_html, options.merge(:only_path => false))
end

private
def absolute_asset_url(asset_url, plugin_asset = true)
if plugin_asset
paths = [
'plugin_assets',
'redmine_lightbox',
asset_url
]
relative_url = File.join(*paths)
else
relative_url = asset_url
end

"#{home_url}#{relative_url}"
end

def preview_available?(attachment)
image = attachment.image?
pdf_or_swf = attachment.filename =~ /.(pdf|swf)$/i
attachment_preview = attachment.attachment_preview
text = attachment.is_text?

image || text || pdf_or_swf || attachment_preview
end

def preview_link_with(attachment, preview_button)
if attachment.attachment_preview
link_class = "attachment_preview"
attachment_action = "preview"
else
if attachment.filename =~ /.(pdf|swf)$/i
link_class = $1
else
link_class = "image"
end
attachment_action = ($1 === 'swf' || attachment.image?) ? 'download_inline' : 'show'
end

if attachment.description.present?
attachment_title = "#{attachment.filename}#{ ('-' + attachment.description)}"
else
attachment_title = attachment.filename
end

if attachment.is_text?
link_to(image_tag(absolute_asset_url('images/magnifier.png', false)),
:controller => 'attachments',
:action => 'show',
:id => attachment,
:filename => attachment.filename,
:only_path => false)
else
link_to(preview_button, {
:controller => 'attachments',
:action => attachment_action,
:id => attachment,
:filename => attachment.filename,
:only_path => false },
:class => link_class,
:rel => 'attachments',
:title => attachment_title
)
end
end

class << self
def included(klass)
klass.class_eval do
alias_method_chain :link_to_attachment, :preview
alias_method_chain :show_detail, :only_path_disabled
end
end
end
end
end
end

IssuesHelper.send(:include, RedmineLightbox::Patches::IssuesHelperPatch)
unless IssuesHelper.included_modules.include?(RedmineLightbox::Patches::IssuesHelperPatch)
IssuesHelper.send(:include, RedmineLightbox::Patches::IssuesHelperPatch)
end
6 changes: 6 additions & 0 deletions lib/redmine_lightbox/patches/wiki_helper_patch.rb
@@ -0,0 +1,6 @@
require_dependency 'application_helper'
require_dependency 'wiki_helper'

unless WikiHelper.included_modules.include?(ApplicationHelper)
WikiHelper.send(:include, ApplicationHelper)
end

0 comments on commit 2a0394d

Please sign in to comment.