Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

prepare subform sortable and use more unobtrusive code

  • Loading branch information...
commit 1bc3bfc6f7fcf0dd0620ffe047c9f2d4628087dc 1 parent 19965cb
scambra authored
2  Gemfile
@@ -9,6 +9,6 @@ gemspec
9 9 # Include everything needed to run rake, tests, features, etc.
10 10 group :development do
11 11 gem "shoulda", ">= 0"
12   - gem "bundler", "~> 1.0.0"
  12 + gem "bundler", ">= 1.0.0"
13 13 gem "rcov", ">= 0"
14 14 end
85 Gemfile.lock
... ... @@ -1,100 +1,19 @@
1 1 PATH
2 2 remote: .
3 3 specs:
4   - active_scaffold_sortable (3.1.2)
5   - active_scaffold (~> 3.0)
  4 + active_scaffold_sortable (3.2.0)
6 5
7 6 GEM
8 7 remote: http://rubygems.org/
9 8 specs:
10   - actionmailer (3.1.3)
11   - actionpack (= 3.1.3)
12   - mail (~> 2.3.0)
13   - actionpack (3.1.3)
14   - activemodel (= 3.1.3)
15   - activesupport (= 3.1.3)
16   - builder (~> 3.0.0)
17   - erubis (~> 2.7.0)
18   - i18n (~> 0.6)
19   - rack (~> 1.3.5)
20   - rack-cache (~> 1.1)
21   - rack-mount (~> 0.8.2)
22   - rack-test (~> 0.6.1)
23   - sprockets (~> 2.0.3)
24   - active_scaffold (3.1.15)
25   - rails (~> 3.1.0)
26   - activemodel (3.1.3)
27   - activesupport (= 3.1.3)
28   - builder (~> 3.0.0)
29   - i18n (~> 0.6)
30   - activerecord (3.1.3)
31   - activemodel (= 3.1.3)
32   - activesupport (= 3.1.3)
33   - arel (~> 2.2.1)
34   - tzinfo (~> 0.3.29)
35   - activeresource (3.1.3)
36   - activemodel (= 3.1.3)
37   - activesupport (= 3.1.3)
38   - activesupport (3.1.3)
39   - multi_json (~> 1.0)
40   - arel (2.2.1)
41   - builder (3.0.0)
42   - erubis (2.7.0)
43   - hike (1.2.1)
44   - i18n (0.6.0)
45   - json (1.6.3)
46   - mail (2.3.0)
47   - i18n (>= 0.4.0)
48   - mime-types (~> 1.16)
49   - treetop (~> 1.4.8)
50   - mime-types (1.17.2)
51   - multi_json (1.0.4)
52   - polyglot (0.3.3)
53   - rack (1.3.5)
54   - rack-cache (1.1)
55   - rack (>= 0.4)
56   - rack-mount (0.8.3)
57   - rack (>= 1.0.0)
58   - rack-ssl (1.3.2)
59   - rack
60   - rack-test (0.6.1)
61   - rack (>= 1.0)
62   - rails (3.1.3)
63   - actionmailer (= 3.1.3)
64   - actionpack (= 3.1.3)
65   - activerecord (= 3.1.3)
66   - activeresource (= 3.1.3)
67   - activesupport (= 3.1.3)
68   - bundler (~> 1.0)
69   - railties (= 3.1.3)
70   - railties (3.1.3)
71   - actionpack (= 3.1.3)
72   - activesupport (= 3.1.3)
73   - rack-ssl (~> 1.3.2)
74   - rake (>= 0.8.7)
75   - rdoc (~> 3.4)
76   - thor (~> 0.14.6)
77   - rake (0.9.2.2)
78 9 rcov (0.9.9)
79   - rdoc (3.12)
80   - json (~> 1.4)
81 10 shoulda (2.11.3)
82   - sprockets (2.0.3)
83   - hike (~> 1.2)
84   - rack (~> 1.0)
85   - tilt (~> 1.1, != 1.3.0)
86   - thor (0.14.6)
87   - tilt (1.3.3)
88   - treetop (1.4.10)
89   - polyglot
90   - polyglot (>= 0.3.1)
91   - tzinfo (0.3.31)
92 11
93 12 PLATFORMS
94 13 ruby
95 14
96 15 DEPENDENCIES
97 16 active_scaffold_sortable!
98   - bundler (~> 1.0.0)
  17 + bundler (>= 1.0.0)
99 18 rcov
100 19 shoulda
BIN  app/assets/images/active_scaffold/sortable.png
1  app/assets/javascripts/active_scaffold_sortable.js.erb
... ... @@ -0,0 +1 @@
  1 +<% require_asset "#{ActiveScaffold.js_framework}/active_scaffold_sortable" %>
