Module: ActionView::Helpers::FormHelper
- Defined in:
- lib/jsort.rb
Instance Method Summary collapse
- #get_sortable_id(item, field) ⇒ Object
-
#jsort(items, name, path, options = {}, &block) ⇒ Object
Renders an ordered list of items and register the javascript-call for drag-n-sort.
Instance Method Details
#get_sortable_id(item, field) ⇒ Object
72 73 74 |
# File 'lib/jsort.rb', line 72 def get_sortable_id(item,field) item.send(field) end |
#jsort(items, name, path, options = {}, &block) ⇒ Object
Renders an ordered list of items and register the javascript-call for drag-n-sort
Options:
* `:text => 'click here to sort'` - Will use this text as a drag-n-drop-handle instead of the image
* `:image => 'yourimage.jpg'` - use your own image (place it in assets/images path of your application
* `:handle_only => true` - Only the handle (image or text) can be used to drag&move. Default is 'false' which allows you to pick up by clicking anywhere on the line.
* `:div_type => ['table','tr','td'] or ['ol',nil,'li'], ....
* `:sortable_id => :id (The field used as css-id)
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/jsort.rb', line 22 def jsort(items,name,path,={},&block) defaults = { :handle_only => false, :image => 'sortable_vertical.png', :text => nil, :div_type => ['ol',nil,'li'], :register => true, :sortable_id => :id } defaults.merge!() concat("<#{defaults[:div_type][0]} id='#{name.pluralize}' path='#{path}'>".html_safe) begin sorted_items = items.sort {|a,b| a.position <=> b.position} rescue sorted_items = items end for item in sorted_items concat("<#{defaults[:div_type][1]}>") unless defaults[:div_type][1].nil? concat("<#{defaults[:div_type][2]} class='#{name}_sort_entry' id='#{name}_#{get_sortable_id(item, defaults[:sortable_id])}'>".html_safe) concat("<span class='handle'>".html_safe) if [:text] concat([:text] + " ") else concat(image_tag(defaults[:image], :style => 'box-shadow: none; border-radius: 0;') + " ") end if defaults[:handle_only] && [:handle_only] == true concat("</span>".html_safe) end if block_given? yield(item) else concat item.to_s end unless defaults[:handle_only] && [:handle_only] == true concat("</span>".html_safe) end concat("</#{defaults[:div_type][2]}>".html_safe) concat("</#{defaults[:div_type][1]}>") unless defaults[:div_type][1].nil? end concat("</#{defaults[:div_type][0]}>".html_safe) if defaults[:register] concat(javascript_tag( "registerSortableList($('##{name.pluralize}'));")) end end |