record_view_helper 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/MIT-LICENSE +20 -0
- data/README.md +42 -0
- data/Rakefile +9 -0
- data/lib/record_view_helper/config.rb +46 -0
- data/lib/record_view_helper/dl.rb +31 -0
- data/lib/record_view_helper/railtie.rb +14 -0
- data/lib/record_view_helper/record_value_setting.rb +175 -0
- data/lib/record_view_helper/table.rb +96 -0
- data/lib/record_view_helper/util.rb +109 -0
- data/lib/record_view_helper/version.rb +4 -0
- data/lib/record_view_helper.rb +34 -0
- metadata +196 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 8dbd093ebca3fa4b721c5de5b988e38e71f725c4
|
4
|
+
data.tar.gz: 9f642317111c040a49362801bbefd2de82b82f18
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 641651508a5381454b3374e6496e08084ff2b738cf7f32700836c8bb0debabc0c631b73ddbeff0e7b137b78c147d696628659cb196806f947bd5c6c40bbfca07
|
7
|
+
data.tar.gz: 8342101d86840591b4de495eb5c3122cb9485a2751bff785e7a4123e3da5d5ea8655e0749d296a21074081a1d38e49687818a4e21827e4c88dab589f76b8e674
|
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright 2017 Narazaka
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
# [RecordViewHelper](https://github.com/Narazaka/record_view_helper)
|
2
|
+
|
3
|
+
table builder and description list (dl) helper for Rails / ActiveModels
|
4
|
+
|
5
|
+
## Usage
|
6
|
+
|
7
|
+
```slim
|
8
|
+
/ on Rails
|
9
|
+
/ foo.html.slim
|
10
|
+
|
11
|
+
= table_for(@bars) do |s|
|
12
|
+
- s.only :id, :name, :kind, :foo_id
|
13
|
+
- s.link :id, :bar_path
|
14
|
+
- s.link :foo_id, :foo_path
|
15
|
+
- s.format :foo_id, [:foo_id, [:foo, :name]]
|
16
|
+
- s.format :kind do |record|
|
17
|
+
= Foo::KIND[record.kind]
|
18
|
+
|
19
|
+
= dl_for(@foo, except: [:created_at, :updated_at])
|
20
|
+
```
|
21
|
+
|
22
|
+
see [API Documents](http://www.rubydoc.info/gems/record_view_helper) for details
|
23
|
+
|
24
|
+
## Installation
|
25
|
+
Add this line to your application's Gemfile:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
gem "record_view_helper"
|
29
|
+
```
|
30
|
+
|
31
|
+
And then execute:
|
32
|
+
```bash
|
33
|
+
$ bundle
|
34
|
+
```
|
35
|
+
|
36
|
+
Or install it yourself as:
|
37
|
+
```bash
|
38
|
+
$ gem install record_view_helper
|
39
|
+
```
|
40
|
+
|
41
|
+
## License
|
42
|
+
The gem is available as open source under the terms of the [MIT License](https://narazaka.net/license/MIT?2017).
|
data/Rakefile
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
module RecordViewHelper
|
2
|
+
# RecordViewHelper config
|
3
|
+
class Config
|
4
|
+
def initialize
|
5
|
+
@true_view = true
|
6
|
+
@false_view = false
|
7
|
+
@locale_namespace = "record_view_helper"
|
8
|
+
end
|
9
|
+
|
10
|
+
# text/html(raw) which is showed when true === value
|
11
|
+
# @return [String] (defalut = true)
|
12
|
+
attr_accessor :true_view
|
13
|
+
|
14
|
+
# text/html(raw) which is showed when false === value
|
15
|
+
# @return [String] (defalut = false)
|
16
|
+
attr_accessor :false_view
|
17
|
+
|
18
|
+
# Proc which returns name for t(name)
|
19
|
+
#
|
20
|
+
# @example in Rails
|
21
|
+
# Rails.application.config.record_view_helper.locale_name =
|
22
|
+
# ->(table_name, column) { "columns.#{table_name}.#{column}" }
|
23
|
+
#
|
24
|
+
# @example basic
|
25
|
+
# class MyView < ActionView::Base
|
26
|
+
# include RecordViewHelper
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
# MyView.record_view_helper_config.locale_name =
|
30
|
+
# ->(table_name, column) { "columns.#{table_name}.#{column}" }
|
31
|
+
#
|
32
|
+
# @return [Proc]
|
33
|
+
attr_accessor :locale_name
|
34
|
+
|
35
|
+
# namespace for `t()`
|
36
|
+
#
|
37
|
+
# @example
|
38
|
+
# # locale_namespace is used like below
|
39
|
+
# t("#{locale_namespace}.#{table_name}.columns.#{column}")
|
40
|
+
#
|
41
|
+
# # if you want to use another hierarchy, you should use #locale_name.
|
42
|
+
#
|
43
|
+
# @return [String] (default = "record_view_helper")
|
44
|
+
attr_accessor :locale_namespace
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module RecordViewHelper
|
2
|
+
# build definition list(dl) for records
|
3
|
+
#
|
4
|
+
# the options is same as `table_for`
|
5
|
+
# @param [Object] record record
|
6
|
+
# @param [Hash] options (same as `table_for`)
|
7
|
+
# @yield [setting] optional setting block (see RecordValueSetting document)
|
8
|
+
# @yieldparam [RecordValueSetting] setting setting object
|
9
|
+
# @yieldreturn [void]
|
10
|
+
# @return [ActiveSupport::SafeBuffer] rendered result
|
11
|
+
def dl_for(record, options = {})
|
12
|
+
setting = RecordValueSetting.build_from_hash!(
|
13
|
+
record.attributes.keys,
|
14
|
+
record.class.name.tableize,
|
15
|
+
options
|
16
|
+
)
|
17
|
+
yield setting if block_given?
|
18
|
+
content_tag(
|
19
|
+
"dl",
|
20
|
+
safe_join(
|
21
|
+
setting.columns.map do |column|
|
22
|
+
safe_join [
|
23
|
+
content_tag("dt", record_view_helper_t(setting.table_name, column), setting.header_attrs[column]),
|
24
|
+
content_tag("dd", format_record_value(record, column, setting.formats[column], setting.links[column]), setting.attrs[column]),
|
25
|
+
]
|
26
|
+
end
|
27
|
+
),
|
28
|
+
options
|
29
|
+
)
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require "active_support/lazy_load_hooks"
|
2
|
+
|
3
|
+
module RecordViewHelper
|
4
|
+
# loader for rails
|
5
|
+
class Railtie < ::Rails::Railtie
|
6
|
+
config.record_view_helper = RecordViewHelper::Config.new
|
7
|
+
|
8
|
+
initializer "record_view_helper.cofigure" do
|
9
|
+
ActiveSupport.on_load(:action_view) do
|
10
|
+
ActionView::Base.include(RecordViewHelper)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
require "active_support/core_ext/enumerable"
|
2
|
+
require "active_support/core_ext/object/blank"
|
3
|
+
|
4
|
+
module RecordViewHelper
|
5
|
+
# columns with formats, links, etc.
|
6
|
+
class RecordValueSetting
|
7
|
+
# format settings
|
8
|
+
attr_reader :formats
|
9
|
+
# link settings
|
10
|
+
attr_reader :links
|
11
|
+
# attr settings
|
12
|
+
attr_reader :attrs
|
13
|
+
# header attr settings
|
14
|
+
attr_reader :header_attrs
|
15
|
+
|
16
|
+
# build from hash
|
17
|
+
#
|
18
|
+
# **caution**: this method deletes given hash options' key
|
19
|
+
# @param [Array<Symbol>] columns default columns
|
20
|
+
# @param [Symbol] table_name default table name
|
21
|
+
# @param [Hash] options option hash
|
22
|
+
# @return [RecordValueSetting]
|
23
|
+
def self.build_from_hash!(columns, table_name, options = {})
|
24
|
+
new(
|
25
|
+
columns,
|
26
|
+
options.delete(:table_name) || table_name,
|
27
|
+
only: options.delete(:only),
|
28
|
+
except: options.delete(:except),
|
29
|
+
formats: options.delete(:formats),
|
30
|
+
links: options.delete(:links),
|
31
|
+
attrs: options.delete(:attrs),
|
32
|
+
header_attrs: options.delete(:header_attrs)
|
33
|
+
)
|
34
|
+
end
|
35
|
+
|
36
|
+
# @param [Array<Symbol>] columns default columns
|
37
|
+
# @param [Symbol] table_name default table name
|
38
|
+
# @param [Array<Symbol>] only only columns
|
39
|
+
# @param [Array<Symbol>] except except columns
|
40
|
+
# @param [Hash<Symbol, Symbol|Array<Symbol|Array<Symbol>>>] formats formats
|
41
|
+
# @param [Hash<Symbol, Symbol|Hash<Symbol, Symbol|Array<Symbol>>>] links links settings
|
42
|
+
# @param [Hash<Symbol, Hash<Symbol, String>>] attrs column value dom tag attrs
|
43
|
+
# @param [Hash<Symbol, Hash<Symbol, String>>] header_attrs column header dom tag attrs
|
44
|
+
def initialize(columns, table_name, only: nil, except: nil, formats: nil, links: nil, attrs: nil, header_attrs: nil)
|
45
|
+
@columns = columns
|
46
|
+
@table_name = table_name
|
47
|
+
@only = only || []
|
48
|
+
@except = except || []
|
49
|
+
@formats = formats || {}
|
50
|
+
@links = links || {}
|
51
|
+
@attrs = attrs || {}
|
52
|
+
@header_attrs = header_attrs || {}
|
53
|
+
end
|
54
|
+
|
55
|
+
# set and get table name
|
56
|
+
# @param [Symbol] table_name
|
57
|
+
# @return [Symbol]
|
58
|
+
#
|
59
|
+
# @example
|
60
|
+
# = dl_for(record) do |s|
|
61
|
+
# - s.table_name :foos
|
62
|
+
def table_name(table_name = nil)
|
63
|
+
if table_name
|
64
|
+
@table_name = table_name
|
65
|
+
else
|
66
|
+
@table_name
|
67
|
+
end
|
68
|
+
end
|
69
|
+
|
70
|
+
# add only columns
|
71
|
+
# @param [Array<Symbol>] columns
|
72
|
+
# @return [void]
|
73
|
+
#
|
74
|
+
# @example
|
75
|
+
# = dl_for(record) do |s|
|
76
|
+
# - s.only :id, :name
|
77
|
+
# - s.only :foo_id
|
78
|
+
def only(*columns)
|
79
|
+
@only += columns.flatten
|
80
|
+
end
|
81
|
+
|
82
|
+
# add except columns
|
83
|
+
# @param [Array<Symbol>] columns
|
84
|
+
# @return [void]
|
85
|
+
#
|
86
|
+
# @example
|
87
|
+
# = dl_for(record) do |s|
|
88
|
+
# - s.except :created_at, :updated_at
|
89
|
+
# - s.except :password
|
90
|
+
def except(*columns)
|
91
|
+
@except += columns.flatten
|
92
|
+
end
|
93
|
+
|
94
|
+
# set column format
|
95
|
+
# @param [Symbol] column target column
|
96
|
+
# @param [Proc|Symbol|Array<Symbol|Array<Symbol>>] format format
|
97
|
+
# @yield [record] format function (optional)
|
98
|
+
# @yieldparam [Object] record record
|
99
|
+
# @yieldreturn [String] formatted value
|
100
|
+
# @return [void]
|
101
|
+
#
|
102
|
+
# @example
|
103
|
+
# = dl_for(record) do |s|
|
104
|
+
# - s.only :id, :foo_id, :statuses
|
105
|
+
# - s.format :foo_id, [:foo_id, [:foo, :name]]
|
106
|
+
# - s.format :statuses do |record|
|
107
|
+
# ul
|
108
|
+
# - record.statuses.split(" ") each do |status|
|
109
|
+
# li = status
|
110
|
+
def format(column, format = nil, &block)
|
111
|
+
@formats[column] = block_given? ? block : format
|
112
|
+
end
|
113
|
+
|
114
|
+
# set column link setting
|
115
|
+
# @param [Symbol] column target column
|
116
|
+
# @param [Symbol|Hash<Symbol|Array<Symbol|Array<Symbol>>>] link setting
|
117
|
+
# @return [void]
|
118
|
+
#
|
119
|
+
# @example
|
120
|
+
# = dl_for(record) do |s|
|
121
|
+
# - s.link :foo_id, :foo_path
|
122
|
+
# - s.link :id, foo_bar_path: [:foo_id, :id]
|
123
|
+
def link(column, link)
|
124
|
+
@links[column] = link
|
125
|
+
end
|
126
|
+
|
127
|
+
# set column value dom tag attrs
|
128
|
+
# @param [Symbol] column target column
|
129
|
+
# @param [Hash<Symbol, String>] attr attr hash
|
130
|
+
# @return [void]
|
131
|
+
#
|
132
|
+
# @example
|
133
|
+
# = dl_for(record) do |s|
|
134
|
+
# - s.attr :foo_id, style: "width: 4em"
|
135
|
+
def attr(column, attr)
|
136
|
+
@attrs[column] = attr
|
137
|
+
end
|
138
|
+
|
139
|
+
# set column header dom tag attrs
|
140
|
+
# @param [Symbol] column target column
|
141
|
+
# @param [Hash<Symbol, String>] header_attr attr hash
|
142
|
+
# @return [void]
|
143
|
+
#
|
144
|
+
# @example
|
145
|
+
# = dl_for(record) do |s|
|
146
|
+
# - s.header_attr :foo_id, style: "font-weight: bold"
|
147
|
+
def header_attr(column, header_attr)
|
148
|
+
@header_attrs[column] = header_attr
|
149
|
+
end
|
150
|
+
|
151
|
+
# set column
|
152
|
+
# @param [Symbol] column target column
|
153
|
+
# @param [Proc|Symbol|Array<Symbol|Array<Symbol>>] format format
|
154
|
+
# @param [Symbol|Hash<Symbol|Array<Symbol|Array<Symbol>>>] link setting
|
155
|
+
# @param [Hash<Symbol, String>] attr column value dom tag attrs
|
156
|
+
# @param [Hash<Symbol, String>] header_attr column header dom tag attrs
|
157
|
+
# @return [void]
|
158
|
+
#
|
159
|
+
# @example
|
160
|
+
# = dl_for(record) do |s|
|
161
|
+
# - s.column :foo_id, format: [:foo_id, [:foo, :name]], link: :foo_path
|
162
|
+
def column(column, format: nil, link: nil, attr: nil, header_attr: nil)
|
163
|
+
@formats[column] if format
|
164
|
+
@links[column] if link
|
165
|
+
@attrs[column] if attr
|
166
|
+
@header_attrs[column] = header_attr if header_attr
|
167
|
+
end
|
168
|
+
|
169
|
+
# calculated columns
|
170
|
+
# @return [Array<Symbol>] (only || default columns) - except
|
171
|
+
def columns
|
172
|
+
(@only.presence || @columns).map(&:to_sym).without(*@except.map(&:to_sym))
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
module RecordViewHelper
|
2
|
+
# build table for records
|
3
|
+
# @param [Enumerable<Object>] records records
|
4
|
+
# @param [Hash] options
|
5
|
+
# @yield [setting] optional setting block (see RecordValueSetting document)
|
6
|
+
# @yieldparam [RecordValueSetting] setting setting object
|
7
|
+
# @yieldreturn [void]
|
8
|
+
# @return [ActiveSupport::SafeBuffer] rendered result
|
9
|
+
#
|
10
|
+
# @example basic
|
11
|
+
# = table_for(records)
|
12
|
+
#
|
13
|
+
# @example only columns
|
14
|
+
# = table_for(records, only: [:id, :name])
|
15
|
+
# / only :id, :name columns will rendered
|
16
|
+
#
|
17
|
+
# @example except columns
|
18
|
+
# = table_for(records, except: [:created_at, :updated_at])
|
19
|
+
# / except timestamp columns
|
20
|
+
#
|
21
|
+
# @example link settings
|
22
|
+
# = table_for(records, links: {id: :foo_path})
|
23
|
+
# / link to foo_path(record.id)
|
24
|
+
#
|
25
|
+
# @example nested path link
|
26
|
+
# = table_for(records, links: {id: {foo_bar_path: [:foo_id, :id]}})
|
27
|
+
# / link to foo_bar_path(record.foo_id, record.id)
|
28
|
+
#
|
29
|
+
# @example formats
|
30
|
+
# = table_for(records, formats: {foo_id: [:foo_id, [:foo, :name]]})
|
31
|
+
# / foo_id column will rendered as "#{foo_id} #{foo.name}"
|
32
|
+
#
|
33
|
+
# @example proc formats
|
34
|
+
# = table_for(records, formats: {statuses: ->(record) { record.statuses.join(" ") } })
|
35
|
+
# / you can use also free format
|
36
|
+
#
|
37
|
+
# @example column header locale
|
38
|
+
# = table_for(records, table_name: "foos")
|
39
|
+
# / table header will rendered by t("record_view_helper.#{table_name}.columns.#{column}")
|
40
|
+
# / table_name is automatically detected from record class name by default
|
41
|
+
# / if you want to change locale namespace, see RecordViewHelper::Config document
|
42
|
+
#
|
43
|
+
# @example attrs / header attrs
|
44
|
+
# = table_for(records, attrs: {id: {style: "width: 3em"}}, header_attrs: {id: {style: "width: 3em"}})
|
45
|
+
# / td(attrs) and th(header_attrs) attrs
|
46
|
+
#
|
47
|
+
# @example table attrs
|
48
|
+
# = table_for(records, width: "100%")
|
49
|
+
# / <table width="100%">
|
50
|
+
#
|
51
|
+
# @example setting block
|
52
|
+
# = table_for(records) do |s|
|
53
|
+
# - s.only :id, :name
|
54
|
+
# / recommended for complex settings
|
55
|
+
# / see RecordValueSetting for details
|
56
|
+
def table_for(records, options = {})
|
57
|
+
setting = RecordValueSetting.build_from_hash!(
|
58
|
+
records.first.attributes.keys,
|
59
|
+
records.first.class.name.tableize,
|
60
|
+
options
|
61
|
+
)
|
62
|
+
yield setting if block_given?
|
63
|
+
content_tag(
|
64
|
+
"table",
|
65
|
+
safe_join([
|
66
|
+
content_tag(
|
67
|
+
"thead",
|
68
|
+
content_tag(
|
69
|
+
"tr",
|
70
|
+
safe_join(
|
71
|
+
setting.columns.map do |column|
|
72
|
+
content_tag("th", record_view_helper_t(setting.table_name, column), setting.header_attrs[column])
|
73
|
+
end
|
74
|
+
)
|
75
|
+
)
|
76
|
+
),
|
77
|
+
content_tag(
|
78
|
+
"tbody",
|
79
|
+
safe_join(
|
80
|
+
records.map do |record|
|
81
|
+
content_tag(
|
82
|
+
"tr",
|
83
|
+
safe_join(
|
84
|
+
setting.columns.map do |column|
|
85
|
+
content_tag("td", format_record_value(record, column, setting.formats[column], setting.links[column]), setting.attrs[column])
|
86
|
+
end
|
87
|
+
)
|
88
|
+
)
|
89
|
+
end
|
90
|
+
)
|
91
|
+
),
|
92
|
+
]),
|
93
|
+
options
|
94
|
+
)
|
95
|
+
end
|
96
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
require "active_support/core_ext/object/try"
|
2
|
+
|
3
|
+
module RecordViewHelper
|
4
|
+
# format record value
|
5
|
+
# @param [Object] record record
|
6
|
+
# @param [Symbol] column target column name
|
7
|
+
# @param [Proc|Symbol|Array<Symbol|Array<Symbol>>] format format
|
8
|
+
# @param [Symbol|Hash<Symbol, Array<Symbol>>] link link options
|
9
|
+
#
|
10
|
+
# @example basic
|
11
|
+
# format_view_value(record, :name) == record.name
|
12
|
+
#
|
13
|
+
# @example with format
|
14
|
+
# format_view_value(record, :foo_id, [:foo_id, [:foo, :name]]) == "#{record.foo_id} #{record.foo.name}"
|
15
|
+
#
|
16
|
+
# @example with link
|
17
|
+
# format_view_value(record, :foo_id, nil, :foo_path) == link_to record.foo_id, foo_path(record.foo_id)
|
18
|
+
#
|
19
|
+
# @example with link with deep call
|
20
|
+
# format_view_value(record, :id, [:id, :name], foo_bar_path: [:foo_id, :id]) ==
|
21
|
+
# link_to "#{record.id} #{record.name}", foo_bar_path(record.foo_id, record.id)
|
22
|
+
#
|
23
|
+
def format_record_value(record, column, format = nil, link = nil)
|
24
|
+
values =
|
25
|
+
case format
|
26
|
+
when Proc
|
27
|
+
[defined?(capture) ? capture(record, &format) : format.call(record)]
|
28
|
+
else
|
29
|
+
deep_record_values(record, format || [column])
|
30
|
+
end
|
31
|
+
formatted_value = safe_join values.map { |value| [format_view_value(value), " "] }.flatten[0..-2]
|
32
|
+
case link
|
33
|
+
when String, Symbol
|
34
|
+
link_record_value(record, formatted_value, link, [column])
|
35
|
+
when Hash
|
36
|
+
path_method, param_columns = link.first
|
37
|
+
link_record_value(record, formatted_value, path_method, param_columns)
|
38
|
+
else
|
39
|
+
formatted_value
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
# deep call as array
|
44
|
+
#
|
45
|
+
# @param [Object] record record
|
46
|
+
# @param [Array<Symbol|Array<Symbol>>] columns call methods by columns
|
47
|
+
#
|
48
|
+
# @example
|
49
|
+
# deep_record_values(record, [:a, [:a, :b]]) == [record.a, record.a.b]
|
50
|
+
#
|
51
|
+
def deep_record_values(record, columns)
|
52
|
+
columns.map { |column| deep_record_value(record, column) }
|
53
|
+
end
|
54
|
+
|
55
|
+
# deep call
|
56
|
+
#
|
57
|
+
# @param [Object] record record
|
58
|
+
# @param [Symbol|Array<Symbol>] column call methods
|
59
|
+
#
|
60
|
+
# @example
|
61
|
+
# deep_record_value(record, [:a, :b]) == record.a.b
|
62
|
+
#
|
63
|
+
def deep_record_value(record, column)
|
64
|
+
Array(column).reduce(record) { |receiver, method| receiver.try(:public_send, method) }
|
65
|
+
end
|
66
|
+
|
67
|
+
# format value
|
68
|
+
# @param [Object] value value
|
69
|
+
def format_view_value(value)
|
70
|
+
case value
|
71
|
+
when Date, Time, DateTime
|
72
|
+
l(value)
|
73
|
+
when true
|
74
|
+
record_view_helper_config.true_view
|
75
|
+
when false
|
76
|
+
record_view_helper_config.false_view
|
77
|
+
else
|
78
|
+
value
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
# make link_to
|
83
|
+
# @param [Object] record record
|
84
|
+
# @param [String] title link_to title
|
85
|
+
# @param [Symbol] path_method path method name (like :root_path)
|
86
|
+
# @param [Symbol|Array<Symbol|Array<Symbol>>] param_columns column call methods for path method's args
|
87
|
+
#
|
88
|
+
# @example
|
89
|
+
# link_record_value(record, "title", :foo_bar_path, [:foo_id, :id])
|
90
|
+
# # makes...
|
91
|
+
# link_to "title", foo_bar_path(record.foo_id, record.id)
|
92
|
+
#
|
93
|
+
def link_record_value(record, title, path_method, param_columns)
|
94
|
+
send_params = [path_method] + deep_record_values(record, Array(param_columns))
|
95
|
+
link_to(title, send(*send_params))
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
|
100
|
+
def record_view_helper_t(table_name, column)
|
101
|
+
t(
|
102
|
+
if record_view_helper_config.locale_name
|
103
|
+
record_view_helper_config.locale_name.call(table_name, column)
|
104
|
+
else
|
105
|
+
"#{record_view_helper_config.locale_namespace}.#{table_name}.columns.#{column}"
|
106
|
+
end
|
107
|
+
)
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require "action_view"
|
2
|
+
require "active_support/concern"
|
3
|
+
require "record_view_helper/config"
|
4
|
+
require "record_view_helper/dl"
|
5
|
+
require "record_view_helper/table"
|
6
|
+
require "record_view_helper/record_value_setting"
|
7
|
+
require "record_view_helper/util"
|
8
|
+
require "record_view_helper/version"
|
9
|
+
|
10
|
+
require "record_view_helper/railtie" if defined?(Rails)
|
11
|
+
|
12
|
+
# table builder and description list (dl) helper for Rails / ActiveModels
|
13
|
+
module RecordViewHelper
|
14
|
+
extend ActiveSupport::Concern
|
15
|
+
include ::ActionView::Helpers::TagHelper
|
16
|
+
include ::ActionView::Helpers::UrlHelper
|
17
|
+
include ::ActionView::Helpers::TextHelper
|
18
|
+
include ::ActionView::Helpers::TranslationHelper
|
19
|
+
|
20
|
+
included do
|
21
|
+
class << self
|
22
|
+
# config
|
23
|
+
# @return [RecordViewHelper::Config]
|
24
|
+
attr_accessor :record_view_helper_config
|
25
|
+
end
|
26
|
+
self.record_view_helper_config ||= Config.new
|
27
|
+
end
|
28
|
+
|
29
|
+
# config
|
30
|
+
# @return [RecordViewHelper::Config]
|
31
|
+
def record_view_helper_config
|
32
|
+
self.class.send(__method__) || Rails.application.config.record_view_helper
|
33
|
+
end
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,196 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: record_view_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Narazaka
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-03-04 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activesupport
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: actionview
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
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: rails
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '4'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '4'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rspec-rails
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: rspec
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: yard
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - ">="
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: simplecov
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - ">="
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: onkcop
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
153
|
+
description: This module provides table_for(model) and dl_for(model) helpers.
|
154
|
+
email:
|
155
|
+
- info@narazaka.net
|
156
|
+
executables: []
|
157
|
+
extensions: []
|
158
|
+
extra_rdoc_files: []
|
159
|
+
files:
|
160
|
+
- MIT-LICENSE
|
161
|
+
- README.md
|
162
|
+
- Rakefile
|
163
|
+
- lib/record_view_helper.rb
|
164
|
+
- lib/record_view_helper/config.rb
|
165
|
+
- lib/record_view_helper/dl.rb
|
166
|
+
- lib/record_view_helper/railtie.rb
|
167
|
+
- lib/record_view_helper/record_value_setting.rb
|
168
|
+
- lib/record_view_helper/table.rb
|
169
|
+
- lib/record_view_helper/util.rb
|
170
|
+
- lib/record_view_helper/version.rb
|
171
|
+
homepage: https://github.com/Narazaka/record_view_helper
|
172
|
+
licenses:
|
173
|
+
- MIT
|
174
|
+
metadata:
|
175
|
+
yard.run: yri
|
176
|
+
post_install_message:
|
177
|
+
rdoc_options: []
|
178
|
+
require_paths:
|
179
|
+
- lib
|
180
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
181
|
+
requirements:
|
182
|
+
- - ">="
|
183
|
+
- !ruby/object:Gem::Version
|
184
|
+
version: '0'
|
185
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
186
|
+
requirements:
|
187
|
+
- - ">="
|
188
|
+
- !ruby/object:Gem::Version
|
189
|
+
version: '0'
|
190
|
+
requirements: []
|
191
|
+
rubyforge_project:
|
192
|
+
rubygems_version: 2.5.1
|
193
|
+
signing_key:
|
194
|
+
specification_version: 4
|
195
|
+
summary: table builder and description list (dl) helper for Rails / ActiveModels
|
196
|
+
test_files: []
|