mongoid_data_table 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/.gitignore ADDED
@@ -0,0 +1,4 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
4
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ gem "rails"
4
+ gem "mongoid"
5
+ gem "will_paginate"
6
+
7
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1,3 @@
1
+ require "bundler"
2
+
3
+ Bundler::GemHelper.install_tasks
@@ -0,0 +1,55 @@
1
+ module Mongoid
2
+ module DataTable
3
+
4
+ def self.included base
5
+ base.send :extend, ClassMethods
6
+ base.send :extend, ActionView::Helpers::JavaScriptHelper
7
+ end
8
+
9
+ module ClassMethods
10
+ def json_for_datatables controller, fields, search_fields=nil, &block
11
+ params = controller.params
12
+ search_fields ||= fields
13
+ objects = self.where(_where_conditions controller, params[:sSearch], search_fields).
14
+ order_by(_order_by_fields params, fields).
15
+ paginate :page => _page(params),
16
+ :per_page => params[:iDisplayLength]
17
+
18
+ {:sEcho => params[:sEcho].to_i,
19
+ :iTotalRecords => self.count,
20
+ :iTotalDisplayRecords => objects.total_entries,
21
+ :aaData => _yield_and_render_array(controller, objects, block)
22
+ }.to_json.html_safe
23
+ end
24
+
25
+ private
26
+
27
+ def _yield_and_render_array controller, objects, block
28
+ objects.map do |object|
29
+ block[object].map do |string|
30
+ controller.instance_eval %{
31
+ Rails.logger.silence do
32
+ render_to_string :inline => "#{string}", :locals => {:#{self.name.downcase} => object}
33
+ end
34
+ }
35
+ end
36
+ end
37
+ end
38
+
39
+ def _where_conditions controller, raw_query, search_fields
40
+ return if (query = escape_javascript(raw_query)).blank?
41
+
42
+ {"$or" => search_fields.map {|field| {field => /#{query}/i} }}
43
+ end
44
+
45
+ def _order_by_fields params, fields
46
+ [fields[params[:iSortCol_0].to_i], params[:sSortDir_0]]
47
+ end
48
+
49
+ def _page params
50
+ params[:iDisplayStart].to_i / params[:iDisplayLength].to_i + 1
51
+ end
52
+ end
53
+
54
+ end
55
+ end
@@ -0,0 +1,5 @@
1
+ module Mongoid
2
+ module DataTable
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1 @@
1
+ require "mongoid/data_table/base"
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "mongoid/data_table/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "mongoid_data_table"
7
+ s.version = Mongoid::DataTable::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jason Dew"]
10
+ s.email = ["jason.dew@gmail.com"]
11
+ s.homepage = "http://rubygems.org/gems/mongoid_data_table"
12
+ s.summary = %q{Simple data preparation from Mongoid to the jQuery DataTables plugin}
13
+ s.description = %q{Simple data preparation from Mongoid to the jQuery DataTables plugin}
14
+
15
+ s.rubyforge_project = "mongoid_data_table"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
metadata ADDED
@@ -0,0 +1,74 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mongoid_data_table
3
+ version: !ruby/object:Gem::Version
4
+ hash: 27
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 1
9
+ - 0
10
+ version: 0.1.0
11
+ platform: ruby
12
+ authors:
13
+ - Jason Dew
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-22 00:00:00 -04:00
19
+ default_executable:
20
+ dependencies: []
21
+
22
+ description: Simple data preparation from Mongoid to the jQuery DataTables plugin
23
+ email:
24
+ - jason.dew@gmail.com
25
+ executables: []
26
+
27
+ extensions: []
28
+
29
+ extra_rdoc_files: []
30
+
31
+ files:
32
+ - .gitignore
33
+ - Gemfile
34
+ - Rakefile
35
+ - lib/mongoid/data_table/base.rb
36
+ - lib/mongoid/data_table/version.rb
37
+ - lib/mongoid_data_table.rb
38
+ - mongoid_data_table.gemspec
39
+ has_rdoc: true
40
+ homepage: http://rubygems.org/gems/mongoid_data_table
41
+ licenses: []
42
+
43
+ post_install_message:
44
+ rdoc_options: []
45
+
46
+ require_paths:
47
+ - lib
48
+ required_ruby_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ hash: 3
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ hash: 3
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ requirements: []
67
+
68
+ rubyforge_project: mongoid_data_table
69
+ rubygems_version: 1.3.7
70
+ signing_key:
71
+ specification_version: 3
72
+ summary: Simple data preparation from Mongoid to the jQuery DataTables plugin
73
+ test_files: []
74
+