usefull_attachment 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +11 -0
- data/Gemfile +23 -0
- data/MIT-LICENSE +20 -0
- data/README.md +42 -0
- data/Rakefile +32 -0
- data/app/controllers/usefull_attachment/links_controller.rb +58 -0
- data/app/helpers/usefull_attachment_helper.rb +80 -0
- data/app/models/usefull_attachment/link.rb +182 -0
- data/app/views/usefull_attachment/links/index.html.erb +30 -0
- data/app/views/usefull_attachment/links/new.html.erb +8 -0
- data/bin/permission.rb +6 -0
- data/config/initializers/paperclip.rb +89 -0
- data/config/locales/custom_errors.it.yml +4 -0
- data/config/routes.rb +9 -0
- data/lib/generators/usefull_attachment/install/install_generator.rb +17 -0
- data/lib/generators/usefull_attachment/install/templates/config/locales/usefull_attachment.it.yml +40 -0
- data/lib/generators/usefull_attachment/migration/migration_generator.rb +29 -0
- data/lib/generators/usefull_attachment/migration/templates/create_migration_template.rb +24 -0
- data/lib/usefull_attachment.rb +9 -0
- data/lib/usefull_attachment/engine.rb +19 -0
- data/lib/usefull_attachment/exceptions.rb +21 -0
- data/lib/usefull_attachment/paperclip.rb +41 -0
- data/lib/usefull_attachment/version.rb +4 -0
- metadata +124 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7c978fba260030fd1217e983f9a0065be5523d67
|
4
|
+
data.tar.gz: 60129f57bf831569c3ad02ac7bd63e3369ea1a33
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 70025cbb96e84d28658dbbe52f95091d85732a99c3974421ec93de26aeeae3816635477107fe5c1695fa248e909e8f0684a214dd32062c6df5b14cf050361ff7
|
7
|
+
data.tar.gz: fd8f65f26ae3e9b30a7f64417286a7c75ac7fd58e295c8d70983e10c6bfc684a9a14b9cecaef767e6fa152f21405af005106b34fe441c42499cc9aeb0ca20c08
|
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
#You can add lacal gems with
|
2
|
+
# gem "my_gem", :path => "/home/www/gems/my_gem"
|
3
|
+
#you must remove the path in production or if you change server and replace with:
|
4
|
+
# git "git@10.2.252.240:my_gem.git"
|
5
|
+
|
6
|
+
source "http://rubygems.org"
|
7
|
+
|
8
|
+
gem "rails", "~>3.0.15"
|
9
|
+
gem "acts_as_monitor", :path => "/home/www/gems/acts_as_monitor"
|
10
|
+
gem "paperclip"
|
11
|
+
gem "usefull_filter", :path => "/home/www/gems/usefull_filter"
|
12
|
+
gem "usefull_table", :path => "/home/www/gems/usefull_table"
|
13
|
+
gem "will_paginate"
|
14
|
+
|
15
|
+
group :development, :test do
|
16
|
+
gem "ruby-debug"
|
17
|
+
gem "capybara", ">= 0.4.0"
|
18
|
+
gem "sqlite3"
|
19
|
+
gem "single_test"
|
20
|
+
end
|
21
|
+
|
22
|
+
gemspec
|
23
|
+
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2012 YOURNAME
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
=UsefullAttachment
|
2
|
+
|
3
|
+
A very beta version for internal use only... try at your own risk!
|
4
|
+
|
5
|
+
==Setup
|
6
|
+
Add this line to your application's Gemfile:
|
7
|
+
gem 'usefull_attachment'
|
8
|
+
|
9
|
+
then execute
|
10
|
+
$ bundle install
|
11
|
+
|
12
|
+
or install it yourself as:
|
13
|
+
$ sudo gem install usefull_attachment
|
14
|
+
|
15
|
+
install addons
|
16
|
+
$ rails g usefull_attachment:install
|
17
|
+
|
18
|
+
==Usage
|
19
|
+
In your controller
|
20
|
+
#app/controllers/home_controller.rb
|
21
|
+
def index
|
22
|
+
...
|
23
|
+
end
|
24
|
+
|
25
|
+
In your view
|
26
|
+
#app/views/home/index.html.erb
|
27
|
+
...
|
28
|
+
|
29
|
+
==Localization
|
30
|
+
|
31
|
+
|
32
|
+
==Contributing
|
33
|
+
|
34
|
+
1. Fork it
|
35
|
+
2. Create your feature branch (git checkout -b my-new-feature)
|
36
|
+
3. Commit your changes (git commit -am 'Added some feature')
|
37
|
+
4. Push to the branch (git push origin my-new-feature)
|
38
|
+
5. Create new Pull Request
|
39
|
+
|
40
|
+
==Thanks
|
41
|
+
|
42
|
+
...
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
require 'rubygems'
|
3
|
+
begin
|
4
|
+
require 'bundler/setup'
|
5
|
+
require "bundler/gem_tasks"
|
6
|
+
rescue LoadError
|
7
|
+
puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
|
8
|
+
end
|
9
|
+
|
10
|
+
require 'rake'
|
11
|
+
require 'rake/testtask'
|
12
|
+
require 'rdoc/task'
|
13
|
+
|
14
|
+
|
15
|
+
|
16
|
+
Rake::TestTask.new(:test) do |t|
|
17
|
+
t.libs << 'lib'
|
18
|
+
t.libs << 'test'
|
19
|
+
t.pattern = 'test/**/*_test.rb'
|
20
|
+
t.verbose = false
|
21
|
+
end
|
22
|
+
|
23
|
+
task :default => :test
|
24
|
+
|
25
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
26
|
+
rdoc.rdoc_dir = 'rdoc'
|
27
|
+
rdoc.title = 'usefull_attachment'
|
28
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
29
|
+
rdoc.rdoc_files.include('README.rdoc')
|
30
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
31
|
+
end
|
32
|
+
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module UsefullAttachment
|
2
|
+
class LinksController < ApplicationController
|
3
|
+
def index
|
4
|
+
@search = Link.search(params[:search])
|
5
|
+
@attachments = @search.paginate(:page => params[:page])
|
6
|
+
end
|
7
|
+
|
8
|
+
def new
|
9
|
+
@attachment = Link.new
|
10
|
+
#UserSession.log("Admin::AttachmentController#new params=#{params[:admin_attachment].inspect}, @attachment=#{@attachment.inspect}")
|
11
|
+
end
|
12
|
+
|
13
|
+
def create
|
14
|
+
##UserSession.log("Admin::AttachmentController#create params=#{params[:admin_attachment].inspect}")
|
15
|
+
user = current_user ? {:created_by => current_user.id, :updated_by => current_user.id} : {}
|
16
|
+
Attachment.create(params[:usefull_attachment_attachment].deep_merge!(user)) if params[:usefull_attachment_attachment].present?
|
17
|
+
Avatar.create(params[:usefull_attachment_avatar].deep_merge!(user)) if params[:usefull_attachment_avatar].present?
|
18
|
+
Link.create(params[:usefull_attachment_link].deep_merge!(user)) if params[:usefull_attachment_link].present?
|
19
|
+
redirect_to :action => "index", :notice => t("created")
|
20
|
+
rescue => e
|
21
|
+
flash[:alert] = t("not_created", :message => e.message)
|
22
|
+
redirect_to :back
|
23
|
+
end
|
24
|
+
|
25
|
+
def download
|
26
|
+
@attachment = Link.find(params[:id])
|
27
|
+
respond_to do |format|
|
28
|
+
format.html do
|
29
|
+
raise FileMissing.new(:file => @attachment.link.path) unless File.exist?(@attachment.link.path)
|
30
|
+
send_file @attachment.link.path, :type => @attachment.link_content_type
|
31
|
+
end
|
32
|
+
end
|
33
|
+
rescue => e
|
34
|
+
flash[:alert] = t("not_downloaded", :message => e.message)
|
35
|
+
redirect_to :back
|
36
|
+
end
|
37
|
+
|
38
|
+
def destroy
|
39
|
+
@attachment = Link.find(params[:id])
|
40
|
+
@attachment.destroy
|
41
|
+
|
42
|
+
respond_to do |format|
|
43
|
+
format.html do
|
44
|
+
redirect_to :back, :notice => t("deleted")
|
45
|
+
end
|
46
|
+
format.xml { head :ok }
|
47
|
+
end
|
48
|
+
|
49
|
+
rescue => e
|
50
|
+
flash[:alert] = t("not_deleted", :message => e.message)
|
51
|
+
redirect_to :back
|
52
|
+
end
|
53
|
+
|
54
|
+
end
|
55
|
+
|
56
|
+
|
57
|
+
|
58
|
+
end
|
@@ -0,0 +1,80 @@
|
|
1
|
+
module UsefullAttachmentHelper
|
2
|
+
|
3
|
+
# Il builder per allegare un file all'interno di un form � definito in usefull_helpers/lib/UsefullHelpers::WgFormBuilder
|
4
|
+
# e si chiama #attachment_field. Non crea un ulteriore submit button, chiaramente.
|
5
|
+
|
6
|
+
# Creates a button to add new file to attachment system
|
7
|
+
# It can't be used inside a form.
|
8
|
+
def new_attachment_for(object, description = true)
|
9
|
+
#ToDo check the relation type and assure is UsefullAttachment::Link
|
10
|
+
if object.respond_to?(:attachments)
|
11
|
+
form_for object.attachments.new, :html => {:multipart => true}, :url => usefull_attachment_links_path do |f|
|
12
|
+
concat(f.hidden_field :attachmentable_type)
|
13
|
+
concat(f.hidden_field :attachmentable_id)
|
14
|
+
concat(f.file_field :link)
|
15
|
+
concat(f.text_field :description) if description
|
16
|
+
concat(f.submit)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def new_avatar_for(object)
|
22
|
+
if object.respond_to?(:avatar)
|
23
|
+
if object.avatar.blank?
|
24
|
+
form_for object.build_avatar, :html => {:multipart => true}, :url => usefull_attachment_links_path do |f|
|
25
|
+
concat(f.hidden_field :attachmentable_type)
|
26
|
+
concat(f.hidden_field :attachmentable_id)
|
27
|
+
concat(f.file_field :link)
|
28
|
+
concat(f.submit 'Avatar')
|
29
|
+
end
|
30
|
+
else
|
31
|
+
link_to 'Delete', usefull_attachment_link_path(object.avatar.id), :method => :delete, :confirm => " Are you sure you want to delete this avatar?"
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
#==Options
|
37
|
+
#
|
38
|
+
#- :full => true # visualizza tutte le opzioni (comprese link_file_size e link_file_updated_at)
|
39
|
+
#- :hide_delete => conditions # se conditions == true nasconde il delete button
|
40
|
+
def list_attachments_for(object, *args)
|
41
|
+
options = args.extract_options!
|
42
|
+
if object.respond_to?(:attachments) && object.attachments.present?
|
43
|
+
table_for object.attachments.where(:id.ne => nil), :export => {:visible => false} do |t|
|
44
|
+
t.monitor
|
45
|
+
t.download :url => Proc.new {|object| download_usefull_attachment_link_path(object.id)}
|
46
|
+
t.destroy(:url => Proc.new {|object| usefull_attachment_link_path(object.id)}) unless options[:hide_delete] == true
|
47
|
+
t.col :link_file_name
|
48
|
+
t.col :description
|
49
|
+
t.col :link_file_size if options[:full]
|
50
|
+
t.col :link_file_updated_at if options[:full]
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
|
56
|
+
#Create a table to show attachments
|
57
|
+
# def list_attachments_for_old(object, full = false)
|
58
|
+
# if object.respond_to?(:attachments) && object.attachments.present?
|
59
|
+
# table_for object.attachments.where(:id.ne => nil), :export => {:visible => false} do |t|
|
60
|
+
# t.monitor
|
61
|
+
# t.download :url => Proc.new {|object| download_usefull_attachment_link_path(object.id)}
|
62
|
+
# t.destroy :url => Proc.new {|object| usefull_attachment_link_path(object.id)}
|
63
|
+
# t.col :link_file_name
|
64
|
+
# t.col :description
|
65
|
+
# t.col :link_file_size if full
|
66
|
+
# t.col :link_file_updated_at if full
|
67
|
+
# end
|
68
|
+
# end
|
69
|
+
# end
|
70
|
+
|
71
|
+
#Draw images marked as :avatar
|
72
|
+
def show_avatar_for(object, *args)
|
73
|
+
if object.respond_to?(:avatar) && object.avatar.present?
|
74
|
+
image_tag(object.avatar.link.url, :size => "200x200")
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
|
79
|
+
end
|
80
|
+
|
@@ -0,0 +1,182 @@
|
|
1
|
+
#Gestisce gli allegati dei documenti
|
2
|
+
#
|
3
|
+
#Salva i documenti in /mnt/Webgatec/<namespace>/<class>/id/file_name
|
4
|
+
# Per implementare nuove tipologie di attachments occorre ereditare da questa
|
5
|
+
# classe e ridefinire all'occorrenza i metodi get_path e get_url (privati)
|
6
|
+
# se occorre modificare gli styles e i processors occorre ridefinire anche
|
7
|
+
# has_attached_file nella classe figlia
|
8
|
+
#==Descrizione Campi
|
9
|
+
#- type => Nome della sottoclasse di Link che ha generato l'allegato
|
10
|
+
#- attachmentable_type => classe a cui collegato l'allegato
|
11
|
+
#- attachmentable_id => id della classa a cui collegato l'allegato
|
12
|
+
#
|
13
|
+
#N.B. Vengono tutti popolati in automatico da Rails
|
14
|
+
#
|
15
|
+
module UsefullAttachment
|
16
|
+
class Link < ActiveRecord::Base
|
17
|
+
|
18
|
+
acts_as_monitor
|
19
|
+
|
20
|
+
#before_validation :check
|
21
|
+
before_save :fill
|
22
|
+
|
23
|
+
#ToDo aggiungere il cambio permessi al postprocessore di default, che fa il ridemsionamento immagine
|
24
|
+
has_attached_file :link,
|
25
|
+
#:path => "/mnt/WebGatec/:type/:type_id/:filename",
|
26
|
+
:path => :get_path,
|
27
|
+
:url => :get_url,
|
28
|
+
#Nota: le options di :styles devono essere String altrimenti si pianta tutto
|
29
|
+
# quando si eredita da Link occorre specificare nuovi styles e processori ad hoc
|
30
|
+
:styles => {:import => "true"},
|
31
|
+
:processors => [:file_processor]
|
32
|
+
|
33
|
+
#validates_attachment_presence :link
|
34
|
+
#validates :link, :attachment_presence => true
|
35
|
+
validates :link_file_name, :presence => true
|
36
|
+
|
37
|
+
belongs_to :attachmentable, :polymorphic => true
|
38
|
+
|
39
|
+
# ==CLASS Methods
|
40
|
+
class<<self
|
41
|
+
# importa allegati attraversando rami di directory
|
42
|
+
# ogni path fatto cos: /mnt/WebGatec/namespace/model/id/filename.ext
|
43
|
+
#
|
44
|
+
def folders_to_records
|
45
|
+
Dir["/mnt/WebGatec/**/*"].each do |file|
|
46
|
+
if File.file?(file)
|
47
|
+
file_path_name = file.split("/")
|
48
|
+
file_name = file_path_name.pop
|
49
|
+
id = file_path_name.pop
|
50
|
+
modello = file_path_name.pop
|
51
|
+
name_space = file_path_name.pop
|
52
|
+
end
|
53
|
+
|
54
|
+
if is_num?(id) && !id.blank?
|
55
|
+
# try to build new record
|
56
|
+
# puts name_space + '::' + modello
|
57
|
+
begin
|
58
|
+
full_class_name = name_space.camelize + '::' + modello.camelize if !name_space.blank?
|
59
|
+
if class_defined?(full_class_name)
|
60
|
+
self.find_or_create_by_type_and_attachmentable_id_and_attachmentable_type(:description => 'prova',
|
61
|
+
:link_file_size => file.size,
|
62
|
+
:link_content_type => MIME::Types.type_for(file).first.content_type,
|
63
|
+
:link_file_name => File.basename(file),
|
64
|
+
:type => get_attachment_type(full_class_name),
|
65
|
+
:attachmentable_id => id,
|
66
|
+
:attachmentable_type => full_class_name)
|
67
|
+
end
|
68
|
+
puts "OK => #{name_space} #{modello} #{id} #{file_name}"
|
69
|
+
puts get_attachment_type(full_class_name)
|
70
|
+
rescue
|
71
|
+
puts "KO => #{name_space} #{modello} #{id} #{file_name}"
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
#Start Private Class Methods
|
78
|
+
private
|
79
|
+
|
80
|
+
def class_defined?(s)
|
81
|
+
begin
|
82
|
+
eval s.to_s
|
83
|
+
true
|
84
|
+
rescue NameError
|
85
|
+
false
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
def is_num?(str)
|
90
|
+
begin
|
91
|
+
!!Integer(str)
|
92
|
+
rescue ArgumentError, TypeError
|
93
|
+
false
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def get_attachment_type(attach_type)
|
98
|
+
case attach_type
|
99
|
+
when 'Magazzino::Document'
|
100
|
+
return 'Documenti::Attachment'
|
101
|
+
when 'Magazzino::Limbo'
|
102
|
+
return 'Documenti::Attachment'
|
103
|
+
when 'Utility::PhoneQueue'
|
104
|
+
return 'Utility::PhoneQueueAttach'
|
105
|
+
when 'Interventi::Activity'
|
106
|
+
return 'Interventi::Attachment'
|
107
|
+
when 'Interventi::Import'
|
108
|
+
return 'Interventi::Attachment'
|
109
|
+
when 'Consuntivazioni::BillingCheck'
|
110
|
+
return 'Admin::Attachment'
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
#Importa il file nel modello dichiarato vedi Spreadsheet::Workbook#to_record
|
117
|
+
#* :model => passa la parent class della tabella per ha sempre la precedenza il nome del foglio di lavoro se un modello valido
|
118
|
+
#
|
119
|
+
#Nel caso il modello (attachmentable_type) sia anche una relazione procede ad inserire i record direttamente. (Vedi PhoneQueueAttach)
|
120
|
+
#==Note
|
121
|
+
#Passa a to_xls solo i mime-type compatibili
|
122
|
+
def to_record(*args)
|
123
|
+
if !link.blank? && self.link_content_type == "application/vnd.ms-excel"
|
124
|
+
options = args.extract_options!
|
125
|
+
options[:model] ||= self.attachmentable_type unless self.attachmentable_type == self.class.name
|
126
|
+
out = self.link.to_record(*(args << options))
|
127
|
+
#Verifico se il modello una collection, in caso affermativo lo inserisco automaticamente
|
128
|
+
association = find_association(options[:model])
|
129
|
+
return self.send(association.to_s + "=", out[options[:model]]) if association
|
130
|
+
return out
|
131
|
+
else
|
132
|
+
{}
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def rename(new)
|
137
|
+
puts("rename new=#{new}")
|
138
|
+
unless self.link_file_name.nil?
|
139
|
+
path = self.link.path.split("/")
|
140
|
+
path.pop
|
141
|
+
File.rename(self.link.path, path.push(new).join("/")) if File.exist?(self.link.path)
|
142
|
+
self.link_file_name = new
|
143
|
+
end
|
144
|
+
end
|
145
|
+
|
146
|
+
def url
|
147
|
+
link.url
|
148
|
+
end
|
149
|
+
|
150
|
+
#Rename file after save if attachmentable provide an attachment_name method
|
151
|
+
def fill
|
152
|
+
to_call_name = "#{self.class.name.underscore.split("/").pop}_name"
|
153
|
+
#mylog("fill respond_to? = #{attachmentable.respond_to?(to_call_name, true)}")
|
154
|
+
rename(self.attachmentable.send(to_call_name, self.link_file_name, self.description)) if self.attachmentable.respond_to?(to_call_name, true)
|
155
|
+
end
|
156
|
+
|
157
|
+
# Controllo che il file che sto caricando non sia gi� stato caricato.
|
158
|
+
def check
|
159
|
+
raise UsefullAttachment::Links::FileDuplicate.new(:file => self.link_file_name) if File.exists?(self.link.path)
|
160
|
+
end
|
161
|
+
|
162
|
+
def get_path
|
163
|
+
"/mnt/WebGatec/:type/:type_id/:base_name.:extension"
|
164
|
+
end
|
165
|
+
|
166
|
+
def get_url
|
167
|
+
get_path
|
168
|
+
end
|
169
|
+
|
170
|
+
def error_missing?
|
171
|
+
!File.exists?(self.link.path)
|
172
|
+
end
|
173
|
+
|
174
|
+
#Gli viene passato un modello e restituisce il nome dell'association relativa se esiste
|
175
|
+
def find_association(model)
|
176
|
+
association = nil
|
177
|
+
self.class.reflections.each {|k,v| association = k if v.class_name == model}
|
178
|
+
association
|
179
|
+
end
|
180
|
+
|
181
|
+
end
|
182
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
<%= filter_for @search, :html => {:class => 'usefull_filter_container span-24'}, :url => usefull_attachment_links_path do |f| %>
|
2
|
+
<%= f.text_field :attachmentable_type %>
|
3
|
+
<%= f.text_field :attachmentable_id %>
|
4
|
+
<%= f.text_field :type %>
|
5
|
+
<%#= f.text_field :typology %>
|
6
|
+
<%= f.text_field :link_content_type %>
|
7
|
+
<%= f.text_field :link_file_name %>
|
8
|
+
<div class="span-22">
|
9
|
+
<%= f.submit %>
|
10
|
+
<% end %>
|
11
|
+
|
12
|
+
<%=table_for(@attachments,@search) do |t| %>
|
13
|
+
<% t.monitor %>
|
14
|
+
<% t.download :url => Proc.new {|object| download_usefull_attachment_link_path(object)} %>
|
15
|
+
<% t.destroy :url => Proc.new {|object| usefull_attachment_link_path(object)}, :link_options => {:confirm => 'Are you sure?', :method => :delete} %>
|
16
|
+
<% t.col :id %>
|
17
|
+
<% t.col :attachmentable_type %>
|
18
|
+
<% t.col :attachmentable_id %>
|
19
|
+
<% t.col :type %>
|
20
|
+
<% t.col :typology %>
|
21
|
+
<% t.col :description %>
|
22
|
+
<% t.col :link_file_name %>
|
23
|
+
<% t.col :link_file_size %>
|
24
|
+
<% t.col :updated_at, :data_type => :DateTime %>
|
25
|
+
<% t.col :link_content_type %>
|
26
|
+
<% t.col "createdby_displayname" %>
|
27
|
+
<% t.col "updatedby_displayname" %>
|
28
|
+
<% end %>
|
29
|
+
|
30
|
+
<%= link_to "New", new_usefull_attachment_link_path %>
|
@@ -0,0 +1,8 @@
|
|
1
|
+
<%= form_for @attachment, :url => usefull_attachment_links_path, :html => { :multipart => true } do |form| %>
|
2
|
+
Parent Class<%= form.text_field :attachmentable_type %>
|
3
|
+
Type<%= form.text_field :type %>
|
4
|
+
Description <%= form.text_field :description %>
|
5
|
+
file <%= form.file_field :link %>
|
6
|
+
Typology <%= form.text_field :typology %>
|
7
|
+
<%= form.submit %>
|
8
|
+
<% end %>
|
data/bin/permission.rb
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
#
|
3
|
+
#Cambia i permessi alle cartelle utilizzate da paperclip per evitare che file creati da console
|
4
|
+
#abbiano le permission root e non siano gestibili quindi dal web
|
5
|
+
enviroment = ARGV[0]
|
6
|
+
puts( `sudo chown git:git /mnt/WebGatec/ -R` ) if enviroment == "development" && `whoami`.strip == "root"
|
@@ -0,0 +1,89 @@
|
|
1
|
+
#require 'lib/paperclip_processors/excel_processor.rb'
|
2
|
+
|
3
|
+
#Paperclip Initializer
|
4
|
+
|
5
|
+
#Percorso di Convert
|
6
|
+
Paperclip.options[:command_path] = "/usr/bin/convert"
|
7
|
+
Paperclip.options[:whiny] = true
|
8
|
+
|
9
|
+
#Definisco dei simboli da utilizzare nel percorso del file
|
10
|
+
Paperclip.interpolates :type do |attachment, style|
|
11
|
+
#attachment.instance.attachmentable_type.blank? ? "default" : attachment.instance.attachmentable_type.underscore
|
12
|
+
attachment.instance.attachmentable_type.blank? ? attachment.instance.class.name.underscore : attachment.instance.attachmentable_type.underscore
|
13
|
+
end
|
14
|
+
|
15
|
+
Paperclip.interpolates :type_id do |attachment, style|
|
16
|
+
attachment.instance.attachmentable_id.blank? ? attachment.instance.id : attachment.instance.attachmentable_id
|
17
|
+
end
|
18
|
+
|
19
|
+
Paperclip.interpolates :base_name do |attachment, style|
|
20
|
+
fn = attachment.instance.link_file_name.split(".")
|
21
|
+
fn.pop
|
22
|
+
fn.join(".")
|
23
|
+
end
|
24
|
+
|
25
|
+
Paperclip.interpolates :extension do |attachment, style|
|
26
|
+
attachment.instance.link_file_name.split(".").pop
|
27
|
+
end
|
28
|
+
|
29
|
+
# Paperclip.configure do |c|
|
30
|
+
# c.register_processor :excel, Paperclip::ExcelProcessor
|
31
|
+
# end
|
32
|
+
|
33
|
+
module Paperclip
|
34
|
+
class Attachment
|
35
|
+
#Se uploaded_file � valorizzato (attachemtn non salvato) usa quello,
|
36
|
+
#altrimenti carica il file dal disco
|
37
|
+
def to_record(*args)
|
38
|
+
self.uploaded_file.blank? ? File.open(self.path).to_record(*args) : self.uploaded_file.to_record(*args)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
class FileProcessor < Processor
|
43
|
+
|
44
|
+
def initialize file, options = {}, attachment = nil
|
45
|
+
@file = file
|
46
|
+
@options = options
|
47
|
+
@instance = attachment.instance
|
48
|
+
@styles = attachment.options[:styles]
|
49
|
+
@whiny = options[:whiny].nil? ? true : options[:whiny]
|
50
|
+
#Rails::logger.info("Paperclip::ExcelProcessor#initialize options=#{options.inspect}, attachment=#{attachment.inspect}")
|
51
|
+
#Rails::logger.info("Paperclip::ExcelProcessor#initialize STYLES=#{@styles.inspect}, INSTANCE=#{@instance.inspect}")
|
52
|
+
#UserSession.log("Paperclip#make @attachment=#{attachment.inspect}")
|
53
|
+
end
|
54
|
+
|
55
|
+
def make
|
56
|
+
begin
|
57
|
+
#UserSession.log("Paperclip#make @options=#{@options.inspect}")
|
58
|
+
#UserSession.log("Paperclip#make @file=#{@file.inspect}")
|
59
|
+
#UserSession.log("Paperclip#make @instance=#{@instance.inspect}")
|
60
|
+
Rails::logger.info("Paperclip user: #{`whoami`}")
|
61
|
+
Rails::logger.info("Paperclip file: #{file.inspect}")
|
62
|
+
file = File.join(UsefullAttachment::Engine.root, "bin/permission.rb")
|
63
|
+
status = `#{file}`
|
64
|
+
#Rails::logger.info("Paperclip::ExcelProcessor#make user=#{`whoami`.strip}")
|
65
|
+
#UserSession.log("Paperclip#make status=#{status.inspect}")
|
66
|
+
# # new record, set contents attribute by reading the attachment file
|
67
|
+
# if(@instance.new_record?)
|
68
|
+
# @file.rewind # move pointer back to start of file in case handled by other processors
|
69
|
+
# file_content = File.read(@file.path)
|
70
|
+
# @instance.send("#{@options[:contents]}=", file_content)
|
71
|
+
# else
|
72
|
+
# # existing record, set contents by reading contents attribute
|
73
|
+
# file_content = @instance.send(@options[:contents])
|
74
|
+
# # create new file with contents from model
|
75
|
+
# tmp = Tempfile.new([@basename, @current_format].compact.join("."))
|
76
|
+
# tmp << file_content
|
77
|
+
# tmp.flush
|
78
|
+
# @file = tmp
|
79
|
+
#end
|
80
|
+
|
81
|
+
@file
|
82
|
+
rescue StandardError => e
|
83
|
+
raise PaperclipError, "There was an error processing the file contents for #{@basename} - #{e}" if @whiny
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
data/config/routes.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
module UsefullAttachment
|
3
|
+
class InstallGenerator < Rails::Generators::Base
|
4
|
+
desc "Install generator for UsefullAttachment gem"
|
5
|
+
source_root File.expand_path("../templates", __FILE__)
|
6
|
+
|
7
|
+
def copy_config
|
8
|
+
directory "."
|
9
|
+
end
|
10
|
+
|
11
|
+
#def copy_public
|
12
|
+
# directory "public"
|
13
|
+
#end
|
14
|
+
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
data/lib/generators/usefull_attachment/install/templates/config/locales/usefull_attachment.it.yml
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
#Will Paginate gem Locales
|
2
|
+
|
3
|
+
it:
|
4
|
+
will_paginate:
|
5
|
+
previous_label: Precedente
|
6
|
+
next_label: Successiva
|
7
|
+
page_gap: "..."
|
8
|
+
page_entries_info:
|
9
|
+
single_page:
|
10
|
+
zero: "Nessun %{model} trovato"
|
11
|
+
one: "Visualizza 1 %{model}"
|
12
|
+
other: "Visualizza tutti e %{count} gli %{model}"
|
13
|
+
single_page_html:
|
14
|
+
zero: "Nessun %{model} trovato"
|
15
|
+
one: "Visualizza 1 %{model}"
|
16
|
+
other: "Visualizza tutti e %{count} gli %{model}"
|
17
|
+
#multi_page: "Displaying %{plural} %{from} - %{to} of %{total} in total"
|
18
|
+
multi_page: "Visualizza %{model} dal %{from} al %{to} di %{count} "
|
19
|
+
#multi_page_html: "Displaying %{plural} <b class='range'>%{from} - %{to}</b> of <b class='total'>%{total}</b> in total"
|
20
|
+
multi_page_html: "Visualizza %{model} dal %{from} al %{to} di %{count} "
|
21
|
+
|
22
|
+
activerecord:
|
23
|
+
models:
|
24
|
+
usefull_attachment:
|
25
|
+
link:
|
26
|
+
one: Allegato
|
27
|
+
other: Allegati
|
28
|
+
attributes:
|
29
|
+
usefull_attachment:
|
30
|
+
link:
|
31
|
+
attachmentable_type: "Classe Proprietaria"
|
32
|
+
attachmentable_id: "Id Proprietario"
|
33
|
+
type: "Tipo"
|
34
|
+
description: "Descrizione"
|
35
|
+
file_file_name: "File"
|
36
|
+
file_file_size: "Kb"
|
37
|
+
updated_at: "Modificato"
|
38
|
+
file_content_type: "Mime-Type"
|
39
|
+
file_updated_at: "Modificato"
|
40
|
+
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rails/generators'
|
2
|
+
require 'rails/generators/migration'
|
3
|
+
|
4
|
+
module UsefullAttachment
|
5
|
+
class MigrationGenerator < Rails::Generators::Base
|
6
|
+
include Rails::Generators::Migration
|
7
|
+
desc "Migration generator for UsefullAttachment gem"
|
8
|
+
source_root File.expand_path("../templates", __FILE__)
|
9
|
+
|
10
|
+
def self.next_migration_number(path)
|
11
|
+
unless @prev_migration_nr
|
12
|
+
@prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
|
13
|
+
else
|
14
|
+
@prev_migration_nr += 1
|
15
|
+
end
|
16
|
+
@prev_migration_nr.to_s
|
17
|
+
end
|
18
|
+
|
19
|
+
|
20
|
+
def copy_migration
|
21
|
+
migration_template "create_migration_template.rb", "db/migrate/usefull_attachment_create_links.rb"
|
22
|
+
end
|
23
|
+
|
24
|
+
#def copy_public
|
25
|
+
# directory "public"
|
26
|
+
#end
|
27
|
+
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
class UsefullAttachmentCreateLinks < ActiveRecord::Migration
|
2
|
+
def self.up
|
3
|
+
create_table :usefull_attachment_links do |t|
|
4
|
+
|
5
|
+
t.string :description
|
6
|
+
t.string :typology
|
7
|
+
|
8
|
+
t.has_attached_file :link
|
9
|
+
|
10
|
+
t.integer :created_by
|
11
|
+
t.integer :updated_by
|
12
|
+
t.timestamps
|
13
|
+
t.string :type
|
14
|
+
t.integer :attachmentable_id
|
15
|
+
t.string :attachmentable_type, :limit => 100, :null => false
|
16
|
+
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.down
|
21
|
+
drop_table :usefull_attachment_links
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
module UsefullAttachment
|
2
|
+
class Engine < Rails::Engine
|
3
|
+
|
4
|
+
config.autoload_paths += Dir["#{config.root}/lib/**/"]
|
5
|
+
|
6
|
+
initializer 'usefull_attachment.helper' do |app|
|
7
|
+
ActiveSupport.on_load(:action_controller) do
|
8
|
+
include UsefullAttachmentHelper
|
9
|
+
end
|
10
|
+
ActiveSupport.on_load(:action_view) do
|
11
|
+
include UsefullAttachmentHelper
|
12
|
+
end
|
13
|
+
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
#Add ere require to specific file or gem used
|
@@ -0,0 +1,21 @@
|
|
1
|
+
#Localize messages in it.CustomError.MyError
|
2
|
+
module UsefullAttachment
|
3
|
+
|
4
|
+
class CustomError < StandardError
|
5
|
+
def initialize(*args)
|
6
|
+
@options = args.extract_options!
|
7
|
+
super
|
8
|
+
end
|
9
|
+
|
10
|
+
def message
|
11
|
+
@options.merge!({:default => "Error : #{@options.inspect}"})
|
12
|
+
I18n.t("#{self.class.name.gsub(/::/,'.')}", @options )
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
module Links
|
17
|
+
class FileDuplicate < CustomError ; end
|
18
|
+
#class MyError < CustomError; end
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
require "paperclip"
|
2
|
+
module Paperclip
|
3
|
+
module Storage
|
4
|
+
module Filesystem
|
5
|
+
|
6
|
+
# Patch alla cancellazione di Paperclip che tenta di cancellare tutto quello che pu� finch� non fallisce la cancellazione
|
7
|
+
# a livello di sistema operativo, tale modalit� di cancellazione per� crea problemi con le share su Alfresco montate
|
8
|
+
# tramite CIFS.
|
9
|
+
def flush_deletes #:nodoc:
|
10
|
+
@queued_for_delete.each do |path|
|
11
|
+
begin
|
12
|
+
log("deleting #{path}")
|
13
|
+
FileUtils.rm(path) if File.exist?(path)
|
14
|
+
rescue Errno::ENOENT => e
|
15
|
+
# ignore file-not-found, let everything else pass
|
16
|
+
end
|
17
|
+
begin
|
18
|
+
# Per controllare se la cartella � vuota verifico il numero di elementi al suo interno e gli tolgo i 2 file "di
|
19
|
+
# servizio" di Alfresco, ossia "__Forza-Versione.exe" e "__Mostra-Dettagli.exe" presenti in ogni cartella esposta
|
20
|
+
# da Alfresco tramite CIFS. Gli tolgo inoltre le/i cartelle/riferimenti del sistema operativo "." e "..". Se il
|
21
|
+
# numero che cos� ottengo � minore o uguale a 0 significa che la cartella era vuota: il numero potrebbe essere
|
22
|
+
# negativo perch� WebGATeC non � sempre montato su Alfresco, per cui a volte mi trovo a togliere anche 2 elementi
|
23
|
+
# che in realt� non c'erano e che quindi non serviva togliere.
|
24
|
+
tmp_dir = File.dirname(path)
|
25
|
+
while( Dir.entries(tmp_dir).size - 2 - 2 <= 0 )
|
26
|
+
FileUtils.rmdir(tmp_dir)
|
27
|
+
tmp_dir = File.dirname(tmp_dir)
|
28
|
+
end
|
29
|
+
rescue Errno::EEXIST, Errno::ENOTEMPTY, Errno::ENOENT, Errno::EINVAL, Errno::ENOTDIR, Errno::EACCES
|
30
|
+
# Stop trying to remove parent directories
|
31
|
+
rescue SystemCallError => e
|
32
|
+
log("There was an unexpected error while deleting directories: #{e.class}")
|
33
|
+
# Ignore it
|
34
|
+
end
|
35
|
+
end
|
36
|
+
@queued_for_delete = []
|
37
|
+
end
|
38
|
+
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: usefull_attachment
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andrea Bignozzi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2013-10-24 00:00:00 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
type: :runtime
|
16
|
+
version_requirements: &id001 !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ~>
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: 3.0.15
|
21
|
+
name: rails
|
22
|
+
prerelease: false
|
23
|
+
requirement: *id001
|
24
|
+
- !ruby/object:Gem::Dependency
|
25
|
+
type: :runtime
|
26
|
+
version_requirements: &id002 !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- &id003
|
29
|
+
- ">="
|
30
|
+
- !ruby/object:Gem::Version
|
31
|
+
version: "0"
|
32
|
+
name: acts_as_monitor
|
33
|
+
prerelease: false
|
34
|
+
requirement: *id002
|
35
|
+
- !ruby/object:Gem::Dependency
|
36
|
+
type: :runtime
|
37
|
+
version_requirements: &id004 !ruby/object:Gem::Requirement
|
38
|
+
requirements:
|
39
|
+
- *id003
|
40
|
+
name: paperclip
|
41
|
+
prerelease: false
|
42
|
+
requirement: *id004
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
type: :runtime
|
45
|
+
version_requirements: &id005 !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- *id003
|
48
|
+
name: usefull_filter
|
49
|
+
prerelease: false
|
50
|
+
requirement: *id005
|
51
|
+
- !ruby/object:Gem::Dependency
|
52
|
+
type: :runtime
|
53
|
+
version_requirements: &id006 !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- *id003
|
56
|
+
name: usefull_table
|
57
|
+
prerelease: false
|
58
|
+
requirement: *id006
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
type: :runtime
|
61
|
+
version_requirements: &id007 !ruby/object:Gem::Requirement
|
62
|
+
requirements:
|
63
|
+
- *id003
|
64
|
+
name: will_paginate
|
65
|
+
prerelease: false
|
66
|
+
requirement: *id007
|
67
|
+
description: Describe Gem Here
|
68
|
+
email:
|
69
|
+
- skylord73@gmail.com
|
70
|
+
executables:
|
71
|
+
- permission.rb
|
72
|
+
extensions: []
|
73
|
+
|
74
|
+
extra_rdoc_files: []
|
75
|
+
|
76
|
+
files:
|
77
|
+
- app/controllers/usefull_attachment/links_controller.rb
|
78
|
+
- app/helpers/usefull_attachment_helper.rb
|
79
|
+
- app/models/usefull_attachment/link.rb
|
80
|
+
- app/views/usefull_attachment/links/index.html.erb
|
81
|
+
- app/views/usefull_attachment/links/new.html.erb
|
82
|
+
- lib/generators/usefull_attachment/install/install_generator.rb
|
83
|
+
- lib/generators/usefull_attachment/install/templates/config/locales/usefull_attachment.it.yml
|
84
|
+
- lib/generators/usefull_attachment/migration/migration_generator.rb
|
85
|
+
- lib/generators/usefull_attachment/migration/templates/create_migration_template.rb
|
86
|
+
- lib/usefull_attachment/engine.rb
|
87
|
+
- lib/usefull_attachment/exceptions.rb
|
88
|
+
- lib/usefull_attachment/paperclip.rb
|
89
|
+
- lib/usefull_attachment/version.rb
|
90
|
+
- lib/usefull_attachment.rb
|
91
|
+
- config/initializers/paperclip.rb
|
92
|
+
- config/locales/custom_errors.it.yml
|
93
|
+
- config/routes.rb
|
94
|
+
- bin/permission.rb
|
95
|
+
- MIT-LICENSE
|
96
|
+
- Rakefile
|
97
|
+
- Gemfile
|
98
|
+
- README.md
|
99
|
+
- CHANGELOG.md
|
100
|
+
homepage:
|
101
|
+
licenses: []
|
102
|
+
|
103
|
+
metadata: {}
|
104
|
+
|
105
|
+
post_install_message:
|
106
|
+
rdoc_options: []
|
107
|
+
|
108
|
+
require_paths:
|
109
|
+
- lib
|
110
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
111
|
+
requirements:
|
112
|
+
- *id003
|
113
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- *id003
|
116
|
+
requirements: []
|
117
|
+
|
118
|
+
rubyforge_project:
|
119
|
+
rubygems_version: 2.0.10
|
120
|
+
signing_key:
|
121
|
+
specification_version: 4
|
122
|
+
summary: Describe Gem Here
|
123
|
+
test_files: []
|
124
|
+
|