Created
August 25, 2012 12:59
-
-
Save benwoodward/3465323 to your computer and use it in GitHub Desktop.
Ruby on Rails Sortable/Reorderable lists on STI models using ranked_model, Twitter Bootstrap and inherited_resources
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%h1= "Listing " + "#{resource_name}s" | |
.btn-group.pull-right | |
= link_to "All #{resource_name(item: resource_class.superclass)}s", polymorphic_path([:admin, resource_class.superclass]), class: 'btn' | |
= link_to "Add #{resource_name}", new_resource_path, class: 'btn-primary btn' | |
%br | |
%br | |
%table.table.table-bordered.table-striped#sortable{:data => {update_url: polymorphic_path([:sort, :admin, resource_class])}} | |
%thead | |
%tr | |
- attributes.each do |attr| | |
%th= resource_class.human_attribute_name(attr) | |
%th | |
%tbody | |
- collection.each do |resource| | |
%tr{data: {item_id: "#{resource.id}"}, class: 'item'} | |
- attributes.each do |attr| | |
%td= resource.public_send(attr).to_s.truncate(20) | |
%td | |
= link_to 'show', resource_path(resource), :class => 'btn' | |
= link_to 'edit', edit_resource_path(resource), :class => 'btn btn-primary' | |
= link_to 'destroy', resource_path(resource), method: :delete, confirm: "Are you sure?", :class => 'btn btn-danger' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#sortable { | |
tr { | |
cursor: row-resize; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Admin::ResourcesHelper | |
def attributes | |
resource_class.attribute_names - %w(id created_at updated_at row_order) | |
end | |
# resource_class is an inherited_resources helper method | |
# resource_name(item: resource_class.superclass) # => Menu Item (from a model that inherits from MenuItem) | |
# resource_name # => Burger | |
def resource_name(options = {}) | |
options[:item] ||= resource_class | |
options[:item].name.underscore.humanize.titleize | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Admin::ResourcesController < Admin::ApplicationController | |
inherit_resources | |
respond_to :html | |
def sort | |
@menu_item = resource_class.find(params[:id]) | |
@menu_item.attributes = params[:menu_item] | |
@menu_item.save | |
render nothing: true | |
end | |
protected | |
def collection | |
collection_variable_name = controller_name.gsub(/^/, '@').to_sym | |
instance_variable_set(collection_variable_name, resource_class.rank(:row_order).all) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://railscasts.com/episodes/269-template-inheritance
http://guides.rubyonrails.org/3_1_release_notes.html#action-dispatch
http://iain.nl/backends-in-rails-3-1
https://github.com/josevalim/inherited_resources