datatables_server_side 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in datatables_server_side.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2015 Layon Ferreira
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,78 @@
1
+ # DatatablesServerSide
2
+
3
+ Simple rails gem to simplify the creation of datatables json response.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```
10
+ gem 'datatables_server_side'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install datatables_server_side
20
+
21
+ ## Usage
22
+
23
+ This gem is inspired on Rails Casts [#340 DataTables](http://railscasts.com/episodes/340-datatables). Its recommended to watch.
24
+
25
+ **Installation**: run the *installer* generator
26
+
27
+ rails g datatables_server_side:installer
28
+ It will create a datatables folder inside the *app* folder
29
+
30
+ After that you will have to create a datatable.
31
+
32
+ To **Generate** a datatable run:
33
+
34
+ rails g dadatatables_server_side:datatable Example
35
+
36
+ this will create a ruby file */app/datatables/example_datatable.rb*
37
+
38
+ You will have to define two methods:
39
+
40
+ A **data** method with the object that will be returned.
41
+ example:
42
+
43
+ def data
44
+ records.map do |example|
45
+ [
46
+ example.id,
47
+ example.name,
48
+ example.description,
49
+ ]
50
+ end
51
+ and a **fetch_records** method, that will be responsible for retrieve the query results from the model example:
52
+
53
+ def fetch_records
54
+ filter_params={}
55
+ filter_params[:offset]= params['iDisplayStart'] if params['iDisplayStart'].present?
56
+ filter_params[:limit]= params['iDisplayLength'].to_i if params['iDisplayLength'].present?
57
+ filter_params[:sort]= params['sSortDir_0'] if params['sSortDir_0'].present?
58
+ Example.find(:all,:params => filter_params)
59
+ end
60
+ finally you should add the the **datatables object** to the json response on your controller. example:
61
+
62
+ def index
63
+ respond_to do |format|
64
+ format.html
65
+ format.json{ render json: RodaDatatable.new(view_context)}
66
+ end
67
+ end
68
+ #Contacts
69
+ Any questions contact me:
70
+
71
+ **Website**:<http://layonferreira.com/>
72
+
73
+ **Email**: <me@layonferreira.com>
74
+
75
+ #License
76
+ MIT License
77
+
78
+ Copyright (c) 2015 [Layon Ferreira](http://layonferreira.com/)
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'datatables_server_side/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'datatables_server_side'
8
+ spec.version = DatatablesServerSide::VERSION
9
+ spec.authors = ['Layon Ferreira']
10
+ spec.email = ['layon@live.com.pt']
11
+ spec.summary = %q{Simple gem that helps the creation of json datatables json responses.}
12
+ spec.description = %q{Simple gem that helps the creation of json datatables json responses.}
13
+ spec.homepage = 'https://github.com/layonferreira/datatables_server_side'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ['lib']
20
+
21
+ spec.add_development_dependency 'rspec', '~> 2.6'
22
+ spec.add_development_dependency 'generator_spec' , '~> 0.9'
23
+ spec.add_development_dependency 'bundler', '~> 1.7'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ # DEPENDENCIES
26
+ spec.add_dependency 'activesupport'
27
+ spec.add_dependency 'actionview'
28
+ spec.add_dependency('railties', '>= 3.2.6', '< 5')
29
+ end
30
+
31
+
@@ -0,0 +1,61 @@
1
+ require 'datatables_server_side/version'
2
+ require 'active_support'
3
+ require 'active_support/core_ext/string/output_safety.rb' #raw() and h() methods
4
+ require 'active_support/core_ext/module/delegation.rb'# delegate method
5
+ require 'action_view'
6
+ module DatatablesServerSide
7
+ class Datatables
8
+ delegate :params, :link_to, :content_tag, :can?, :current_user, :t, to: :@view
9
+ # delegate :h, to: ERB::Util
10
+ include ActionView::Helpers::OutputSafetyHelper # this is to use raw()
11
+ # include ApplicationHelper
12
+
13
+ def initialize(view)
14
+ @view = view
15
+ end
16
+
17
+ def as_json(options = {})
18
+ raise NotImplementedError
19
+ end
20
+
21
+ protected
22
+
23
+ def data
24
+ raise NotImplementedError
25
+ end
26
+
27
+ def safe_attributes(data)
28
+ data.map! do |array_value|
29
+ if array_value.class==Array
30
+ safe_attributes(array_value)
31
+ elsif array_value.class!=NilClass
32
+ array_value.to_s.html_safe
33
+ end
34
+ end
35
+ end
36
+
37
+ def records
38
+ @records ||= fetch_records
39
+ end
40
+
41
+ def fetch_records
42
+ raise NotImplementedError
43
+ end
44
+
45
+ def page
46
+ params[:iDisplayStart].present? ? params[:iDisplayStart].to_i/per_page + 1 : 1
47
+ end
48
+
49
+ def per_page
50
+ (params[:sSortDir_0].present? && params[:iDisplayLength].to_i > 0) ? params[:iDisplayLength].to_i : 10
51
+ end
52
+
53
+ def sort_column
54
+ raise NotImplementedError
55
+ end
56
+
57
+ def sort_direction
58
+ (params[:sSortDir_0].present? && params[:sSortDir_0] == 'desc') ? 'desc' : 'asc'
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,3 @@
1
+ module DatatablesServerSide
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,12 @@
1
+ require 'thor/group'
2
+ module DatatablesServerSide
3
+ module Generators
4
+ class DatatableGenerator < Rails::Generators::Base
5
+ source_root File.expand_path("../../templates", __FILE__)
6
+ argument :table_name, :type => :string
7
+ def generate_datatable
8
+ template "generic_datatable.rb","app/datatables/#{table_name.underscore}_datatable.rb"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ require 'thor/group'
2
+ module DatatablesServerSide
3
+ module Generators
4
+ class InstallerGenerator < Rails::Generators::Base
5
+ source_root File.expand_path("../../templates", __FILE__)
6
+ def generate_installer
7
+ empty_directory 'app/datatables'
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,37 @@
1
+ class <%= table_name.camelize %>Datatable < DatatablesServerSide::Datatables
2
+ #You can delegate paths to the @view context, example
3
+ #delegate :model_path, :delete_model_path, :edit_model_path, to: :@view
4
+
5
+ # the json response that will be returned
6
+ def as_json(options = {})
7
+ {
8
+ sEcho: params[:sEcho].to_i,
9
+ iTotalRecords: <%= table_name.camelize.singularize %>.count,
10
+ iTotalDisplayRecords: records.length,
11
+ aaData: safe_attributes(data)
12
+ }
13
+ end
14
+
15
+ private
16
+
17
+ # returns the data object, it should contain the object that datatable will parse, example
18
+ # def data
19
+ # records.map do |example|
20
+ # [
21
+ # example.id,
22
+ # example.name,
23
+ # example.description,
24
+ # ]
25
+ # end
26
+
27
+ def data
28
+ # TODO ADD your code here
29
+ end
30
+
31
+ #this method is responsible for connect with model, here you should add your filters.
32
+ def fetch_records
33
+ <%= table_name.underscore %> = <%= table_name.camelize.singularize %>.all
34
+ <%= table_name.underscore %>
35
+ end
36
+
37
+ end
@@ -0,0 +1,20 @@
1
+ require 'generator_spec'
2
+ require 'generators/datatables_server_side/datatable_generator'
3
+ describe DatatablesServerSide::Generators::DatatableGenerator, type: :generator do
4
+ destination File.expand_path("../tmp", File.dirname(__FILE__))
5
+ arguments %w(TestOfGenerator)
6
+ before(:all) do
7
+ prepare_destination
8
+ run_generator
9
+ end
10
+ it 'creates a file with the correct name' do
11
+ run_generator
12
+ assert_file 'app/datatables/test_of_generator_datatable.rb'
13
+ end
14
+
15
+ it 'creates a file with correct class' do
16
+ run_generator
17
+ assert_file 'app/datatables/test_of_generator_datatable.rb',/class TestOfGeneratorDatatable < DatatablesServerSide::Datatables/
18
+ end
19
+
20
+ end
@@ -0,0 +1,70 @@
1
+ require 'datatables_server_side'
2
+ describe DatatablesServerSide::Datatables do
3
+ def getTable
4
+ context = double(:render => "some content...", :link_to => "<a href='www.somewhere.com'>somewhere</a>") #TODO improve this stub
5
+ DatatablesServerSide::Datatables.new(context)
6
+ end
7
+
8
+ # checks each element inside array to be html_safe
9
+ def is_all_values_safe?(data)
10
+ is_valid=true
11
+ data.each do |array_value|
12
+ if array_value.class==Array
13
+ is_valid= is_valid && is_all_values_safe?(array_value)
14
+ elsif array_value.class!=NilClass
15
+ is_valid= is_valid && array_value.html_safe?
16
+ end
17
+ end
18
+ return is_valid
19
+ end
20
+
21
+ def get_dummy_data
22
+ array_of_text = Array.new
23
+ data=Array.new
24
+ array_of_text[0]="teste"
25
+ array_of_text[1]="teste"
26
+ array_of_text[2]="<a>teste</a>"
27
+ data[0]="teste"
28
+ data[1]=array_of_text
29
+ data
30
+ end
31
+
32
+ it 'initializes' do
33
+ my_table=getTable
34
+ my_table.class.should eq(DatatablesServerSide::Datatables)
35
+ end
36
+
37
+
38
+ it 'protects records with html safe' do
39
+ my_table=getTable
40
+ safe_records=my_table.send(:safe_attributes, get_dummy_data)
41
+ is_all_values_safe?(safe_records).should eq true
42
+ end
43
+
44
+
45
+ it 'gets the correct page' do
46
+ my_table=getTable
47
+ my_table.stub(:per_page){10}
48
+ my_table.stub(:params){{iDisplayStart: nil}}
49
+ my_table.send(:page).should eq 1
50
+ my_table.stub(:params){{iDisplayStart: 2}}
51
+ my_table.send(:page).should eq 1
52
+ my_table.stub(:params){{iDisplayStart: 10}}
53
+ my_table.send(:page).should eq 2
54
+ end
55
+
56
+
57
+ it 'have default per page of 10' do
58
+ my_table=getTable
59
+ my_table.stub(:params){{iDisplayLength: nil}}
60
+ my_table.send(:per_page).should eq 10
61
+ end
62
+
63
+
64
+ it 'have default sor direction of asc' do
65
+ my_table=getTable
66
+ my_table.stub(:params){{sSortDir_0: nil}}
67
+ my_table.send(:sort_direction).should eq 'asc'
68
+ end
69
+
70
+ end
@@ -0,0 +1,15 @@
1
+ require 'generator_spec'
2
+ require 'generators/datatables_server_side/installer_generator'
3
+ describe DatatablesServerSide::Generators::InstallerGenerator, type: :generator do
4
+ destination File.expand_path("../tmp", File.dirname(__FILE__))
5
+ before(:all) do
6
+ prepare_destination
7
+ run_generator
8
+ end
9
+ it 'creates the empty directory' do
10
+ run_generator
11
+ assert_directory 'app/datatables'
12
+ end
13
+
14
+ end
15
+
metadata ADDED
@@ -0,0 +1,175 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: datatables_server_side
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Layon Ferreira
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rspec
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: generator_spec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.7'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.7'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: activesupport
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: actionview
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: railties
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: 3.2.6
104
+ - - "<"
105
+ - !ruby/object:Gem::Version
106
+ version: '5'
107
+ type: :runtime
108
+ prerelease: false
109
+ version_requirements: !ruby/object:Gem::Requirement
110
+ requirements:
111
+ - - ">="
112
+ - !ruby/object:Gem::Version
113
+ version: 3.2.6
114
+ - - "<"
115
+ - !ruby/object:Gem::Version
116
+ version: '5'
117
+ description: Simple gem that helps the creation of json datatables json responses.
118
+ email:
119
+ - layon@live.com.pt
120
+ executables: []
121
+ extensions: []
122
+ extra_rdoc_files: []
123
+ files:
124
+ - ".gitignore"
125
+ - ".idea/.name"
126
+ - ".idea/.rakeTasks"
127
+ - ".idea/datatable_server_side.iml"
128
+ - ".idea/dictionaries/Layon.xml"
129
+ - ".idea/encodings.xml"
130
+ - ".idea/misc.xml"
131
+ - ".idea/modules.xml"
132
+ - ".idea/scopes/scope_settings.xml"
133
+ - ".idea/vcs.xml"
134
+ - ".idea/workspace.xml"
135
+ - Gemfile
136
+ - LICENSE.txt
137
+ - README.md
138
+ - Rakefile
139
+ - datatables_server_side.gemspec
140
+ - lib/datatables_server_side.rb
141
+ - lib/datatables_server_side/version.rb
142
+ - lib/generators/datatables_server_side/datatable_generator.rb
143
+ - lib/generators/datatables_server_side/installer_generator.rb
144
+ - lib/generators/templates/generic_datatable.rb
145
+ - spec/datatable_generator_spec.rb
146
+ - spec/datatables_spec.rb
147
+ - spec/installer_generator_spec.rb
148
+ homepage: https://github.com/layonferreira/datatables_server_side
149
+ licenses:
150
+ - MIT
151
+ metadata: {}
152
+ post_install_message:
153
+ rdoc_options: []
154
+ require_paths:
155
+ - lib
156
+ required_ruby_version: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - ">="
159
+ - !ruby/object:Gem::Version
160
+ version: '0'
161
+ required_rubygems_version: !ruby/object:Gem::Requirement
162
+ requirements:
163
+ - - ">="
164
+ - !ruby/object:Gem::Version
165
+ version: '0'
166
+ requirements: []
167
+ rubyforge_project:
168
+ rubygems_version: 2.2.2
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: Simple gem that helps the creation of json datatables json responses.
172
+ test_files:
173
+ - spec/datatable_generator_spec.rb
174
+ - spec/datatables_spec.rb
175
+ - spec/installer_generator_spec.rb