Permalink
Browse files

Added opportunity to set order of variants in admin panel.

Special thanks to Gabe Anzelini for idea and initial contribution.
  • Loading branch information...
romul committed Jan 10, 2011
1 parent 3776ab5 commit 24ea4430e0b05783bd4336c3634182988a4642ea
@@ -35,6 +35,13 @@ def destroy
format.js { render_js_for_destroy }
end
end
+
+ def update_positions
+ params[:positions].each do |id, index|
+ Variant.update_all(['position=?', index], ['id=?', id])
+ end
+ render :text => 'Ok'
+ end
private
def create_before
@@ -43,7 +43,8 @@ class Product < ActiveRecord::Base
after_save :save_master
has_many :variants,
- :conditions => ["variants.is_master = ? AND variants.deleted_at IS NULL", false]
+ :conditions => ["variants.is_master = ? AND variants.deleted_at IS NULL", false],
+ :order => 'variants.position ASC'
has_many :variants_including_master,
@@ -2,7 +2,7 @@
<%= render :partial => 'admin/shared/product_tabs', :locals => {:current => "Variants"} %>
-<table class="index">
+<table class="index sortable">
<tr>
<th><%= t("options") %></th>
<th><%= t("price") %></th>
@@ -17,7 +17,7 @@
<!-- you can skip variant with no options: that's just the default variant that all products have -->
<% next if variant.option_values.empty? %>
<tr id="<%= dom_id(variant) %>" <%= 'style="color:red;"' if variant.deleted? %>>
- <td><%= variant_options variant %></td>
+ <td><span class="handle"></span> <%= variant_options variant %></td>
<td><%= variant.price %></td>
<td><%= variant.sku %></td>
<% Variant.additional_fields.select{|f| f[:only].nil? || f[:only].include?(:variant) }.each do |field| %>
View
@@ -79,7 +79,11 @@
member do
get :clone
end
- resources :variants
+ resources :variants do
+ collection do
+ put :update_positions
+ end
+ end
resources :option_types do
member do
get :select
@@ -0,0 +1,9 @@
+class AddPositionToVariants < ActiveRecord::Migration
+ def self.up
+ add_column :variants, :position, :integer
+ end
+
+ def self.down
+ remove_column :variants, :position
+ end
+end
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@@ -12,4 +12,31 @@ $(document).ready(function(){
return false;
});
+
+ jQuery('table.sortable').ready(function(){
+ jQuery('table.sortable tbody').sortable(
+ {
+ handle: '.handle',
+ update: function(event, ui) {
+ $("#progress").show();
+ positions = {};
+ type = '';
+ jQuery.each(jQuery('table.sortable tbody tr'), function(position, obj){
+ reg = /(\w+_?)+_(\d+)/;
+ parts = reg.exec(jQuery(obj).attr('id'));
+ if (parts) {
+ positions['positions['+parts[2]+']'] = position;
+ type = parts[1];
+ }
+ });
+ jQuery.ajax({
+ type: 'PUT',
+ url: type+'s/update_positions',
+ data: positions,
+ success: function(data){ $("#progress").hide(); }
+ });
+ }
+ });
+ });
+
});
@@ -577,3 +577,9 @@ table#product_scopes tr td table tr td {
}
+span.handle{
+ background-image: url(../../images/reorder.jpg);
+ display: inline-block;
+ width:14px;
+ height:15px;
+}

0 comments on commit 24ea443

Please sign in to comment.