easy_data_tables 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/Gemfile.lock +34 -0
- data/LICENSE.txt +21 -0
- data/README.md +40 -0
- data/Rakefile +6 -0
- data/app/helpers/table_helper.rb +12 -0
- data/app/models/column.rb +46 -0
- data/app/models/combined_column.rb +33 -0
- data/app/models/data_table.rb +31 -0
- data/app/views/easy_data_tables/_data_table.html.erb +26 -0
- data/bin/rails +14 -0
- data/easy_data_tables.gemspec +29 -0
- data/lib/easy_data_tables.rb +9 -0
- data/lib/easy_data_tables/engine.rb +5 -0
- data/lib/easy_data_tables/version.rb +3 -0
- metadata +65 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 175851ae1970a77c3852ec84c03273bf471236d5d221287e4a52808d546baa11
|
4
|
+
data.tar.gz: 963dd66681346f045d2d0342244280ee8e39c47edff793683e9d70b4138efbde
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d3c73640d78792ebcba8b6d77378f14f45e01e72d4e5d5f7ff38a4f67bcdf24ad71c3394237b952b6d9fabc2748a2da091102e4a0cd195638e0e0f5e7f4e3394
|
7
|
+
data.tar.gz: 34e061900219b92aeb9d1afba6c922c920b5c0922bae62de175c0401fa6890a8402275a42f72a09f5e93539015b0150258605df0e3e10674df95512450ce412d
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
easy_data_tables (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
diff-lcs (1.4.4)
|
10
|
+
rake (12.3.3)
|
11
|
+
rspec (3.10.0)
|
12
|
+
rspec-core (~> 3.10.0)
|
13
|
+
rspec-expectations (~> 3.10.0)
|
14
|
+
rspec-mocks (~> 3.10.0)
|
15
|
+
rspec-core (3.10.1)
|
16
|
+
rspec-support (~> 3.10.0)
|
17
|
+
rspec-expectations (3.10.1)
|
18
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
19
|
+
rspec-support (~> 3.10.0)
|
20
|
+
rspec-mocks (3.10.2)
|
21
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
22
|
+
rspec-support (~> 3.10.0)
|
23
|
+
rspec-support (3.10.2)
|
24
|
+
|
25
|
+
PLATFORMS
|
26
|
+
ruby
|
27
|
+
|
28
|
+
DEPENDENCIES
|
29
|
+
easy_data_tables!
|
30
|
+
rake (~> 12.0)
|
31
|
+
rspec (~> 3.0)
|
32
|
+
|
33
|
+
BUNDLED WITH
|
34
|
+
2.1.4
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Pablo Curell
|
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
|
13
|
+
all 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
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# EasyDataTables
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/easy_data_tables`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'easy_data_tables'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle install
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install easy_data_tables
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
30
|
+
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/easy_data_tables.
|
36
|
+
|
37
|
+
|
38
|
+
## License
|
39
|
+
|
40
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Column
|
4
|
+
attr_reader :label
|
5
|
+
|
6
|
+
def initialize(args = {}) #values = {}, label = '', type = 'Integer')
|
7
|
+
@label = args[:label] || ''
|
8
|
+
@type = args[:type] || 'Integer'
|
9
|
+
@default = args[:default] || 0
|
10
|
+
@collection = args[:collection]
|
11
|
+
@grouping = args[:grouping]
|
12
|
+
@agregate_function = args[:agregate_function]
|
13
|
+
@values = construct_values
|
14
|
+
end
|
15
|
+
|
16
|
+
|
17
|
+
def formated_data_at(row)
|
18
|
+
case @type
|
19
|
+
when 'Integer'
|
20
|
+
helpers.number_with_delimiter(data_at(row))
|
21
|
+
when 'Percentage'
|
22
|
+
helpers.number_to_percentage(data_at(row), precision: 2)
|
23
|
+
when 'Currency'
|
24
|
+
helpers.number_to_currency(data_at(row))
|
25
|
+
else
|
26
|
+
data_at(row)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def data_at(row)
|
31
|
+
@values[row]
|
32
|
+
end
|
33
|
+
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def construct_values
|
38
|
+
Hash.new(@default)
|
39
|
+
.merge(@collection.send(*@grouping).send(*@agregate_function))
|
40
|
+
.merge({ 'TOTAL' => @collection.send(*@agregate_function) })
|
41
|
+
end
|
42
|
+
|
43
|
+
def helpers
|
44
|
+
ActionController::Base.helpers
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
|
4
|
+
class CombinedColumn < Column
|
5
|
+
attr_accessor :columns
|
6
|
+
|
7
|
+
def initialize(args = {})
|
8
|
+
super(args)
|
9
|
+
@columns = args[:columns]
|
10
|
+
@method = args[:method]
|
11
|
+
end
|
12
|
+
|
13
|
+
def data_at(row)
|
14
|
+
combine(row)
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def combine(row)
|
20
|
+
case @method
|
21
|
+
when 'rate'
|
22
|
+
columns[0].data_at(row).fdiv(columns[1].data_at(row)) * 100
|
23
|
+
when 'substract'
|
24
|
+
columns[0].data_at(row) - columns[1].data_at(row)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def construct_values; end
|
29
|
+
|
30
|
+
def helpers
|
31
|
+
ActionController::Base.helpers
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
class DataTable
|
2
|
+
attr_reader :columns, :rows, :labels
|
3
|
+
|
4
|
+
def initialize(columns, rows, grouping)
|
5
|
+
@grouping = grouping
|
6
|
+
@rows = rows + ["TOTAL"]
|
7
|
+
@columns = treat_columns(columns)
|
8
|
+
@labels = @columns.map(&:label)
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
|
14
|
+
def treat_columns(columns)
|
15
|
+
columns.map! do |col|
|
16
|
+
if col[:column_type] == 'combined'
|
17
|
+
CombinedColumn.new(col)
|
18
|
+
else
|
19
|
+
Column.new(col.merge(grouping: @grouping))
|
20
|
+
end
|
21
|
+
end
|
22
|
+
columns.map! do |col|
|
23
|
+
if col.is_a?(CombinedColumn)
|
24
|
+
col.columns = columns.find_all { |col2| col.columns.include?(col2.label) }.sort do |column|
|
25
|
+
col.columns.index(column.label)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
col
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
<table class="table datatable" data-controller="datatable">
|
2
|
+
<thead>
|
3
|
+
<tr>
|
4
|
+
<th></th>
|
5
|
+
<% data_table.labels.each do |label| %>
|
6
|
+
<th title="<%=t(".#{label}_title")%>" >
|
7
|
+
<%= t(".#{label}") %>
|
8
|
+
</th>
|
9
|
+
<% end %>
|
10
|
+
</tr>
|
11
|
+
</thead>
|
12
|
+
<tbody>
|
13
|
+
<% data_table.rows.each do |row| %>
|
14
|
+
<tr>
|
15
|
+
<td>
|
16
|
+
<%= row %>
|
17
|
+
</td>
|
18
|
+
<% data_table.columns.each do |column| %>
|
19
|
+
<td>
|
20
|
+
<%= column.formated_data_at(row) %>
|
21
|
+
</td>
|
22
|
+
<% end %>
|
23
|
+
</tr>
|
24
|
+
<% end %>
|
25
|
+
</tbody>
|
26
|
+
</table>
|
data/bin/rails
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# This command will automatically be run when you run "rails" with Rails gems
|
3
|
+
# installed from the root of your application.
|
4
|
+
|
5
|
+
ENGINE_ROOT = File.expand_path('..', __dir__)
|
6
|
+
ENGINE_PATH = File.expand_path('../lib/easy_data_tables/engine', __dir__)
|
7
|
+
APP_PATH = File.expand_path('../test/dummy/config/application', __dir__)
|
8
|
+
|
9
|
+
# Set up gems listed in the Gemfile.
|
10
|
+
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
|
11
|
+
require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])
|
12
|
+
|
13
|
+
require "rails/all"
|
14
|
+
require "rails/engine/commands"
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require_relative 'lib/easy_data_tables/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = "easy_data_tables"
|
5
|
+
spec.version = EasyDataTables::VERSION
|
6
|
+
spec.authors = ["Pablo Curell"]
|
7
|
+
spec.email = ["pablocm90@gmail.com"]
|
8
|
+
|
9
|
+
spec.summary = "Gem to easily create data tables"
|
10
|
+
spec.description = "tbc"
|
11
|
+
spec.homepage = "https://github.com/pablocm90/easy_data_tables"
|
12
|
+
spec.license = "MIT"
|
13
|
+
spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
|
14
|
+
|
15
|
+
spec.metadata["allowed_push_host"] = "https://rubygems.org/"
|
16
|
+
|
17
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
18
|
+
spec.metadata["source_code_uri"] = "https://github.com/pablocm90/easy_data_tables"
|
19
|
+
spec.metadata["changelog_uri"] = "https://github.com/pablocm90/easy_data_tables"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
24
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
25
|
+
end
|
26
|
+
spec.bindir = "exe"
|
27
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
28
|
+
spec.require_paths = ["lib"]
|
29
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
require "easy_data_tables/version"
|
2
|
+
# require "app/helpers/helper"
|
3
|
+
require "easy_data_tables/engine"
|
4
|
+
# require "easy_data_tables/models/column"
|
5
|
+
# require "easy_data_tables/models/combined_column"
|
6
|
+
|
7
|
+
module EasyDataTables
|
8
|
+
class Error < StandardError; end
|
9
|
+
end
|
metadata
ADDED
@@ -0,0 +1,65 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: easy_data_tables
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Pablo Curell
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-06-01 00:00:00.000000000 Z
|
12
|
+
dependencies: []
|
13
|
+
description: tbc
|
14
|
+
email:
|
15
|
+
- pablocm90@gmail.com
|
16
|
+
executables: []
|
17
|
+
extensions: []
|
18
|
+
extra_rdoc_files: []
|
19
|
+
files:
|
20
|
+
- ".gitignore"
|
21
|
+
- ".rspec"
|
22
|
+
- ".travis.yml"
|
23
|
+
- Gemfile
|
24
|
+
- Gemfile.lock
|
25
|
+
- LICENSE.txt
|
26
|
+
- README.md
|
27
|
+
- Rakefile
|
28
|
+
- app/helpers/table_helper.rb
|
29
|
+
- app/models/column.rb
|
30
|
+
- app/models/combined_column.rb
|
31
|
+
- app/models/data_table.rb
|
32
|
+
- app/views/easy_data_tables/_data_table.html.erb
|
33
|
+
- bin/rails
|
34
|
+
- easy_data_tables.gemspec
|
35
|
+
- lib/easy_data_tables.rb
|
36
|
+
- lib/easy_data_tables/engine.rb
|
37
|
+
- lib/easy_data_tables/version.rb
|
38
|
+
homepage: https://github.com/pablocm90/easy_data_tables
|
39
|
+
licenses:
|
40
|
+
- MIT
|
41
|
+
metadata:
|
42
|
+
allowed_push_host: https://rubygems.org/
|
43
|
+
homepage_uri: https://github.com/pablocm90/easy_data_tables
|
44
|
+
source_code_uri: https://github.com/pablocm90/easy_data_tables
|
45
|
+
changelog_uri: https://github.com/pablocm90/easy_data_tables
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options: []
|
48
|
+
require_paths:
|
49
|
+
- lib
|
50
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 2.3.0
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - ">="
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '0'
|
60
|
+
requirements: []
|
61
|
+
rubygems_version: 3.0.3
|
62
|
+
signing_key:
|
63
|
+
specification_version: 4
|
64
|
+
summary: Gem to easily create data tables
|
65
|
+
test_files: []
|