Redmine already has classes for work with attachments - model Attachment
, controller AttachmentsController
and attachments views and helpers.
You can use them in your own classes.
Add acts_as_attachable...
with neccesary options to your model class after unloadable
row. Options are:
- Permission options. Eg
:view_permission => :view_attachments_permissions
, where view_attachments_permissions
is standard or plugin permission. If user wants to download attachments, he must have role with that permission, or that permission must be public (only plugin permissions - public option sets in source code).
- Behavior options (actions after adding, removing, etc).
- And perhaps other options.
Add <%= render :partial => 'attachments/form' %>
in your views.
And call save_attachments
method in your controller when it saves your model instance.
Or add attachments manually after instance saved:
params[:attachments].each do |attachment_param|
attachment = Attachment.where('filename = ?', attachment_param[1][:filename]).first
unless attachment.nil?
attachment.container_type = YourModel.name
attachment.container_id = set.id
attachment.save
end
end
Attachments saves immediately after adding, but without container info
Also You can add attachments to existing redmine class via patching.
For example, I patched TimeEntry
class:
require_dependency 'time_entry'
module TimeEntryPatch
def self.included(base) # :nodoc:
base.send(:include, InstanceMethods)
base.class_eval do
unloadable # Send unloadable so it will not be unloaded in development
acts_as_attachable :after_add => :attachment_added, :after_remove => :attachment_removed
end
end
...
You can look examples directly in Redmine code.
Attachments are used by issue
, Project
and some other models.
I found answers for my questions there!
For view attached images You can use plugins like Lightbox 2.
Add plugin to your Redmine or copy its code and stylesheets to your plugin.