easy_data_tables 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 175851ae1970a77c3852ec84c03273bf471236d5d221287e4a52808d546baa11
4
- data.tar.gz: 963dd66681346f045d2d0342244280ee8e39c47edff793683e9d70b4138efbde
3
+ metadata.gz: 3cb80cf6b75f6538198a91b5e47dda21b23d33f16ad59017e23601c19c1f7f4b
4
+ data.tar.gz: f157300ea30e55dee0fbf082c8da2e9e3681a7e19b6ebf39d56e71b1e3279820
5
5
  SHA512:
6
- metadata.gz: d3c73640d78792ebcba8b6d77378f14f45e01e72d4e5d5f7ff38a4f67bcdf24ad71c3394237b952b6d9fabc2748a2da091102e4a0cd195638e0e0f5e7f4e3394
7
- data.tar.gz: 34e061900219b92aeb9d1afba6c922c920b5c0922bae62de175c0401fa6890a8402275a42f72a09f5e93539015b0150258605df0e3e10674df95512450ce412d
6
+ metadata.gz: f9db24e2600639ee0f711be2e123e410af17922cbb793380f930c2c137681fd85e1237cfe00e5a262242578b217897296aceeac4ff2a0b28c8dfb4eec2bfa394
7
+ data.tar.gz: 5b069d81d97bf2c07efbac3e05065dc5357d95111e74a5a56dc0dcfedd37f82d16746e64609fff0f8400f8318fc8470cc53058126ed1ccbe86a1461246e78ae8
data/.gitignore CHANGED
@@ -9,3 +9,4 @@
9
9
 
10
10
  # rspec failure tracking
11
11
  .rspec_status
12
+ *.gem
data/.rubocop.yml ADDED
@@ -0,0 +1,2 @@
1
+ AllCops:
2
+ TargetRubyVersion: 2.4
data/Gemfile CHANGED
@@ -1,7 +1,9 @@
1
- source "https://rubygems.org"
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
2
4
 
3
5
  # Specify your gem's dependencies in easy_data_tables.gemspec
4
6
  gemspec
5
7
 
