kono_utils 0.15.5

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.
Files changed (112) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.rdoc +442 -0
  4. data/Rakefile +23 -0
  5. data/app/assets/javascripts/kono_utils/utilities.coffee +456 -0
  6. data/app/assets/stylesheets/kono_utils/utils.css.scss +43 -0
  7. data/app/controllers/kono_utils/change_log_controller.rb +6 -0
  8. data/app/input/bs_aceeditor_input.rb +53 -0
  9. data/app/input/bs_autocomplete_input.rb +60 -0
  10. data/app/input/bs_datepicker_input.rb +16 -0
  11. data/app/input/bs_datetimepicker_input.rb +80 -0
  12. data/app/input/bs_file_download_input.rb +35 -0
  13. data/app/input/bs_image_input.rb +35 -0
  14. data/app/input/bs_label_with_container_input.rb +22 -0
  15. data/app/input/bs_location_picker_input.rb +95 -0
  16. data/app/input/bs_readonly_input.rb +52 -0
  17. data/app/input/bs_timepicker_input.rb +14 -0
  18. data/app/policies/kono_utils/base_editing_policy_concern.rb +27 -0
  19. data/app/views/kono_utils/base_editing/_edit.html.erb +3 -0
  20. data/app/views/kono_utils/base_editing/_edit_page_side_title_header.html.erb +3 -0
  21. data/app/views/kono_utils/base_editing/_edit_page_title_header.html.erb +3 -0
  22. data/app/views/kono_utils/base_editing/_form.html.erb +15 -0
  23. data/app/views/kono_utils/base_editing/_index_buttons.html.erb +2 -0
  24. data/app/views/kono_utils/base_editing/_index_page_side_title_header.html.erb +3 -0
  25. data/app/views/kono_utils/base_editing/_index_page_title_header.html.erb +3 -0
  26. data/app/views/kono_utils/base_editing/_index_tfoot.html.erb +0 -0
  27. data/app/views/kono_utils/base_editing/_new.html.erb +3 -0
  28. data/app/views/kono_utils/base_editing/_new_page_side_title_header.html.erb +3 -0
  29. data/app/views/kono_utils/base_editing/_new_page_title_header.html.erb +3 -0
  30. data/app/views/kono_utils/base_editing/_search_form.html.erb +1 -0
  31. data/app/views/kono_utils/base_editing/application/_edit_page_side_title_header.html.erb +1 -0
  32. data/app/views/kono_utils/base_editing/application/_edit_page_title_header.html.erb +1 -0
  33. data/app/views/kono_utils/base_editing/application/_index_page_side_title_header.html.erb +1 -0
  34. data/app/views/kono_utils/base_editing/application/_index_page_title_header.html.erb +1 -0
  35. data/app/views/kono_utils/base_editing/application/_new_page_side_title_header.html.erb +1 -0
  36. data/app/views/kono_utils/base_editing/application/_new_page_title_header.html.erb +1 -0
  37. data/app/views/kono_utils/base_editing/application/edit.html.erb +1 -0
  38. data/app/views/kono_utils/base_editing/application/new.html.erb +1 -0
  39. data/app/views/kono_utils/base_editing/edit.html.erb +1 -0
  40. data/app/views/kono_utils/base_editing/index.html.erb +36 -0
  41. data/app/views/kono_utils/base_editing/new.html.erb +1 -0
  42. data/app/views/kono_utils/change_log/index.html.erb +3 -0
  43. data/config/initializers/mysql.rb +12 -0
  44. data/config/initializers/time.rb +12 -0
  45. data/config/locales/it.yml +18 -0
  46. data/lib/generators/kono_utils/install/install_generator.rb +24 -0
  47. data/lib/generators/templates/initializer.rb +3 -0
  48. data/lib/kono_utils.rb +41 -0
  49. data/lib/kono_utils/application_helper.rb +625 -0
  50. data/lib/kono_utils/base_editing_helper.rb +194 -0
  51. data/lib/kono_utils/base_search.rb +173 -0
  52. data/lib/kono_utils/concerns.rb +10 -0
  53. data/lib/kono_utils/concerns/active_record_translation.rb +47 -0
  54. data/lib/kono_utils/concerns/base_editing.rb +195 -0
  55. data/lib/kono_utils/concerns/base_modals.rb +97 -0
  56. data/lib/kono_utils/concerns/success_message.rb +25 -0
  57. data/lib/kono_utils/encoder.rb +55 -0
  58. data/lib/kono_utils/engine.rb +13 -0
  59. data/lib/kono_utils/fiscal_code.rb +47 -0
  60. data/lib/kono_utils/params_hash_array.rb +37 -0
  61. data/lib/kono_utils/percentage.rb +60 -0
  62. data/lib/kono_utils/search_attribute.rb +57 -0
  63. data/lib/kono_utils/tmp_file.rb +81 -0
  64. data/lib/kono_utils/version.rb +3 -0
  65. data/lib/kono_utils/virtual_model.rb +22 -0
  66. data/lib/tasks/kono_utils_tasks.rake +4 -0
  67. data/spec/dummy/README.rdoc +28 -0
  68. data/spec/dummy/Rakefile +6 -0
  69. data/spec/dummy/app/assets/images/.keep +0 -0
  70. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  71. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  72. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  73. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  74. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  75. data/spec/dummy/app/mailers/.keep +0 -0
  76. data/spec/dummy/app/models/.keep +0 -0
  77. data/spec/dummy/app/models/concerns/.keep +0 -0
  78. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  79. data/spec/dummy/bin/bundle +3 -0
  80. data/spec/dummy/bin/rails +4 -0
  81. data/spec/dummy/bin/rake +4 -0
  82. data/spec/dummy/bin/setup +29 -0
  83. data/spec/dummy/config.ru +4 -0
  84. data/spec/dummy/config/application.rb +32 -0
  85. data/spec/dummy/config/boot.rb +5 -0
  86. data/spec/dummy/config/database.yml +25 -0
  87. data/spec/dummy/config/environment.rb +5 -0
  88. data/spec/dummy/config/environments/development.rb +41 -0
  89. data/spec/dummy/config/environments/production.rb +79 -0
  90. data/spec/dummy/config/environments/test.rb +42 -0
  91. data/spec/dummy/config/initializers/assets.rb +11 -0
  92. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  93. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  94. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  95. data/spec/dummy/config/initializers/inflections.rb +16 -0
  96. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  97. data/spec/dummy/config/initializers/session_store.rb +3 -0
  98. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  99. data/spec/dummy/config/locales/en.yml +23 -0
  100. data/spec/dummy/config/routes.rb +56 -0
  101. data/spec/dummy/config/secrets.yml +22 -0
  102. data/spec/dummy/lib/assets/.keep +0 -0
  103. data/spec/dummy/log/.keep +0 -0
  104. data/spec/dummy/public/404.html +67 -0
  105. data/spec/dummy/public/422.html +67 -0
  106. data/spec/dummy/public/500.html +66 -0
  107. data/spec/dummy/public/favicon.ico +0 -0
  108. data/spec/lib/kono_utils/fiscal_code_spec.rb +56 -0
  109. data/spec/rails_helper.rb +53 -0
  110. data/spec/spec_helper.rb +92 -0
  111. data/vendor/assets/javascripts/EventEmitter.js +473 -0
  112. metadata +425 -0
