3

I am trying to create Redmine plugin and in that plugin i want to upload file or image and also display image or download file on show action. can anyone help me.

In Model

class UserInformation < ActiveRecord::Base
  unloadable
  belongs_to :project

  acts_as_attachable :view_permission => :view_files,
                    :edit_permission => :manage_files,
                    :delete_permission => :manage_files

In Controller

    class UserInformationsController < ApplicationController
      unloadable
      require File.dirname(__FILE__) + '/../../../../app/helpers/attachments_helper'
        include AttachmentsHelper
        helper :attachments

In new.html.erb

        <p> <%= render :partial => 'attachments/form' %></p>

In show.html.erb

    <%= link_to_attachments @info, :thumbnails => true %>

Can you help me is it correct way?

Nikhil Thombare
  • 1,058
  • 2
  • 11
  • 26

2 Answers2

2

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.

General Failure
  • 2,421
  • 4
  • 23
  • 49
  • WOW Thats work for me.now I am able to upload the file but how to download file. i am facing error You are not authorized to access this page. see now i am added model code as per you suggest – Nikhil Thombare Jul 21 '15 at 09:08
  • I added attachments recently, and still have same error too I haven't yet figured out, but most likely it resolved by editing permissions in ```init.rb``` file: ... project_module :your_plugin do permission :permission_name, {:your_model => [:actions_array]}, :public => true end .... look http://www.redmine.org/projects/redmine/wiki/Plugin_Tutorial#Adding-new-permissions – General Failure Jul 21 '15 at 09:18
  • facing same issue after add init permission. – Nikhil Thombare Jul 21 '15 at 09:41
  • I understood! In ```acts_as_attachable``` row you must add permissions that your current role have. I added my public permission acts_as_attachable :view_permission => :attachments_sets, :delete_permission => :attachments_sets and now can download my attachments! In your code sample you must have :view_files permission to download attachments – General Failure Jul 21 '15 at 09:55
  • Where is mention permission I just copied this permission from project model..please help me? – Nikhil Thombare Jul 21 '15 at 10:06
  • If you are use standart permissions (from project), you need not write any more code, set this permissions to some role and give that role to your user in work project If you want your own permission, add it to init.rb, like me: permission :attachments_sets, {:attachments_sets => [:edit]}, :public => true and use it in model: acts_as_attachable :view_permission => :attachments_sets, :delete_permission => :attachments_sets – General Failure Jul 21 '15 at 10:16
0

I've done that in this plugin: https://github.com/iridakos/release_logs. Check the related code.

Lazarus Lazaridis
  • 5,803
  • 2
  • 21
  • 35