6
- gem "rake", "~> 12.0"
7
- gem "rspec", "~> 3.0"
8
+ gem 'rake', '~> 12.0'
9
+ gem 'rspec', '~> 3.0'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- easy_data_tables (0.1.0)
4
+ easy_data_tables (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
data/README.md CHANGED
@@ -1,8 +1,6 @@
1
1
  # EasyDataTables
2
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
3
+ This gem provides a way to create fast tables based on the models of your db. It will expose a helper method `easy_data_table(columns, rows, grouping)`that will output a datatable with the rows and the columns you indicated.
6
4
 
7
5
  ## Installation
8
6
 
@@ -22,17 +20,122 @@ Or install it yourself as:
22
20
 
23
21
  ## Usage
24
22
 
25
- TODO: Write usage instructions here
23
+ ### Helper Method
24
+
25
+ You will have a method available once you install this gem: `easy_data_table(columns, rows, grouping)` in order to expose it, you need to add `helper EasyDataTables::Engine.helpers` to your app's ApplicationController. You can call this helper method in any view and it will output a table with the data you provide.
26
+
27
+ ### Parameters
28
+
29
+ #### Columns
30
+
31
+ Columns are an array of hashes. There are two types of columns: 'normal' and 'combined'
32
+
33
+ Normal column hashes accept the following keys:
34
+
35
+ - **label**: string, default: '' => will inform the label of the column on the table
36
+ - **type**: string, default: 'Integer', options: 'Integer', 'Currency', 'Percentage' => will inform the formating of the column
37
+ - **default**: any, default: 0 => Will inform the default when a value is not found
38
+ - **collection**: ActiveRecord::Relation, **required** => The data of the column (e.g. User.where(active: true)- -
39
+ - **agregate_function**: Array of symbols OR symbol, **required** => aggregate function to run (e.g. [:average, :expense] OR :count)
40
+ - **column_type**: string => will inform the type of column to use
41
+
42
+ Cobmbined column hashes accept the following keys:
43
+
44
+ - **label**: string, default: '' => will inform the label of the column on the table
45
+ - **type**: string, default: 'Integer', options: 'Integer', 'Currency', 'Percentage' => will inform the formating of the column
46
+ - **columns**: Array, **required** => Will inform the columns to combine (e.g. ['expenditure', 'user_count'])
47
+ - **method**: string, options: 'rate', 'substract', **required** => how to combine the columns to produce the data cell value
48
+ - **column_type**: string, **must be set to 'combined'** => will inform the type of column to use
49
+
50
+ ### Rows
51
+
52
+ Here you must pass an Array of strings to inform the label of each row.
53
+
54
+ ### Grouping
55
+
56
+ Array of strings where the first string is the grouping method you are calling and the rest are the arguments of said method **e.g.** ['group', 'users.full_name'}
57
+
58
+
59
+
60
+ ### Example:
61
+
62
+ ```ruby
63
+ easy_data_table(
64
+ [
65
+ {
66
+ label: 'user_count',
67
+ type: 'Integer',
68
+ collection: User.all,
69
+ agregate_function: :count
70
+ },
71
+ {
72
+ label: 'active_user_count',
73
+ type: 'Integer',
74
+ collection: User.where(active: true),
75
+ agregate_function: :count
76
+ },
77
+ {
78
+ label: 'active_user_expense',
79
+ type: 'Currency',
80
+ collection: Expense.joins(:user).where(users: {active: true})
81
+ agregate_function: [:sum, :total]
82
+ },
83
+ {
84
+ column_type: 'combined',
85
+ columns: ['active_user_count', 'user_count'],
86
+ method: 'rate',
87
+ label: 'active_user_rate'
88
+ }
89
+ ],
90
+ User.all.pluck(:status).uniq,
91
+ ['group', 'users.status']
92
+ )
93
+
94
+ ```
95
+ will generate a table that looks like this:
96
+
97
+ | | User count | Active user count | Active user expense | Active User Rate |
98
+ |------------|------------|-------------------|---------------------|------------------|
99
+ | Premium | 10 | 8 | 90 $ | 80 % |
100
+ | Freemium | 5 | 3 | 0 $ | 60 % |
101
+ | Premium ++ | 3 | 1 | 150 $ | 33.33 % |
102
+
103
+ The table has the classes : "table" and "datatable"
104
+
105
+ in order to have correct looking column labels you must have a I18n file that will have:
106
+
107
+ ```yaml
108
+ en:
109
+ easy_data_tables:
110
+ data_table:
111
+ user_count: User count
112
+ user_count_title: "Count of all the users that have the row's status"
113
+ active_user_count: Active User Count
114
+ active_user_count_title: Active users for each status
115
+ active_user_expense: Active User Expense
116
+ active_user_expense_title: Sum of the expenses for the active users of each status
117
+ active_user_rate: Active User Rate
118
+ active_user_rate_title: % of active users over total users per status
119
+ ```
120
+
121
+ On hover on a column label, you will have the title that appears as a tooltip.
122
+
123
+
124
+
125
+ ## Contributing
26
126
 
27
- ## Development
127
+ In order to contribute, do not hesitate to fork the repository and submit a pull request.
28
128
 
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.
129
+ Known to-dos:
30
130
 
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).
131
+ - Test the codebase
132
+ - Add more methods to combined columns
133
+ - Add more types
134
+ - As is now, a TOTAL row will be appended at the end, ideally we should be able to provide our own total if we want to overwrite it.
32
135
 
33
136
  ## Contributing
34
137
 
35
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/easy_data_tables.
138
+ Bug reports and pull requests are welcome on GitHub at https://github.com/pablocm90/easy_data_tables.
36
139
 
37
140
 
38
141
  ## License
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Creates and exposes the helper method
3
4
  module TableHelper
4
5
  def easy_data_table(columns, label, grouping)
5
6
  data_table = DataTable.new(
@@ -7,6 +8,6 @@ module TableHelper
7
8
  label,
8
9
  grouping
9
10
  )
10
- render "easy_data_tables/data_table", data_table: data_table
11
+ render 'easy_data_tables/data_table', data_table: data_table
11
12
  end
12
- end
13
+ end
data/app/models/column.rb CHANGED
@@ -1,9 +1,11 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Column class, we can access the formated data and the data of a particular cell
3
4
  class Column
4
5
  attr_reader :label
5
6
 
6
- def initialize(args = {}) #values = {}, label = '', type = 'Integer')
7
+ # values = {}, label = '', type = 'Integer')
8
+ def initialize(args = {})
7
9
  @label = args[:label] || ''
8
10
  @type = args[:type] || 'Integer'
9
11
  @default = args[:default] || 0
@@ -13,7 +15,6 @@ class Column
13
15
  @values = construct_values
14
16
  end
15
17
 
16
-
17
18
  def formated_data_at(row)
18
19
  case @type
19
20
  when 'Integer'
@@ -31,13 +32,12 @@ class Column
31
32
  @values[row]
32
33
  end
33
34
 
34
-
35
35
  private
36
36
 
37
37
  def construct_values
38
38
  Hash.new(@default)
39
- .merge(@collection.send(*@grouping).send(*@agregate_function))
40
- .merge({ 'TOTAL' => @collection.send(*@agregate_function) })
39
+ .merge(@collection.send(*@grouping).send(*@agregate_function))
40
+ .merge({ 'TOTAL' => @collection.send(*@agregate_function) })
41
41
  end
42
42
 
43
43
  def helpers
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
-
3
+ # combined columns. can substract and create rates
4
4
  class CombinedColumn < Column
5
5
  attr_accessor :columns
6
6
 
@@ -8,6 +8,8 @@ class CombinedColumn < Column
8
8
  super(args)
9
9
  @columns = args[:columns]
10
10
  @method = args[:method]
11
+ @type = args[:type]
12
+ @label = args[:label]
11
13
  end
12
14
 
13
15
  def data_at(row)
@@ -1,24 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ # creates the datatable and it's columns
1
4
  class DataTable
2
5
  attr_reader :columns, :rows, :labels
3
6
 
4
7
  def initialize(columns, rows, grouping)
5
8
  @grouping = grouping
6
- @rows = rows + ["TOTAL"]
9
+ @rows = rows + ['TOTAL']
7
10
  @columns = treat_columns(columns)
8
11
  @labels = @columns.map(&:label)
9
12
  end
10
13
 
11
14
  private
12
15
 
13
-
14
16
  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
17
+ convert_columns(columns)
22
18
  columns.map! do |col|
23
19
  if col.is_a?(CombinedColumn)
24
20
  col.columns = columns.find_all { |col2| col.columns.include?(col2.label) }.sort do |column|
@@ -28,4 +24,14 @@ class DataTable
28
24
  col
29
25
  end
30
26
  end
31
- end
27
+
28
+ def convert_columns(columns)
29
+ columns.map! do |col|
30
+ if col[:column_type] == 'combined'
31
+ CombinedColumn.new(col)
32
+ else
33
+ Column.new(col.merge(grouping: @grouping))
34
+ end
35
+ end
36
+ end
37
+ end
data/bin/rails CHANGED
@@ -1,4 +1,6 @@
1
1
  #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
2
4
  # This command will automatically be run when you run "rails" with Rails gems
3
5
  # installed from the root of your application.
4
6
 
@@ -8,7 +10,7 @@ APP_PATH = File.expand_path('../test/dummy/config/application', __dir__)
8
10
 
9
11
  # Set up gems listed in the Gemfile.
10
12
  ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
11
- require "bundler/setup" if File.exist?(ENV["BUNDLE_GEMFILE"])
13
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
12
14
 
13
- require "rails/all"
14
- require "rails/engine/commands"
15
+ require 'rails/all'
16
+ require 'rails/engine/commands'
@@ -1,29 +1,31 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'lib/easy_data_tables/version'
2
4
 
3
5
  Gem::Specification.new do |spec|
4
- spec.name = "easy_data_tables"
6
+ spec.name = 'easy_data_tables'
5
7
  spec.version = EasyDataTables::VERSION
6
- spec.authors = ["Pablo Curell"]
7
- spec.email = ["pablocm90@gmail.com"]
8
+ spec.authors = ['Pablo Curell']
9
+ spec.email = ['pablocm90@gmail.com']
8
10
 
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")
11
+ spec.summary = 'Gem to easily create data tables'
12
+ spec.description = 'Create fast tables based on the models of your db'
13
+ spec.homepage = 'https://github.com/pablocm90/easy_data_tables'
14
+ spec.license = 'MIT'
15
+ spec.required_ruby_version = Gem::Requirement.new('>= 2.4.0')
14
16
 
15
- spec.metadata["allowed_push_host"] = "https://rubygems.org/"
17
+ spec.metadata['allowed_push_host'] = "https://rubygems.org/"
16
18
 
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"
19
+ spec.metadata['homepage_uri'] = spec.homepage
20
+ spec.metadata['source_code_uri'] = 'https://github.com/pablocm90/easy_data_tables'
21
+ spec.metadata['changelog_uri'] = 'https://github.com/pablocm90/easy_data_tables'
20
22
 
21
23
  # Specify which files should be added to the gem when it is released.
22
24
  # 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
25
+ spec.files = Dir.chdir(File.expand_path(__dir__)) do
24
26
  `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
25
27
  end
26
- spec.bindir = "exe"
28
+ spec.bindir = 'exe'
27
29
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
- spec.require_paths = ["lib"]
30
+ spec.require_paths = ['lib']
29
31
  end
@@ -1,6 +1,8 @@
1
- require "easy_data_tables/version"
1
+ # frozen_string_literal: true
2
+
3
+ require 'easy_data_tables/version'
2
4
  # require "app/helpers/helper"
3
- require "easy_data_tables/engine"
5
+ require 'easy_data_tables/engine'
4
6
  # require "easy_data_tables/models/column"
5
7
  # require "easy_data_tables/models/combined_column"
6
8
 
@@ -1,5 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EasyDataTables
2
4
  class Engine < Rails::Engine
3
5
  isolate_namespace EasyDataTables
4
6
  end
5
- end
7
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module EasyDataTables
2
- VERSION = "0.1.0"
4
+ VERSION = '0.1.1'
3
5
  end
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: easy_data_tables
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Pablo Curell
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-06-01 00:00:00.000000000 Z
11
+ date: 2021-06-02 00:00:00.000000000 Z
12
12
  dependencies: []
13
- description: tbc
13
+ description: Create fast tables based on the models of your db
14
14
  email:
15
15
  - pablocm90@gmail.com
16
16
  executables: []
@@ -19,6 +19,7 @@ extra_rdoc_files: []
19
19
  files:
20
20
  - ".gitignore"
21
21
  - ".rspec"
22
+ - ".rubocop.yml"
22
23
  - ".travis.yml"
23
24
  - Gemfile
24
25
  - Gemfile.lock
@@ -51,14 +52,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
51
52
  requirements:
52
53
  - - ">="
53
54
  - !ruby/object:Gem::Version
54
- version: 2.3.0
55
+ version: 2.4.0
55
56
  required_rubygems_version: !ruby/object:Gem::Requirement
56
57
  requirements:
57
58
  - - ">="
58
59
  - !ruby/object:Gem::Version
59
60
  version: '0'
60
61
  requirements: []
61
- rubygems_version: 3.0.3
62
+ rubygems_version: 3.1.2
62
63
  signing_key:
63
64
  specification_version: 4
64
65
  summary: Gem to easily create data tables