action_table 0.1.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 +7 -0
- data/.gitignore +14 -0
- data/.rspec +3 -0
- data/.rubocop.yml +7 -0
- data/.ruby-style-guide.yml +265 -0
- data/.travis.yml +5 -0
- data/Gemfile +8 -0
- data/LICENSE +21 -0
- data/README.md +56 -0
- data/Rakefile +8 -0
- data/action_table.gemspec +29 -0
- data/bin/console +15 -0
- data/bin/setup +8 -0
- data/lib/action_table/helper.rb +28 -0
- data/lib/action_table/railtie.rb +20 -0
- data/lib/action_table/version.rb +5 -0
- data/lib/action_table/view.rb +95 -0
- data/lib/action_table/views/action_table/_table.html.erb +23 -0
- data/lib/action_table.rb +33 -0
- metadata +120 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: cf3b17d46afaa1ab042b61676da77b28c45cd47310a2a732244d1c5cc87046a6
|
4
|
+
data.tar.gz: 14d761c446ee13aa507fa7482db9a0b9ceb9d01249dbee256a0d3bcd642f9434
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 827a3e9853fb70eb2cf5c5a95cd7df169ef74c765aed64a9e7066bfe80b29a66c0a1b3313d6cb768e8d6ac968731eb3ec208fd93435fd92a16ddf7a4bed0994b
|
7
|
+
data.tar.gz: b3092add50499a80c5e033ab206094d42cea7371cb78eafaf85e386fb422931393ca8439ca426a21d7283c0cc5ca0ceef078b95f2c7018479d5dfac8b6bb3518
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.rubocop.yml
ADDED
@@ -0,0 +1,265 @@
|
|
1
|
+
Rails:
|
2
|
+
Enabled: false
|
3
|
+
AllCops:
|
4
|
+
TargetRubyVersion: 2.3
|
5
|
+
Exclude:
|
6
|
+
- "vendor/**/*"
|
7
|
+
UseCache: true
|
8
|
+
Style/CollectionMethods:
|
9
|
+
Description: Preferred collection methods.
|
10
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#map-find-select-reduce-size
|
11
|
+
Enabled: true
|
12
|
+
PreferredMethods:
|
13
|
+
collect: map
|
14
|
+
collect!: map!
|
15
|
+
find: detect
|
16
|
+
find_all: select
|
17
|
+
reduce: inject
|
18
|
+
Style/RedundantFreeze:
|
19
|
+
Description: "Checks usages of Object#freeze on immutable objects."
|
20
|
+
Enabled: false
|
21
|
+
Layout/DotPosition:
|
22
|
+
Description: Checks the position of the dot in multi-line method calls.
|
23
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-multi-line-chains
|
24
|
+
Enabled: true
|
25
|
+
EnforcedStyle: trailing
|
26
|
+
SupportedStyles:
|
27
|
+
- leading
|
28
|
+
- trailing
|
29
|
+
Naming/FileName:
|
30
|
+
Description: Use snake_case for source file names.
|
31
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#snake-case-files
|
32
|
+
Enabled: false
|
33
|
+
Exclude: []
|
34
|
+
Naming/MemoizedInstanceVariableName:
|
35
|
+
Description: Memoized method name should match memo instance variable name.
|
36
|
+
Enabled: false
|
37
|
+
Naming/UncommunicativeMethodParamName:
|
38
|
+
Description: >-
|
39
|
+
Checks for method parameter names that contain capital letters,
|
40
|
+
end in numbers, or do not meet a minimal length.
|
41
|
+
Enabled: false
|
42
|
+
Style/GuardClause:
|
43
|
+
Description: Check for conditionals that can be replaced with guard clauses
|
44
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-nested-conditionals
|
45
|
+
Enabled: true
|
46
|
+
MinBodyLength: 3
|
47
|
+
Style/IfUnlessModifier:
|
48
|
+
Description: Favor modifier if/unless usage when you have a single-line body.
|
49
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#if-as-a-modifier
|
50
|
+
Enabled: false
|
51
|
+
Style/OptionHash:
|
52
|
+
Description: Don't use option hashes when you can use keyword arguments.
|
53
|
+
Enabled: false
|
54
|
+
Style/PercentLiteralDelimiters:
|
55
|
+
Description: Use `%`-literal delimiters consistently
|
56
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#percent-literal-braces
|
57
|
+
Enabled: false
|
58
|
+
PreferredDelimiters:
|
59
|
+
"%": "()"
|
60
|
+
"%i": "()"
|
61
|
+
"%q": "()"
|
62
|
+
"%Q": "()"
|
63
|
+
"%r": "{}"
|
64
|
+
"%s": "()"
|
65
|
+
"%w": "()"
|
66
|
+
"%W": "()"
|
67
|
+
"%x": "()"
|
68
|
+
Naming/PredicateName:
|
69
|
+
Description: Check the names of predicate methods.
|
70
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#bool-methods-qmark
|
71
|
+
Enabled: true
|
72
|
+
NamePrefix:
|
73
|
+
- is_
|
74
|
+
- has_
|
75
|
+
- have_
|
76
|
+
NamePrefixBlacklist:
|
77
|
+
- is_
|
78
|
+
Exclude:
|
79
|
+
- spec/**/*
|
80
|
+
Style/RaiseArgs:
|
81
|
+
Description: Checks the arguments passed to raise/fail.
|
82
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#exception-class-messages
|
83
|
+
Enabled: false
|
84
|
+
EnforcedStyle: exploded
|
85
|
+
SupportedStyles:
|
86
|
+
- compact
|
87
|
+
- exploded
|
88
|
+
Style/SignalException:
|
89
|
+
Description: Checks for proper usage of fail and raise.
|
90
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#fail-method
|
91
|
+
Enabled: false
|
92
|
+
EnforcedStyle: semantic
|
93
|
+
SupportedStyles:
|
94
|
+
- only_raise
|
95
|
+
- only_fail
|
96
|
+
- semantic
|
97
|
+
Style/SingleLineBlockParams:
|
98
|
+
Description: Enforces the names of some block params.
|
99
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#reduce-blocks
|
100
|
+
Enabled: false
|
101
|
+
Methods:
|
102
|
+
- reduce:
|
103
|
+
- a
|
104
|
+
- e
|
105
|
+
- inject:
|
106
|
+
- a
|
107
|
+
- e
|
108
|
+
Style/TrivialAccessors:
|
109
|
+
Enabled: false
|
110
|
+
Style/SingleLineMethods:
|
111
|
+
Description: Avoid single-line methods.
|
112
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-single-line-methods
|
113
|
+
Enabled: false
|
114
|
+
AllowIfMethodIsEmpty: true
|
115
|
+
Style/StringLiterals:
|
116
|
+
Description: Checks if uses of quotes match the configured preference.
|
117
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#consistent-string-literals
|
118
|
+
Enabled: true
|
119
|
+
EnforcedStyle: single_quotes
|
120
|
+
SupportedStyles:
|
121
|
+
- single_quotes
|
122
|
+
- double_quotes
|
123
|
+
Style/MixinUsage:
|
124
|
+
Enabled: true
|
125
|
+
Exclude:
|
126
|
+
- exe/*
|
127
|
+
Style/StringLiteralsInInterpolation:
|
128
|
+
Description: Checks if uses of quotes inside expressions in interpolated strings
|
129
|
+
match the configured preference.
|
130
|
+
Enabled: true
|
131
|
+
EnforcedStyle: single_quotes
|
132
|
+
SupportedStyles:
|
133
|
+
- single_quotes
|
134
|
+
- double_quotes
|
135
|
+
Style/TrailingCommaInArguments:
|
136
|
+
Enabled: true
|
137
|
+
EnforcedStyleForMultiline: comma
|
138
|
+
Style/TrailingCommaInArrayLiteral:
|
139
|
+
Description: Checks for trailing comma in parameter lists and literals.
|
140
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas
|
141
|
+
Enabled: true
|
142
|
+
EnforcedStyleForMultiline: comma
|
143
|
+
Style/TrailingCommaInHashLiteral:
|
144
|
+
Description: Checks for trailing comma in parameter lists and literals.
|
145
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-trailing-array-commas
|
146
|
+
Enabled: true
|
147
|
+
EnforcedStyleForMultiline: comma
|
148
|
+
Metrics/AbcSize:
|
149
|
+
Description: A calculated magnitude based on number of assignments, branches, and
|
150
|
+
conditions.
|
151
|
+
Enabled: false
|
152
|
+
Max: 15
|
153
|
+
Metrics/ClassLength:
|
154
|
+
Description: Avoid classes longer than 100 lines of code.
|
155
|
+
Enabled: false
|
156
|
+
CountComments: false
|
157
|
+
Max: 100
|
158
|
+
Metrics/ModuleLength:
|
159
|
+
CountComments: false
|
160
|
+
Max: 100
|
161
|
+
Description: Avoid modules longer than 100 lines of code.
|
162
|
+
Enabled: false
|
163
|
+
Metrics/CyclomaticComplexity:
|
164
|
+
Description: A complexity metric that is strongly correlated to the number of test
|
165
|
+
cases needed to validate a method.
|
166
|
+
Enabled: false
|
167
|
+
Max: 6
|
168
|
+
Metrics/MethodLength:
|
169
|
+
Description: Avoid methods longer than 10 lines of code.
|
170
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#short-methods
|
171
|
+
Enabled: false
|
172
|
+
CountComments: false
|
173
|
+
Max: 10
|
174
|
+
Metrics/ParameterLists:
|
175
|
+
Description: Avoid parameter lists longer than three or four parameters.
|
176
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#too-many-params
|
177
|
+
Enabled: false
|
178
|
+
Max: 5
|
179
|
+
CountKeywordArgs: true
|
180
|
+
Metrics/PerceivedComplexity:
|
181
|
+
Description: A complexity metric geared towards measuring complexity for a human
|
182
|
+
reader.
|
183
|
+
Enabled: false
|
184
|
+
Max: 7
|
185
|
+
Metrics/LineLength:
|
186
|
+
Description: Maximum line length
|
187
|
+
Enabled: true
|
188
|
+
Max: 95
|
189
|
+
Exclude:
|
190
|
+
- Gemfile
|
191
|
+
- action_table.gemspec
|
192
|
+
- spec/**/*
|
193
|
+
Metrics/BlockLength:
|
194
|
+
Enabled: true
|
195
|
+
Exclude:
|
196
|
+
- spec/**/*
|
197
|
+
Lint/AssignmentInCondition:
|
198
|
+
Description: Don't use assignment in conditions.
|
199
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#safe-assignment-in-condition
|
200
|
+
Enabled: false
|
201
|
+
AllowSafeAssignment: true
|
202
|
+
Style/InlineComment:
|
203
|
+
Description: Avoid inline comments.
|
204
|
+
Enabled: false
|
205
|
+
Naming/AccessorMethodName:
|
206
|
+
Description: Check the naming of accessor methods for get_/set_.
|
207
|
+
Enabled: false
|
208
|
+
Style/Alias:
|
209
|
+
Description: Use alias_method instead of alias.
|
210
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#alias-method
|
211
|
+
Enabled: false
|
212
|
+
Style/Documentation:
|
213
|
+
Description: Document classes and non-namespace modules.
|
214
|
+
Enabled: false
|
215
|
+
Style/DoubleNegation:
|
216
|
+
Description: Checks for uses of double negation (!!).
|
217
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-bang-bang
|
218
|
+
Enabled: false
|
219
|
+
Style/EachWithObject:
|
220
|
+
Description: Prefer `each_with_object` over `inject` or `reduce`.
|
221
|
+
Enabled: false
|
222
|
+
Style/EmptyLiteral:
|
223
|
+
Description: Prefer literals to Array.new/Hash.new/String.new.
|
224
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#literal-array-hash
|
225
|
+
Enabled: false
|
226
|
+
Style/ModuleFunction:
|
227
|
+
Description: Checks for usage of `extend self` in modules.
|
228
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#module-function
|
229
|
+
Enabled: false
|
230
|
+
Style/OneLineConditional:
|
231
|
+
Description: Favor the ternary operator(?:) over if/then/else/end constructs.
|
232
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#ternary-operator
|
233
|
+
Enabled: false
|
234
|
+
Style/PerlBackrefs:
|
235
|
+
Description: Avoid Perl-style regex back references.
|
236
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-perl-regexp-last-matchers
|
237
|
+
Enabled: false
|
238
|
+
Style/Send:
|
239
|
+
Description: Prefer `Object#__send__` or `Object#public_send` to `send`, as `send`
|
240
|
+
may overlap with existing methods.
|
241
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#prefer-public-send
|
242
|
+
Enabled: false
|
243
|
+
Style/SpecialGlobalVars:
|
244
|
+
Description: Avoid Perl-style global variables.
|
245
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#no-cryptic-perlisms
|
246
|
+
Enabled: false
|
247
|
+
Style/VariableInterpolation:
|
248
|
+
Description: Don't interpolate global, instance and class variables directly in
|
249
|
+
strings.
|
250
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#curlies-interpolate
|
251
|
+
Enabled: false
|
252
|
+
Style/WhenThen:
|
253
|
+
Description: Use when x then ... for one-line cases.
|
254
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#one-line-cases
|
255
|
+
Enabled: false
|
256
|
+
Lint/EachWithObjectArgument:
|
257
|
+
Description: Check for immutable argument given to each_with_object.
|
258
|
+
Enabled: true
|
259
|
+
Lint/HandleExceptions:
|
260
|
+
Description: Don't suppress exception.
|
261
|
+
StyleGuide: https://github.com/bbatsov/ruby-style-guide#dont-hide-exceptions
|
262
|
+
Enabled: false
|
263
|
+
Lint/LiteralInInterpolation:
|
264
|
+
Description: Checks for literals used in interpolation.
|
265
|
+
Enabled: false
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Jacob Burenstam Linder
|
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,56 @@
|
|
1
|
+
# ActionTable
|
2
|
+
|
3
|
+
Render ActiveModel or ActiveRecord objects as HTML tables with one line of Ruby - supports some simple configuration options.
|
4
|
+
|
5
|
+
:warning: Only works with Ruby on Rails.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'action_table'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
```
|
17
|
+
$ bundle
|
18
|
+
```
|
19
|
+
|
20
|
+
Or install it yourself as:
|
21
|
+
```
|
22
|
+
$ gem install action_table
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
Generate table with `name` and `created_at` columns.
|
28
|
+
```
|
29
|
+
<%= action_table User.all, %i[name created_at] %>
|
30
|
+
```
|
31
|
+
|
32
|
+
Add bootstrap classes to table
|
33
|
+
```
|
34
|
+
<%= action_table User.all, %i[name created_at], styles: %i[hover striped] %>
|
35
|
+
```
|
36
|
+
|
37
|
+
Configure the default styles
|
38
|
+
```ruby
|
39
|
+
ActionTable.configure do |config|
|
40
|
+
config.table_styles = %i[striped bordered]
|
41
|
+
end
|
42
|
+
```
|
43
|
+
|
44
|
+
## Development
|
45
|
+
|
46
|
+
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.
|
47
|
+
|
48
|
+
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).
|
49
|
+
|
50
|
+
## Contributing
|
51
|
+
|
52
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/buren/action_table.
|
53
|
+
|
54
|
+
## License
|
55
|
+
|
56
|
+
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,29 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('lib', __dir__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'action_table/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'action_table'
|
9
|
+
spec.version = ActionTable::VERSION
|
10
|
+
spec.authors = ['Jacob Burenstam']
|
11
|
+
spec.email = ['burenstam@gmail.com']
|
12
|
+
|
13
|
+
spec.summary = 'Render ActiveModel or ActiveRecord objects as HTML tables with one line of Ruby.'
|
14
|
+
spec.description = 'Render ActiveModel or ActiveRecord objects as HTML tables with one line of Ruby - supports some simple configuration options.'
|
15
|
+
spec.homepage = 'https://github.com/buren/action_table'
|
16
|
+
spec.license = 'MIT'
|
17
|
+
|
18
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
19
|
+
f.match(%r{^(test|spec|features)/})
|
20
|
+
end
|
21
|
+
spec.bindir = 'exe'
|
22
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
23
|
+
spec.require_paths = ['lib']
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.16'
|
26
|
+
spec.add_development_dependency 'byebug'
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
29
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
require 'bundler/setup'
|
5
|
+
require 'action_table'
|
6
|
+
|
7
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
8
|
+
# with your gem easier. You can also use a different console, if you like.
|
9
|
+
|
10
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
11
|
+
# require "pry"
|
12
|
+
# Pry.start
|
13
|
+
|
14
|
+
require 'irb'
|
15
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'erb'
|
4
|
+
require 'action_table/view'
|
5
|
+
|
6
|
+
module ActionTable
|
7
|
+
module Helper
|
8
|
+
def action_table(
|
9
|
+
records,
|
10
|
+
fields,
|
11
|
+
styles: %i[bordered striped hover],
|
12
|
+
link: :name,
|
13
|
+
paginate: false,
|
14
|
+
actions: []
|
15
|
+
)
|
16
|
+
action_table = View.new(
|
17
|
+
cols: fields,
|
18
|
+
records: records,
|
19
|
+
paginate: paginate,
|
20
|
+
link: link,
|
21
|
+
actions: actions,
|
22
|
+
styles: styles,
|
23
|
+
)
|
24
|
+
|
25
|
+
render('action_table/table', table: action_table)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module ActionTable
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
initializer 'action_table.add_helper' do |app|
|
6
|
+
ActionTable.configure do |config|
|
7
|
+
config.rails_host_app = app
|
8
|
+
end
|
9
|
+
|
10
|
+
ActiveSupport.on_load(:action_view) do
|
11
|
+
require 'action_table/helper'
|
12
|
+
include ActionTable::Helper
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
initializer 'action_table.add_views' do |_app|
|
17
|
+
ActionController::Base.prepend_view_path "#{__dir__}/views"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'set'
|
4
|
+
|
5
|
+
module ActionTable
|
6
|
+
class View
|
7
|
+
include ActionView::Helpers::UrlHelper
|
8
|
+
include ActionTable.config.rails_host_app.routes.url_helpers
|
9
|
+
|
10
|
+
attr_reader :model_name, :rows
|
11
|
+
|
12
|
+
def initialize(
|
13
|
+
cols:,
|
14
|
+
records:,
|
15
|
+
paginate: false,
|
16
|
+
link: nil,
|
17
|
+
actions: [],
|
18
|
+
styles: ActionTable.config.table_styles
|
19
|
+
)
|
20
|
+
@col_names = cols.map(&:to_s)
|
21
|
+
@rows = records
|
22
|
+
@table_name = records.table_name
|
23
|
+
@model_name = @table_name.singularize
|
24
|
+
@paginate = paginate
|
25
|
+
@link = Set.new(Array(link.to_s)).reject(&:empty?)
|
26
|
+
@actions = Array(actions).map(&:to_s)
|
27
|
+
@styles = Array(styles)
|
28
|
+
end
|
29
|
+
|
30
|
+
def headers
|
31
|
+
@headers ||= @col_names.map { |name| t_col(name) }
|
32
|
+
end
|
33
|
+
|
34
|
+
# add header column padding for actions
|
35
|
+
def actions_header
|
36
|
+
@actions_header ||= [''] * @actions.length
|
37
|
+
end
|
38
|
+
|
39
|
+
def cols(record)
|
40
|
+
attribute_columns = @col_names.map do |name|
|
41
|
+
title = record.public_send(name)
|
42
|
+
if title.present? && @link.include?(name)
|
43
|
+
link_to(title, record_path(record))
|
44
|
+
else
|
45
|
+
title
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
actions = t_actions.zip(@actions).map do |data|
|
50
|
+
title, name = data
|
51
|
+
link_to(title, record_path(record, action: name))
|
52
|
+
end
|
53
|
+
|
54
|
+
attribute_columns + actions
|
55
|
+
end
|
56
|
+
|
57
|
+
def styles_class
|
58
|
+
@styles.map { |style| "table-#{style}" }.join(' ')
|
59
|
+
end
|
60
|
+
|
61
|
+
def paginate?
|
62
|
+
@paginate
|
63
|
+
end
|
64
|
+
|
65
|
+
def paginate_param_name
|
66
|
+
"#{model_name}_page"
|
67
|
+
end
|
68
|
+
|
69
|
+
# This must be defined for record_path to work
|
70
|
+
def controller
|
71
|
+
@table_name
|
72
|
+
end
|
73
|
+
|
74
|
+
# Contstruct path to record, i.e programs_path(record)
|
75
|
+
def record_path(record, action: nil)
|
76
|
+
action = nil if action.to_s == 'show'
|
77
|
+
path = [action, model_name, 'path'].compact.join('_')
|
78
|
+
public_send(path, record)
|
79
|
+
end
|
80
|
+
|
81
|
+
def t_actions
|
82
|
+
@actions.map { |name| t_action(name) }
|
83
|
+
end
|
84
|
+
|
85
|
+
def t_action(name)
|
86
|
+
name = name.to_s
|
87
|
+
I18n.t("actions.#{name}", default: name.titleize)
|
88
|
+
end
|
89
|
+
|
90
|
+
def t_col(col_name)
|
91
|
+
t_key = "activerecord.attributes.#{@model_name}.#{col_name}"
|
92
|
+
I18n.t(t_key, default: col_name.titleize)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
<table class="table <%= table.styles_class %>">
|
2
|
+
<thead>
|
3
|
+
<% table.headers.each do |header| %>
|
4
|
+
<th><%= header %></th>
|
5
|
+
<% end %>
|
6
|
+
<% if (header_length = table.actions_header.length) > 0 %>
|
7
|
+
<th colspan="<%= header_length %>"></th>
|
8
|
+
<% end %>
|
9
|
+
</thead>
|
10
|
+
<tbody>
|
11
|
+
<% table.rows.each do |row| %>
|
12
|
+
<tr>
|
13
|
+
<% table.cols(row).each do |col| %>
|
14
|
+
<td><%= col %></td>
|
15
|
+
<% end %>
|
16
|
+
</tr>
|
17
|
+
<% end %>
|
18
|
+
</tbody>
|
19
|
+
</table>
|
20
|
+
|
21
|
+
<% if table.paginate? %>
|
22
|
+
<%= paginate table.rows, param_name: table.paginate_param_name %>
|
23
|
+
<% end %>
|
data/lib/action_table.rb
ADDED
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'action_table/version'
|
4
|
+
require 'action_table/railtie'
|
5
|
+
|
6
|
+
module ActionTable
|
7
|
+
def self.configuration
|
8
|
+
@configuration ||= Configuration.new
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.config
|
12
|
+
configuration
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.configure
|
16
|
+
yield(configuration) if block_given?
|
17
|
+
configuration
|
18
|
+
end
|
19
|
+
|
20
|
+
class Configuration
|
21
|
+
attr_accessor :table_styles
|
22
|
+
attr_writer :rails_host_app
|
23
|
+
|
24
|
+
def initialize
|
25
|
+
@rails_host_app = nil
|
26
|
+
@table_styles = []
|
27
|
+
end
|
28
|
+
|
29
|
+
def rails_host_app
|
30
|
+
@rails_host_app || raise('rails host app must be configured!')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
metadata
ADDED
@@ -0,0 +1,120 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: action_table
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jacob Burenstam
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-08-03 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.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: byebug
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rake
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '10.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '10.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.0'
|
69
|
+
description: Render ActiveModel or ActiveRecord objects as HTML tables with one line
|
70
|
+
of Ruby - supports some simple configuration options.
|
71
|
+
email:
|
72
|
+
- burenstam@gmail.com
|
73
|
+
executables: []
|
74
|
+
extensions: []
|
75
|
+
extra_rdoc_files: []
|
76
|
+
files:
|
77
|
+
- ".gitignore"
|
78
|
+
- ".rspec"
|
79
|
+
- ".rubocop.yml"
|
80
|
+
- ".ruby-style-guide.yml"
|
81
|
+
- ".travis.yml"
|
82
|
+
- Gemfile
|
83
|
+
- LICENSE
|
84
|
+
- README.md
|
85
|
+
- Rakefile
|
86
|
+
- action_table.gemspec
|
87
|
+
- bin/console
|
88
|
+
- bin/setup
|
89
|
+
- lib/action_table.rb
|
90
|
+
- lib/action_table/helper.rb
|
91
|
+
- lib/action_table/railtie.rb
|
92
|
+
- lib/action_table/version.rb
|
93
|
+
- lib/action_table/view.rb
|
94
|
+
- lib/action_table/views/action_table/_table.html.erb
|
95
|
+
homepage: https://github.com/buren/action_table
|
96
|
+
licenses:
|
97
|
+
- MIT
|
98
|
+
metadata: {}
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
|
+
requirements:
|
110
|
+
- - ">="
|
111
|
+
- !ruby/object:Gem::Version
|
112
|
+
version: '0'
|
113
|
+
requirements: []
|
114
|
+
rubyforge_project:
|
115
|
+
rubygems_version: 2.7.6
|
116
|
+
signing_key:
|
117
|
+
specification_version: 4
|
118
|
+
summary: Render ActiveModel or ActiveRecord objects as HTML tables with one line of
|
119
|
+
Ruby.
|
120
|
+
test_files: []
|