datatables_records_mapper 1.0.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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 36786fef7544a37d18a5cced22bfbbcfafac284d
4
+ data.tar.gz: bfbf32616e9a58c0a011905d12383538d3916164
5
+ SHA512:
6
+ metadata.gz: f48f14a597d99120a0051bd4ed8b7d9769c77669fe70626a48f29e03680b4b0ee07ff6a46fabd45ce1ab5bc99db40a8c5a93271f3693bb19177402a25d655688
7
+ data.tar.gz: 0f73c8b57ff1f04abe6ac266b623d6d635fca6053462b4e45974fceda2c97406f3021e9a33b1b6d0acae2516d5b10beabbdc1c35ef36fba143d789d13b65747f
data/.gitignore ADDED
@@ -0,0 +1,44 @@
1
+ *.rbc
2
+ capybara-*.html
3
+ .rspec
4
+ /log
5
+ /tmp
6
+ /db/*.sqlite3
7
+ /db/*.sqlite3-journal
8
+ /public/system
9
+ /coverage/
10
+ /spec/tmp
11
+ *.orig
12
+ rerun.txt
13
+ pickle-email-*.html
14
+
15
+ # TODO Comment out this rule if you are OK with secrets being uploaded to the repo
16
+ config/initializers/secret_token.rb
17
+
18
+ # Only include if you have production secrets in this file, which is no longer a Rails default
19
+ # config/secrets.yml
20
+
21
+ # dotenv
22
+ # TODO Comment out this rule if environment variables can be committed
23
+ .env
24
+
25
+ ## Environment normalization:
26
+ /.bundle
27
+ /vendor/bundle
28
+
29
+ # these should all be checked in to normalize the environment:
30
+ # Gemfile.lock, .ruby-version, .ruby-gemset
31
+
32
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
33
+ .rvmrc
34
+
35
+ # if using bower-rails ignore default bower_components path bower.json files
36
+ /vendor/assets/bower_components
37
+ *.bowerrc
38
+ bower.json
39
+
40
+ # Ignore pow environment settings
41
+ .powenv
42
+
43
+ # Ignore Byebug command history file.
44
+ .byebug_history
data/Gemfile ADDED
@@ -0,0 +1,15 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Declare your gem's dependencies in datatables_records_mapper.gemspec.
4
+ # Bundler will treat runtime dependencies like base dependencies, and
5
+ # development dependencies will be added by default to the :development group.
6
+ gemspec
7
+
8
+ # Declare any dependencies that are still in development here instead of in
9
+ # your gemspec. These might include edge Rails or gems from your path or
10
+ # Git. Remember to move these dependencies to your gemspec before releasing
11
+ # your gem to rubygems.org.
12
+
13
+ # To use a debugger
14
+ # gem 'byebug', group: [:development, :test]
15
+
data/Gemfile.lock ADDED
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ datatables_records_mapper (1.0.0)
5
+ activemodel (~> 4.2.6)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ activemodel (4.2.9)
11
+ activesupport (= 4.2.9)
12
+ builder (~> 3.1)
13
+ activesupport (4.2.9)
14
+ i18n (~> 0.7)
15
+ minitest (~> 5.1)
16
+ thread_safe (~> 0.3, >= 0.3.4)
17
+ tzinfo (~> 1.1)
18
+ builder (3.2.3)
19
+ byebug (9.1.0)
20
+ coderay (1.1.1)
21
+ diff-lcs (1.3)
22
+ i18n (0.8.6)
23
+ method_source (0.8.2)
24
+ minitest (5.10.3)
25
+ pry (0.10.4)
26
+ coderay (~> 1.1.0)
27
+ method_source (~> 0.8.1)
28
+ slop (~> 3.4)
29
+ pry-byebug (3.5.0)
30
+ byebug (~> 9.1)
31
+ pry (~> 0.10)
32
+ rake (10.5.0)
33
+ rspec (3.6.0)
34
+ rspec-core (~> 3.6.0)
35
+ rspec-expectations (~> 3.6.0)
36
+ rspec-mocks (~> 3.6.0)
37
+ rspec-core (3.6.0)
38
+ rspec-support (~> 3.6.0)
39
+ rspec-expectations (3.6.0)
40
+ diff-lcs (>= 1.2.0, < 2.0)
41
+ rspec-support (~> 3.6.0)
42
+ rspec-mocks (3.6.0)
43
+ diff-lcs (>= 1.2.0, < 2.0)
44
+ rspec-support (~> 3.6.0)
45
+ rspec-support (3.6.0)
46
+ shoulda-matchers (3.1.1)
47
+ activesupport (>= 4.0.0)
48
+ slop (3.6.0)
49
+ thread_safe (0.3.6)
50
+ tzinfo (1.2.3)
51
+ thread_safe (~> 0.1)
52
+
53
+ PLATFORMS
54
+ ruby
55
+
56
+ DEPENDENCIES
57
+ bundler (~> 1.14)
58
+ byebug
59
+ datatables_records_mapper!
60
+ pry
61
+ pry-byebug
62
+ rake (~> 10.0)
63
+ rspec (~> 3.0)
64
+ shoulda-matchers
65
+
66
+ BUNDLED WITH
67
+ 1.15.4
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2017 Klismann
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017 Klismann
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,88 @@
1
+ The purpose of this gem is to handle the ajax calls made by the [datatables plugin](https://datatables.net).
2
+
3
+ It parses some of the parameters (not all are yet supported) and returns the required data.
4
+
5
+ USAGE:
6
+ To use this gem you will need to call the mapper class passing as arguments the model, parameters and a mapper. In this
7
+ example, it is used a mapper which uses a partial view to render the actions column.
8
+
9
+ First, let's take a look at a piece of controller code:
10
+ ```erb
11
+ def services_list
12
+ respond_to do |format|
13
+ format.json { render json: services_list_data }
14
+ end
15
+ end
16
+
17
+ private
18
+
19
+ def services_list_data
20
+ mapper = ModelMappers::ServiceMapper.new(service_actions)
21
+ DatatablesRecordsMapper::Mapper
22
+ .new(Service, params, mapper)
23
+ .mapped_records
24
+ end
25
+
26
+ def service_actions
27
+ @service_actions ||= Proc.new do |service|
28
+ render_to_string(
29
+ 'actions',
30
+ locals: { service_id: service.id },
31
+ formats: :html,
32
+ layout: false
33
+ )
34
+ end
35
+ end
36
+ ```
37
+
38
+ This code is responsible for the endpoint 'GET services/services_list'. It instanciates a mapper for the model Service,
39
+ which uses the service_acitons proc to render the actions column.
40
+
41
+ The mapper code:
42
+ ```erb
43
+ module ModelMappers
44
+ class ServiceMapper
45
+ def initialize(actions_renderer)
46
+ @actions_renderer = actions_renderer
47
+ end
48
+
49
+ def map(service)
50
+ {
51
+ id: service.id,
52
+ title: service.title,
53
+ description: service.description,
54
+ actions: @actions_renderer.call(service)
55
+ }
56
+ end
57
+ end
58
+ end
59
+ ```
60
+
61
+ The only restriction in this class is the method 'map', which is used by the gem to return the data.
62
+
63
+ The actions partial view:
64
+ ```erb
65
+ a.btn.btn-success.btn-margin-right title=(I18n.t 'links.default.actions.show') href=(service_path(service_id))
66
+ span.fa.fa-search
67
+ ```
68
+
69
+ The model code:
70
+ ```erb
71
+ class Service < ActiveRecord::Base
72
+ class << self
73
+ def search(search_parameter, options)
74
+ services = Service.all
75
+ return services unless search_parameter.present?
76
+ search_parameter = "%#{search_parameter.downcase}%"
77
+ services.where('LOWER(title) like ? OR LOWER(description) like ?', search_parameter, search_parameter)
78
+ end
79
+
80
+ def paginate(services, limit, offset)
81
+ services.
82
+ order('id desc').
83
+ limit(limit).
84
+ offset(offset)
85
+ end
86
+ end
87
+ end
88
+ ```
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = DatatablesRecordsMapper
2
+
3
+ This project rocks and uses MIT-LICENSE.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'DatatablesRecordsMapper'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+
18
+
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "datatables_records_mapper"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,32 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "datatables_records_mapper/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "datatables_records_mapper"
8
+ spec.version = DatatablesRecordsMapper::VERSION
9
+ spec.authors = ["klismannsm"]
10
+ spec.email = ["klismannsm@gmail.com"]
11
+
12
+ spec.summary = 'Map records to a format used by jquery datatables plugin'
13
+ spec.description = 'This gem interprets the parameters passed by the ajax call made by the datatables plugin and returns the requested records'
14
+ spec.homepage = 'https://github.com/klismannsm/datatables_records_mapper'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.14"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "rspec", "~> 3.0"
27
+ spec.add_development_dependency "shoulda-matchers"
28
+ spec.add_development_dependency 'pry-byebug'
29
+ spec.add_development_dependency 'pry'
30
+ spec.add_development_dependency 'byebug'
31
+ spec.add_dependency "activemodel", "~> 4.2.6"
32
+ end
@@ -0,0 +1,6 @@
1
+ require 'datatables_records_mapper/version'
2
+ require 'datatables_records_mapper/mapper'
3
+ require 'datatables_records_mapper/filter'
4
+
5
+ module DatatablesRecordsMapper
6
+ end
@@ -0,0 +1,44 @@
1
+ module DatatablesRecordsMapper
2
+ class Filter
3
+ def initialize(model, params, options = {})
4
+ @model = model
5
+ @params = params
6
+ @options = options
7
+ end
8
+
9
+ def record_list
10
+ number_of_filtered_records = records.length
11
+ paginated_records = @model.paginate(records, limit, offset)
12
+ {
13
+ 'recordsTotal' => @model.count,
14
+ 'recordsFiltered' => number_of_filtered_records,
15
+ 'data' => paginated_records
16
+ }
17
+ end
18
+
19
+ private
20
+
21
+ def records
22
+ @records ||= begin
23
+ return @model.search(search_parameter, param_options) unless @options.has_key? :records
24
+ @model.search(@options[:records], search_parameter, param_options)
25
+ end
26
+ end
27
+
28
+ def search_parameter
29
+ @params[:search][:value]
30
+ end
31
+
32
+ def param_options
33
+ @param_options ||= @params[:options] || {}
34
+ end
35
+
36
+ def limit
37
+ @params[:length]
38
+ end
39
+
40
+ def offset
41
+ @params[:start]
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,21 @@
1
+ module DatatablesRecordsMapper
2
+ class Mapper
3
+ def initialize(model, params, mapper, options = {})
4
+ @model = model
5
+ @params = params
6
+ @mapper = mapper
7
+ @options = options
8
+ end
9
+
10
+ def mapped_records
11
+ records['data'] = records['data'].map { |r| @mapper.map(r) }
12
+ records
13
+ end
14
+
15
+ private
16
+
17
+ def records
18
+ @records ||= DatatablesRecordsMapper::Filter.new(@model, @params, @options).record_list
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,3 @@
1
+ module DatatablesRecordsMapper
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :datatables_records_mapper do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,172 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: datatables_records_mapper
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - klismannsm
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-10-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.14'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.14'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: shoulda-matchers
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry-byebug
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
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: pry
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
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: byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: activemodel
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 4.2.6
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 4.2.6
125
+ description: This gem interprets the parameters passed by the ajax call made by the
126
+ datatables plugin and returns the requested records
127
+ email:
128
+ - klismannsm@gmail.com
129
+ executables: []
130
+ extensions: []
131
+ extra_rdoc_files: []
132
+ files:
133
+ - ".gitignore"
134
+ - Gemfile
135
+ - Gemfile.lock
136
+ - LICENSE
137
+ - MIT-LICENSE
138
+ - README.md
139
+ - README.rdoc
140
+ - Rakefile
141
+ - bin/console
142
+ - datatables_records_mapper.gemspec
143
+ - lib/datatables_records_mapper.rb
144
+ - lib/datatables_records_mapper/filter.rb
145
+ - lib/datatables_records_mapper/mapper.rb
146
+ - lib/datatables_records_mapper/version.rb
147
+ - lib/tasks/datatables_records_mapper_tasks.rake
148
+ homepage: https://github.com/klismannsm/datatables_records_mapper
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.6.12
169
+ signing_key:
170
+ specification_version: 4
171
+ summary: Map records to a format used by jquery datatables plugin
172
+ test_files: []