active_scaffold_batch 3.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (45) hide show
  1. data/LICENSE.txt +20 -0
  2. data/README +4 -0
  3. data/app/assets/javascripts/jquery/active_scaffold_batch.js +13 -0
  4. data/app/assets/javascripts/prototype/active_scaffold_batch.js +13 -0
  5. data/app/assets/stylesshets/active_scaffold_batch.css +36 -0
  6. data/app/views/active_scaffold_overrides/_batch_create_form.html.erb +9 -0
  7. data/app/views/active_scaffold_overrides/_batch_create_form_attribute.html.erb +19 -0
  8. data/app/views/active_scaffold_overrides/_batch_create_form_body.html.erb +25 -0
  9. data/app/views/active_scaffold_overrides/_batch_create_form_footer.html.erb +4 -0
  10. data/app/views/active_scaffold_overrides/_batch_create_form_horizontal.html.erb +14 -0
  11. data/app/views/active_scaffold_overrides/_batch_create_form_horizontal_header.html.erb +8 -0
  12. data/app/views/active_scaffold_overrides/_batch_create_form_horizontal_record.html.erb +26 -0
  13. data/app/views/active_scaffold_overrides/_batch_create_form_multiple.html.erb +8 -0
  14. data/app/views/active_scaffold_overrides/_batch_create_form_vertical.html.erb +7 -0
  15. data/app/views/active_scaffold_overrides/_batch_create_form_vertical_record.html.erb +6 -0
  16. data/app/views/active_scaffold_overrides/_batch_update_form.html.erb +7 -0
  17. data/app/views/active_scaffold_overrides/_batch_update_form_attribute.html.erb +12 -0
  18. data/app/views/active_scaffold_overrides/_batch_update_form_attribute_scope.html.erb +14 -0
  19. data/app/views/active_scaffold_overrides/_batch_update_form_body.html.erb +25 -0
  20. data/app/views/active_scaffold_overrides/_form_messages.html.erb +9 -0
  21. data/app/views/active_scaffold_overrides/batch_add.js.erb +5 -0
  22. data/app/views/active_scaffold_overrides/batch_create.html.erb +5 -0
  23. data/app/views/active_scaffold_overrides/batch_update.html.erb +5 -0
  24. data/app/views/active_scaffold_overrides/on_batch_base.js.erb +10 -0
  25. data/app/views/active_scaffold_overrides/on_batch_create.js.erb +15 -0
  26. data/app/views/active_scaffold_overrides/on_batch_update.js.erb +10 -0
  27. data/config/locales/de.yml +22 -0
  28. data/config/locales/en.yml +21 -0
  29. data/config/locales/es.yml +21 -0
  30. data/lib/active_scaffold/actions/batch_base.rb +135 -0
  31. data/lib/active_scaffold/actions/batch_create.rb +247 -0
  32. data/lib/active_scaffold/actions/batch_destroy.rb +77 -0
  33. data/lib/active_scaffold/actions/batch_update.rb +281 -0
  34. data/lib/active_scaffold/config/batch_create.rb +79 -0
  35. data/lib/active_scaffold/config/batch_destroy.rb +54 -0
  36. data/lib/active_scaffold/config/batch_update.rb +52 -0
  37. data/lib/active_scaffold/helpers/batch_create_column_helpers.rb +38 -0
  38. data/lib/active_scaffold/helpers/calendar_date_select_update_column_helpers.rb +33 -0
  39. data/lib/active_scaffold/helpers/datepicker_update_column_helpers.rb +29 -0
  40. data/lib/active_scaffold/helpers/update_column_helpers.rb +93 -0
  41. data/lib/active_scaffold_batch/config/core.rb +13 -0
  42. data/lib/active_scaffold_batch/engine.rb +23 -0
  43. data/lib/active_scaffold_batch/version.rb +9 -0
  44. data/lib/active_scaffold_batch.rb +25 -0
  45. metadata +125 -0
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2012 Sergio Cambra
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 ADDED
@@ -0,0 +1,4 @@
1
+ ActiveScaffold batch is a gem which adds batch_update, batch_create and batch_destroy actions
2
+ to ActiveScaffold, which means you can perform operations on more than just one record.
3
+
4
+ gem 'active_scaffold_batch'
@@ -0,0 +1,13 @@
1
+ jQuery(document).ready(function() {
2
+ jQuery('.batch-create-rows a').live('ajax:beforeSend', function(event, xhr, settings) {
3
+ var num_records = jQuery(this).closest('.batch-create-rows').find('input[name=num_records]').val();
4
+ if (num_records) settings.url += (settings.url.indexOf('?') != -1 ? '&' : '?') + 'num_records=' + num_records;
5
+ return true;
6
+ });
7
+ jQuery('.multiple .form_record a.remove').live('click', function(event) {
8
+ event.preventDefault();
9
+ var record = jQuery(this).closest('.form_record');
10
+ record.prev('.form_record-errors').remove();
11
+ record.remove();
12
+ });
13
+ });
@@ -0,0 +1,13 @@
1
+ document.observe("dom:loaded", function() {
2
+ document.on('ajax:create', '.batch-create-rows a', function(event, response) {
3
+ var num_records = $(this).up('.batch-create-rows').down('input[name=num_records]').value();
4
+ if (num_records) response.request.url += (response.request.url.include('?') ? '&' : '?') + 'num_records=' + num_records;
5
+ return true;
6
+ });
7
+ document.on('click', '.multiple .form_record > a.remove', function(event) {
8
+ event.stop();
9
+ var record = $(this).up('.form_record'), errors = record.previous('.form_record-errors');
10
+ if (errors) errors.remove();
11
+ record.remove();
12
+ });
13
+ });
@@ -0,0 +1,36 @@
1
+ .active-scaffold .multiple .form_record .errorExplanation {
2
+ clear: left;
3
+ margin-top: 10px;
4
+ }
5
+ .active-scaffold .multiple .form_record {
6
+ border-top: 1px solid #7FCF00;
7
+ }
8
+ .active-scaffold .multiple .form_record:first-child {
9
+ border-top: 0;
10
+ }
11
+ .active-scaffold .multiple .form_record a.remove {
12
+ background-image: url("/assets/active_scaffold/cross.png");
13
+ display: block;
14
+ font-weight: bold;
15
+ height: 16px;
16
+ padding: 0;
17
+ text-indent: -4000px;
18
+ width: 16px;
19
+ margin: 0 5px 10px;
20
+ }
21
+ .batch-create-rows {
22
+ float: right;
23
+ }
24
+ .batch-create-rows input {
25
+ width: 4em;
26
+ }
27
+ .active-scaffold-header div.actions > a.batch_new {
28
+ background-position: 5px 50%;
29
+ background-repeat: no-repeat;
30
+ background-image: url("/assets/active_scaffold/add.png");
31
+ margin: 0;
32
+ padding: 5px 5px 5px 25px;
33
+ }
34
+ .active-scaffold .active-scaffold .active-scaffold-header div.actions > a.batch_new {
35
+ padding-left: 25px;
36
+ }
@@ -0,0 +1,9 @@
1
+ <%= render :partial => "base_form", :locals => {:xhr => xhr ||= nil,
2
+ :form_action => form_action ||= :batch_create,
3
+ :method => method ||= :post,
4
+ :cancel_link => cancel_link ||= true,
5
+ :body_partial => batch_create_by_column ? 'batch_create_form_body' : 'batch_create_form_multiple',
6
+ :footer_extension => ('batch_create_form_footer' unless batch_create_by_column),
7
+ :scope => @scope,
8
+ :headline => headline ||= active_scaffold_config.batch_create.label} %>
9
+
@@ -0,0 +1,19 @@
1
+ <% scope ||= nil %>
2
+ <dl>
3
+ <dt>
4
+ <label for="<%= active_scaffold_input_options(column, scope)[:id] %>"><%= column.label %></label>
5
+ </dt>
6
+ <dd>
7
+ <% if batch_create_by_column.to_sym == column %>
8
+ <%= active_scaffold_batch_create_by_column column, scope %>
9
+ <% else %>
10
+ <%= active_scaffold_input_for column, scope %>
11
+ <% if column.update_columns -%>
12
+ <%= loading_indicator_tag(:action => :render_field, :id => params[:id]) %>
13
+ <% end -%>
14
+ <% end %>
15
+ <% if column.description -%>
16
+ <span class="description"><%= column.description %></span>
17
+ <% end -%>
18
+ </dd>
19
+ </dl>
@@ -0,0 +1,25 @@
1
+ <% subsection_id ||= nil %>
2
+ <ol class="form" <%= "id=#{subsection_id}" unless subsection_id.nil? %> <%= 'style="display: none;"' if columns.collapsed -%>>
3
+ <li class="form-element">
4
+ <%= hidden_field_tag("batch_scope", (batch_scope || 'LISTED')) %>
5
+ </li>
6
+ <% columns.each :for => @record do |column| %>
7
+ <% next if column.plural_association? || (column.association && [:has_one].include?(column.association.macro)) %>
8
+ <% renders_as = column_renders_as(column) %>
9
+ <% if renders_as == :subsection -%>
10
+ <% subsection_id = sub_section_id(:sub_section => column.label) %>
11
+ <li class="sub-section">
12
+ <h5><%= column.label %></h5>
13
+ <%= render :partial => 'form', :locals => { :columns => column, :subsection_id => subsection_id} %>
14
+ <%= link_to_visibility_toggle(subsection_id, {:default_visible => !column.collapsed}) -%>
15
+ </li>
16
+ <% elsif column.readonly_association?
17
+ next %>
18
+ <% else
19
+ renders_as = :field if renders_as == :subform -%>
20
+ <li class="form-element <%= column.css_class unless column.css_class.nil? %>">
21
+ <%= render :partial => 'batch_create_form_attribute', :locals => { :column => column } -%>
22
+ </li>
23
+ <% end -%>
24
+ <% end -%>
25
+ </ol>
@@ -0,0 +1,4 @@
1
+ <span class='batch-create-rows'>
2
+ <%= as_(:add_some_records, :model => active_scaffold_config.label(:count => 2), :number_input => text_field_tag(:num_records, 1, :id => nil)).html_safe %>
3
+ <%= link_to as_(:add), params_for(:action => :batch_add), :remote => true %>
4
+ </span>
@@ -0,0 +1,14 @@
1
+ <div class="form sub-form horizontal-sub-form">
2
+ <table cellpadding="0" cellspacing="0">
3
+ <% @record = records.first -%>
4
+ <%= render :partial => 'batch_create_form_horizontal_header', :locals => {:columns => columns, :record => @record} %>
5
+
6
+ <tbody id="<%= element_form_id :action => form_action %>-content" class="multiple">
7
+ <% first = true -%>
8
+ <% records.each do |scope, @record| %>
9
+ <%= render :partial => 'batch_create_form_horizontal_record', :locals => { :columns => columns, :form_action => form_action, :scope => "[#{scope}]", :first => first } %>
10
+ <% first = false -%>
11
+ <% end -%>
12
+ </tbody>
13
+ </table>
14
+ </div>
@@ -0,0 +1,8 @@
1
+ <thead>
2
+ <tr>
3
+ <% columns.each :flatten => true do |column| -%>
4
+ <% next if column_renders_as(column) == :hidden -%>
5
+ <th class="<%= 'required' if column.required? %>"><label><%= column.label %></label></th>
6
+ <% end -%>
7
+ </tr>
8
+ </thead>
@@ -0,0 +1,26 @@
1
+ <% first = true unless local_assigns.has_key? :first -%>
2
+ <% if @record.errors.count -%>
3
+ <tr class="form_record-errors">
4
+ <td colspan="<%= columns.length + 1 %>" id="<%= element_messages_id :action => @record.class.name.underscore %>">
5
+ <%= active_scaffold_error_messages_for :record, :object_name => @record.class.model_name.human.downcase %>
6
+ </td>
7
+ </tr>
8
+ <% end %>
9
+ <tr class="form_record">
10
+ <% columns.each :flatten => true do |column| -%>
11
+ <%
12
+ next if column_renders_as(column) == :hidden
13
+ next unless @record.authorized_for?(:crud_type => :create, :column => column.name)
14
+ column = column.clone
15
+ column.form_ui ||= :select if column.association
16
+
17
+ col_class = []
18
+ col_class << 'required' if column.required?
19
+ col_class << column.css_class unless column.css_class.nil?
20
+ -%>
21
+ <td class="<%= col_class.join(' ') %>">
22
+ <%= render :partial => form_partial_for_column(column), :locals => { :column => column, :scope => scope } -%>
23
+ </td>
24
+ <% end -%>
25
+ <td><%= batch_create_multiple_remove_link unless first %></td>
26
+ </tr>
@@ -0,0 +1,8 @@
1
+ <%
2
+ records = if @error_records.present?
3
+ @error_records
4
+ else
5
+ {scope => @record}
6
+ end
7
+ -%>
8
+ <%= render :partial => batch_create_multiple_layout, :locals => { :columns => columns, :form_action => form_action, :records => records } %>
@@ -0,0 +1,7 @@
1
+ <div id="<%= element_form_id :action => form_action %>-content" class="multiple">
2
+ <% first = true -%>
3
+ <% records.each do |scope, @record| -%>
4
+ <%= render :partial => 'batch_create_form_vertical_record', :locals => { :columns => columns, :form_action => form_action, :scope => "[#{scope}]", :first => first } %>
5
+ <% first = false -%>
6
+ <% end -%>
7
+ </div>
@@ -0,0 +1,6 @@
1
+ <% first = true unless local_assigns.has_key? :first -%>
2
+ <div class="form_record">
3
+ <%= render :partial => 'form_messages' %>
4
+ <%= render :partial => 'form', :locals => { :columns => columns, :form_action => form_action, :scope => scope } %>
5
+ <%= batch_create_multiple_remove_link unless first %>
6
+ </div>
@@ -0,0 +1,7 @@
1
+ <%= render :partial => "base_form", :locals => {:xhr => xhr ||= nil,
2
+ :form_action => form_action ||= :batch_update,
3
+ :method => method ||= :post,
4
+ :cancel_link => cancel_link ||= true,
5
+ :body_partial => 'batch_update_form_body',
6
+ :headline => headline ||= active_scaffold_config.batch_update.label} %>
7
+
@@ -0,0 +1,12 @@
1
+ <% scope ||= nil %>
2
+ <dl>
3
+ <dt>
4
+ <label for="<%= active_scaffold_input_options(column, scope)[:id] %>"><%= column.label %></label>
5
+ </dt>
6
+ <dd>
7
+ <%= active_scaffold_update_for column, scope %>
8
+ <% if column.description -%>
9
+ <span class="description"><%= column.description %></span>
10
+ <% end -%>
11
+ </dd>
12
+ </dl>
@@ -0,0 +1,14 @@
1
+ <% scope ||= nil
2
+ scope_select_options = active_scaffold_update_scope_select_options %>
3
+ <% if scope_select_options.length < 2 %>
4
+ <%= active_scaffold_update_scope_select(scope_select_options) %>
5
+ <% else %>
6
+ <dl>
7
+ <dt>
8
+ <label for="record_batch_scope"><%= as_(:records) %></label>
9
+ </dt>
10
+ <dd>
11
+ <%= active_scaffold_update_scope_select(scope_select_options) %>
12
+ </dd>
13
+ </dl>
14
+ <% end %>
@@ -0,0 +1,25 @@
1
+ <% subsection_id ||= nil %>
2
+ <ol class="form" <%= "id=#{subsection_id}" unless subsection_id.nil? %> <%= 'style="display: none;"' if columns.collapsed -%>>
3
+ <li class="form-element">
4
+ <%= render :partial => 'batch_update_form_attribute_scope' -%>
5
+ </li>
6
+ <% columns.each :for => @record do |column| %>
7
+ <% next if column.plural_association? || (column.association && [:has_one].include?(column.association.macro)) %>
8
+ <% renders_as = column_renders_as(column) %>
9
+ <% if renders_as == :subsection -%>
10
+ <% subsection_id = sub_section_id(:sub_section => column.label) %>
11
+ <li class="sub-section">
12
+ <h5><%= column.label %></h5>
13
+ <%= render :partial => 'form', :locals => { :columns => column, :subsection_id => subsection_id} %>
14
+ <%= link_to_visibility_toggle(subsection_id, {:default_visible => !column.collapsed}) -%>
15
+ </li>
16
+ <% elsif column.readonly_association?
17
+ next %>
18
+ <% else
19
+ renders_as = :field if renders_as == :subform -%>
20
+ <li class="form-element <%= column.css_class unless column.css_class.nil? %>">
21
+ <%= render :partial => 'batch_update_form_attribute', :locals => { :column => column } -%>
22
+ </li>
23
+ <% end -%>
24
+ <% end -%>
25
+ </ol>
@@ -0,0 +1,9 @@
1
+ <% if @error_records.present? && @record_errors.is_a?(Array) -%>
2
+ <%= render :partial => 'messages' unless request.xhr? %>
3
+
4
+ <% @error_records.each do |record| %>
5
+ <%= active_scaffold_error_messages_for record, :object_name => "#{record.class.model_name.human.downcase}#{record.new_record? ? '' : ": #{record.to_label}"}" %>
6
+ <% end %>
7
+ <% else %>
8
+ <%= render :super %>
9
+ <% end %>
@@ -0,0 +1,5 @@
1
+ <% html = ''.html_safe -%>
2
+ <% @records.each do |scope, @record| -%>
3
+ <% html << render(:partial => "#{batch_create_multiple_layout}_record", :locals => { :columns => active_scaffold_config.batch_create.columns, :form_action => :batch_create, :scope => "[#{scope}]", :first => false }) %>
4
+ <% end %>
5
+ ActiveScaffold.create_associated_record_form('<%= element_form_id(:action => :batch_create) %>-content','<%= escape_javascript(html) %>', <%= {:singular => false}.to_json.html_safe %>);
@@ -0,0 +1,5 @@
1
+ <div class="active-scaffold">
2
+ <div class="batch-create-view <%= "#{params[:controller]}-view" %> view">
3
+ <%= render :partial => 'batch_create_form' -%>
4
+ </div>
5
+ </div>
@@ -0,0 +1,5 @@
1
+ <div class="active-scaffold">
2
+ <div class="batch-update-view <%= "#{params[:controller]}-view" %> view">
3
+ <%= render :partial => 'batch_update_form' -%>
4
+ </div>
5
+ </div>
@@ -0,0 +1,10 @@
1
+ <%
2
+ if @error_records.empty?
3
+ flash[:info] = as_(:batch_processing_successful) if flash[:info].blank?
4
+ else
5
+ flash[:error] = @error_records.collect do |_, record|
6
+ active_scaffold_error_messages_for(record, :object_name => "#{record.class.model_name.human.downcase}#{record.new_record? ? '' : ": #{record.to_label}"}", :header_message => '', :message => "#{record.class.model_name.human.downcase}#{record.new_record? ? '' : ": #{record.to_label}"}", :container_tag => nil, :list_type => :br)
7
+ end.join.html_safe
8
+ end
9
+ %>
10
+ ActiveScaffold.replace_html('<%= active_scaffold_content_id%>','<%=escape_javascript(render(:partial => 'list', :layout => false))%>');
@@ -0,0 +1,15 @@
1
+ <% form_selector = "#{element_form_id(:action => :batch_create)}" %>
2
+
3
+ <% if controller.send :batch_successful? %>
4
+ <% called_by_controller = params.delete(:batch_create_by) %>
5
+ <% if called_by_controller.nil? %>
6
+ ActiveScaffold.replace_html('<%= active_scaffold_content_id %>','<%= escape_javascript(render(:partial => 'list', :layout => false)) %>');
7
+ <% else %>
8
+ ActiveScaffold.mark_records('<%= "#{controller_id(called_by_controller)}-tbody" %>', <%= {:checked => false, :include_checkboxes => true}.to_json.html_safe %>);
9
+ <% end %>
10
+ ActiveScaffold.find_action_link('<%= form_selector %>').close();
11
+ <% else %>
12
+ ActiveScaffold.find_action_link('<%= form_selector %>').update_flash_messages('<%= escape_javascript(render(:partial => 'messages')) %>');
13
+ ActiveScaffold.replace('<%= form_selector %>', '<%= escape_javascript(render(:partial => 'batch_create_form', :locals => {:xhr => true})) %>');
14
+ ActiveScaffold.scroll_to('<%= form_selector %>', true);
15
+ <% end %>
@@ -0,0 +1,10 @@
1
+ <% form_selector = "#{element_form_id(:action => :batch_update)}" %>
2
+
3
+ <% if controller.send :batch_successful? %>
4
+ ActiveScaffold.replace_html('<%= active_scaffold_content_id %>','<%= escape_javascript(render(:partial => 'list', :layout => false)) %>');
5
+ ActiveScaffold.find_action_link('<%= form_selector %>').close();
6
+ <% else %>
7
+ ActiveScaffold.find_action_link('<%= form_selector %>').update_flash_messages('<%= escape_javascript(render(:partial => 'messages')) %>');
8
+ ActiveScaffold.replace('<%= form_selector %>', '<%= escape_javascript(render(:partial => 'batch_update_form', :locals => {:xhr => true})) %>');
9
+ ActiveScaffold.scroll_to('<%= form_selector %>', true);
10
+ <% end %>
@@ -0,0 +1,22 @@
1
+ de:
2
+ active_scaffold:
3
+ no_update: '- Auswählen -'
4
+ replace: Ersetzen
5
+ plus: '+'
6
+ minus: '-'
7
+ times: '*'
8
+ division: '/'
9
+ absolute: abs
10
+ percent: '%'
11
+ batch_update: 'Speichern'
12
+ add_some_records: 'Add %{number_input} %{model}'
13
+ records: 'Records'
14
+ batch_processing_successful: "Batch processing successful"
15
+ batch: Batch
16
+ some_records_created:
17
+ one: "1 %{model} was created"
18
+ other: "%{count} %{model} were created"
19
+ delete_model: "Delete %{model}"
20
+ listed: listed
21
+ marked: marked
22
+
@@ -0,0 +1,21 @@
1
+ en:
2
+ active_scaffold:
3
+ no_update: No Update
4
+ replace: Replace
5
+ plus: '+'
6
+ minus: '-'
7
+ times: '*'
8
+ division: '/'
9
+ absolute: abs
10
+ percent: '%'
11
+ batch_update: 'Batch Update'
12
+ add_some_records: 'Add %{number_input} %{model}'
13
+ records: 'Records'
14
+ batch_processing_successful: "Batch processing successful"
15
+ batch: Batch
16
+ some_records_created:
17
+ one: "1 %{model} was created"
18
+ other: "%{count} %{model} were created"
19
+ delete_model: "Delete %{model}"
20
+ listed: listed
21
+ marked: marked
@@ -0,0 +1,21 @@
1
+ es:
2
+ active_scaffold:
3
+ no_update: No Actualizar
4
+ replace: Reemplazar
5
+ plus: '+'
6
+ minus: '-'
7
+ times: '*'
8
+ division: '/'
9
+ absolute: abs
10
+ percent: '%'
11
+ batch_update: 'Actualizar Lote'
12
+ add_some_records: 'Añadir %{number_input} %{model}'
13
+ records: 'Registros'
14
+ batch_processing_successful: "Proceso en lote realizado correctamente"
15
+ batch: Lote
16
+ some_records_created:
17
+ one: "Se creo 1 %{model}"
18
+ other: "Se crearon %{count} %{model}"
19
+ delete_model: "Eliminar %{model}"
20
+ listed: listados
21
+ marked: marcados
@@ -0,0 +1,135 @@
1
+ module ActiveScaffold::Actions
2
+ module BatchBase
3
+
4
+ def self.included(base)
5
+ base.helper_method :batch_scope
6
+ end
7
+
8
+ protected
9
+
10
+ def batch_action(batch_action = :batch_base)
11
+ process_action_link_action(batch_action) do
12
+ process_batch
13
+ end
14
+ end
15
+
16
+ def batch_scope
17
+ if @batch_scope.nil? && params[:batch_scope]
18
+ @batch_scope = params[:batch_scope] if ['LISTED', 'MARKED'].include?(params[:batch_scope])
19
+ params.delete :batch_scope
20
+ end
21
+ @batch_scope
22
+ end
23
+
24
+ def error_records
25
+ @error_records ||= []
26
+ end
27
+
28
+ def set_record_attribute(column, attribute, value)
29
+ form_ui = column_form_ui(column)
30
+ if form_ui && (method = send("override_#{action_name}_value", form_ui))
31
+ @record.send("#{attribute}=", send(method, column, @record, value))
32
+ else
33
+ @record.send("#{attribute}=", action_name == 'batch_update' ? value[:value] : value)
34
+ end
35
+ end
36
+
37
+ def column_form_ui(column)
38
+ form_ui = column.form_ui
39
+ form_ui = column.column.type if form_ui.nil? && column.column
40
+ form_ui
41
+ end
42
+
43
+ # in case of an error we have to prepare @record object to have assigned all
44
+ # defined batch_update values, however, do not set those ones with an override
45
+ # these ones will manage on their own
46
+ def prepare_error_record
47
+ do_new
48
+ send("#{action_name}_values").each do |attribute, value|
49
+ form_ui = column_form_ui(value[:column])
50
+ set_record_attribute(value[:column], attribute, value[:value]) unless form_ui && send("override_#{action_name}_value", form_ui)
51
+ end
52
+ end
53
+
54
+ def batch_successful?
55
+ error_records.empty?
56
+ end
57
+
58
+ def process_batch
59
+ send("before_do_#{action_name}")
60
+ send("#{action_name}_#{batch_scope.downcase}") if !batch_scope.nil? && respond_to?("#{action_name}_#{batch_scope.downcase}")
61
+ prepare_error_record unless batch_successful?
62
+ end
63
+
64
+ def authorized_for_job?(record)
65
+ if record.authorized_for?(:crud_type => active_scaffold_config.send(action_name).crud_type)
66
+ true
67
+ else
68
+ record.errors.add(:base, as_(:no_authorization_for_action, :action => action_name))
69
+ error_records << record
70
+ false
71
+ end
72
+ end
73
+
74
+ def temporary_id
75
+ (Time.now.to_f*1000).to_i.to_s
76
+ end
77
+
78
+ def batch_base_respond_to_html
79
+ if respond_to? "#{action_name}_respond_to_html"
80
+ send("#{action_name}_respond_to_html")
81
+ else
82
+ if params[:iframe]=='true' # was this an iframe post ?
83
+ do_refresh_list
84
+ responds_to_parent do
85
+ render :action => 'on_batch_base.js', :layout => false
86
+ end
87
+ else # just a regular post
88
+ flash[:info] = as_(:batch_processing_successful) if batch_successful?
89
+ return_to_main
90
+ end
91
+ end
92
+ end
93
+
94
+ def batch_base_respond_to_js
95
+ if respond_to? "#{action_name}_respond_to_js"
96
+ send("#{action_name}_respond_to_js")
97
+ else
98
+ do_refresh_list
99
+ render :action => "on_batch_base"
100
+ end
101
+ end
102
+
103
+ def batch_base_respond_to_xml
104
+ if respond_to? "#{action_name}_respond_to_xml"
105
+ send("#{action_name}_respond_to_xml")
106
+ else
107
+ render :xml => response_object.to_xml(:only => active_scaffold_config.send(action_name).columns.names), :content_type => Mime::XML, :status => response_status
108
+ end
109
+ end
110
+
111
+ def batch_base_respond_to_json
112
+ if respond_to? "#{action_name}_respond_to_json"
113
+ send("#{action_name}_respond_to_json")
114
+ else
115
+ render :text => response_object.to_json(:only => active_scaffold_config.send(action_name).columns.names), :content_type => Mime::JSON, :status => response_status
116
+ end
117
+ end
118
+
119
+ def batch_base_respond_to_yaml
120
+ if respond_to? "#{action_name}_respond_to_yaml"
121
+ send("#{action_name}_respond_to_yaml")
122
+ else
123
+ render :text => Hash.from_xml(response_object.to_xml(:only => active_scaffold_config.send(action_name).columns.names)).to_yaml, :content_type => Mime::YAML, :status => response_status
124
+ end
125
+ end
126
+
127
+ def batch_base_formats
128
+ if respond_to? "#{action_name}_formats"
129
+ send("#{action_name}_formats")
130
+ else
131
+ (default_formats + active_scaffold_config.formats + active_scaffold_config.send(action_name).formats).uniq
132
+ end
133
+ end
134
+ end
135
+ end