grid_table 1.3 → 1.4

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
  SHA1:
3
- metadata.gz: 2be2017a419f82b1e67189ec8d7bac5ab5e23eda
4
- data.tar.gz: 2b15115fd9480db7a9dc83dfe8de2c73af690678
3
+ metadata.gz: 4e0f68c331f3af5023c4259e67dd20e1a54e5679
4
+ data.tar.gz: 2cf32700cc72c621f88e8d0fb94f76094c638021
5
5
  SHA512:
6
- metadata.gz: 8e7133ad725d439b8ca7c2848938eff5402af46c69bee188807a629c33e4674f7df146181c4f0d303f67e4b5249639c0b6a455d528dca12f644c7686b8278727
7
- data.tar.gz: dbd6739a159c4ab75bad5a3cf2b091dc39770040cae3863837817da707bbcf34a68263909c644d5ab27ec8a1e5d8cf74212ff09a1053664c16bfdea77abf03ad
6
+ metadata.gz: a164408606bd8da871a3e434b5896bad3aba26cb666df8e19a22ef0215b6ee1dc40c7f892083f55f87805f4b6940192e87c5dd9a3b8dff849c1f57700ac87fca
7
+ data.tar.gz: 12f2edc524f24d218b0ec301a1d59de22853c7a87c146156491b2c593547433573fea095507d05cc49c6c581f7588cd22bd5352e92cac2afeb3c68acc4a07523
data/.rubocop.yml ADDED
@@ -0,0 +1,42 @@
1
+ AllCops:
2
+ Include:
3
+ - Rakefile
4
+ - '**/Gemfile'
5
+ - config.ru
6
+ - lib/**/*.rake
7
+ Exclude:
8
+ - db/schema.rb
9
+ - spec/*
10
+ - test/*
11
+
12
+ Metrics/AbcSize:
13
+ Enabled: false
14
+
15
+ # Offense count: 1
16
+ # Configuration parameters: CountComments.
17
+ Metrics/ClassLength:
18
+ Enabled: false
19
+
20
+ # Offense count: 16
21
+ # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
22
+ # URISchemes: http, https
23
+ Metrics/LineLength:
24
+ Enabled: false
25
+
26
+ # Offense count: 4
27
+ # Configuration parameters: CountComments.
28
+ Metrics/MethodLength:
29
+ Enabled: false
30
+
31
+ # Offense count: 2
32
+ # Configuration parameters: EnforcedStyle, SupportedStyles.
33
+ # SupportedStyles: nested, compact
34
+ Style/ClassAndModuleChildren:
35
+ Enabled: false
36
+
37
+ # Offense count: 4
38
+ Style/Documentation:
39
+ Enabled: false
40
+
41
+ Style/FrozenStringLiteralComment:
42
+ Enabled: false
data/Gemfile CHANGED
@@ -3,4 +3,5 @@ source 'https://rubygems.org'
3
3
  # Specify your gem's dependencies in gridtable.gemspec
4
4
  gemspec
5
5
 
6
- gem 'activemodel', require: "active_model"
6
+ gem 'activemodel', require: 'active_model'
7
+ gem 'rubocop', require: false
data/Rakefile CHANGED
@@ -1,2 +1 @@
1
- require "bundler/gem_tasks"
2
-
1
+ require 'bundler/gem_tasks'
data/grid_table.gemspec CHANGED
@@ -1,23 +1,24 @@
1
1
  # coding: utf-8
2
+
2
3
  lib = File.expand_path('../lib', __FILE__)
3
4
  $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
5
  require 'grid_table/version'
5
6
 
6
7
  Gem::Specification.new do |spec|
7
- spec.name = "grid_table"
8
+ spec.name = 'grid_table'
8
9
  spec.version = GridTable::VERSION
9
- spec.authors = ["Jon Principe", "Mike Raimondi"]
10
- spec.email = ["jp@adharmonics.com"]
11
- spec.summary = %q{Utility for powerful HTML Tables}
12
- spec.description = %q{Library to help manage powerful HTML Tables through the Model, View and Controller.}
13
- spec.homepage = "http://github.com/jprincipe/grid_table"
14
- spec.license = "MIT"
10
+ spec.authors = ['Jon Principe', 'Mike Raimondi']
11
+ spec.email = ['jonathan.principe@gmail.com']
12
+ spec.summary = 'Utility for powerful HTML Tables'
13
+ spec.description = 'Library to help manage powerful HTML Tables through the Model, View and Controller.'
14
+ spec.homepage = 'http://github.com/jprincipe/grid_table'
15
+ spec.license = 'MIT'
15
16
 
16
17
  spec.files = `git ls-files -z`.split("\x0")
17
18
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
- spec.require_paths = ["lib"]
20
+ spec.require_paths = ['lib']
20
21
 
21
- spec.add_development_dependency "bundler", "~> 1.6"
22
- spec.add_development_dependency "rake"
22
+ spec.add_development_dependency 'bundler', '~> 1.6'
23
+ spec.add_development_dependency 'rake'
23
24
  end
@@ -15,10 +15,8 @@ class GridTable::Control
15
15
  # person = Person.new(name: 'bob', age: '18')
16
16
  # person.name # => "bob"
17
17
  # person.age # => "18"
18
- def initialize(params={})
19
- params.each do |attr, value|
20
- self.public_send("#{attr}=", value)
21
- end if params
18
+ def initialize(params = {})
19
+ params&.each { |attr, value| public_send("#{attr}=", value) }
22
20
 
23
21
  super()
24
22
  end
@@ -36,36 +34,32 @@ class GridTable::Control
36
34
  false
37
35
  end
38
36
 
39
- attr_writer :model, :attribute, :source, :source_class, :source_column, :filter, :polymorphic
37
+ attr_writer :model, :attribute, :source, :source_class, :source_column, :source_alias, :filter, :polymorphic
40
38
 
41
39
  def filter(param_filter_value, records)
42
- unless @filter == false
43
- arel_query = nil
44
- strategy_map = {
45
- exact_match: ->(col) { col.eq(param_filter_value) },
46
- prefix: ->(col) { col.matches("#{param_filter_value}%") },
47
- suffix: ->(col) { col.matches("%#{param_filter_value}") },
48
- fuzzy: ->(col) { col.matches("%#{param_filter_value}%") },
49
- array: ->(col) { "#{column.to_s} @> ARRAY[#{[param_filter_value].flatten.join(',')}]" },
50
- date_range: ->(col) { "#{source_table.name}.#{column.to_s} BETWEEN #{param_filter_value}" }
51
- }
52
-
53
- polymorphic_models.each_with_index do |klass, i|
54
- # TODO implement array filtering
55
- if i == 0
56
- arel_query = strategy_map[strategy].call(klass.arel_table[column])
57
- else
58
- arel_query = arel_query.or(strategy_map[strategy].call(klass.arel_table[column]))
59
- end
60
- end
61
-
62
- arel_query ||= strategy_map[strategy].call(source_table[column])
63
- prepared_records(records).where(arel_query)
40
+ return if @filter.nil?
41
+
42
+ arel_query = nil
43
+ strategy_map = {
44
+ exact_match: ->(col) { col.eq(param_filter_value) },
45
+ prefix: ->(col) { col.matches("#{param_filter_value}%") },
46
+ suffix: ->(col) { col.matches("%#{param_filter_value}") },
47
+ fuzzy: ->(col) { col.matches("%#{param_filter_value}%") },
48
+ array: ->(_col) { "#{column} @> ARRAY[#{[param_filter_value].flatten.join(',')}]" },
49
+ date_range: ->(_col) { "#{source_table.name}.#{column} BETWEEN #{param_filter_value}" }
50
+ }
51
+
52
+ polymorphic_models.each_with_index do |klass, i|
53
+ # TODO: implement array filtering
54
+ arel_query = i.zero? ? strategy_map[strategy].call(klass.arel_table[column]) : arel_query.or(strategy_map[strategy].call(klass.arel_table[column]))
64
55
  end
56
+
57
+ arel_query ||= strategy_map[strategy].call(source_table[column])
58
+ prepared_records(records).where(arel_query)
65
59
  end
66
60
 
67
61
  def sort(param_sort_order, records)
68
- sort_order = %w[asc, desc].include?(param_sort_order) ? param_sort_order : 'asc'
62
+ sort_order = %w[asc desc].include?(param_sort_order) ? param_sort_order : 'asc'
69
63
 
70
64
  if @polymorphic
71
65
  models = polymorphic_models
@@ -119,7 +113,7 @@ class GridTable::Control
119
113
  end
120
114
 
121
115
  def table_with_column
122
- "#{source_table.name}.#{column}"
116
+ @source_alias || "#{source_table.name}.#{column}"
123
117
  end
124
118
 
125
119
  def model_fk
@@ -132,7 +126,7 @@ class GridTable::Control
132
126
  models.each_with_index do |klass, i|
133
127
  if models.length == 1
134
128
  sql = "#{klass.table_name}.#{column}"
135
- elsif i == 0
129
+ elsif i.zero?
136
130
  sql << "(CASE WHEN #{klass.table_name}.#{column} IS NOT NULL THEN #{klass.table_name}.#{column}"
137
131
  elsif i == models.length - 1
138
132
  sql << " ELSE #{klass.table_name}.#{column} END)"
@@ -18,7 +18,7 @@ module GridTable
18
18
  end
19
19
  end
20
20
 
21
- def grid_table_export_for(resource, params, options = {})
21
+ def grid_table_export_for(resource, params)
22
22
  grid_table = resource.grid_table
23
23
  params[:skip_paging] ||= true
24
24
  grid_table.populate!(resource, params)
@@ -7,15 +7,15 @@ class GridTable::Table
7
7
 
8
8
  def add_control(model, attribute, options)
9
9
  @controls << GridTable::Control.new(
10
- {
11
- model: model.name.underscore.to_sym,
12
- attribute: attribute,
13
- source: options[:source],
14
- source_class: options[:source_class],
15
- source_column: options[:source_column],
16
- filter: options[:filter],
17
- polymorphic: options[:polymorphic]
18
- })
10
+ model: model.name.underscore.to_sym,
11
+ attribute: attribute,
12
+ source: options[:source],
13
+ source_class: options[:source_class],
14
+ source_column: options[:source_column],
15
+ source_alias: options[:source_alias],
16
+ filter: options[:filter],
17
+ polymorphic: options[:polymorphic]
18
+ )
19
19
  end
20
20
 
21
21
  def populate!(resource, params)
@@ -26,8 +26,8 @@ class GridTable::Table
26
26
  @records = resource
27
27
 
28
28
  filter! unless params[:skip_filtering]
29
+ @total_rows = @records.length
29
30
  sort! unless params[:skip_sorting]
30
- @total_rows = @records.size
31
31
  page! unless params[:skip_paging]
32
32
 
33
33
  @records
@@ -42,7 +42,7 @@ class GridTable::Table
42
42
  private
43
43
 
44
44
  def common_strong_params
45
- [:page, :page_size, :sort, :sort_order]
45
+ %w[page page_size sort sort_order]
46
46
  end
47
47
 
48
48
  def page
@@ -54,7 +54,8 @@ class GridTable::Table
54
54
  end
55
55
 
56
56
  def filter!
57
- @params.each do |attribute, attribute_value|
57
+ filter_params = @params.reject { |k| common_strong_params.include?(k) }
58
+ filter_params.each do |attribute, attribute_value|
58
59
  control = GridTable::Control.find_by_param(attribute, @controls)
59
60
  @records = control.filter(attribute_value, @records) if control.present?
60
61
  end
@@ -63,13 +64,10 @@ class GridTable::Table
63
64
  def sort!
64
65
  control = GridTable::Control.find_by_param(@params[:sort], @controls)
65
66
 
66
- if control.present?
67
- @records = control.sort(@params[:sort_order], records)
68
- end
67
+ @records = control.sort(@params[:sort_order], records) if control.present?
69
68
  end
70
69
 
71
70
  def page!
72
71
  @records = @records.offset(page * page_size).limit(page_size)
73
72
  end
74
-
75
73
  end
@@ -1,3 +1,3 @@
1
1
  module GridTable
2
- VERSION = "1.3"
2
+ VERSION = '1.4'.freeze
3
3
  end
data/lib/grid_table.rb CHANGED
@@ -1,8 +1,8 @@
1
- require "grid_table/version"
2
- require "grid_table/control"
3
- require "grid_table/table"
4
- require "grid_table/controller"
5
- require "grid_table/model"
1
+ require 'grid_table/version'
2
+ require 'grid_table/control'
3
+ require 'grid_table/table'
4
+ require 'grid_table/controller'
5
+ require 'grid_table/model'
6
6
 
7
7
  module GridTable
8
8
  class Engine < ::Rails::Engine
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grid_table
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.3'
4
+ version: '1.4'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Principe
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2017-06-19 00:00:00.000000000 Z
12
+ date: 2017-08-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -42,12 +42,13 @@ dependencies:
42
42
  description: Library to help manage powerful HTML Tables through the Model, View and
43
43
  Controller.
44
44
  email:
45
- - jp@adharmonics.com
45
+ - jonathan.principe@gmail.com
46
46
  executables: []
47
47
  extensions: []
48
48
  extra_rdoc_files: []
49
49
  files:
50
50
  - ".gitignore"
51
+ - ".rubocop.yml"
51
52
  - Gemfile
52
53
  - LICENSE.txt
53
54
  - README.md
@@ -80,7 +81,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
80
81
  version: '0'
81
82
  requirements: []
82
83
  rubyforge_project:
83
- rubygems_version: 2.5.2
84
+ rubygems_version: 2.6.11
84
85
  signing_key:
85
86
  specification_version: 4
86
87
  summary: Utility for powerful HTML Tables