From 6c48e916912dfca33f17e2b9f6afbbe188624f90 Mon Sep 17 00:00:00 2001 From: Joel Quenneville Date: Mon, 30 Apr 2012 14:05:00 -0500 Subject: [PATCH 1/6] added testing infrastructure --- .rspec | 1 + Rakefile | 4 ++++ ajax-datatables-rails.gemspec | 2 ++ spec/ajax-datatables-rails/ajax_datatables_rails_spec.rb | 7 +++++++ spec/spec_helper.rb | 1 + 5 files changed, 15 insertions(+) create mode 100644 .rspec create mode 100644 spec/ajax-datatables-rails/ajax_datatables_rails_spec.rb create mode 100644 spec/spec_helper.rb diff --git a/.rspec b/.rspec new file mode 100644 index 00000000..4e1e0d2f --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--color diff --git a/Rakefile b/Rakefile index f57ae68a..5be68f6e 100644 --- a/Rakefile +++ b/Rakefile @@ -1,2 +1,6 @@ #!/usr/bin/env rake require "bundler/gem_tasks" +require 'rspec/core/rake_task' + +RSpec::Core::RakeTask.new(:spec) +task default: :spec \ No newline at end of file diff --git a/ajax-datatables-rails.gemspec b/ajax-datatables-rails.gemspec index f22a1f23..a266644a 100644 --- a/ajax-datatables-rails.gemspec +++ b/ajax-datatables-rails.gemspec @@ -14,4 +14,6 @@ Gem::Specification.new do |gem| gem.name = "ajax-datatables-rails" gem.require_paths = ["lib"] gem.version = AjaxDatatablesRails::VERSION + + gem.add_development_dependency "rspec" end diff --git a/spec/ajax-datatables-rails/ajax_datatables_rails_spec.rb b/spec/ajax-datatables-rails/ajax_datatables_rails_spec.rb new file mode 100644 index 00000000..935ee52e --- /dev/null +++ b/spec/ajax-datatables-rails/ajax_datatables_rails_spec.rb @@ -0,0 +1,7 @@ +require 'spec_helper' + +describe AjaxDatatablesRails do + describe "as_json" do + it "should return the correct json feed" + end +end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..8d9b55a1 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1 @@ +require 'ajax-datatables-rails' \ No newline at end of file From 0968543d0bdf61940f327d9833d11f005c13f4f9 Mon Sep 17 00:00:00 2001 From: Joel Quenneville Date: Mon, 30 Apr 2012 14:12:39 -0500 Subject: [PATCH 2/6] changes to the README --- README.md | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index dff233bb..cfb741fe 100644 --- a/README.md +++ b/README.md @@ -6,22 +6,23 @@ Datatables is a nifty jquery plugin that adds the ability to paginate, sort, and ## Installation -Add this line to your application's Gemfile: +Add these lines to your application's Gemfile: + gem 'jquery-datatables-rails' gem 'ajax-datatables-rails' And then execute: $ bundle -Or install it yourself as: - - $ gem install ajax-datatables-rails - ## Usage -TODO: Write usage instructions here +### Model +Run the following command: + + rails generate ajaxdatatable +This will generate a file in `app/datatables` ## Contributing 1. Fork it From 68c7dcf0fbaad3602e18a907eb2a89a859759739 Mon Sep 17 00:00:00 2001 From: Joel Quenneville Date: Mon, 30 Apr 2012 14:20:54 -0500 Subject: [PATCH 3/6] added to rEADME --- README.md | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index cfb741fe..5a47b9b5 100644 --- a/README.md +++ b/README.md @@ -16,13 +16,25 @@ And then execute: $ bundle ## Usage - +*The following examples assume that we are setting up ajax-datatables-rails for an index of users from a `User` model* ### Model Run the following command: - rails generate ajaxdatatable + $ rails generate ajaxdatatable User + +This will generate a file named `users_datatable.rb` in `app/datatables`. Open the file and customize in the functions as directed by the comments + +#### Initializer +```ruby +def initialize(view) + @model_name = User + @columns = # insert array of column names here + @searchable_columns = #insert array of columns that will be searched + super(view) +end +``` + -This will generate a file in `app/datatables` ## Contributing 1. Fork it From 8139a4c2d3677e346241ad5c5995d4bd1a344c40 Mon Sep 17 00:00:00 2001 From: Joel Quenneville Date: Mon, 30 Apr 2012 14:27:41 -0500 Subject: [PATCH 4/6] added some code examples to readme --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/README.md b/README.md index 5a47b9b5..8ff3567a 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,20 @@ def initialize(view) end ``` +For `@columns`, assign an array of the database columns that correspond to the columns in our view table. For example `[users.f_name, users.l_name, users.bio]`. This array is used for sorting by various columns + +For `@searchable_columns`, assign an array of the database columns that you want searchable by datatables. For example `[users.f_name, users.l_name]` + +This gives us: +```ruby +def initialize(view) + @model_name = User + @columns = [users.f_name, users.l_name, users.bio] + @searchable_columns = [users.f_name, users.l_name] + super(view) +end +``` + ## Contributing From d9f52d4fc602ad04a14b08fd5c52a81bd42cf6ab Mon Sep 17 00:00:00 2001 From: Joel Quenneville Date: Mon, 30 Apr 2012 14:35:44 -0500 Subject: [PATCH 5/6] added documentation for the model side of things --- README.md | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/README.md b/README.md index 8ff3567a..8af6ee94 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,45 @@ def initialize(view) end ``` +#### Data +```ruby +def data + users.map do |user| + [ + # comma separated list of the values for each cell of a table row + ] + end +end +``` + +This method builds a 2d array that is used by datatables to construct the html table. Insert the values you want on each column. + +```ruby +def data + users.map do |user| + [ + user.f_name, + user.l_name, + user.bio + ] + end +end +``` + +#### Get Raw Records +```ruby +def get_raw_records + # insert query here +end +``` + +This is where your query goes. + +```ruby +def get_raw_records + User.all +end +``` ## Contributing From 853d8986ba4c335ca1459f13fed3ca2fd8ea251c Mon Sep 17 00:00:00 2001 From: Joel Quenneville Date: Mon, 30 Apr 2012 14:49:24 -0500 Subject: [PATCH 6/6] finished basic documentation --- README.md | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 8af6ee94..3d4f5b12 100644 --- a/README.md +++ b/README.md @@ -34,9 +34,9 @@ def initialize(view) end ``` -For `@columns`, assign an array of the database columns that correspond to the columns in our view table. For example `[users.f_name, users.l_name, users.bio]`. This array is used for sorting by various columns +* For `@columns`, assign an array of the database columns that correspond to the columns in our view table. For example `[users.f_name, users.l_name, users.bio]`. This array is used for sorting by various columns -For `@searchable_columns`, assign an array of the database columns that you want searchable by datatables. For example `[users.f_name, users.l_name]` +* For `@searchable_columns`, assign an array of the database columns that you want searchable by datatables. For example `[users.f_name, users.l_name]` This gives us: ```ruby @@ -88,6 +88,51 @@ def get_raw_records end ``` +### Controller +Set up the controller to respond to JSON + +```ruby +def index + respond_to do |format| + format.html + format.json { render json: UsersDatatable.new(view_context) } + end +end +``` + +### View +* Set up an html `` with a `` and `` +* Add in your table headers if desired +* Don't add any rows to the body of the table, datatables does this automatically +* Add a data attribute to the `
` tag with the url of the JSON feed + +The resulting view may look like this: + +```erb +
+ + + + + + + + + +
First NameLast NameBrief Bio
+``` + +### Javascript +Finally, the javascript to tie this all together. In the appropriate `js.coffee` file: + +```coffeescript +$ -> + $('#users-table').dataTable + bProcessing: true + bServerSide: true + sAjaxSource: $('#users-table').data('source') +``` + ## Contributing 1. Fork it