Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

prepare subform sortable and use more unobtrusive code

  • Loading branch information...
commit 1bc3bfc6f7fcf0dd0620ffe047c9f2d4628087dc 1 parent 19965cb
scambra authored
View
2  Gemfile
@@ -9,6 +9,6 @@ gemspec
# Include everything needed to run rake, tests, features, etc.
group :development do
gem "shoulda", ">= 0"
- gem "bundler", "~> 1.0.0"
+ gem "bundler", ">= 1.0.0"
gem "rcov", ">= 0"
end
View
85 Gemfile.lock
@@ -1,100 +1,19 @@
PATH
remote: .
specs:
- active_scaffold_sortable (3.1.2)
- active_scaffold (~> 3.0)
+ active_scaffold_sortable (3.2.0)
GEM
remote: http://rubygems.org/
specs:
- actionmailer (3.1.3)
- actionpack (= 3.1.3)
- mail (~> 2.3.0)
- actionpack (3.1.3)
- activemodel (= 3.1.3)
- activesupport (= 3.1.3)
- builder (~> 3.0.0)
- erubis (~> 2.7.0)
- i18n (~> 0.6)
- rack (~> 1.3.5)
- rack-cache (~> 1.1)
- rack-mount (~> 0.8.2)
- rack-test (~> 0.6.1)
- sprockets (~> 2.0.3)
- active_scaffold (3.1.15)
- rails (~> 3.1.0)
- activemodel (3.1.3)
- activesupport (= 3.1.3)
- builder (~> 3.0.0)
- i18n (~> 0.6)
- activerecord (3.1.3)
- activemodel (= 3.1.3)
- activesupport (= 3.1.3)
- arel (~> 2.2.1)
- tzinfo (~> 0.3.29)
- activeresource (3.1.3)
- activemodel (= 3.1.3)
- activesupport (= 3.1.3)
- activesupport (3.1.3)
- multi_json (~> 1.0)
- arel (2.2.1)
- builder (3.0.0)
- erubis (2.7.0)
- hike (1.2.1)
- i18n (0.6.0)
- json (1.6.3)
- mail (2.3.0)
- i18n (>= 0.4.0)
- mime-types (~> 1.16)
- treetop (~> 1.4.8)
- mime-types (1.17.2)
- multi_json (1.0.4)
- polyglot (0.3.3)
- rack (1.3.5)
- rack-cache (1.1)
- rack (>= 0.4)
- rack-mount (0.8.3)
- rack (>= 1.0.0)
- rack-ssl (1.3.2)
- rack
- rack-test (0.6.1)
- rack (>= 1.0)
- rails (3.1.3)
- actionmailer (= 3.1.3)
- actionpack (= 3.1.3)
- activerecord (= 3.1.3)
- activeresource (= 3.1.3)
- activesupport (= 3.1.3)
- bundler (~> 1.0)
- railties (= 3.1.3)
- railties (3.1.3)
- actionpack (= 3.1.3)
- activesupport (= 3.1.3)
- rack-ssl (~> 1.3.2)
- rake (>= 0.8.7)
- rdoc (~> 3.4)
- thor (~> 0.14.6)
- rake (0.9.2.2)
rcov (0.9.9)
- rdoc (3.12)
- json (~> 1.4)
shoulda (2.11.3)
- sprockets (2.0.3)
- hike (~> 1.2)
- rack (~> 1.0)
- tilt (~> 1.1, != 1.3.0)
- thor (0.14.6)
- tilt (1.3.3)
- treetop (1.4.10)
- polyglot
- polyglot (>= 0.3.1)
- tzinfo (0.3.31)
PLATFORMS
ruby
DEPENDENCIES
active_scaffold_sortable!
- bundler (~> 1.0.0)
+ bundler (>= 1.0.0)
rcov
shoulda
View
BIN  app/assets/images/active_scaffold/sortable.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
View
1  app/assets/javascripts/active_scaffold_sortable.js.erb
@@ -0,0 +1 @@
+<% require_asset "#{ActiveScaffold.js_framework}/active_scaffold_sortable" %>
View
28 app/assets/javascripts/jquery/active_scaffold_sortable.js
@@ -0,0 +1,28 @@
+ActiveScaffold.sortable = function(element) {
+ var form = element.closest('form.as_form'), content, sortable_options = {};
+ if (form.length) content = element.find('.sub-form:first');
+ else content = element.find('.records:first');
+ if (element.data('update')) {
+ var csrf = {};
+ csrf[jQuery('meta[name=csrf-param]').attr('content')] = jQuery('meta[name=csrf-token]').attr('content');
+ var url = element.data('reorder-url').append_params(csrf);
+ 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 params = element.data('with');
+ if (params) url += '&' + params;
+ jQuery.post(url, {data: body});
+ }
+ }
+ sortable_options.handle = element.data('handle');
+ sortable_options.items = element.data('tag');
+ content.sortable(sortable_options);
+};
+
+jQuery(document).ready(function($) {
+ $(document).on('as:action_success', 'a.as_action', function(e, action_link) {
+ var sortable = $('.sortable-container', action_link.adapter);
+ if (sortable.length) $.each(sortable, function(i, s) { ActiveScaffold.sortable(s); });
+ });
+ var sortable = $('.sortable-container');
+ if (sortable.length) ActiveScaffold.sortable(sortable);
+});
View
28 app/assets/javascripts/prototype/active_scaffold_sortable.js
@@ -0,0 +1,28 @@
+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 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});
+ }
+ }
+ sortable_options.handle = element.readAttribute('data-handle');
+ sortable_options.items = element.readAttribute('data-tag');
+ Sortable.create(content, sortable_options);
+};
+
+document.observe('dom:loaded', function(){
+ document.on('as:action_success', 'a.as_action', function(e, action_link) {
+ var sortable = $(action_link.adapter).find('.sortable-container');
+ if (sortable.length) sortable.invoke(function(s) { ActiveScaffold.sortable(s); });
+ });
+ var sortable = $$('.sortable-container');
+ if (sortable.length) ActiveScaffold.sortable(sortable[0]);
+});
View
3  app/assets/stylesheets/active_scaffold_sortable.css
@@ -1,3 +0,0 @@
-.sortable-handle {
- cursor: move;
-}
View
5 app/assets/stylesheets/active_scaffold_sortable.css.scss
@@ -0,0 +1,5 @@
+.sortable-handle {
+ cursor: move;
+ background: image-url('active_scaffold/sortable.png') no-repeat center center;
+ width: 16px;
+}
View
1  frontends/default/active_scaffold_overrides/_form_association.html.erb
@@ -0,0 +1 @@
+<%= content_tag :div, render(:super), :class => 'sortable-container' :data => sort_params %>
View
9 frontends/default/active_scaffold_overrides/_list.html.erb
@@ -1,8 +1 @@
-<%= render :super %>
-<% if ActiveScaffold.js_framework == :prototype %>
-<%= sortable_element *sort_params %>
-<% elsif ActiveScaffold.js_framework == :jquery %>
- <% reorder_params = params_for.except(:controller, :action)
- options = {:update => true, :action => 'reorder'}.merge(active_scaffold_config.sortable.options) %>
- <%= javascript_tag "ActiveScaffold.sortable('#{sort_params[0]}', #{url_for(:controller => controller_name).to_json}, #{options.to_json}, #{reorder_params.to_json});" %>
-<% end %>
+<%= content_tag :div, render(:super), :class => 'sortable-container', :data => sort_params %>
View
1  lib/active_scaffold/actions/sortable.rb
@@ -12,6 +12,7 @@ def self.included(base)
config.list.sorting = { sortable_column => "asc" }
config.actions.each do |action_name|
+ next if action_name == :subform
action = config.send(action_name)
action.columns.exclude(sortable_column) if action.respond_to? :columns
end
View
10 lib/active_scaffold/config/sortable.rb
@@ -30,7 +30,14 @@ def initialize(core_config)
attr_reader :column
def column=(column_name)
@column = @core.columns[column_name]
- Rails.logger.error("ActiveScaffold Sortable: postion column: #{column_name} not found in model") if @column.nil?
+ if @column.nil?
+ Rails.logger.error("ActiveScaffold Sortable: postion column: #{column_name} not found in model")
+ else
+ @column.form_ui = :hidden
+ @column.css_class = 'sortable-handle'
+ @column.label = ''
+ @column.weight = -2**(0.size * 8 -2)
+ end
@column
end
@@ -46,6 +53,7 @@ def add_handle_column=(where)
if where == :first
@core.list.columns = [:active_scaffold_sortable] + @core.list.columns.names_without_auth_check unless @core.list.columns.include? :active_scaffold_sortable
else
+ @column.weight = 2**(0.size * 8 -2) -1
@core.list.columns.add :active_scaffold_sortable
end
end
View
14 lib/active_scaffold/helpers/sortable_helpers.rb
@@ -3,17 +3,11 @@ module SortableHelpers
def sort_params
options = {
:tag => 'tr',
- :url => {:action => :reorder, :controller => controller_name },
- :format => '/^[^_-](?:[A-Za-z0-9_-]*)-(.*)-row$/',
- :with => "Sortable.serialize(#{active_scaffold_tbody_id.to_json})"
+ :reorder_url => url_for(params_for(:action => :reorder, :controller => controller_name)),
+ :format => '^[^_-](?:[A-Za-z0-9_-]*)-(.*)-row$',
+ :update => true
}
- additional_params = [:parent_controller, :eid, :controller].reject {|param| params[param].blank?}
- options[:with] = additional_params.inject(options[:with]) do |string, param|
- "#{string} + '&#{param}=' + encodeURIComponent('#{escape_javascript params[param]}')"
- end
- options.merge! active_scaffold_config.sortable.options
-
- [active_scaffold_tbody_id, options]
+ options.merge active_scaffold_config.sortable.options
end
def list_row_class(record)
View
2  lib/active_scaffold_sortable.rb
@@ -21,6 +21,6 @@ module Helpers
ActiveScaffold.autoload_subdir('helpers', self, File.dirname(__FILE__))
end
end
-
ActiveScaffold::Config::Core.send :include, ActiveScaffoldSortable::Core
ActiveScaffold.stylesheets << 'active_scaffold_sortable'
+ActiveScaffold.javascripts << 'active_scaffold_sortable'
View
5 lib/active_scaffold_sortable/core.rb
@@ -7,7 +7,10 @@ def self.included(base)
def initialize_with_sortable(model_id)
initialize_without_sortable(model_id)
# seems some rubies are returning strings in instance_methods and other symbols...
- self.actions << :sortable if !(model.instance_methods & ['acts_as_list_class', :acts_as_list_class, 'nested_set_scope', :nested_set_scope]).empty?
+ if !(model.instance_methods & ['acts_as_list_class', :acts_as_list_class, 'nested_set_scope', :nested_set_scope]).empty?
+ self.actions << :sortable
+ self.sortable # force to load
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.