Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added opportunity to set order of variants in admin panel.

Special thanks to Gabe Anzelini for idea and initial contribution.
  • Loading branch information...
commit 24ea4430e0b05783bd4336c3634182988a4642ea 1 parent 3776ab5
Roman Smirnov romul authored
7 core/app/controllers/admin/variants_controller.rb
@@ -35,6 +35,13 @@ def destroy
35 35 format.js { render_js_for_destroy }
36 36 end
37 37 end
  38 +
  39 + def update_positions
  40 + params[:positions].each do |id, index|
  41 + Variant.update_all(['position=?', index], ['id=?', id])
  42 + end
  43 + render :text => 'Ok'
  44 + end
38 45
39 46 private
40 47 def create_before
3  core/app/models/product.rb
@@ -43,7 +43,8 @@ class Product < ActiveRecord::Base
43 43 after_save :save_master
44 44
45 45 has_many :variants,
46   - :conditions => ["variants.is_master = ? AND variants.deleted_at IS NULL", false]
  46 + :conditions => ["variants.is_master = ? AND variants.deleted_at IS NULL", false],
  47 + :order => 'variants.position ASC'
47 48
48 49
49 50 has_many :variants_including_master,
4 core/app/views/admin/variants/index.html.erb
@@ -2,7 +2,7 @@
2 2
3 3 <%= render :partial => 'admin/shared/product_tabs', :locals => {:current => "Variants"} %>
4 4
5   -<table class="index">
  5 +<table class="index sortable">
6 6 <tr>
7 7 <th><%= t("options") %></th>
8 8 <th><%= t("price") %></th>
@@ -17,7 +17,7 @@
17 17 <!-- you can skip variant with no options: that's just the default variant that all products have -->
18 18 <% next if variant.option_values.empty? %>
19 19 <tr id="<%= dom_id(variant) %>" <%= 'style="color:red;"' if variant.deleted? %>>
20   - <td><%= variant_options variant %></td>
  20 + <td><span class="handle"></span> <%= variant_options variant %></td>
21 21 <td><%= variant.price %></td>
22 22 <td><%= variant.sku %></td>
23 23 <% Variant.additional_fields.select{|f| f[:only].nil? || f[:only].include?(:variant) }.each do |field| %>
6 core/config/routes.rb
@@ -79,7 +79,11 @@
79 79 member do
80 80 get :clone
81 81 end
82   - resources :variants
  82 + resources :variants do
  83 + collection do
  84 + put :update_positions
  85 + end
  86 + end
83 87 resources :option_types do
84 88 member do
85 89 get :select
9 core/db/migrate/20101223215658_add_position_to_variants.rb
... ... @@ -0,0 +1,9 @@
  1 +class AddPositionToVariants < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :variants, :position, :integer
  4 + end
  5 +
  6 + def self.down
  7 + remove_column :variants, :position
  8 + end
  9 +end
BIN  core/public/images/reorder.jpg
27 core/public/javascripts/admin/unobtrusive_handlers.js
@@ -12,4 +12,31 @@ $(document).ready(function(){
12 12 return false;
13 13 });
14 14
  15 +
  16 + jQuery('table.sortable').ready(function(){
  17 + jQuery('table.sortable tbody').sortable(
  18 + {
  19 + handle: '.handle',
  20 + update: function(event, ui) {
  21 + $("#progress").show();
  22 + positions = {};
  23 + type = '';
  24 + jQuery.each(jQuery('table.sortable tbody tr'), function(position, obj){
  25 + reg = /(\w+_?)+_(\d+)/;
  26 + parts = reg.exec(jQuery(obj).attr('id'));
  27 + if (parts) {
  28 + positions['positions['+parts[2]+']'] = position;
  29 + type = parts[1];
  30 + }
  31 + });
  32 + jQuery.ajax({
  33 + type: 'PUT',
  34 + url: type+'s/update_positions',
  35 + data: positions,
  36 + success: function(data){ $("#progress").hide(); }
  37 + });
  38 + }
  39 + });
  40 + });
  41 +
15 42 });
6 core/public/stylesheets/admin/admin.css
@@ -577,3 +577,9 @@ table#product_scopes tr td table tr td {
577 577 }
578 578
579 579
  580 +span.handle{
  581 + background-image: url(../../images/reorder.jpg);
  582 + display: inline-block;
  583 + width:14px;
  584 + height:15px;
  585 +}

0 comments on commit 24ea443

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