Permalink
Browse files

sort subforms

  • Loading branch information...
1 parent cc84a72 commit 5d41298460044bfcdf8e5278c36f13ae3b44f7a9 scambra committed Dec 17, 2012
View
26 app/assets/javascripts/jquery/active_scaffold_sortable.js
@@ -1,23 +1,29 @@
ActiveScaffold.sortable = function(element) {
- var content, sortable_options = {};
+ var form, content, sortable_options = {};
if (typeof(element) == 'string') {
- content = $('#' + element);
+ content = jQuery('#' + element);
element = content.closest('.sortable-container');
} else {
- if (element.closest('form.as_form').length) content = element.find('.sub-form:first');
+ var form = element.closest('form.as_form').length > 0;
+ if (form) content = element;
else content = element.find('.records:first');
}
- if (element.data('update')) {
- var csrf = {};
- var params = jQuery('meta[name=csrf-param]').attr('content') + '=' + jQuery('meta[name=csrf-token]').attr('content');
+ if (form) {
+ sortable_options.update = function(event, ui) {
+ jQuery.each(content.find('.association-record input[name$="[' + element.data('column') + ']"]'), function(i, field) {
+ jQuery(field).val(i);
+ });
+ };
+ } else {
+ var csrf = jQuery('meta[name=csrf-param]').attr('content') + '=' + jQuery('meta[name=csrf-token]').attr('content');
var url = element.data('reorder-url');
sortable_options.update = function(event, ui) {
var body = jQuery(this).sortable('serialize',{key: encodeURIComponent(jQuery(this).attr('id') + '[]'), expression: new RegExp(element.data('format'))});
- var extra_params = element.data('with');
- if (extra_params) body += '&' + extra_params;
- jQuery.post(url, body + '&' + params);
- }
+ var params = element.data('with');
+ if (params) body += '&' + params;
+ jQuery.post(url, body + '&' + csrf);
+ };
}
sortable_options.handle = element.data('handle');
sortable_options.items = element.data('tag');
View
22 app/assets/javascripts/prototype/active_scaffold_sortable.js
@@ -1,16 +1,22 @@
ActiveScaffold.sortable = function(element) {
- var form = element.up('form.as_form'), content, sortable_options = {};
- if (form) content = element.down('.sub-form');
- else content = element.down('.records');
- if (element.readAttribute('data-update')) {
- var csrf = {};
- csrf[$$('meta[name=csrf-param]')[0].readAttribute('content')] = $$('meta[name=csrf-token]')[0].readAttribute('content');
+ var form, content, sortable_options = {};
+ if (typeof(element) == 'string') {
+ content = $(element);
+ element = content.up('.sortable-container');
+ } else {
+ var form = !!element.up('form.as_form');
+ if (form) content = element;
+ else content = element.down('.records');
+ }
+
+ if (!form) {
+ var csrf = $$('meta[name=csrf-param]')[0].readAttribute('content') + '=' + $$('meta[name=csrf-token]')[0].readAttribute('content');
var url = element.readAttribute('data-reorder-url').append_params(csrf);
sortable_options.onUpdate = function() {
var body = Sortable.serialize(content);
var params = element.readAttribute('data-with');
- if (params) url += '&' + params;
- new Ajax.Request(url, {method: 'post', parameters: body});
+ if (params) body += '&' + params;
+ new Ajax.Request(url, {method: 'post', parameters: body + '&' + csrf});
}
}
sortable_options.handle = element.readAttribute('data-handle');
View
6 app/views/active_scaffold_overrides/_form_association.html.erb
@@ -0,0 +1,6 @@
+<% config = active_scaffold_config_for(column.association.klass) %>
+<% if config.actions.include? :sortable %>
+<%= content_tag :div, render(:super), :class => 'sortable-container', :data => sort_params(config) %>
+<% else %>
+<%= render :super %>
+<% end %>
View
5 app/views/active_scaffold_overrides/edit_associated.js.erb
@@ -0,0 +1,5 @@
+<%= render :super %>
+<% config = active_scaffold_config_for(@column.association.klass) %>
+<% if config.actions.include? :sortable %>
+ActiveScaffold.sortable('<%= sub_form_list_id(:association => @column.name) %>');
+<% end %>
View
1 frontends/default/active_scaffold_overrides/_form_association.html.erb
@@ -1 +0,0 @@
-<%= content_tag :div, render(:super), :class => 'sortable-container', :data => sort_params %>
View
14 lib/active_scaffold/helpers/sortable_helpers.rb
@@ -1,15 +1,5 @@
module ActiveScaffold::Helpers
module SortableHelpers
- def sort_params
- options = {
- :tag => 'tr',
- :reorder_url => url_for(params_for(:action => :reorder, :controller => controller_name)),
- :format => '^[^_-](?:[A-Za-z0-9_-]*)-(.*)-row$',
- :update => true
- }
- options.merge active_scaffold_config.sortable.options
- end
-
def list_row_class(record)
"#{'sortable-handle' unless active_scaffold_config.sortable.add_handle_column} #{super}"
end
@@ -21,9 +11,5 @@ def column_class(column, column_value, record)
super
end
end
-
- def active_scaffold_sortable_column(record, column)
- ''
- end
end
end
View
2 lib/active_scaffold_sortable.rb
@@ -6,6 +6,7 @@ module ActiveScaffoldSortable
def self.root
File.dirname(__FILE__) + "/.."
end
+ autoload 'ViewHelpers', 'active_scaffold_sortable/view_helpers.rb'
end
module ActiveScaffold
@@ -21,6 +22,7 @@ module Helpers
ActiveScaffold.autoload_subdir('helpers', self, File.dirname(__FILE__))
end
end
+ActionView::Base.send :include, ActiveScaffoldSortable::ViewHelpers
ActiveScaffold::Config::Core.send :include, ActiveScaffoldSortable::Core
ActiveScaffold.stylesheets << 'active_scaffold_sortable'
ActiveScaffold.javascripts << 'active_scaffold_sortable'
View
17 lib/active_scaffold_sortable/view_helpers.rb
@@ -0,0 +1,17 @@
+module ActiveScaffoldSortable
+ module ViewHelpers
+ def sort_params(config = active_scaffold_config)
+ options = {
+ :tag => 'tr',
+ :reorder_url => url_for(params_for(:action => :reorder, :controller => controller_name)),
+ :format => '^[^_-](?:[A-Za-z0-9_-]*)-(.*)-row$',
+ :column => config.sortable.column.name
+ }
+ options.merge config.sortable.options
+ end
+
+ def active_scaffold_sortable_column(record, column)
+ ''
+ end
+ end
+end

0 comments on commit 5d41298

Please sign in to comment.