28 app/assets/javascripts/jquery/active_scaffold_sortable.js
... ... @@ -0,0 +1,28 @@
  1 +ActiveScaffold.sortable = function(element) {
  2 + var form = element.closest('form.as_form'), content, sortable_options = {};
  3 + if (form.length) content = element.find('.sub-form:first');
  4 + else content = element.find('.records:first');
  5 + if (element.data('update')) {
  6 + var csrf = {};
  7 + csrf[jQuery('meta[name=csrf-param]').attr('content')] = jQuery('meta[name=csrf-token]').attr('content');
  8 + var url = element.data('reorder-url').append_params(csrf);
  9 + sortable_options.update = function(event, ui) {
  10 + var body = jQuery(this).sortable('serialize',{key: encodeURIComponent(jQuery(this).attr('id') + '[]'), expression: new RegExp(element.data('format'))});
  11 + var params = element.data('with');
  12 + if (params) url += '&' + params;
  13 + jQuery.post(url, {data: body});
  14 + }
  15 + }
  16 + sortable_options.handle = element.data('handle');
  17 + sortable_options.items = element.data('tag');
  18 + content.sortable(sortable_options);
  19 +};
  20 +
  21 +jQuery(document).ready(function($) {
  22 + $(document).on('as:action_success', 'a.as_action', function(e, action_link) {
  23 + var sortable = $('.sortable-container', action_link.adapter);
  24 + if (sortable.length) $.each(sortable, function(i, s) { ActiveScaffold.sortable(s); });
  25 + });
  26 + var sortable = $('.sortable-container');
  27 + if (sortable.length) ActiveScaffold.sortable(sortable);
  28 +});
28 app/assets/javascripts/prototype/active_scaffold_sortable.js
... ... @@ -0,0 +1,28 @@
  1 +ActiveScaffold.sortable = function(element) {
  2 + var form = element.up('form.as_form'), content, sortable_options = {};
  3 + if (form) content = element.down('.sub-form');
  4 + else content = element.down('.records');
  5 + if (element.readAttribute('data-update')) {
  6 + var csrf = {};
  7 + csrf[$$('meta[name=csrf-param]')[0].readAttribute('content')] = $$('meta[name=csrf-token]')[0].readAttribute('content');
  8 + var url = element.readAttribute('data-reorder-url').append_params(csrf);
  9 + sortable_options.onUpdate = function() {
  10 + var body = Sortable.serialize(content);
  11 + var params = element.readAttribute('data-with');
  12 + if (params) url += '&' + params;
  13 + new Ajax.Request(url, {method: 'post', parameters: body});
  14 + }
  15 + }
  16 + sortable_options.handle = element.readAttribute('data-handle');
  17 + sortable_options.items = element.readAttribute('data-tag');
  18 + Sortable.create(content, sortable_options);
  19 +};
  20 +
  21 +document.observe('dom:loaded', function(){
  22 + document.on('as:action_success', 'a.as_action', function(e, action_link) {
  23 + var sortable = $(action_link.adapter).find('.sortable-container');
  24 + if (sortable.length) sortable.invoke(function(s) { ActiveScaffold.sortable(s); });
  25 + });
  26 + var sortable = $$('.sortable-container');
  27 + if (sortable.length) ActiveScaffold.sortable(sortable[0]);
  28 +});
3  app/assets/stylesheets/active_scaffold_sortable.css
... ... @@ -1,3 +0,0 @@
1   -.sortable-handle {
2   - cursor: move;
3   -}
5 app/assets/stylesheets/active_scaffold_sortable.css.scss
... ... @@ -0,0 +1,5 @@
  1 +.sortable-handle {
  2 + cursor: move;
  3 + background: image-url('active_scaffold/sortable.png') no-repeat center center;
  4 + width: 16px;
  5 +}
1  frontends/default/active_scaffold_overrides/_form_association.html.erb
... ... @@ -0,0 +1 @@
  1 +<%= content_tag :div, render(:super), :class => 'sortable-container' :data => sort_params %>
9 frontends/default/active_scaffold_overrides/_list.html.erb
... ... @@ -1,8 +1 @@
1   -<%= render :super %>
2   -<% if ActiveScaffold.js_framework == :prototype %>
3   -<%= sortable_element *sort_params %>
4   -<% elsif ActiveScaffold.js_framework == :jquery %>
5   - <% reorder_params = params_for.except(:controller, :action)
6   - options = {:update => true, :action => 'reorder'}.merge(active_scaffold_config.sortable.options) %>
7   - <%= javascript_tag "ActiveScaffold.sortable('#{sort_params[0]}', #{url_for(:controller => controller_name).to_json}, #{options.to_json}, #{reorder_params.to_json});" %>
8   -<% end %>
  1 +<%= content_tag :div, render(:super), :class => 'sortable-container', :data => sort_params %>
