kono_utils 0.15.15 → 1.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.rdoc +17 -78
- data/app/assets/javascripts/kono_utils/core.coffee +477 -0
- data/app/assets/stylesheets/kono_utils/{_kono_styles.css.scss → core.css.scss} +1 -16
- data/app/policies/kono_utils/base_editing_policy_concern.rb +52 -3
- data/app/policies/kono_utils/base_search_form_policy_concern.rb +25 -0
- data/config/initializers/mysql.rb +2 -2
- data/config/locales/it.yml +3 -1
- data/lib/generators/kono_utils/install/install_generator.rb +44 -0
- data/lib/generators/templates/base_editing_controller.template +3 -0
- data/lib/generators/templates/base_editing_helper.template +3 -0
- data/lib/generators/templates/base_editing_policy.template +4 -0
- data/lib/generators/templates/initializer.rb +3 -1
- data/lib/kono_utils.rb +29 -18
- data/lib/kono_utils/application_core_helper.rb +524 -0
- data/lib/kono_utils/application_enum_helper.rb +53 -0
- data/lib/kono_utils/application_helper.rb +4 -578
- data/lib/kono_utils/base_editing_core_helper.rb +194 -0
- data/lib/kono_utils/base_editing_helper.rb +3 -186
- data/lib/kono_utils/base_search.rb +32 -19
- data/lib/kono_utils/concerns.rb +12 -10
- data/lib/kono_utils/concerns/active_record_translation.rb +8 -4
- data/lib/kono_utils/concerns/active_storage_remover_helper.rb +61 -0
- data/lib/kono_utils/concerns/base_editing.rb +199 -53
- data/lib/kono_utils/configuration.rb +18 -0
- data/lib/kono_utils/engine.rb +15 -6
- data/lib/kono_utils/paginate_proxer.rb +17 -0
- data/lib/kono_utils/search_form_builder.rb +24 -0
- data/lib/kono_utils/version.rb +1 -1
- data/spec/dummy/.gitignore +2 -0
- data/spec/dummy/app/assets/config/manifest.js +2 -0
- data/spec/dummy/app/assets/javascripts/application.js +2 -0
- data/spec/dummy/app/controllers/application_controller.rb +10 -0
- data/spec/dummy/app/controllers/base_editing_controller.rb +3 -0
- data/spec/dummy/app/controllers/users_controller.rb +2 -0
- data/spec/dummy/app/helpers/application_helper.rb +1 -0
- data/spec/dummy/app/helpers/base_editing_helper.rb +3 -0
- data/spec/dummy/app/models/application_record.rb +5 -0
- data/spec/dummy/app/models/user.rb +2 -0
- data/spec/dummy/app/policies/application_policy.rb +49 -0
- data/spec/dummy/app/policies/base_editing_policy.rb +4 -0
- data/spec/dummy/app/policies/user_policy.rb +23 -0
- data/spec/dummy/bin/rails +1 -1
- data/spec/dummy/bin/setup +20 -13
- data/spec/dummy/bin/update +31 -0
- data/spec/dummy/bin/yarn +11 -0
- data/spec/dummy/config/application.rb +15 -17
- data/spec/dummy/config/environment.rb +1 -1
- data/spec/dummy/config/environments/development.rb +31 -11
- data/spec/dummy/config/environments/production.rb +26 -16
- data/spec/dummy/config/environments/test.rb +10 -6
- data/spec/dummy/config/initializers/application_controller_renderer.rb +8 -0
- data/spec/dummy/config/initializers/assets.rb +6 -3
- data/spec/dummy/config/initializers/content_security_policy.rb +25 -0
- data/spec/dummy/config/initializers/cookies_serializer.rb +2 -0
- data/spec/dummy/config/initializers/kono_utils.rb +5 -0
- data/spec/dummy/config/initializers/new_framework_defaults_5_2.rb +38 -0
- data/spec/dummy/config/initializers/wrap_parameters.rb +2 -2
- data/spec/dummy/config/locales/en.yml +10 -0
- data/spec/dummy/config/routes.rb +3 -52
- data/spec/dummy/config/storage.yml +34 -0
- data/spec/dummy/db/migrate/20190716093114_create_users.rb +11 -0
- data/spec/dummy/db/schema.rb +23 -0
- data/spec/dummy/package.json +5 -0
- data/spec/dummy/spec/models/user_spec.rb +5 -0
- data/spec/dummy/yarn.lock +21 -0
- metadata +83 -144
- data/app/assets/javascripts/kono_utils/utilities.coffee +0 -481
- data/app/assets/stylesheets/kono_utils/utils.css.scss +0 -4
- data/app/input/bs_aceeditor_input.rb +0 -53
- data/app/input/bs_autocomplete_input.rb +0 -60
- data/app/input/bs_datepicker_input.rb +0 -16
- data/app/input/bs_datetimepicker_input.rb +0 -80
- data/app/input/bs_file_download_input.rb +0 -35
- data/app/input/bs_image_input.rb +0 -35
- data/app/input/bs_label_with_container_input.rb +0 -22
- data/app/input/bs_location_picker_input.rb +0 -95
- data/app/input/bs_readonly_input.rb +0 -52
- data/app/input/bs_timepicker_input.rb +0 -14
- data/app/views/kono_utils/application/_search_panel.html.erb +0 -36
- data/app/views/kono_utils/base_editing/_edit.html.erb +0 -3
- data/app/views/kono_utils/base_editing/_edit_page_side_title_header.html.erb +0 -3
- data/app/views/kono_utils/base_editing/_edit_page_title_header.html.erb +0 -3
- data/app/views/kono_utils/base_editing/_form.html.erb +0 -15
- data/app/views/kono_utils/base_editing/_index_buttons.html.erb +0 -2
- data/app/views/kono_utils/base_editing/_index_page_side_title_header.html.erb +0 -3
- data/app/views/kono_utils/base_editing/_index_page_title_header.html.erb +0 -3
- data/app/views/kono_utils/base_editing/_index_tfoot.html.erb +0 -0
- data/app/views/kono_utils/base_editing/_new.html.erb +0 -3
- data/app/views/kono_utils/base_editing/_new_page_side_title_header.html.erb +0 -3
- data/app/views/kono_utils/base_editing/_new_page_title_header.html.erb +0 -3
- data/app/views/kono_utils/base_editing/_search_form.html.erb +0 -1
- data/app/views/kono_utils/base_editing/application/_edit_page_side_title_header.html.erb +0 -1
- data/app/views/kono_utils/base_editing/application/_edit_page_title_header.html.erb +0 -1
- data/app/views/kono_utils/base_editing/application/_index_page_side_title_header.html.erb +0 -1
- data/app/views/kono_utils/base_editing/application/_index_page_title_header.html.erb +0 -1
- data/app/views/kono_utils/base_editing/application/_new_page_side_title_header.html.erb +0 -1
- data/app/views/kono_utils/base_editing/application/_new_page_title_header.html.erb +0 -1
- data/app/views/kono_utils/base_editing/application/edit.html.erb +0 -1
- data/app/views/kono_utils/base_editing/application/new.html.erb +0 -1
- data/app/views/kono_utils/base_editing/edit.html.erb +0 -1
- data/app/views/kono_utils/base_editing/index.html.erb +0 -36
- data/app/views/kono_utils/base_editing/new.html.erb +0 -1
- data/lib/kono_utils/encoder.rb +0 -47
- data/lib/kono_utils/fiscal_code.rb +0 -47
- data/lib/kono_utils/params_hash_array.rb +0 -37
- data/lib/kono_utils/percentage.rb +0 -60
- data/lib/kono_utils/tmp_file.rb +0 -81
- data/lib/kono_utils/virtual_model.rb +0 -22
- data/lib/tasks/kono_utils_tasks.rake +0 -4
- data/spec/lib/kono_utils/fiscal_code_spec.rb +0 -56
- data/vendor/assets/javascripts/EventEmitter.js +0 -473
@@ -1,14 +0,0 @@
|
|
1
|
-
class BsTimepickerInput < BsDatetimepickerInput
|
2
|
-
|
3
|
-
def icon
|
4
|
-
fa_icon("clock-o".to_sym)
|
5
|
-
end
|
6
|
-
|
7
|
-
def default_javascript_options
|
8
|
-
{
|
9
|
-
server_format: 'YYYY-MM-DD HH:mm:ss UTC',
|
10
|
-
server_match: '/^[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2} UTC$/',
|
11
|
-
format: 'HH:mm'
|
12
|
-
}
|
13
|
-
end
|
14
|
-
end
|
@@ -1,36 +0,0 @@
|
|
1
|
-
<div class="panel panel-default search_panel">
|
2
|
-
<div class="panel-heading">
|
3
|
-
<h3 class="panel-title collapse_search">
|
4
|
-
<span><%= t(:search) %></span>
|
5
|
-
<div class="pull-right icon-search">
|
6
|
-
<%= fa_icon("search") %>
|
7
|
-
<span class="caret"></span>
|
8
|
-
</div>
|
9
|
-
</h3>
|
10
|
-
</div>
|
11
|
-
|
12
|
-
|
13
|
-
<div class="collapsible_panel <%= search_model.data_loaded? ? 'uncollapsed' : '' %>">
|
14
|
-
<div class="panel-body">
|
15
|
-
<%= form.fields_builder(:field_options => field_option) %>
|
16
|
-
</div>
|
17
|
-
|
18
|
-
<div class="panel-footer text-right">
|
19
|
-
|
20
|
-
<%= button_tag(t(:search), type: "submit", class: "btn btn-primary") %>
|
21
|
-
|
22
|
-
<% if search_model.data_loaded? and !reset_path.nil? %>
|
23
|
-
<%= link_to(content_tag(:span, nil, class: 'glyphicon glyphicon-remove'), reset_path, class: 'btn btn-info') %>
|
24
|
-
<% end %>
|
25
|
-
|
26
|
-
|
27
|
-
<% bf = ActiveSupport::SafeBuffer.new
|
28
|
-
buttons_editor.call(form, bf)
|
29
|
-
%>
|
30
|
-
<%= bf %>
|
31
|
-
|
32
|
-
|
33
|
-
</div>
|
34
|
-
</div>
|
35
|
-
|
36
|
-
</div>
|
@@ -1,15 +0,0 @@
|
|
1
|
-
<%= semantic_form_for(*semantic_form_attributes(@object)) do |f| %>
|
2
|
-
|
3
|
-
<div class="row">
|
4
|
-
<% form_attributes(@object).each do |field| %>
|
5
|
-
|
6
|
-
<div class="<%= cell_column_class(field) %>">
|
7
|
-
<%= editing_form_print_field(f, field) %>
|
8
|
-
</div>
|
9
|
-
|
10
|
-
<% end %>
|
11
|
-
</div>
|
12
|
-
|
13
|
-
<%= form_submit f %>
|
14
|
-
|
15
|
-
<% end %>
|
File without changes
|
@@ -1 +0,0 @@
|
|
1
|
-
<%= search_form @search, {:reset_path => polymorphic_path(base_class)} %>
|
@@ -1 +0,0 @@
|
|
1
|
-
<%= render 'base_editing/edit_page_side_title_header' %>
|
@@ -1 +0,0 @@
|
|
1
|
-
<%= render 'base_editing/edit_page_title_header' %>
|
@@ -1 +0,0 @@
|
|
1
|
-
<%= render 'base_editing/index_page_side_title_header' %>
|
@@ -1 +0,0 @@
|
|
1
|
-
<%= render 'base_editing/index_page_title_header' %>
|
@@ -1 +0,0 @@
|
|
1
|
-
<%= render 'base_editing/new_page_title_header' %>
|
@@ -1 +0,0 @@
|
|
1
|
-
<%= render 'base_editing/new_page_title_header' %>
|
@@ -1 +0,0 @@
|
|
1
|
-
<%= render 'base_editing/edit' %>
|
@@ -1 +0,0 @@
|
|
1
|
-
<%= render 'base_editing/new' %>
|
@@ -1 +0,0 @@
|
|
1
|
-
<%= render 'base_editing/edit' %>
|
@@ -1,36 +0,0 @@
|
|
1
|
-
<%= render 'index_page_title_header' %>
|
2
|
-
<%= render 'index_page_side_title_header' %>
|
3
|
-
|
4
|
-
<%= render 'search_form' unless @search.nil? %>
|
5
|
-
|
6
|
-
<table class="table table-bordered table-hover">
|
7
|
-
<thead>
|
8
|
-
<tr>
|
9
|
-
<% table_columns.each do |t| %>
|
10
|
-
<%= index_column_builder(t, :th) do %>
|
11
|
-
<%= index_print_column_head(t) %>
|
12
|
-
<% end %>
|
13
|
-
<% end %>
|
14
|
-
<th class="index-col-btn"></th>
|
15
|
-
</tr>
|
16
|
-
</thead>
|
17
|
-
|
18
|
-
<tbody>
|
19
|
-
<% @objects.each do |r| %>
|
20
|
-
<tr>
|
21
|
-
<% table_columns.each do |t| %>
|
22
|
-
<%= index_column_builder(t, :td, record: r) do %>
|
23
|
-
<%= index_print_column(r, t) %>
|
24
|
-
<% end %>
|
25
|
-
<% end %>
|
26
|
-
<th class="index-col-btn">
|
27
|
-
<%= render 'index_buttons', record: r %>
|
28
|
-
</th>
|
29
|
-
</tr>
|
30
|
-
<% end %>
|
31
|
-
</tbody>
|
32
|
-
<%= render 'index_tfoot' %>
|
33
|
-
</table>
|
34
|
-
|
35
|
-
<%= will_paginate_bst(@objects) %>
|
36
|
-
|
@@ -1 +0,0 @@
|
|
1
|
-
<%= render 'base_editing/new' %>
|
data/lib/kono_utils/encoder.rb
DELETED
@@ -1,47 +0,0 @@
|
|
1
|
-
module KonoUtils
|
2
|
-
##
|
3
|
-
# Classe che si occupa di decodificare una qualsiasi stringa in formato utf8,
|
4
|
-
# cercando di trovare l'encoding iniziale a tentativi.
|
5
|
-
class Encoder
|
6
|
-
attr_accessor :string
|
7
|
-
|
8
|
-
##
|
9
|
-
# * *Attributes* :
|
10
|
-
# - string -> Stringa da elaborare
|
11
|
-
def initialize(string)
|
12
|
-
self.string = string
|
13
|
-
end
|
14
|
-
|
15
|
-
##
|
16
|
-
# Funcione di rimozione del carattere BOM http://en.wikipedia.org/wiki/Byte_order_mark
|
17
|
-
# e encoding normale
|
18
|
-
def remove_bom
|
19
|
-
string_encoder.gsub("\xEF\xBB\xBF".force_encoding('UTF-8'), '')
|
20
|
-
end
|
21
|
-
|
22
|
-
##
|
23
|
-
# Funzione di encoding semplice
|
24
|
-
def string_encoder
|
25
|
-
return string if string.valid_encoding?
|
26
|
-
str = string
|
27
|
-
Encoding.list.each do |e|
|
28
|
-
begin
|
29
|
-
str.force_encoding(e.name)
|
30
|
-
tmp_string = str.encode('UTF-8')
|
31
|
-
return tmp_string if tmp_string.valid_encoding?
|
32
|
-
rescue
|
33
|
-
Rails.logger.debug { "Rescue -> #{e.name}" } if defined?(::Rails)
|
34
|
-
end
|
35
|
-
end
|
36
|
-
|
37
|
-
impossible_encoding
|
38
|
-
|
39
|
-
string
|
40
|
-
end
|
41
|
-
|
42
|
-
##
|
43
|
-
# Metodo placeholder, volendo si può estendere la funzione e sovrascrivere questa funzione
|
44
|
-
# per essere notificati in caso di mancata decodifica
|
45
|
-
def impossible_encoding; end
|
46
|
-
end
|
47
|
-
end
|
@@ -1,47 +0,0 @@
|
|
1
|
-
module KonoUtils
|
2
|
-
module FiscalCode
|
3
|
-
class ControllaCF
|
4
|
-
LT = [[1, '0'], [0, '1'], [5, '2'], [7, '3'], [9, '4'], [13, '5'], [15, '6'], [17, '7'], [19, '8'], [21, '9'], [1, 'A'], [0, 'B'], [5, 'C'], [7, 'D'], [9, 'E'], [13, 'F'], [15, 'G'], [17, 'H'], [19, 'I'], [21, 'J'], [2, 'K'], [4, 'L'], [18, 'M'], [20, 'N'], [11, 'O'], [3, 'P'], [6, 'Q'], [8, 'R'], [12, 'S'], [14, 'T'], [16, 'U'], [10, 'V'], [22, 'W'], [25, 'X'], [24, 'Y'], [23, 'Z']]
|
5
|
-
Z9 = ('0'..'9').to_a
|
6
|
-
AZ = ('A'..'Z').to_a
|
7
|
-
class EmptyString < Exception;
|
8
|
-
end
|
9
|
-
class InvalidLength < Exception;
|
10
|
-
end
|
11
|
-
class CaseError < Exception;
|
12
|
-
end
|
13
|
-
|
14
|
-
def self.valid?(cf, strict = false)
|
15
|
-
cf = cf.to_s
|
16
|
-
raise EmptyString.new("codice fiscale non può essere lasciato in bianco") if cf.empty?
|
17
|
-
raise InvalidLength.new("codice fiscale dev'essere composto da 16 caratteri alfanumerici") if cf.size != 16
|
18
|
-
if strict==true && cf != cf.upcase;
|
19
|
-
raise CaseError.new("i caratteri del codice fiscale devono essere maiuscoli");
|
20
|
-
else
|
21
|
-
cf.upcase!
|
22
|
-
end;
|
23
|
-
s = (0..14).collect { |i| (i&1)!=0 ? ([Z9.include?(cf[i, 1]) ? cf[i, 1].to_i : AZ.index(cf[i, 1]), cf[i, 1]]) : LT.rassoc(cf[i, 1]) }
|
24
|
-
s.include?(nil) ? false : AZ.at((s.transpose[0].inject(0) { |t, n| t+n })%26) == cf[-1, 1]
|
25
|
-
end
|
26
|
-
|
27
|
-
|
28
|
-
end
|
29
|
-
|
30
|
-
class ControllaPI
|
31
|
-
NU = ('0'..'9').to_a
|
32
|
-
class EmptyString < Exception;
|
33
|
-
end
|
34
|
-
class InvalidLength < Exception;
|
35
|
-
end
|
36
|
-
|
37
|
-
def self.valid?(pi)
|
38
|
-
pi = pi.to_s
|
39
|
-
raise EmptyString.new("partita iva non può essere lasciata in bianco") if pi.empty?
|
40
|
-
raise InvalidLength.new("partita iva dev'essere composta da 11 cifre") if pi.size != 11
|
41
|
-
s = (0..9).collect { |i| NU.include?(pi[i, 1]) ? ((i&1)!=0 ? (pi[i, 1].to_i > 4 ? ((pi[i, 1].to_i*2) - 9) : pi[i, 1].to_i * 2) : pi[i, 1].to_i) : nil }
|
42
|
-
r = s.include?(nil) ? false : ((s.inject(0) { |t, n| t+n }) % 10)
|
43
|
-
r != false && (r==0 ? r : 10-r) == pi[-1, 1].to_i
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
@@ -1,37 +0,0 @@
|
|
1
|
-
module KonoUtils
|
2
|
-
module ParamsHashArray
|
3
|
-
extend ActiveSupport::Concern
|
4
|
-
|
5
|
-
included do
|
6
|
-
##
|
7
|
-
# Si occupa di trasformare un hash con elementi che sono chiramente array in un hash con elementi array:
|
8
|
-
#
|
9
|
-
# {"DatiOrdineAcquisto"=>{"0"=>{"RiferimentoNumeroLinea"=>{"0"=>""}, "IdDocumento"=>"", "Data"=>"", "NumItem"=>"", "CodiceCommessaConvenzione"=>"", "CodiceCUP"=>"", "CodiceCIG"=>""}}}
|
10
|
-
# {"DatiOrdineAcquisto"=>[{"RiferimentoNumeroLinea"=>[""], "IdDocumento"=>"", "Data"=>"", "NumItem"=>"", "CodiceCommessaConvenzione"=>"", "CodiceCUP"=>"", "CodiceCIG"=>""}]}
|
11
|
-
#
|
12
|
-
def elaborate_params_to_hash_array(params)
|
13
|
-
|
14
|
-
out = params
|
15
|
-
##Controllo se abbiamo solamente chiavi che rappresentano numeri
|
16
|
-
if params.is_a?(Hash)
|
17
|
-
|
18
|
-
if params.keys.select { |k| !(k.to_s.match(/\A[+-]?\d+?(\.\d+)?\Z/) == nil ? false : true) }.length==0
|
19
|
-
out = []
|
20
|
-
params.each_value do |v|
|
21
|
-
out << elaborate_params_to_hash_array(v)
|
22
|
-
end
|
23
|
-
else
|
24
|
-
out={}
|
25
|
-
params.keys.each do |k|
|
26
|
-
out[k.to_sym] = elaborate_params_to_hash_array(params[k])
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
Rails.logger.debug "DEBUG #{out.inspect}"
|
32
|
-
out
|
33
|
-
end
|
34
|
-
end
|
35
|
-
|
36
|
-
end
|
37
|
-
end
|
@@ -1,60 +0,0 @@
|
|
1
|
-
require 'action_view'
|
2
|
-
module KonoUtils
|
3
|
-
##
|
4
|
-
# Classe che si occupa di rappresentare un numero percentuale
|
5
|
-
# ES:
|
6
|
-
# p = Percentage.new(100,20)
|
7
|
-
# p.percentage -> ritorna il valore percentuale float
|
8
|
-
# p.to_i -> ritorna percentuale intera con relativi arrotondamenti
|
9
|
-
# p.to_percentage -> si comporta come l'helper number_to_percentage
|
10
|
-
class Percentage
|
11
|
-
|
12
|
-
include ActionView::Helpers::NumberHelper
|
13
|
-
|
14
|
-
attr_accessor :total, :partial
|
15
|
-
|
16
|
-
def initialize(total=0, partial=0)
|
17
|
-
@total = total
|
18
|
-
@partial = partial
|
19
|
-
end
|
20
|
-
|
21
|
-
##
|
22
|
-
#
|
23
|
-
# * *Args* :
|
24
|
-
# - options -> Hash :
|
25
|
-
# :locale - Sets the locale to be used for formatting (defaults to current locale).
|
26
|
-
#
|
27
|
-
# :precision - Sets the precision of the number (defaults to 3).
|
28
|
-
#
|
29
|
-
# :significant - If true, precision will be the # of significant_digits. If false, the # of fractional digits (defaults to false).
|
30
|
-
#
|
31
|
-
# :separator - Sets the separator between the fractional and integer digits (defaults to “.”).
|
32
|
-
#
|
33
|
-
# :delimiter - Sets the thousands delimiter (defaults to “”).
|
34
|
-
#
|
35
|
-
# :strip_insignificant_zeros - If true removes insignificant zeros after the decimal separator (defaults to false).
|
36
|
-
#
|
37
|
-
# :format - Specifies the format of the percentage string The number field is %n (defaults to “%n%”).
|
38
|
-
#
|
39
|
-
# :raise - If true, raises InvalidNumberError when the argument is invalid.
|
40
|
-
# * *Returns* :
|
41
|
-
# - String
|
42
|
-
#
|
43
|
-
def to_percentage(options = {})
|
44
|
-
number_to_percentage(percentage, options)
|
45
|
-
end
|
46
|
-
|
47
|
-
def percentage
|
48
|
-
return 0 if @partial==0
|
49
|
-
return 0 if @total<@partial
|
50
|
-
@partial.to_f/@total.to_f*100.0
|
51
|
-
end
|
52
|
-
|
53
|
-
def to_i
|
54
|
-
return 100 if @total.to_s == @partial.to_s
|
55
|
-
percentage.to_i
|
56
|
-
end
|
57
|
-
|
58
|
-
|
59
|
-
end
|
60
|
-
end
|
data/lib/kono_utils/tmp_file.rb
DELETED
@@ -1,81 +0,0 @@
|
|
1
|
-
require 'fileutils'
|
2
|
-
module KonoUtils
|
3
|
-
##
|
4
|
-
# Classe Funzionante prettamente con RAILS
|
5
|
-
# la classe si occupa dei files temporanei creando una cartella dentro a tmp del progetto
|
6
|
-
# crea una cartella random per ogni file per ovviare al problema di duplicazioni dei nomi
|
7
|
-
# Estende Tempfile,
|
8
|
-
# la root della struttura è quella di Rails.root
|
9
|
-
# e di default la classe scrive dentro a tmp
|
10
|
-
# non ha bisogno di essere cancellata, ci penserà in automatico la classe una
|
11
|
-
# la prossima volta che viene chiamata cancellando tutti i files con un TTL
|
12
|
-
# maggiore di 1.day di default
|
13
|
-
## Utilizzo:
|
14
|
-
# tmp = Ptmpfile.new('nomefile.ext')
|
15
|
-
# tmp.path -> path completa
|
16
|
-
# tmp.write.....
|
17
|
-
# tmp.original_filename.......
|
18
|
-
# tmp.unique_filename.......
|
19
|
-
#
|
20
|
-
#
|
21
|
-
class TmpFile < Tempfile
|
22
|
-
|
23
|
-
PATH = 'tmp/ptmpfile'
|
24
|
-
TIME_LIMIT = 1.day
|
25
|
-
attr_accessor :root_dir
|
26
|
-
attr_accessor :tmp_dir
|
27
|
-
attr_accessor :original_filename, :unique_filename
|
28
|
-
|
29
|
-
|
30
|
-
##
|
31
|
-
# Generate a new file with a given name
|
32
|
-
#
|
33
|
-
# * *Args* :
|
34
|
-
# - name -> String of the name file
|
35
|
-
# * *Returns* :
|
36
|
-
# - Tempfile
|
37
|
-
#
|
38
|
-
def initialize(name)
|
39
|
-
self.original_filename=name
|
40
|
-
self.root_dir = controll_root
|
41
|
-
clean_tmpdir
|
42
|
-
#creo una nuova cartella per il file attuale
|
43
|
-
self.tmp_dir= "#{self.root_dir.path}/#{Time.now.to_f}"
|
44
|
-
Dir.mkdir(self.tmp_dir)
|
45
|
-
ext =File.extname(name)
|
46
|
-
name = [File.basename(name), ext] unless ext.blank?
|
47
|
-
super(name, self.tmp_dir)
|
48
|
-
self.unique_filename= File.basename(self.path)
|
49
|
-
end
|
50
|
-
|
51
|
-
|
52
|
-
private
|
53
|
-
|
54
|
-
##
|
55
|
-
# controll presence of root dir
|
56
|
-
# if not exists create it
|
57
|
-
# * *Returns* :
|
58
|
-
# - Dir
|
59
|
-
#
|
60
|
-
def controll_root
|
61
|
-
unless File.exist?(::Rails.root.join(PATH))
|
62
|
-
Dir.mkdir(::Rails.root.join(PATH))
|
63
|
-
end
|
64
|
-
Dir.open(::Rails.root.join(PATH))
|
65
|
-
end
|
66
|
-
|
67
|
-
##
|
68
|
-
# Clean the directory
|
69
|
-
#
|
70
|
-
def clean_tmpdir
|
71
|
-
self.root_dir.each do |d|
|
72
|
-
if d != '..' and d!='.'
|
73
|
-
if d.to_i < Time.now.to_i-TIME_LIMIT
|
74
|
-
FileUtils.rm_rf(File.join(self.root_dir.path, d))
|
75
|
-
end
|
76
|
-
end
|
77
|
-
end
|
78
|
-
end
|
79
|
-
|
80
|
-
end
|
81
|
-
end
|