administrate-field-jsonb 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +8 -2
- data/README.md +79 -8
- data/administrate-field-jsonb.gemspec +1 -1
- data/app/assets/javascripts/administrate-field-jsonb/application.js +1 -1
- data/app/assets/javascripts/administrate-field-jsonb/components/accordion.js +7 -0
- data/app/assets/javascripts/administrate-field-jsonb/components/editor.js +1 -1
- data/app/assets/javascripts/administrate-field-jsonb/components/viewer.js +1 -1
- data/app/assets/stylesheets/administrate-field-jsonb/accordion.css +36 -0
- data/app/assets/stylesheets/administrate-field-jsonb/application.css +1 -1
- data/app/assets/stylesheets/administrate-field-jsonb/editor.css +1 -1
- data/app/views/fields/jsonb/_array.html.erb +8 -0
- data/app/views/fields/jsonb/_field.html.erb +9 -0
- data/app/views/fields/jsonb/_form.html.erb +1 -7
- data/app/views/fields/jsonb/_hash.html.erb +12 -0
- data/app/views/fields/jsonb/_index.html.erb +1 -1
- data/app/views/fields/jsonb/_show.html.erb +16 -3
- data/docs/images/advanced_view_array.png +0 -0
- data/docs/images/advanced_view_full.png +0 -0
- data/docs/images/advanced_view_object.png +0 -0
- data/docs/images/form.png +0 -0
- data/docs/images/show_jsoneditor.png +0 -0
- data/lib/administrate/field/jsonb.rb +17 -1
- metadata +11 -5
- data/docs/images/index.png +0 -0
- data/docs/images/show.png +0 -0
- data/vendor/assets/.DS_Store +0 -0
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d7434f7a732965280d6da5120b27bbdb159387f6
|
4
|
+
data.tar.gz: 75a9f459a5f3f84c115986436ca8b8b84a14dc3a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ae8a41eac399b3d9a60c4e852688ac481c8a78a79b7117e35e339dcc687f9aaca05a9c2ec9e514838a970b169d7397bc026442ea865c1127ebc6bc7d5a5d73b0
|
7
|
+
data.tar.gz: 94e48d3d6dcf04e7b409cfe8381500f45c8525af0f37aa703df4567011d0916697a30c3ff27284b59e3dbd0459ae8180b48051e5180f86489f3f5cc671a14152
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Administrate::Field::Jsonb
|
2
2
|
|
3
|
-
A plugin to show and edit JSON objects within [Administrate](https://github.com/thoughtbot/administrate). inspired by [Administrate::Field::JSON](https://github.com/eddietejeda/administrate-field-json)
|
3
|
+
A plugin to show and edit JSON objects within [Administrate](https://github.com/thoughtbot/administrate). inspired by [Administrate::Field::JSON](https://github.com/eddietejeda/administrate-field-json).
|
4
4
|
|
5
|
-
This gem uses [jsoneditor](https://github.com/josdejong/jsoneditor)
|
5
|
+
This gem uses [jsoneditor](https://github.com/josdejong/jsoneditor).
|
6
6
|
|
7
7
|
## Installation
|
8
8
|
|
@@ -23,7 +23,7 @@ bundle
|
|
23
23
|
```ruby
|
24
24
|
ATTRIBUTE_TYPES = {
|
25
25
|
# ...
|
26
|
-
|
26
|
+
details: Field::JSONB
|
27
27
|
}.freeze
|
28
28
|
```
|
29
29
|
|
@@ -32,12 +32,34 @@ If you have some kind of serialization, you can call methods on your object with
|
|
32
32
|
```ruby
|
33
33
|
ATTRIBUTE_TYPES = {
|
34
34
|
# ...
|
35
|
-
|
36
|
-
transform: %w[to_h]
|
35
|
+
details: Field::JSONB.with_options(
|
36
|
+
transform: %w[to_h symbolize_keys]
|
37
37
|
)
|
38
38
|
}.freeze
|
39
39
|
```
|
40
40
|
|
41
|
+
If you want to edit json displaying on `show` page, you can use `advanced_view` option (both JSON and arrays are supported).
|
42
|
+
|
43
|
+
```ruby
|
44
|
+
ATTRIBUTE_TYPES = {
|
45
|
+
# ...
|
46
|
+
details: Field::JSONB.with_options(transform: %i[to_h symbolize_keys], advanced_view: {
|
47
|
+
company: Field::String,
|
48
|
+
position: Field::String,
|
49
|
+
skills: Field::JSONB.with_options(advanced_view: {
|
50
|
+
'name' => Field::String,
|
51
|
+
'years' => Field::Number.with_options(suffix: ' years')
|
52
|
+
})
|
53
|
+
}),
|
54
|
+
languages: Field::JSONB.with_options(advanced_view: {
|
55
|
+
'title' => Field::String,
|
56
|
+
'code' => Field::String,
|
57
|
+
})
|
58
|
+
}.freeze
|
59
|
+
```
|
60
|
+
|
61
|
+
NOTE: don't define `advanced_view` option if you want to display you JSON with the [jsoneditor](https://github.com/josdejong/jsoneditor).
|
62
|
+
|
41
63
|
## How it looks like
|
42
64
|
|
43
65
|
### Form
|
@@ -48,16 +70,65 @@ ATTRIBUTE_TYPES = {
|
|
48
70
|
|
49
71
|
### Show
|
50
72
|
|
73
|
+
#### jsoneditor mode
|
74
|
+
|
51
75
|
<p align="center">
|
52
|
-
<img src="docs/images/
|
76
|
+
<img src="docs/images/show_jsoneditor.png">
|
53
77
|
</p>
|
54
78
|
|
55
|
-
|
79
|
+
#### advanced_view mode
|
56
80
|
|
57
81
|
<p align="center">
|
58
|
-
<img src="docs/images/
|
82
|
+
<img src="docs/images/advanced_view_full.png">
|
59
83
|
</p>
|
60
84
|
|
85
|
+
#### advanced_view object
|
86
|
+
|
87
|
+
<p align="center">
|
88
|
+
<img src="docs/images/advanced_view_object.png">
|
89
|
+
</p>
|
90
|
+
|
91
|
+
#### advanced_view array
|
92
|
+
|
93
|
+
<p align="center">
|
94
|
+
<img src="docs/images/advanced_view_array.png">
|
95
|
+
</p>
|
96
|
+
|
97
|
+
## Recipes
|
98
|
+
|
99
|
+
If you want to store your JSON in hash format and not a string add this to your model.
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
def your_field_name=(value)
|
103
|
+
self[:your_field_name] = value.is_a?(String) ? JSON.parse(value) : value
|
104
|
+
end
|
105
|
+
```
|
106
|
+
|
107
|
+
Example:
|
108
|
+
|
109
|
+
```ruby
|
110
|
+
def details=(value)
|
111
|
+
self[:details] = value.is_a?(String) ? JSON.parse(value) : value
|
112
|
+
end
|
113
|
+
```
|
114
|
+
|
115
|
+
<hr>
|
116
|
+
|
117
|
+
If you don't see details in `advanced_view`, try to add this
|
118
|
+
|
119
|
+
```ruby
|
120
|
+
transform: %i[to_h symbolize_keys]
|
121
|
+
```
|
122
|
+
|
123
|
+
or use string keys.
|
124
|
+
|
125
|
+
```ruby
|
126
|
+
languages: Field::JSONB.with_options(advanced_view: {
|
127
|
+
'title' => Field::String,
|
128
|
+
'code' => Field::String,
|
129
|
+
})
|
130
|
+
```
|
131
|
+
|
61
132
|
## License
|
62
133
|
|
63
134
|
Copyright © 2015-2019 Codica. It is released under the [MIT License](https://opensource.org/licenses/MIT).
|
@@ -2,7 +2,7 @@ $LOAD_PATH.push File.expand_path('lib', __dir__)
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |gem|
|
4
4
|
gem.name = 'administrate-field-jsonb'
|
5
|
-
gem.version = '0.
|
5
|
+
gem.version = '0.2.0'
|
6
6
|
gem.authors = ['Sergey Volkov', 'Codica']
|
7
7
|
gem.email = ['sergvolkov.codica@gmail.com']
|
8
8
|
gem.homepage = 'https://github.com/codica2/administrate-field-jsonb'
|
@@ -0,0 +1,36 @@
|
|
1
|
+
.administrate-field-jsonb-accordion {
|
2
|
+
--margin: calc(15% - .5rem);
|
3
|
+
background-color: #eee;
|
4
|
+
color: #444;
|
5
|
+
cursor: pointer;
|
6
|
+
padding: 18px;
|
7
|
+
width: calc(100% - var(--margin));
|
8
|
+
text-align: left;
|
9
|
+
border: none;
|
10
|
+
outline: none;
|
11
|
+
transition: 0.4s;
|
12
|
+
margin-left: calc(var(--margin));
|
13
|
+
}
|
14
|
+
|
15
|
+
.administrate-field-jsonb-active, .administrate-field-jsonb-accordion:hover {
|
16
|
+
background-color: #ccc;
|
17
|
+
}
|
18
|
+
|
19
|
+
.administrate-field-jsonb-panel {
|
20
|
+
padding: 0 18px;
|
21
|
+
background-color: white;
|
22
|
+
display: none;
|
23
|
+
overflow: hidden;
|
24
|
+
}
|
25
|
+
|
26
|
+
.administrate-field-jsonb-accordion:after {
|
27
|
+
content: '\02795'; /* Unicode character for "plus" sign (+) */
|
28
|
+
font-size: 13px;
|
29
|
+
color: #777;
|
30
|
+
float: right;
|
31
|
+
margin-left: 5px;
|
32
|
+
}
|
33
|
+
|
34
|
+
.administrate-field-jsonb-active:after {
|
35
|
+
content: "\2796"; /* Unicode character for "minus" sign (-) */
|
36
|
+
}
|
@@ -0,0 +1,8 @@
|
|
1
|
+
<% data.each.with_index(1) do |d, index| %>
|
2
|
+
<dt class='attribute-label administrate-field-jsonb-accordion'><%= "#{field.attribute.to_s.singularize.titleize} - #{index}" %></dt>
|
3
|
+
<dd class='administrate-field-jsonb-panel'>
|
4
|
+
<dl>
|
5
|
+
<%= render field.to_partial_path('hash'), field: field, structure: structure, data: d %>
|
6
|
+
<dl>
|
7
|
+
</dd>
|
8
|
+
<% end %>
|
@@ -1,13 +1,7 @@
|
|
1
|
-
|
2
|
-
<%#
|
3
|
-
# JSONB Form Partial
|
4
|
-
This partial renders an JSON database into structured form
|
5
|
-
%>
|
6
|
-
|
7
1
|
<div class="field-unit__label">
|
8
2
|
<%= f.label field.attribute %>
|
9
3
|
</div>
|
10
4
|
|
11
5
|
<div class="field-unit__field administrate-jsoneditor">
|
12
|
-
<%= f.text_area field.attribute, value: field.
|
6
|
+
<%= f.text_area field.attribute, value: field.transform.to_json.html_safe.squish %>
|
13
7
|
</div>
|
@@ -0,0 +1,12 @@
|
|
1
|
+
<% structure.each do |sk, sv| %>
|
2
|
+
<% if sv.is_a?(Hash) %>
|
3
|
+
<dt class='attribute-label administrate-field-jsonb-accordion'><%= sk.to_s.titleize %></dt>
|
4
|
+
<dd class='administrate-field-jsonb-panel'>
|
5
|
+
<dl>
|
6
|
+
<%= render field.to_partial_path('hash'), field: field, structure: structure[sk], data: data[sk] %>
|
7
|
+
<dl>
|
8
|
+
</dd>
|
9
|
+
<% else %>
|
10
|
+
<%= render field.to_partial_path('field'), type: sv, key: sk, value: data[sk] %>
|
11
|
+
<% end %>
|
12
|
+
<% end %>
|
@@ -1 +1 @@
|
|
1
|
-
<%= JSON.pretty_generate field.transform %>
|
1
|
+
<%= JSON.pretty_generate field.transform %>
|
@@ -1,3 +1,16 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
1
|
+
<% if field.advanced_view? %>
|
2
|
+
<% if field.array? %>
|
3
|
+
<%= render field.to_partial_path('array'), field: field, structure: field.advanced_view, data: field.transform %>
|
4
|
+
<% else %>
|
5
|
+
<dt class='attribute-label administrate-field-jsonb-accordion'><%= field.attribute.to_s.titleize %></dt>
|
6
|
+
<dd class='administrate-field-jsonb-panel'>
|
7
|
+
<dl>
|
8
|
+
<%= render field.to_partial_path('hash'), field: field, structure: field.advanced_view, data: field.transform %>
|
9
|
+
<dl>
|
10
|
+
</dd>
|
11
|
+
<% end %>
|
12
|
+
<% else %>
|
13
|
+
<div class='administrate-jsoneditor-viewer'>
|
14
|
+
<p><%= text_area_tag field.attribute, field.transform.to_json.html_safe.squish %></p>
|
15
|
+
</div>
|
16
|
+
<% end %>
|
Binary file
|
Binary file
|
Binary file
|
data/docs/images/form.png
CHANGED
Binary file
|
Binary file
|
@@ -13,11 +13,27 @@ module Administrate
|
|
13
13
|
|
14
14
|
result = data
|
15
15
|
options[:transform].each do |method|
|
16
|
-
result =
|
16
|
+
result = result.is_a?(Array) ? result.map(&method) : result.public_send(method)
|
17
17
|
end
|
18
18
|
result
|
19
19
|
end
|
20
20
|
|
21
|
+
def array?
|
22
|
+
transform.is_a?(Array)
|
23
|
+
end
|
24
|
+
|
25
|
+
def advanced_view?
|
26
|
+
advanced_view.present? && advanced_view.is_a?(Hash)
|
27
|
+
end
|
28
|
+
|
29
|
+
def advanced_view
|
30
|
+
options[:advanced_view]
|
31
|
+
end
|
32
|
+
|
33
|
+
def to_partial_path(partial = page)
|
34
|
+
"/fields/jsonb/#{partial}"
|
35
|
+
end
|
36
|
+
|
21
37
|
class Engine < ::Rails::Engine
|
22
38
|
|
23
39
|
Administrate::Engine.add_javascript 'administrate-field-jsonb/application'
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: administrate-field-jsonb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Sergey Volkov
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2019-
|
12
|
+
date: 2019-10-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: administrate
|
@@ -73,18 +73,24 @@ files:
|
|
73
73
|
- README.md
|
74
74
|
- administrate-field-jsonb.gemspec
|
75
75
|
- app/assets/javascripts/administrate-field-jsonb/application.js
|
76
|
+
- app/assets/javascripts/administrate-field-jsonb/components/accordion.js
|
76
77
|
- app/assets/javascripts/administrate-field-jsonb/components/editor.js
|
77
78
|
- app/assets/javascripts/administrate-field-jsonb/components/viewer.js
|
79
|
+
- app/assets/stylesheets/administrate-field-jsonb/accordion.css
|
78
80
|
- app/assets/stylesheets/administrate-field-jsonb/application.css
|
79
81
|
- app/assets/stylesheets/administrate-field-jsonb/editor.css
|
82
|
+
- app/views/fields/jsonb/_array.html.erb
|
83
|
+
- app/views/fields/jsonb/_field.html.erb
|
80
84
|
- app/views/fields/jsonb/_form.html.erb
|
85
|
+
- app/views/fields/jsonb/_hash.html.erb
|
81
86
|
- app/views/fields/jsonb/_index.html.erb
|
82
87
|
- app/views/fields/jsonb/_show.html.erb
|
88
|
+
- docs/images/advanced_view_array.png
|
89
|
+
- docs/images/advanced_view_full.png
|
90
|
+
- docs/images/advanced_view_object.png
|
83
91
|
- docs/images/form.png
|
84
|
-
- docs/images/
|
85
|
-
- docs/images/show.png
|
92
|
+
- docs/images/show_jsoneditor.png
|
86
93
|
- lib/administrate/field/jsonb.rb
|
87
|
-
- vendor/assets/.DS_Store
|
88
94
|
- vendor/assets/images/jsoneditor-icons.svg
|
89
95
|
- vendor/assets/javascripts/jsoneditor-minimalist.min.js
|
90
96
|
- vendor/assets/stylesheets/jsoneditor.min.css
|
data/docs/images/index.png
DELETED
Binary file
|
data/docs/images/show.png
DELETED
Binary file
|
data/vendor/assets/.DS_Store
DELETED
Binary file
|