From 531ba0a35f8598868caa25d0366aa212e91c4f93 Mon Sep 17 00:00:00 2001 From: Pavel Chipiga Date: Fri, 30 Mar 2012 00:24:57 +0300 Subject: [PATCH] Add rspec to dev env, change repo source. Take back rails_datatables to plugins with all files. Refs #263 --- Gemfile | 4 +- Gemfile.lock | 18 +-- .../20120329181830_remove_auto_build_list.rb | 3 +- .../20120329182602_delete_categories.rb | 3 +- db/schema.rb | 2 +- lib/plugins/rails_datatables.rb | 4 + lib/plugins/rails_datatables/MIT-LICENSE | 20 +++ lib/plugins/rails_datatables/README.md | 138 ++++++++++++++++++ .../rails_datatables}/rails_datatables.rb | 2 - 9 files changed, 178 insertions(+), 16 deletions(-) create mode 100644 lib/plugins/rails_datatables.rb create mode 100644 lib/plugins/rails_datatables/MIT-LICENSE create mode 100644 lib/plugins/rails_datatables/README.md rename lib/{ext => plugins/rails_datatables}/rails_datatables.rb (98%) diff --git a/Gemfile b/Gemfile index 37b2ca096..a8d3c9714 100644 --- a/Gemfile +++ b/Gemfile @@ -22,7 +22,7 @@ gem 'jbuilder' # gem 'rugged', '~> 0.16.0' gem 'grack', :git => 'git://github.com/rdblue/grack.git', :require => 'git_http' -gem "grit", :git => 'git://github.com/chipiga/grit.git' #, :path => '~/Sites/code/grit' +gem "grit", :git => 'git://github.com/warpc/grit.git' #, :path => '~/Sites/code/grit' gem 'charlock_holmes', '~> 0.6.8' #, :git => 'git://github.com/brianmario/charlock_holmes.git', :branch => 'bundle-icu' gem 'diff-display', '~> 0.0.1' @@ -73,7 +73,7 @@ group :development do end group :test do - gem 'rspec-rails', '~> 2.9.0' + gem 'rspec-rails', '~> 2.9.0', :group => 'development' gem 'factory_girl_rails', '~> 3.0.0' gem 'rr', '~> 1.0.4' gem 'shoulda' diff --git a/Gemfile.lock b/Gemfile.lock index e6ce422fd..893a65ef5 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,12 +1,3 @@ -GIT - remote: git://github.com/chipiga/grit.git - revision: 696b0967cb7e6bac044569d898e5acef431d4f97 - specs: - grit (2.4.1) - diff-lcs (~> 1.1) - mime-types (~> 1.15) - posix-spawn (~> 0.3.6) - GIT remote: git://github.com/chipiga/redhillonrails_core.git revision: 5f58167c41882890c223168b0a5521d99e8d92aa @@ -21,6 +12,15 @@ GIT specs: grack (0.2.0) +GIT + remote: git://github.com/warpc/grit.git + revision: 696b0967cb7e6bac044569d898e5acef431d4f97 + specs: + grit (2.4.1) + diff-lcs (~> 1.1) + mime-types (~> 1.15) + posix-spawn (~> 0.3.6) + GEM remote: http://rubygems.org/ specs: diff --git a/db/migrate/20120329181830_remove_auto_build_list.rb b/db/migrate/20120329181830_remove_auto_build_list.rb index a6fe98332..4c2faf71e 100644 --- a/db/migrate/20120329181830_remove_auto_build_list.rb +++ b/db/migrate/20120329181830_remove_auto_build_list.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- class RemoveAutoBuildList < ActiveRecord::Migration def self.up drop_table :auto_build_lists @@ -13,4 +14,4 @@ class RemoveAutoBuildList < ActiveRecord::Migration t.datetime "updated_at" end end -end \ No newline at end of file +end diff --git a/db/migrate/20120329182602_delete_categories.rb b/db/migrate/20120329182602_delete_categories.rb index 7b2c9b985..c02664b7c 100644 --- a/db/migrate/20120329182602_delete_categories.rb +++ b/db/migrate/20120329182602_delete_categories.rb @@ -1,3 +1,4 @@ +# -*- encoding : utf-8 -*- class DeleteCategories < ActiveRecord::Migration def self.up remove_column :projects, :category_id @@ -14,4 +15,4 @@ class DeleteCategories < ActiveRecord::Migration end add_column :projects, :category_id, :integer end -end \ No newline at end of file +end diff --git a/db/schema.rb b/db/schema.rb index 9195d3504..0bf60d4d1 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,4 +1,4 @@ -# encoding: UTF-8 +# -*- encoding : utf-8 -*- # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. diff --git a/lib/plugins/rails_datatables.rb b/lib/plugins/rails_datatables.rb new file mode 100644 index 000000000..5ad8eb843 --- /dev/null +++ b/lib/plugins/rails_datatables.rb @@ -0,0 +1,4 @@ +# -*- encoding : utf-8 -*- +require_relative 'rails_datatables/rails_datatables' + +ActionView::Base.send :include, RailsDatatables diff --git a/lib/plugins/rails_datatables/MIT-LICENSE b/lib/plugins/rails_datatables/MIT-LICENSE new file mode 100644 index 000000000..f1fed1a0e --- /dev/null +++ b/lib/plugins/rails_datatables/MIT-LICENSE @@ -0,0 +1,20 @@ +Copyright (c) 2009 [name of plugin creator] + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/lib/plugins/rails_datatables/README.md b/lib/plugins/rails_datatables/README.md new file mode 100644 index 000000000..0106b7206 --- /dev/null +++ b/lib/plugins/rails_datatables/README.md @@ -0,0 +1,138 @@ +## RailsDatatables + +A simpler, Rails-friendly interface to using the [DataTables](http://datatables.net) jQuery library. + +### Prerequisites +Make sure you have jQuery.js and jQuery.dataTables.js in /public/javascripts/ and that they're included in your layout. + +### Setup + +Give table a class of 'datatable' so that the Javascript knows which table to alter. NOTE: If you want to use multiple tables on a single page, include the :table_dom_id in the options hash to specify the ID table to be altered. + +Add thead around the table header (These elements will associate to the columns array created below, allowing sorting). + +Add tbody around the table rows (These are the elements that will be sorted and paginated.) + +Activate using <%= datatable() %>, passing in the columns, how to filter them (sorting type), and any other settings (ajax source, search?, label for search, processing image) + + <% columns = [{:type => 'html', :class => "first"}, {:type => 'html'}, {:type => 'html'}, {:type => nil, :class => "last"}] %> + <%= datatable(columns, {:sort_by => "[0, 'desc']", :processing => image_tag("spinner.gif") }) %> + + + + + + + + + + + + <%- @users.each do |user| -%> + + + + + + + <%- end -%> + +
NameAccount LevelEmailActions
<%= user.name %><%= user.account.account_level.name %><%= user.email %><%= link_to "Edit", edit_system_user_path(user) %>
+ +### Options + +#### Table Options + + :sort_by - array, default column number (0 - n-1) and sort order. e.g. "[2, 'desc']". Defaults to initial order. + :search - boolean, display the search field. Defaults to true. + :search_label - string, the label for the search field. Defaults to "Search". + :processing - string, the text or image to display while processing data. Defaults to "Processing". + :persist_state - boolean, remember the sorting and page of the tables for the user. Defaults to true. + :additional_data - hash, pass along additional data, such as filter values. Default is none. + :table_dom_id - string, the ID of the table to alter. If nothing is passed, it will look for a class of 'datatable'. Necessary if you want to have multiple DataTables on a single page. + :per_page - the number of rows to show per page (renamed from display_length) + :append - functions to all at the end of the dataTable() call. Useful for [Datatables plugins](http://www.datatables.net/plug-ins/api) + :no_records_message - Message to display if no records are found, whether on load or after searching + :auto_width - Automatically adjust the width of the columns. Defaults to true. + :row_callback - a function to run on each row in the table. Inserted in to "'fnRowCallback': function( nRow, aData, iDisplayIndex ) { }". See [documentation for fnRowCallback](http://www.datatables.net/usage/callbacks) for more information. + +#### Column Options + + :class - string, the class to assign to the table cell. Default is none. + :type - string, the type of content in the column, for non-Ajax tables. 'html' will strip all HTML and sort on the inner value, as a string. Default is string. + :sortable - boolean, allow this column to be sorted on. Default is true. + :searchable - boolean, allow this column to be searched, for non-Ajax tables. Default is true. + +#### AJAX Options + + When you're working with large datasets it's not reasonable to load everything on page load. Use an :ajax_source to load just the records that are being displayed, do custom searching (DB, Solr, etc). + + :ajax_source - string, for large datasets, use an ajax source to load each page on its own. For smaller datasets, just load the whole set and let datatable do the sorting + +Add a datatable method on your controller to return JSON +* Return the objects to be displayed +* Return the total number of objects +* Add a method to handle sorting - DataTables returns the column that is being sorted (0 - n), so you need to know which column is which and sort on it. + +### AJAX Example + +#### Datatable view example - datatable.html.erb + + {"sEcho": <%= params[:sEcho] || -1 %>, + "iTotalRecords": <%= @total_objects %>, + "iTotalDisplayRecords": <%= @total_object %>, + "aaData":[ + <% @objects.each do |object| %> + ['<%= link_to(object.user.name, user) %>', + '<%= object.description || "-" %>', + '<%= object.created_at %>' + ], + <% end %> + ]} + +#### Controller example - using will_paginate + + def datatable + @objects = current_objects(params) + @total_objectss = total_objects(params) + render :layout => false + end + + private + + def current_objects(params={}) + current_page = (params[:iDisplayStart].to_i/params[:iDisplayLength].to_i rescue 0)+1 + @current_objects = Object.paginate :page => current_page, + :include => [:user], + :order => "#{datatable_columns(params[:iSortCol_0])} #{params[:sSortDir_0] || "DESC"}", + :conditions => conditions, + :per_page => params[:iDisplayLength] + end + + def total_objects(params={}) + @total_objects = Object.count :include => [:user], :conditions => conditions + end + + def datatable_columns(column_id) + case column_id.to_i + when 1 + return "objects.description" + when 2 + return "objects.created_at" + else + return "users.name" + end + end + + def conditions + conditions = [] + conditions << "(objects.description ILIKE '%#{params[:sSearch]}%' OR users.name ILIKE '%#{params[:sSearch]}%')" if(params[:sSearch]) + return conditions.join(" AND ") + end + +### Note +There is a more functionality offered by DataTables than this plugin currently provides. We add to it as we find need for other features. If there's a feature of DataTables that you'd like to see, fork this repo and add it so we can all benefit. + +### Credits + +Copyright (c) 2009 [Phronos](http://phronos.com), released under the MIT license \ No newline at end of file diff --git a/lib/ext/rails_datatables.rb b/lib/plugins/rails_datatables/rails_datatables.rb similarity index 98% rename from lib/ext/rails_datatables.rb rename to lib/plugins/rails_datatables/rails_datatables.rb index e5c998e27..08b2560cc 100644 --- a/lib/ext/rails_datatables.rb +++ b/lib/plugins/rails_datatables/rails_datatables.rb @@ -115,5 +115,3 @@ module RailsDatatables }.join(",") end end - -ActionView::Base.send :include, RailsDatatables