@@ -0,0 +1,194 @@
1
+ module KonoUtils
2
+ module BaseEditingHelper
3
+
4
+ def BaseEditingHelper.included(mod)
5
+ if ::Rails.application.config.action_controller.include_all_helpers!=false
6
+ raise "Devi definire in config/application.rb config.action_controller.include_all_helpers=false
7
+ in modo da far funzionare correttamente l'override degli helper come per i controller"
8
+ end
9
+ end
10
+
11
+ ##
12
+ # Serie di metodi che identificano i titoli generati automaticamente
13
+ # Viene utilizzato i18n per identificare se un determinato modello è di tipo F o M
14
+ # é presente quindi una chiave in YML che identifica ogni modello(default M)
15
+ def title_mod_g(model)
16
+ "#{t("edit_title_#{model_gender(model)}", default: 'Modifica')} #{model.mn}"
17
+ end
18
+
19
+ def title_new_g(model)
20
+ "#{t("new_title_#{model_gender(model)}", default: 'Nuovo')} #{model.mn}"
21
+ end
22
+
23
+ def title_del_g(model)
24
+ "#{t("del_title_#{model_gender(model)}", default: 'Cancella')} #{model.mn}"
25
+ end
26
+
27
+ def model_gender(model)
28
+ t("activerecord.modelgender.#{model.name.underscore.to_sym}", :default => :m).to_sym
29
+ end
30
+
31
+ def new_button(path, base_class=nil)
32
+ options = {class: 'btn btn-success btn-xs'}
33
+ options[:title] = title_new(base_class) unless base_class.nil?
34
+
35
+ link_to fa_icon("plus fw"), path, options
36
+ end
37
+
38
+ def edit_button(path)
39
+ link_to fa_icon("pencil fw"), path, class: 'btn btn-primary btn-xs'
40
+ end
41
+
42
+ def delete_button(path)
43
+ link_to fa_icon("trash fw"), path, method: :delete, data: {confirm: t(:are_you_sure)}, class: 'btn btn-danger btn-xs'
44
+ end
45
+
46
+ def form_submit(f)
47
+ f.actions do
48
+ f.action :submit, :button_html => {:class => "btn btn-primary", :disable_with => t('wait', default: 'Wait...')}
49
+ end
50
+ end
51
+
52
+ ##
53
+ # Questa funzione serve per essere sovrascritta nell'helper specializzato del controller
54
+ def index_print_column(record, field)
55
+ record.send(field)
56
+ end
57
+
58
+ ##
59
+ # Questa funzione serve per stampare il contenuto dell'header
60
+ def index_print_column_head(field)
61
+ block_given? ? yield(field) : base_class.han(field)
62
+ end
63
+
64
+ ##
65
+ # Questa funzione serve per generare la colonna della tabella
66
+ #
67
+ # * *Attributes* :
68
+ # - colonna
69
+ # - tipo di colonna td|th
70
+ # - opzionali : hash{record} per fare altro
71
+ def index_column_builder(field, column, record: nil)
72
+ column_class = "column_#{field}"
73
+ column_id=''
74
+ if record
75
+ column_id = "#{column_class}-#{dom_id(record)}"
76
+ end
77
+ content_tag column, class: column_class, id: column_id do
78
+ yield column_class, column_id
79
+ end
80
+ end
81
+
82
+ def list_button(path)
83
+ link_to fa_icon("list fw"), path, class: 'btn btn-default btn-xs'
84
+ end
85
+
86
+ ##
87
+ # Questa funzione serve per essere sovrascritta nell'helper specializzato del controller
88
+ # e quindi stampare un determinato campo in modo differente
89
+ # si occupa anche di gestire i campi provenienti dalla policy nel caso siano a più livelli con i nested
90
+ # prendiamo in considerazione la situazione con has_many :campo=>[] o con :campo=>[:ciao,:pippo,:pluto]
91
+ def editing_form_print_field(form, field)
92
+ if field.is_a?(Hash)
93
+ #devo nestarlo
94
+ bf = ActiveSupport::SafeBuffer.new
95
+
96
+ field.each do |k, v|
97
+ if v.length==0
98
+ #caso in cui è un elemento normale, ma che ha una selezione multipla
99
+ bf<< editing_form_print_field(form, k)
100
+ else
101
+ #caso in cui potremmo essere in un campo di multipli elementi con vari valori ognuno
102
+ bf<< nest_editing_form_print_field(form, k, v)
103
+ end
104
+ end
105
+ bf
106
+ else
107
+ form.input field
108
+ end
109
+ end
110
+
111
+ ##
112
+ # Questa funzione può essere sovrascritta per gestire in modo personale la renderizzazine dei nested attributes
113
+ # * *Attributes* :
114
+ # - form -> form di formtastic
115
+ # - contenitore -> campo principale
116
+ # - campi -> i campi interni
117
+ def nest_editing_form_print_field(form, contenitore, campi)
118
+ form.semantic_fields_for contenitore do |item|
119
+ item.inputs :name => t(".#{form.object.mn}.#{contenitore}", :default => contenitore.to_s.camelcase) do
120
+ bf = ActiveSupport::SafeBuffer.new
121
+ campi.each do |c|
122
+ bf<<editing_form_print_field(item, c)
123
+ end
124
+ bf
125
+ end
126
+ end
127
+ end
128
+
129
+ ##
130
+ # Per divisione in celle della form
131
+ #
132
+ def cell_column_class(field)
133
+ "col-md-12"
134
+ end
135
+
136
+
137
+ ##
138
+ # Funzione che visualizza il caricamento e l'immagine caricata precedente se presente
139
+ #
140
+ # * *Args* :
141
+ # - form
142
+ # - attribute
143
+ # * *Returns* :
144
+ # - ActiveSupport:SafeBuffer
145
+ #
146
+ def image_show_upload(form, attribute)
147
+
148
+ buff = ActiveSupport::SafeBuffer.new
149
+
150
+ buff<<form.label(attribute)
151
+
152
+ buff<<content_tag(:div, class: 'row') do
153
+ tmp = ActiveSupport::SafeBuffer.new
154
+
155
+
156
+ tmp << content_tag(:div, class: "col-md-5") do
157
+ form.input attribute, :label => false
158
+ end
159
+
160
+ if form.object.send(attribute).exists?
161
+ tmp << content_tag(:div, class: "col-md-2 col-lg-5") do
162
+ link_to image_tag(form.object.send(attribute).url(:original), class: 'img-thumbnail logo-original'), form.object.send(attribute).url
163
+ end
164
+ end
165
+
166
+ tmp
167
+ end
168
+
169
+
170
+ end
171
+
172
+
173
+ def semantic_form_attributes(obj)
174
+ obj
175
+ end
176
+
177
+
178
+ ##
179
+ # Helper per visualizzare l'alert del destroy
180
+ def destroy_link_to(path, options)
181
+ link_to t('.destroy'), path,
182
+ :method => :delete,
183
+ :class => "btn",
184
+ :confirm => t('.destroy_confirm.body', :item => options[:item]),
185
+ "data-confirm-fade" => true,
186
+ "data-confirm-title" => t('.destroy_confirm.title', :item => options[:item]),
187
+ "data-confirm-cancel" => t('.destroy_confirm.cancel', :item => options[:item]),
188
+ "data-confirm-cancel-class" => "btn-cancel",
189
+ "data-confirm-proceed" => t('.destroy_confirm.proceed', :item => options[:item]),
190
+ "data-confirm-proceed-class" => "btn-danger"
191
+ end
192
+
193
+ end
194
+ end
@@ -0,0 +1,173 @@
1
+ require 'kono_utils/search_attribute'
2
+ module KonoUtils
3
+ ##
4
+ # Classe base per i form di ricerca nel sistema
5
+ class BaseSearch < VirtualModel
6
+
7
+ class UndefinedSearchModel < StandardError
8
+ def initialize(msg = 'Definire nella classe, attraverso set_search_model, il modello da utilizzare come base per la ricerca')
9
+ super(msg)
10
+ end
11
+ end
12
+
13
+ class_attribute :_search_model, :_search_attributes, instance_writer: false
14
+ attr_accessor :scope
15
+
16
+ define_model_callbacks :set_scope, :make_query
17
+
18
+ ##
19
+ # Definisce per la classe quale modello utilizzare per la ricerca
20
+ #
21
+ def self.set_search_model(model)
22
+ self._search_model = model
23
+ end
24
+
25
+ ##
26
+ # Definisce gli attributi da utilizzare per la ricerca
27
+ # passandogli un hash finale si possono passare parametri di default
28
+ # per ognuno dei campi settati a formtastic per renderizzare il campo
29
+ # ES:
30
+ # set_search_attributes :nome,:cognome,:as=>:string
31
+ # renderizzerà nome e cognome come stringhe
32
+ #
33
+ # set_search_attributes :datadinascita, :as=>:datetimepicker
34
+ # renderizzerà un campo predisposto per attivare il datetimepicker
35
+ #
36
+ # Possiamo anche passare una Proc come ultimo elemento per la generazione degli attributi per la form,
37
+ # come argomento è presente il current_user
38
+ # set_search_attributes :datadinascita, Proc.new { |current_user,form| funzione da lanciare per eseguire la generazione
39
+ # degli attibuti da passare alla form per generare il campo }
40
+ #
41
+ # come hash di opzioni possiamo anche passargli una chiave :field_options
42
+ # con dentro configurazioni vedi TikalCore::SearchAttribute
43
+ #
44
+ # CALLBACKS
45
+ # Quando vengono creati gli attributi, vengono anche creati gli eventi per ogni attributi, sia per la
46
+ # chiamata del getter (nome_metodo) sia per setter (nome_metodo=), ogni callback ha un prefisso
47
+ # per avere quindi un namespace specifico per questa funzionalità:
48
+ # ES:
49
+ # set_search_attributes :datadinascita....
50
+ #
51
+ # genererà:
52
+ # - before_search_attr_datadinascita
53
+ # - before_search_attr_datadinascita_set => relativo al setter
54
+ # - around_search_attr_datadinascita
55
+ # - around_search_attr_datadinascita_set => relativo al setter
56
+ # - after_search_attr_datadinascita
57
+ # - after_search_attr_datadinascita_set => relativo al setter
58
+ #
59
+ def self.set_search_attributes(*attributes)
60
+ options = attributes.extract_options!
61
+ options = {:as => :string}.merge(options)
62
+
63
+ if attributes.last.is_a?(Proc)
64
+ options = attributes.pop
65
+ end
66
+
67
+ self._search_attributes = self._search_attributes || []
68
+ attributes.each do |a|
69
+
70
+ attr_accessor(a.to_sym)
71
+
72
+ # instance_variable_set "@#{a}".to_sym, nil
73
+ #
74
+ # unless method_defined? a.to_sym
75
+ define_method(a.to_sym) do
76
+ run_callbacks "search_attr_#{a}" do
77
+ # logger.debug { "Chiamata a metodo virtuale #{a} " }
78
+ instance_variable_get "@#{a}".to_sym
79
+ end
80
+ end
81
+ # end
82
+ #
83
+ # unless method_defined? "#{a}=".to_sym
84
+ define_method("#{a}=".to_sym) do |*args|
85
+ run_callbacks "search_attr_#{a}_set" do
86
+ # logger.debug { "Chiamata a metodo virtuale #{a}= -> #{args.inspect}" }
87
+ instance_variable_set "@#{a}".to_sym, *args
88
+ end
89
+ end
90
+ # end
91
+
92
+
93
+ #Definisco delle callbacks per ogni attributo
94
+ define_model_callbacks "search_attr_#{a}".to_sym, "search_attr_#{a}_set".to_sym
95
+ self._search_attributes += [KonoUtils::SearchAttribute.new(a, options)]
96
+ end
97
+ self._search_attributes.uniq!
98
+ end
99
+
100
+ ##
101
+ # Restituisce il modello di ricerca
102
+ def search_model
103
+ self.class._search_model
104
+ end
105
+
106
+ ##
107
+ # Attributi di ricerca
108
+ def search_attributes
109
+ self.class._search_attributes
110
+ end
111
+
112
+ def initialize
113
+ raise UndefinedSearchModel if search_model.nil?
114
+ super
115
+ self.scope = self.class._search_model
116
+ end
117
+
118
+
119
+ ##
120
+ # deve indicarmi se i dati della ricerca sono stati inseriti
121
+ def data_loaded?
122
+ get_query_params.length>0
123
+ end
124
+
125
+ ##
126
+ # Setta lo scope iniziale del modello
127
+ def set_scope(scope)
128
+ run_callbacks :set_scope do
129
+ self.scope = scope
130
+ end
131
+ end
132
+
133
+
134
+ ##
135
+ # Genera la query di ricerca, passando i parametri da
136
+ # ricercare nello scoper di ricerca del modelo
137
+ def make_query
138
+ run_callbacks :make_query do
139
+ self.scope.search(get_query_params)
140
+ end
141
+ end
142
+
143
+
144
+ ##
145
+ # Restituisce un hash con tutti i parametri da implementare sulla ricerca
146
+ #
147
+ def get_query_params
148
+ out = {}
149
+ search_attributes.each do |val|
150
+ out[val.field]=self.send(val.field) unless self.send(val.field).blank?
151
+ end
152
+
153
+ out
154
+ end
155
+
156
+ ##
157
+ # Si occupa di aggiornare i valori interni di ricerca
158
+ def update_attributes(datas)
159
+ search_attributes.each do |val|
160
+ self.send("#{val.field}=", val.cast_value(datas[val.field]))
161
+ end
162
+ end
163
+
164
+ def method_missing(m, *args, &block)
165
+ if self.search_attributes.collect(&:field).include?(m.to_s.gsub(/=$/, ''))
166
+ self.send(m, *args)
167
+ else
168
+ super
169
+ end
170
+ end
171
+
172
+ end
173
+ end
@@ -0,0 +1,10 @@
1
+ module KonoUtils::Concerns
2
+
3
+ extend ActiveSupport::Autoload
4
+
5
+ autoload :BaseModals
6
+ autoload :SuccessMessage
7
+ autoload :BaseEditing
8
+
9
+ end
10
+
@@ -0,0 +1,47 @@
1
+ require 'active_support/concern'
2
+ module KonoUtils::Concerns
3
+ module ActiveRecordTranslation
4
+
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+
9
+ def han(attr)
10
+ self.class.han(attr)
11
+ end
12
+
13
+ def mnp
14
+ self.class.mnp
15
+ end
16
+
17
+ def mn
18
+ self.class.mn
19
+ end
20
+
21
+ end
22
+
23
+ class_methods do
24
+
25
+ ##
26
+ # E' un'alias per il TikalCore::Registration.human_attribute_name(:isee)
27
+ def han(attr)
28
+ self.human_attribute_name(attr)
29
+ end
30
+
31
+ ##
32
+ # E' un alias di model_name.human(count:2)
33
+ def mnp
34
+ self.model_name.human(count: 2)
35
+ end
36
+
37
+ ##
38
+ # E' un alias di model_name.human
39
+ def mn
40
+ self.model_name.human
41
+ end
42
+
43
+
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,195 @@
1
+ require 'active_support/concern'
2
+
3
+ module KonoUtils
4
+ module Concerns
5
+ module BaseEditing
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+
10
+ before_action :append_view_paths
11
+
12
+ before_action :load_object, except: [:index, :new, :create]
13
+ helper_method :base_class
14
+ helper_method :form_attributes
15
+ helper_method :table_columns
16
+ helper_method :new_custom_polymorphic_path
17
+ helper_method :edit_custom_polymorphic_path
18
+ helper_method :index_custom_polymorphic_path
19
+ after_action :check_errors, only: [:create, :update]
20
+
21
+ ##
22
+ # E' possibile passare una callback per poter
23
+ # renderizzare ulteriori formati oppure cambiare la normale
24
+ # renderizzazione. La callback riceve il format della respond_to
25
+ # GET /utenti
26
+ # GET /utenti.xml
27
+ def index(respond_to_call: nil)
28
+ @objects = policy_scope(base_scope).all
29
+ @objects = yield(@objects) if block_given?
30
+ @objects = @objects.paginate(:page => params[:page])
31
+
32
+ respond_to do |format|
33
+ format.html # index.html.erb
34
+ format.xml { render :xml => @objects }
35
+ unless respond_to_call.nil?
36
+ respond_to_call.call(format)
37
+ end
38
+ end
39
+ end
40
+
41
+ # GET /utenti/new
42
+ # GET /utenti/new.xml
43
+ def new
44
+ @object = base_class.new
45
+ authorize @object
46
+ @object = yield(@object) if block_given?
47
+ logger.debug { "Nuovo oggetto #{@object.inspect}" }
48
+
49
+ respond_to do |format|
50
+ format.html
51
+ format.xml { render :xml => @object }
52
+ end
53
+ end
54
+
55
+ # GET /utenti/1/edit
56
+ def edit
57
+ @object = yield(@object) if block_given?
58
+ end
59
+
60
+ # PUT /utenti/1
61
+ # PUT /utenti/1.xml
62
+ def update
63
+ @object = yield(@object) if block_given?
64
+ respond_to do |format|
65
+ if @object.update_attributes(clean_params)
66
+ format.html { redirect_to edit_custom_polymorphic_path(@object), :notice => success_update_message(@object) }
67
+ format.xml { head :ok }
68
+ else
69
+ format.html { render :action => :edit }
70
+ format.xml { render :xml => @object.errors, :status => :unprocessable_entity }
71
+ end
72
+ end
73
+ end
74
+
75
+ # POST /utenti
76
+ # POST /utenti.xml
77
+ def create
78
+ @object = base_class.new(clean_params)
79
+ authorize @object
80
+ @object = yield(@object) if block_given?
81
+ logger.debug { "Nuovo oggetto #{@object.inspect}" }
82
+
83
+ respond_to do |format|
84
+ if @object.save
85
+ format.html { redirect_to edit_custom_polymorphic_path(@object), :notice => success_create_message(@object) }
86
+ format.xml { render :xml => @object, :status => :created, :location => @object }
87
+ else
88
+ format.html { render :action => :new }
89
+ format.xml { render :xml => @object.errors, :status => :unprocessable_entity }
90
+ end
91
+ end
92
+ end
93
+
94
+ # DELETE /utenti/1
95
+ # DELETE /utenti/1.xml
96
+ def destroy
97
+ @object = yield(@object) if block_given?
98
+
99
+
100
+ respond_to do |format|
101
+ if @object.destroy
102
+ format.html { redirect_to index_custom_polymorphic_path(base_class),
103
+ :notice => success_destroy_message(@object) }
104
+ format.xml { head :ok }
105
+ else
106
+ format.html { redirect_to index_custom_polymorphic_path(base_class),
107
+ :flash => {:error => @object.errors.full_messages.join(',')} }
108
+ format.xml { head :ko }
109
+ end
110
+ end
111
+ end
112
+
113
+ ##
114
+ # Elenco degli attributi da visualizzare nella form
115
+ def form_attributes(model = base_class.new)
116
+ policy(model).permitted_attributes
117
+ end
118
+
119
+ ##
120
+ # Elenco ordinato dei campi da utilizzare nella visualizzazione della tabella index
121
+ def table_columns
122
+ policy(base_class.new).permitted_attributes
123
+ end
124
+
125
+ private
126
+ def load_object
127
+ @object = base_class.find(params[:id])
128
+ authorize @object
129
+ logger.debug { "Oggetto #{@object.inspect}" }
130
+
131
+ end
132
+
133
+ def base_class
134
+ controller = controller_name
135
+ modello = controller.singularize.camelize.safe_constantize
136
+ logger.debug { "Editazione del controller:#{controller} per modello: #{modello.to_s}" }
137
+
138
+ raise "Non riesco a restituire la classe base per il controller #{controller}" if modello.nil?
139
+
140
+ modello
141
+ end
142
+
143
+ ##
144
+ # Scope iniziale per index, sovrascrivibile per poter inizializzare ricerca,
145
+ # viene passato al policy_scope in index
146
+ def base_scope
147
+ base_class
148
+ end
149
+
150
+ def clean_params
151
+ permitted = policy(base_class.new).permitted_attributes
152
+ dati = params.required(base_class.name.underscore.gsub('/', '_').to_sym).permit(permitted)
153
+ ::Rails.logger.info { "Permitted Attributes: #{permitted.inspect}" }
154
+ ::Rails.logger.info { "Parametri puliti: #{dati.inspect}" }
155
+ dati
156
+ end
157
+
158
+ def check_errors
159
+ unless @object.valid?
160
+ logger.debug { "Invalid Obj:" }
161
+ logger.debug { @object.errors.inspect }
162
+ end
163
+ end
164
+
165
+ ##
166
+ # Aggiungo una path alla vista del base editing controller,
167
+ # nel caso non siamo derivati dal controller ma ho solamente incluso il concern
168
+ def append_view_paths
169
+ append_view_path KonoUtils::Engine.root.join("app", "views", "kono_utils")
170
+ append_view_path KonoUtils::Engine.root.join("app", "views", "kono_utils", "base_editing")
171
+ end
172
+
173
+
174
+ def new_custom_polymorphic_path(*base_class)
175
+ new_polymorphic_path(*base_class)
176
+ end
177
+
178
+ def edit_custom_polymorphic_path(*rec)
179
+ edit_polymorphic_path(*rec)
180
+ end
181
+
182
+ def index_custom_polymorphic_path(*rec)
183
+ polymorphic_path(*rec)
184
+ end
185
+
186
+
187
+ end
188
+
189
+ # module ClassMethods
190
+ #
191
+ #
192
+ # end
193
+ end
194
+ end
195
+ end