record_view_helper 1.0.1 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/README.md +4 -0
- data/lib/record_view_helper/config.rb +17 -0
- data/lib/record_view_helper/dl.rb +5 -6
- data/lib/record_view_helper/record_value_setting.rb +11 -2
- data/lib/record_view_helper/table.rb +11 -12
- data/lib/record_view_helper/util.rb +16 -11
- data/lib/record_view_helper/version.rb +1 -1
- metadata +22 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 484e0e2bc1f93978d015d58e826dec01598b1297d1dec6e4dbf30b9b4bf04d77
|
4
|
+
data.tar.gz: b56a7796e056fd245510ded3c773fd625b4d4c2000d9a88f9a5016d24761ab9f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 5473baa96d7ac03c39cb5ea4b9db5f728961980a093775b0e9803f5485ef2f0629bdcbdfafaa5ca28b55885a3583599f10aec5d8651c9708035e71cdaa853a45
|
7
|
+
data.tar.gz: 8d19a02261b49c17d17d4bcab66368e0527d653f5f8fde03aa41b6f63b27adb2e692adb8ea9042d47fd564ec48e979e1f8e06e10a8775738b74ac56811ec9361
|
data/README.md
CHANGED
@@ -45,5 +45,9 @@ Or install it yourself as:
|
|
45
45
|
$ gem install record_view_helper
|
46
46
|
```
|
47
47
|
|
48
|
+
## See also
|
49
|
+
|
50
|
+
- [table-for](https://github.com/hunterae/table-for)
|
51
|
+
|
48
52
|
## License
|
49
53
|
The gem is available as open source under the terms of the [MIT License](https://narazaka.net/license/MIT?2017).
|
@@ -42,5 +42,22 @@ module RecordViewHelper
|
|
42
42
|
#
|
43
43
|
# @return [String] (default = "record_view_helper")
|
44
44
|
attr_accessor :locale_namespace
|
45
|
+
|
46
|
+
# Proc which returns name for column
|
47
|
+
#
|
48
|
+
# @example in Rails
|
49
|
+
# Rails.application.config.record_view_helper.column_name =
|
50
|
+
# ->(table_class, column) { "[#{column}]<br>#{table_class.columns_hash[column.to_s].comment}".html_safe }
|
51
|
+
#
|
52
|
+
# @example basic
|
53
|
+
# class MyView < ActionView::Base
|
54
|
+
# include RecordViewHelper
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# MyView.record_view_helper_config.locale_name =
|
58
|
+
# ->(table_class, column) { "[#{column}]<br>#{table_class.columns_hash[column.to_s].comment}".html_safe }
|
59
|
+
#
|
60
|
+
# @return [Proc]
|
61
|
+
attr_accessor :column_name
|
45
62
|
end
|
46
63
|
end
|
@@ -8,12 +8,11 @@ module RecordViewHelper
|
|
8
8
|
# @yieldparam [RecordValueSetting] setting setting object
|
9
9
|
# @yieldreturn [void]
|
10
10
|
# @return [ActiveSupport::SafeBuffer] rendered result
|
11
|
-
def dl_for(record, options = {})
|
12
|
-
return if record.nil?
|
11
|
+
def dl_for(record, options = {}) # rubocop:disable Metrics/AbcSize
|
13
12
|
setting = RecordValueSetting.build_from_hash!(
|
14
13
|
record.attributes.keys,
|
15
14
|
record.class.name.tableize,
|
16
|
-
options
|
15
|
+
options,
|
17
16
|
)
|
18
17
|
yield setting if block_given?
|
19
18
|
content_tag(
|
@@ -21,12 +20,12 @@ module RecordViewHelper
|
|
21
20
|
safe_join(
|
22
21
|
setting.columns.map do |column|
|
23
22
|
safe_join [
|
24
|
-
content_tag("dt", record_view_helper_t(
|
23
|
+
content_tag("dt", record_view_helper_t(record.class, column), setting.header_attrs[column]),
|
25
24
|
content_tag("dd", format_record_value(record, column, setting.formats[column], setting.links[column]), setting.attrs[column]),
|
26
25
|
]
|
27
|
-
end
|
26
|
+
end,
|
28
27
|
),
|
29
|
-
options
|
28
|
+
options,
|
30
29
|
)
|
31
30
|
end
|
32
31
|
end
|
@@ -29,7 +29,7 @@ module RecordViewHelper
|
|
29
29
|
formats: options.delete(:formats),
|
30
30
|
links: options.delete(:links),
|
31
31
|
attrs: options.delete(:attrs),
|
32
|
-
header_attrs: options.delete(:header_attrs)
|
32
|
+
header_attrs: options.delete(:header_attrs),
|
33
33
|
)
|
34
34
|
end
|
35
35
|
|
@@ -41,7 +41,16 @@ module RecordViewHelper
|
|
41
41
|
# @param [Hash<Symbol, Symbol|Hash<Symbol, Symbol|Array<Symbol>>>] links links settings
|
42
42
|
# @param [Hash<Symbol, Hash<Symbol, String>>] attrs column value dom tag attrs
|
43
43
|
# @param [Hash<Symbol, Hash<Symbol, String>>] header_attrs column header dom tag attrs
|
44
|
-
def initialize(
|
44
|
+
def initialize( # rubocop:disable Metrics/ParameterLists
|
45
|
+
columns,
|
46
|
+
table_name,
|
47
|
+
only: nil,
|
48
|
+
except: nil,
|
49
|
+
formats: nil,
|
50
|
+
links: nil,
|
51
|
+
attrs: nil,
|
52
|
+
header_attrs: nil
|
53
|
+
)
|
45
54
|
@columns = columns
|
46
55
|
@table_name = table_name
|
47
56
|
@only = only || []
|
@@ -53,12 +53,11 @@ module RecordViewHelper
|
|
53
53
|
# - s.only :id, :name
|
54
54
|
# / recommended for complex settings
|
55
55
|
# / see RecordValueSetting for details
|
56
|
-
def table_for(records, options = {})
|
57
|
-
return if records.empty?
|
56
|
+
def table_for(records, options = {}) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
|
58
57
|
setting = RecordValueSetting.build_from_hash!(
|
59
58
|
records.first.attributes.keys,
|
60
59
|
records.first.class.name.tableize,
|
61
|
-
options
|
60
|
+
options,
|
62
61
|
)
|
63
62
|
yield setting if block_given?
|
64
63
|
content_tag(
|
@@ -70,10 +69,10 @@ module RecordViewHelper
|
|
70
69
|
"tr",
|
71
70
|
safe_join(
|
72
71
|
setting.columns.map do |column|
|
73
|
-
content_tag("th", record_view_helper_t(
|
74
|
-
end
|
75
|
-
)
|
76
|
-
)
|
72
|
+
content_tag("th", record_view_helper_t(records.first.class, column), setting.header_attrs[column])
|
73
|
+
end,
|
74
|
+
),
|
75
|
+
),
|
77
76
|
),
|
78
77
|
content_tag(
|
79
78
|
"tbody",
|
@@ -84,14 +83,14 @@ module RecordViewHelper
|
|
84
83
|
safe_join(
|
85
84
|
setting.columns.map do |column|
|
86
85
|
content_tag("td", format_record_value(record, column, setting.formats[column], setting.links[column]), setting.attrs[column])
|
87
|
-
end
|
88
|
-
)
|
86
|
+
end,
|
87
|
+
),
|
89
88
|
)
|
90
|
-
end
|
91
|
-
)
|
89
|
+
end,
|
90
|
+
),
|
92
91
|
),
|
93
92
|
]),
|
94
|
-
options
|
93
|
+
options,
|
95
94
|
)
|
96
95
|
end
|
97
96
|
end
|
@@ -28,7 +28,7 @@ module RecordViewHelper
|
|
28
28
|
else
|
29
29
|
deep_record_values(record, format || [column])
|
30
30
|
end
|
31
|
-
formatted_value = safe_join values.map {
|
31
|
+
formatted_value = safe_join values.map {|value| [format_view_value(value), " "] }.flatten[0..-2]
|
32
32
|
case link
|
33
33
|
when String, Symbol
|
34
34
|
link_record_value(record, formatted_value, link, [column])
|
@@ -49,7 +49,7 @@ module RecordViewHelper
|
|
49
49
|
# deep_record_values(record, [:a, [:a, :b]]) == [record.a, record.a.b]
|
50
50
|
#
|
51
51
|
def deep_record_values(record, columns)
|
52
|
-
columns.map {
|
52
|
+
columns.map {|column| deep_record_value(record, column) }
|
53
53
|
end
|
54
54
|
|
55
55
|
# deep call
|
@@ -61,7 +61,7 @@ module RecordViewHelper
|
|
61
61
|
# deep_record_value(record, [:a, :b]) == record.a.b
|
62
62
|
#
|
63
63
|
def deep_record_value(record, column)
|
64
|
-
Array(column).reduce(record) {
|
64
|
+
Array(column).reduce(record) {|receiver, method| receiver.try(:public_send, method) }
|
65
65
|
end
|
66
66
|
|
67
67
|
# format value
|
@@ -97,13 +97,18 @@ module RecordViewHelper
|
|
97
97
|
|
98
98
|
private
|
99
99
|
|
100
|
-
def record_view_helper_t(
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
100
|
+
def record_view_helper_t(table_class, column)
|
101
|
+
if record_view_helper_config.column_name
|
102
|
+
record_view_helper_config.column_name.call(table_class, column)
|
103
|
+
else
|
104
|
+
table_name = table_class.name.tableize
|
105
|
+
t(
|
106
|
+
if record_view_helper_config.locale_name
|
107
|
+
record_view_helper_config.locale_name.call(table_name, column)
|
108
|
+
else
|
109
|
+
"#{record_view_helper_config.locale_namespace}.#{table_name}.columns.#{column}"
|
110
|
+
end,
|
111
|
+
)
|
112
|
+
end
|
108
113
|
end
|
109
114
|
end
|
metadata
CHANGED
@@ -1,17 +1,17 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: record_view_helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0
|
4
|
+
version: 1.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Narazaka
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2018-09-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
|
-
name:
|
14
|
+
name: actionview
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
17
|
- - ">="
|
@@ -25,7 +25,7 @@ dependencies:
|
|
25
25
|
- !ruby/object:Gem::Version
|
26
26
|
version: '0'
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
|
-
name:
|
28
|
+
name: activesupport
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
@@ -39,47 +39,47 @@ dependencies:
|
|
39
39
|
- !ruby/object:Gem::Version
|
40
40
|
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
|
-
name:
|
42
|
+
name: bundler
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
45
|
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
|
-
name:
|
56
|
+
name: onkcop
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '0'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
|
-
name:
|
70
|
+
name: rails
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
73
|
- - ">="
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '4'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
80
|
- - ">="
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '4'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: rake
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
@@ -109,19 +109,19 @@ dependencies:
|
|
109
109
|
- !ruby/object:Gem::Version
|
110
110
|
version: '0'
|
111
111
|
- !ruby/object:Gem::Dependency
|
112
|
-
name:
|
112
|
+
name: rspec-rails
|
113
113
|
requirement: !ruby/object:Gem::Requirement
|
114
114
|
requirements:
|
115
|
-
- - "
|
115
|
+
- - "~>"
|
116
116
|
- !ruby/object:Gem::Version
|
117
|
-
version: '
|
117
|
+
version: '3'
|
118
118
|
type: :development
|
119
119
|
prerelease: false
|
120
120
|
version_requirements: !ruby/object:Gem::Requirement
|
121
121
|
requirements:
|
122
|
-
- - "
|
122
|
+
- - "~>"
|
123
123
|
- !ruby/object:Gem::Version
|
124
|
-
version: '
|
124
|
+
version: '3'
|
125
125
|
- !ruby/object:Gem::Dependency
|
126
126
|
name: simplecov
|
127
127
|
requirement: !ruby/object:Gem::Requirement
|
@@ -137,7 +137,7 @@ dependencies:
|
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
139
|
- !ruby/object:Gem::Dependency
|
140
|
-
name:
|
140
|
+
name: yard
|
141
141
|
requirement: !ruby/object:Gem::Requirement
|
142
142
|
requirements:
|
143
143
|
- - ">="
|
@@ -189,7 +189,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
189
189
|
version: '0'
|
190
190
|
requirements: []
|
191
191
|
rubyforge_project:
|
192
|
-
rubygems_version: 2.
|
192
|
+
rubygems_version: 2.7.3
|
193
193
|
signing_key:
|
194
194
|
specification_version: 4
|
195
195
|
summary: table builder and description list (dl) helper for Rails / ActiveModels
|