Skip to content
Browse files

added sortability on sponsors admin panel

  • Loading branch information...
1 parent e247ae3 commit c81195f926323c7f3a3857f6d9548057f7393c1e @danallison danallison committed Apr 11, 2012
View
2 app/controllers/admin/sponsors_controller.rb
@@ -3,7 +3,7 @@ class Admin::SponsorsController < Admin::AdminController
# COLLECTION ACTIONS
# ---------------------------------------------------------------------------------------------------------
def index
- @sponsors = Sponsor.search_sort_paginate(params)
+ @sponsors = Sponsor.by_sort.search_sort_paginate(params)
end
# MEMBER ACTIONS
View
15 app/models/sponsor.rb
@@ -17,6 +17,21 @@ class Sponsor < ActiveRecord::Base
scope :by_name, order('name asc')
scope :featured_sponsors, :conditions => { :featured => true }
+ scope :by_sort, order('sort asc')
+
+ # when this model is created, set the sort order to the last in the current set (unless it was already set)
+ before_validation {|record|
+ return true if record.sort.present?
+ record.sort = Sponsor.maximum(:sort).to_i + 1
+ }
+
+ # Sort the model records all at once
+ def self.sort(ids)
+ update_all(
+ ['sort = FIND_IN_SET(id, ?)', ids.join(',')],
+ { :id => ids }
+ )
+ end
# tell the dynamic form that we need to post to an iframe to accept the file upload
# TODO:: find a more elegant solution to this problem, can we detect the use of has_attached_file?
View
26 app/models/sponsorship.rb
@@ -1,3 +1,29 @@
class Sponsorship < ActiveRecord::Base
belongs_to :sponsorship_level
+
+ scope :by_sort, order('sort asc')
+
+ # when this model is created, set the sort order to the last in the current set (unless it was already set)
+ before_validation {|record|
+ return true if record.sort.present?
+ record.sort = Sponsorship.maximum(:sort).to_i + 1
+ }
+
+ # Sort the model records all at once
+ def self.sort(ids)
+ update_all(
+ ['sort = FIND_IN_SET(id, ?)', ids.join(',')],
+ { :id => ids }
+ )
+ end
+
+ # the hash representing this model that is returned by the api
+ def api_attributes
+ {
+ :id => id.to_s,
+ :name => name,
+ :sort => sort,
+ }
+ end
+
end
View
2 app/views/admin/sponsors/_sponsor.html.haml
@@ -1,4 +1,4 @@
-%tr{:sponsor => sponsor.id}
+%tr{:sponsor => sponsor.id, :class => 'sortable', :id => "node_#{sponsor.id}"}
%td.name= link_to sponsor.name, admin_sponsor_path(sponsor)
%td.sponsorship_level_id= sponsor.sponsorship_level.name
%td.featured=raw sponsor.featured ? "&#10004;" : ""
View
39 app/views/admin/sponsors/_table.html.haml
@@ -5,3 +5,42 @@
%th.featured= ssp_sort_link :featured
%th
= render :partial => 'admin/sponsors/sponsor.html.haml', :collection => sponsors
+
+:javascript
+ $(document).ready(function() {
+ $('#sponsors')
+
+ .sortable({
+ forcePlaceholderSize: true,
+ helper: 'clone',
+ items: 'tr.sortable',
+ opacity: .6,
+ placeholder: 'item_placeholder',
+ revert: 250,
+ tolerance: 'pointer',
+ distance: 20,
+ })
+
+ .bind( 'sortstop', function(event, ui) {
+
+ // set up variables
+ var node_id = ui.item.attr('sponsor');
+ var serialized_data = $('#sponsors').sortable('serialize');
+ console.log(serialized_data);
+
+ $.ajax({
+
+ type: 'POST',
+ url: '/admin/sponsors/sort',
+ data: serialized_data,
+
+ success: function(results) {
+
+ }
+
+ });
+
+
+ });
+
+ });
View
3 config/routes.rb
@@ -341,6 +341,9 @@
end
resources :sponsors do
+ collection do
+ post :sort
+ end
member do
# pages
get :notes
View
5 db/migrate/20120410214906_add_sort_to_sponsors.rb
@@ -0,0 +1,5 @@
+class AddSortToSponsors < ActiveRecord::Migration
+ def change
+ add_column :sponsors, :sort, :integer
+ end
+end
View
3 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20120404192823) do
+ActiveRecord::Schema.define(:version => 20120410214906) do
create_table "affiliate_event_applications", :force => true do |t|
t.string "first_name", :null => false
@@ -384,6 +384,7 @@
t.datetime "updated_at"
t.string "url"
t.boolean "featured"
+ t.integer "sort"
end
add_index "sponsors", ["sponsorship_level_id"], :name => "index_sponsors_on_sponsorship_level_id"

0 comments on commit c81195f

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