action_table 0.2.0 → 0.3.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 +4 -4
- data/CHANGELOG.md +17 -0
- data/README.md +1 -1
- data/action_table.gemspec +2 -2
- data/lib/action_table.rb +3 -3
- data/lib/action_table/bootstrap_styles.rb +95 -0
- data/lib/action_table/helper.rb +5 -5
- data/lib/action_table/version.rb +1 -1
- data/lib/action_table/view.rb +12 -15
- data/lib/action_table/views/action_table/_table.html.erb +12 -2
- metadata +6 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 764eb83721f4fdcb2d2ba4d83b4e475193dfd942ba092ba66fa51d9b99500792
|
4
|
+
data.tar.gz: 28cf0551ac9a6db16b109c45e9fd480876112989480dc87d3b7a5e77604e8ccc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 2c54233fa2ff645ba5068bad3dec99e22f77459f400fc559e828c48c998a6d40529847cebcf110df09fbaae6055f20461edd4685ce6290ed6708528e5a8bfec2
|
7
|
+
data.tar.gz: 0b4595270ccfb5cbff0e08d51614eabc82658e0a949d02d174f2c785b1c6cc511bce64c9ec47f837993855abe8cd610782d09e62541ceef56784b3186546f7b5
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
# ActionTable - Change Log
|
2
|
+
|
3
|
+
## HEAD
|
4
|
+
|
5
|
+
|
6
|
+
## v0.3.0
|
7
|
+
|
8
|
+
- Validate styles params against known Bootstrap styles
|
9
|
+
- Add support for responsive tables
|
10
|
+
- Remove `View#styles_class` in favor of `View#styles` that returns an instance of `BootstrapStyles`
|
11
|
+
- Update default value of `Configuration#links` to be empty
|
12
|
+
- Change `View#cols` to `View#columns`
|
13
|
+
- Change `Configuration#link_method` to `Configuration#links`
|
14
|
+
|
15
|
+
---
|
16
|
+
|
17
|
+
_The rest is history.._
|
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# ActionTable
|
2
2
|
|
3
|
-
Render
|
3
|
+
Render ActiveRecord objects as HTML tables with one line of Ruby - supports some simple configuration options.
|
4
4
|
|
5
5
|
:warning: Only works with Ruby on Rails.
|
6
6
|
|
data/action_table.gemspec
CHANGED
@@ -10,8 +10,8 @@ Gem::Specification.new do |spec|
|
|
10
10
|
spec.authors = ['Jacob Burenstam']
|
11
11
|
spec.email = ['burenstam@gmail.com']
|
12
12
|
|
13
|
-
spec.summary = 'Render
|
14
|
-
spec.description = 'Render
|
13
|
+
spec.summary = 'Render ActiveRecord objects as HTML tables with one line of Ruby.'
|
14
|
+
spec.description = 'Render ActiveRecord objects as HTML tables with one line of Ruby - supports some simple configuration options.'
|
15
15
|
spec.homepage = 'https://github.com/buren/action_table'
|
16
16
|
spec.license = 'MIT'
|
17
17
|
|
data/lib/action_table.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'action_table/version'
|
4
|
-
require 'action_table/railtie'
|
4
|
+
require 'action_table/railtie' if defined?(Rails)
|
5
5
|
|
6
6
|
module ActionTable
|
7
7
|
def self.configuration
|
@@ -18,14 +18,14 @@ module ActionTable
|
|
18
18
|
end
|
19
19
|
|
20
20
|
class Configuration
|
21
|
-
attr_accessor :styles, :actions, :
|
21
|
+
attr_accessor :styles, :actions, :links
|
22
22
|
attr_writer :rails_host_app
|
23
23
|
|
24
24
|
def initialize
|
25
25
|
@rails_host_app = nil
|
26
26
|
@styles = []
|
27
27
|
@actions = []
|
28
|
-
@
|
28
|
+
@links = []
|
29
29
|
end
|
30
30
|
|
31
31
|
def rails_host_app
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActionTable
|
4
|
+
class BootstrapStyles
|
5
|
+
BOOTSTRAP_TABLE_WRAPERS = Set.new(
|
6
|
+
%w[
|
7
|
+
responsive
|
8
|
+
responsive-sm
|
9
|
+
responsive-md
|
10
|
+
responsive-lg
|
11
|
+
responsive-xl
|
12
|
+
],
|
13
|
+
).freeze
|
14
|
+
|
15
|
+
# All the below can be used on <table>, <thead>, <tr>, <td>
|
16
|
+
BOOTSTRAP_UTILS = Set.new(
|
17
|
+
%w[
|
18
|
+
dark
|
19
|
+
light
|
20
|
+
primary
|
21
|
+
success
|
22
|
+
danger
|
23
|
+
info
|
24
|
+
warning
|
25
|
+
active
|
26
|
+
secondary
|
27
|
+
],
|
28
|
+
)
|
29
|
+
|
30
|
+
# .thead-light or .thead-dark
|
31
|
+
# <th scope="row">1</th>
|
32
|
+
# <caption>List of users</caption> # directly under <table>
|
33
|
+
BOOTSTRAP_TABLE_STYLES = (
|
34
|
+
Set.new(
|
35
|
+
%w[
|
36
|
+
striped
|
37
|
+
sm
|
38
|
+
bordered
|
39
|
+
hover
|
40
|
+
borderless
|
41
|
+
],
|
42
|
+
) + BOOTSTRAP_UTILS
|
43
|
+
).freeze
|
44
|
+
|
45
|
+
def initialize(styles)
|
46
|
+
@styles = Array(styles).map { |style| style.to_s.tr('_', '-') }
|
47
|
+
@responsive_wrapper_class = nil
|
48
|
+
@table_classes = nil
|
49
|
+
|
50
|
+
validate_styles!(@styles)
|
51
|
+
end
|
52
|
+
|
53
|
+
def responsive_wrapper_class
|
54
|
+
return @responsive_wrapper_class if @responsive_wrapper_class
|
55
|
+
|
56
|
+
wrappers = BOOTSTRAP_TABLE_WRAPERS.select { |style| @styles.include?(style) }
|
57
|
+
return if wrappers.empty?
|
58
|
+
|
59
|
+
@responsive_wrapper_class = wrappers.map do |style|
|
60
|
+
"table-#{style}" if wrapper_class?(style)
|
61
|
+
end.join(' ')
|
62
|
+
end
|
63
|
+
|
64
|
+
def table_classes
|
65
|
+
return @table_classes if @table_classes
|
66
|
+
|
67
|
+
style_classes = @styles.map do |style|
|
68
|
+
"table-#{style}" if table_class?(style)
|
69
|
+
end.compact
|
70
|
+
|
71
|
+
@table_classes = "table #{style_classes.join(' ')}"
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
def table_class?(style)
|
77
|
+
BOOTSTRAP_TABLE_STYLES.include?(style)
|
78
|
+
end
|
79
|
+
|
80
|
+
def wrapper_class?(style)
|
81
|
+
BOOTSTRAP_TABLE_WRAPERS.include?(style)
|
82
|
+
end
|
83
|
+
|
84
|
+
def validate_styles!(styles)
|
85
|
+
styles.each do |style|
|
86
|
+
next if table_class?(style) || wrapper_class?(style)
|
87
|
+
|
88
|
+
raise(
|
89
|
+
ArgumentError,
|
90
|
+
"unknown bootstrap style #{style}, must be in #{BOOTSTRAP_TABLE_STYLES}",
|
91
|
+
)
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
data/lib/action_table/helper.rb
CHANGED
@@ -13,8 +13,8 @@ module ActionTable
|
|
13
13
|
# table columns (must map to methods on each ActiveRecord instance)
|
14
14
|
# @param styles [Array<String>, Array<Symbol>]
|
15
15
|
# no, one or many of bootstrap table styles (table- prefix will be added)
|
16
|
-
# @param link [Symbol]
|
17
|
-
#
|
16
|
+
# @param link [String, Symbol, Array<String>, Array<Symbol>]
|
17
|
+
# field(s) that will link to the resource
|
18
18
|
# @param paginate [true, false]
|
19
19
|
# whether to render pagination links (default: false)
|
20
20
|
# @param actions [Array<String>, Array<Symbol>]
|
@@ -23,15 +23,15 @@ module ActionTable
|
|
23
23
|
records,
|
24
24
|
fields,
|
25
25
|
paginate: false,
|
26
|
-
|
26
|
+
links: ActionTable.config.links,
|
27
27
|
actions: ActionTable.config.actions,
|
28
28
|
styles: ActionTable.config.styles
|
29
29
|
)
|
30
30
|
action_table = View.new(
|
31
|
-
|
31
|
+
columns: fields,
|
32
32
|
records: records,
|
33
33
|
paginate: paginate,
|
34
|
-
|
34
|
+
links: links,
|
35
35
|
actions: actions,
|
36
36
|
styles: styles,
|
37
37
|
)
|
data/lib/action_table/version.rb
CHANGED
data/lib/action_table/view.rb
CHANGED
@@ -1,34 +1,35 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'set'
|
4
|
+
require 'action_table/bootstrap_styles'
|
4
5
|
|
5
6
|
module ActionTable
|
6
7
|
class View
|
7
8
|
include ActionView::Helpers::UrlHelper
|
8
9
|
include ActionTable.config.rails_host_app.routes.url_helpers
|
9
10
|
|
10
|
-
attr_reader :model_name, :rows
|
11
|
+
attr_reader :model_name, :rows, :styles
|
11
12
|
|
12
13
|
def initialize(
|
13
|
-
|
14
|
+
columns:,
|
14
15
|
records:,
|
15
16
|
paginate: false,
|
16
|
-
|
17
|
+
links: ActionTable.config.links,
|
17
18
|
actions: ActionTable.config.actions,
|
18
19
|
styles: ActionTable.config.styles
|
19
20
|
)
|
20
|
-
@
|
21
|
+
@columns = columns.map(&:to_s)
|
21
22
|
@rows = records
|
22
23
|
@table_name = records.table_name
|
23
24
|
@model_name = @table_name.singularize
|
24
25
|
@paginate = paginate
|
25
|
-
@
|
26
|
+
@links = Set.new(Array(links).map(&:to_s)).reject(&:empty?)
|
26
27
|
@actions = Array(actions).map(&:to_s)
|
27
|
-
@styles =
|
28
|
+
@styles = BootstrapStyles.new(styles)
|
28
29
|
end
|
29
30
|
|
30
31
|
def headers
|
31
|
-
@headers ||= @
|
32
|
+
@headers ||= @columns.map { |name| t_col(name) }
|
32
33
|
end
|
33
34
|
|
34
35
|
# add header column padding for actions
|
@@ -36,17 +37,17 @@ module ActionTable
|
|
36
37
|
@actions_header ||= [''] * @actions.length
|
37
38
|
end
|
38
39
|
|
39
|
-
def
|
40
|
-
attribute_columns = @
|
40
|
+
def columns(record)
|
41
|
+
attribute_columns = @columns.map do |name|
|
41
42
|
title = record.public_send(name)
|
42
|
-
if title.present? && @
|
43
|
+
if title.present? && @links.include?(name)
|
43
44
|
link_to(title, record_path(record))
|
44
45
|
else
|
45
46
|
title
|
46
47
|
end
|
47
48
|
end
|
48
49
|
|
49
|
-
actions = t_actions.zip(@actions).map do |data|
|
50
|
+
actions = t_actions.zip(@actions).map! do |data|
|
50
51
|
title, name = data
|
51
52
|
link_to(title, record_path(record, action: name))
|
52
53
|
end
|
@@ -54,10 +55,6 @@ module ActionTable
|
|
54
55
|
attribute_columns + actions
|
55
56
|
end
|
56
57
|
|
57
|
-
def styles_class
|
58
|
-
@styles.map { |style| "table-#{style}" }.join(' ')
|
59
|
-
end
|
60
|
-
|
61
58
|
def paginate?
|
62
59
|
@paginate
|
63
60
|
end
|
@@ -1,4 +1,10 @@
|
|
1
|
-
|
1
|
+
<% wrapper_class = table.styles.responsive_wrapper_class %>
|
2
|
+
|
3
|
+
<% if wrapper_class %>
|
4
|
+
<div class="<%= wrapper_class %>">
|
5
|
+
<% end %>
|
6
|
+
|
7
|
+
<table class="<%= table.styles.table_classes %>">
|
2
8
|
<thead>
|
3
9
|
<% table.headers.each do |header| %>
|
4
10
|
<th><%= header %></th>
|
@@ -10,7 +16,7 @@
|
|
10
16
|
<tbody>
|
11
17
|
<% table.rows.each do |row| %>
|
12
18
|
<tr>
|
13
|
-
<% table.
|
19
|
+
<% table.columns(row).each do |col| %>
|
14
20
|
<td><%= col %></td>
|
15
21
|
<% end %>
|
16
22
|
</tr>
|
@@ -18,6 +24,10 @@
|
|
18
24
|
</tbody>
|
19
25
|
</table>
|
20
26
|
|
27
|
+
<% if wrapper_class %>
|
28
|
+
</div>
|
29
|
+
<% end %>
|
30
|
+
|
21
31
|
<% if table.paginate? %>
|
22
32
|
<%= paginate table.rows, param_name: table.paginate_param_name %>
|
23
33
|
<% end %>
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: action_table
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jacob Burenstam
|
@@ -66,8 +66,8 @@ dependencies:
|
|
66
66
|
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: '3.0'
|
69
|
-
description: Render
|
70
|
-
|
69
|
+
description: Render ActiveRecord objects as HTML tables with one line of Ruby - supports
|
70
|
+
some simple configuration options.
|
71
71
|
email:
|
72
72
|
- burenstam@gmail.com
|
73
73
|
executables: []
|
@@ -79,6 +79,7 @@ files:
|
|
79
79
|
- ".rubocop.yml"
|
80
80
|
- ".ruby-style-guide.yml"
|
81
81
|
- ".travis.yml"
|
82
|
+
- CHANGELOG.md
|
82
83
|
- Gemfile
|
83
84
|
- LICENSE
|
84
85
|
- README.md
|
@@ -87,6 +88,7 @@ files:
|
|
87
88
|
- bin/console
|
88
89
|
- bin/setup
|
89
90
|
- lib/action_table.rb
|
91
|
+
- lib/action_table/bootstrap_styles.rb
|
90
92
|
- lib/action_table/helper.rb
|
91
93
|
- lib/action_table/railtie.rb
|
92
94
|
- lib/action_table/version.rb
|
@@ -115,6 +117,5 @@ rubyforge_project:
|
|
115
117
|
rubygems_version: 2.7.6
|
116
118
|
signing_key:
|
117
119
|
specification_version: 4
|
118
|
-
summary: Render
|
119
|
-
Ruby.
|
120
|
+
summary: Render ActiveRecord objects as HTML tables with one line of Ruby.
|
120
121
|
test_files: []
|