1  lib/active_scaffold/actions/sortable.rb
@@ -12,6 +12,7 @@ def self.included(base)
12 12 config.list.sorting = { sortable_column => "asc" }
13 13
14 14 config.actions.each do |action_name|
  15 + next if action_name == :subform
15 16 action = config.send(action_name)
16 17 action.columns.exclude(sortable_column) if action.respond_to? :columns
17 18 end
10 lib/active_scaffold/config/sortable.rb
@@ -30,7 +30,14 @@ def initialize(core_config)
30 30 attr_reader :column
31 31 def column=(column_name)
32 32 @column = @core.columns[column_name]
33   - Rails.logger.error("ActiveScaffold Sortable: postion column: #{column_name} not found in model") if @column.nil?
  33 + if @column.nil?
  34 + Rails.logger.error("ActiveScaffold Sortable: postion column: #{column_name} not found in model")
  35 + else
  36 + @column.form_ui = :hidden
  37 + @column.css_class = 'sortable-handle'
  38 + @column.label = ''
  39 + @column.weight = -2**(0.size * 8 -2)
  40 + end
34 41 @column
35 42 end
36 43
@@ -46,6 +53,7 @@ def add_handle_column=(where)
46 53 if where == :first
47 54 @core.list.columns = [:active_scaffold_sortable] + @core.list.columns.names_without_auth_check unless @core.list.columns.include? :active_scaffold_sortable
48 55 else
  56 + @column.weight = 2**(0.size * 8 -2) -1
49 57 @core.list.columns.add :active_scaffold_sortable
50 58 end
51 59 end
14 lib/active_scaffold/helpers/sortable_helpers.rb
@@ -3,17 +3,11 @@ module SortableHelpers
3 3 def sort_params
4 4 options = {
5 5 :tag => 'tr',
6   - :url => {:action => :reorder, :controller => controller_name },
7   - :format => '/^[^_-](?:[A-Za-z0-9_-]*)-(.*)-row$/',
8   - :with => "Sortable.serialize(#{active_scaffold_tbody_id.to_json})"
  6 + :reorder_url => url_for(params_for(:action => :reorder, :controller => controller_name)),
  7 + :format => '^[^_-](?:[A-Za-z0-9_-]*)-(.*)-row$',
  8 + :update => true
9 9 }
10   - additional_params = [:parent_controller, :eid, :controller].reject {|param| params[param].blank?}
11   - options[:with] = additional_params.inject(options[:with]) do |string, param|
12   - "#{string} + '&#{param}=' + encodeURIComponent('#{escape_javascript params[param]}')"
13   - end
14   - options.merge! active_scaffold_config.sortable.options
15   -
16   - [active_scaffold_tbody_id, options]
  10 + options.merge active_scaffold_config.sortable.options
17 11 end
18 12
19 13 def list_row_class(record)
2  lib/active_scaffold_sortable.rb
@@ -21,6 +21,6 @@ module Helpers
21 21 ActiveScaffold.autoload_subdir('helpers', self, File.dirname(__FILE__))
22 22 end
23 23 end
24   -
25 24 ActiveScaffold::Config::Core.send :include, ActiveScaffoldSortable::Core
26 25 ActiveScaffold.stylesheets << 'active_scaffold_sortable'
  26 +ActiveScaffold.javascripts << 'active_scaffold_sortable'
5 lib/active_scaffold_sortable/core.rb
@@ -7,7 +7,10 @@ def self.included(base)
7 7 def initialize_with_sortable(model_id)
8 8 initialize_without_sortable(model_id)
9 9 # seems some rubies are returning strings in instance_methods and other symbols...
10   - self.actions << :sortable if !(model.instance_methods & ['acts_as_list_class', :acts_as_list_class, 'nested_set_scope', :nested_set_scope]).empty?
  10 + if !(model.instance_methods & ['acts_as_list_class', :acts_as_list_class, 'nested_set_scope', :nested_set_scope]).empty?
  11 + self.actions << :sortable
  12 + self.sortable # force to load
  13 + end
11 14 end
12 15 end
13 16 end

0 comments on commit 1bc3bfc

Please sign in to comment.
Something went wrong with that request. Please try again.