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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 74b6d21a2f93c23b057e873aaad220655c3c817d
4
- data.tar.gz: 606946be845f50d6b98b5553c5148c812ddad650
3
+ metadata.gz: d7434f7a732965280d6da5120b27bbdb159387f6
4
+ data.tar.gz: 75a9f459a5f3f84c115986436ca8b8b84a14dc3a
5
5
  SHA512:
6
- metadata.gz: 3eba72e6e53ec34666eb55b23884d9a9f41f41ed112e82fbb06e16c3d1e6c8083474d476e11b4800b2ed10e09b10788d61724b277178cce6b03837ecadc7fdbf
7
- data.tar.gz: 22faae00b2abff4c56d1a4e538ddb4a7213c0d83831e2a2987bf0c4ecc7dce6ac79243b6768131d1a88b76113f462cbd9b2ad79042e7c1a39770c3a9a4305826
6
+ metadata.gz: ae8a41eac399b3d9a60c4e852688ac481c8a78a79b7117e35e339dcc687f9aaca05a9c2ec9e514838a970b169d7397bc026442ea865c1127ebc6bc7d5a5d73b0
7
+ data.tar.gz: 94e48d3d6dcf04e7b409cfe8381500f45c8525af0f37aa703df4567011d0916697a30c3ff27284b59e3dbd0459ae8180b48051e5180f86489f3f5cc671a14152
data/.gitignore CHANGED
@@ -1,3 +1,9 @@
1
+ !.keep
2
+ *.DS_Store
3
+ *.swo
4
+ *.swp
5
+ /.bundle
6
+ /.env
1
7
  /.bundle/
2
8
  /.yardoc
3
9
  /_yardoc/
@@ -6,6 +12,6 @@
6
12
  /pkg/
7
13
  /spec/reports/
8
14
  /tmp/
9
-
10
- # rspec failure tracking
15
+ /.idea
16
+ .byebug_history
11
17
  .rspec_status
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
- data: Field::JSONB
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
- data: Field::JSONB.with_options(
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/show.png">
76
+ <img src="docs/images/show_jsoneditor.png">
53
77
  </p>
54
78
 
55
- ### Index
79
+ #### advanced_view mode
56
80
 
57
81
  <p align="center">
58
- <img src="docs/images/index.png">
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.1.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'
@@ -11,4 +11,4 @@
11
11
  // GO AFTER THE REQUIRES BELOW.
12
12
  //
13
13
  //= require jsoneditor-minimalist.min
14
- //= require_tree .
14
+ //= require_tree .
@@ -0,0 +1,7 @@
1
+ $(function () {
2
+ $(".administrate-field-jsonb-accordion").each(function () {
3
+ $(this).click(function() {
4
+ $(this).toggleClass("administrate-field-jsonb-active").next().toggle();
5
+ });
6
+ });
7
+ });
@@ -29,4 +29,4 @@ $(function () {
29
29
 
30
30
  editor.set(JSON.parse($current.val()));
31
31
  });
32
- });
32
+ });
@@ -17,4 +17,4 @@ $(function () {
17
17
 
18
18
  viewer.set(JSON.parse($current.val()));
19
19
  });
20
- });
20
+ });
@@ -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
+ }
@@ -2,4 +2,4 @@
2
2
  *= require_self
3
3
  *= require jsoneditor.min
4
4
  *= require_tree .
5
- */
5
+ */
@@ -21,4 +21,4 @@ textarea.jsoneditor-text {
21
21
 
22
22
  table.jsoneditor-values tbody > tr {
23
23
  border: none;
24
- }
24
+ }
@@ -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 %>
@@ -0,0 +1,9 @@
1
+ <% field = type.new(key, value, :show) %>
2
+ <dt class='attribute-label'><%= t(
3
+ "helpers.label.#{resource_name}.#{key}",
4
+ default: key.to_s.titleize,
5
+ ) %>
6
+ </dt>
7
+ <dd class='attribute-data'>
8
+ <%= render_field field, page: :show %>
9
+ </dd>
@@ -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.data.to_json.html_safe.squish %>
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
- <div class="administrate-jsoneditor-viewer">
2
- <%= text_area_tag field.attribute, field.transform.to_json.html_safe.squish %>
3
- </div>
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
@@ -13,11 +13,27 @@ module Administrate
13
13
 
14
14
  result = data
15
15
  options[:transform].each do |method|
16
- result = data.public_send(method)
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.1.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-09-27 00:00:00.000000000 Z
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/index.png
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
Binary file
Binary file
Binary file