administrate-field-jsonb 0.1.0 → 0.4.2

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
- SHA1:
3
- metadata.gz: 74b6d21a2f93c23b057e873aaad220655c3c817d
4
- data.tar.gz: 606946be845f50d6b98b5553c5148c812ddad650
2
+ SHA256:
3
+ metadata.gz: '00360160780ad78be533891d768702812fbb1dde867179a3e29ec4a5c642fa0a'
4
+ data.tar.gz: 488b361fffa40de1517a2ca7c9e9b526b02212b54bef930faf4463b37d3c86f4
5
5
  SHA512:
6
- metadata.gz: 3eba72e6e53ec34666eb55b23884d9a9f41f41ed112e82fbb06e16c3d1e6c8083474d476e11b4800b2ed10e09b10788d61724b277178cce6b03837ecadc7fdbf
7
- data.tar.gz: 22faae00b2abff4c56d1a4e538ddb4a7213c0d83831e2a2987bf0c4ecc7dce6ac79243b6768131d1a88b76113f462cbd9b2ad79042e7c1a39770c3a9a4305826
6
+ metadata.gz: c987c1cb1269330ccec85f0ddd61562bc8e7bb1f68ad9bb643475a9e127082ff2e676b1e9608df901c0961316b1b69e501571f300e102eaf96579eeefac8241f
7
+ data.tar.gz: 7b864d81e23944aa57e9fd7330877610d582b36732d0d41df3ea21144a70bd6d6bd7e74936a815033e0025fc54549653f8880244c340a406f8cfaabce61c2a7f
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
 
@@ -18,12 +18,21 @@ And then execute:
18
18
  bundle
19
19
  ```
20
20
 
21
+ If you are using asset pipeline, add the following lines to your `manifest.js`:
22
+
23
+ ```js
24
+ //= link administrate-field-jsonb/application.css
25
+ //= link administrate-field-jsonb/application.js
26
+ ```
27
+
28
+ The manifest file is at `app/assets/config` by default.
29
+
21
30
  ## Usage
22
31
 
23
32
  ```ruby
24
33
  ATTRIBUTE_TYPES = {
25
34
  # ...
26
- data: Field::JSONB
35
+ details: Field::JSONB
27
36
  }.freeze
28
37
  ```
29
38
 
@@ -32,8 +41,41 @@ If you have some kind of serialization, you can call methods on your object with
32
41
  ```ruby
33
42
  ATTRIBUTE_TYPES = {
34
43
  # ...
35
- data: Field::JSONB.with_options(
36
- transform: %w[to_h]
44
+ details: Field::JSONB.with_options(
45
+ transform: %w[to_h symbolize_keys]
46
+ )
47
+ }.freeze
48
+ ```
49
+
50
+ If you want to edit json displaying on `show` page, you can use `advanced_view` option (both JSON and arrays are supported).
51
+
52
+ ```ruby
53
+ ATTRIBUTE_TYPES = {
54
+ # ...
55
+ details: Field::JSONB.with_options(transform: %i[to_h symbolize_keys], advanced_view: {
56
+ company: Field::String,
57
+ position: Field::String,
58
+ skills: Field::JSONB.with_options(advanced_view: {
59
+ 'name' => Field::String,
60
+ 'years' => Field::Number.with_options(suffix: ' years')
61
+ })
62
+ }),
63
+ languages: Field::JSONB.with_options(advanced_view: {
64
+ 'title' => Field::String,
65
+ 'code' => Field::String,
66
+ })
67
+ }.freeze
68
+ ```
69
+
70
+ NOTE: don't define `advanced_view` option if you want to display JSON with the [jsoneditor](https://github.com/josdejong/jsoneditor).
71
+
72
+ To customize what to display if you have an empty value, use `blank_sign` option, by default it's `-`.
73
+
74
+ ```ruby
75
+ ATTRIBUTE_TYPES = {
76
+ # ...
77
+ details: Field::JSONB.with_options(
78
+ blank_sign: 'oops, missed'
37
79
  )
38
80
  }.freeze
39
81
  ```
@@ -48,16 +90,65 @@ ATTRIBUTE_TYPES = {
48
90
 
49
91
  ### Show
50
92
 
93
+ #### jsoneditor mode
94
+
51
95
  <p align="center">
52
- <img src="docs/images/show.png">
96
+ <img src="docs/images/show_jsoneditor.png">
53
97
  </p>
54
98
 
55
- ### Index
99
+ #### advanced_view mode
56
100
 
57
101
  <p align="center">
58
- <img src="docs/images/index.png">
102
+ <img src="docs/images/advanced_view_full.png">
59
103
  </p>
60
104
 
105
+ #### advanced_view object
106
+
107
+ <p align="center">
108
+ <img src="docs/images/advanced_view_object.png">
109
+ </p>
110
+
111
+ #### advanced_view array
112
+
113
+ <p align="center">
114
+ <img src="docs/images/advanced_view_array.png">
115
+ </p>
116
+
117
+ ## Recipes
118
+
119
+ If you want to store your JSON in hash format and not a string add this to your model.
120
+
121
+ ```ruby
122
+ def your_field_name=(value)
123
+ self[:your_field_name] = value.is_a?(String) ? JSON.parse(value) : value
124
+ end
125
+ ```
126
+
127
+ Example:
128
+
129
+ ```ruby
130
+ def details=(value)
131
+ self[:details] = value.is_a?(String) ? JSON.parse(value) : value
132
+ end
133
+ ```
134
+
135
+ <hr>
136
+
137
+ If you don't see details in `advanced_view`, try to add this
138
+
139
+ ```ruby
140
+ transform: %i[to_h symbolize_keys]
141
+ ```
142
+
143
+ or use string keys.
144
+
145
+ ```ruby
146
+ languages: Field::JSONB.with_options(advanced_view: {
147
+ 'title' => Field::String,
148
+ 'code' => Field::String,
149
+ })
150
+ ```
151
+
61
152
  ## License
62
153
 
63
154
  Copyright © 2015-2019 Codica. It is released under the [MIT License](https://opensource.org/licenses/MIT).
@@ -66,6 +157,6 @@ Copyright © 2015-2019 Codica. It is released under the [MIT License](https://op
66
157
 
67
158
  [![Codica logo](https://www.codica.com/assets/images/logo/logo.svg)](https://www.codica.com)
68
159
 
69
- Timebot is maintained and funded by Codica. The names and logos for Codica are trademarks of Codica.
160
+ Administrate::Field::Jsonb is maintained by Codica. The names and logos for Codica are trademarks of Codica.
70
161
 
71
162
  We love open source software! See [our other projects](https://github.com/codica2) or [hire us](https://www.codica.com/) to design, develop, and grow your product.
@@ -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.4.2'
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
+ $(document).on(typeof Turbolinks === 'undefined' ? 'ready' : 'turbolinks:load', function() {
2
+ $(".administrate-field-jsonb-accordion").each(function() {
3
+ $(this).click(function() {
4
+ $(this).toggleClass("administrate-field-jsonb-active").next().toggle();
5
+ });
6
+ });
7
+ });
@@ -1,6 +1,6 @@
1
- $(function () {
1
+ $(document).on(typeof Turbolinks === 'undefined' ? 'ready' : 'turbolinks:load', function() {
2
2
  let editor, updatedJson;
3
- $('.administrate-jsoneditor').each(function (index) {
3
+ $('.administrate-jsoneditor').each(function(index) {
4
4
 
5
5
  let $current = $(this).find("textarea");
6
6
 
@@ -14,7 +14,7 @@ $(function () {
14
14
 
15
15
  $current.val(JSON.stringify(updatedJson));
16
16
  },
17
- onError: function (err) {
17
+ onError: function(err) {
18
18
  alert(err.toString());
19
19
  },
20
20
  navigationBar: false,
@@ -29,4 +29,4 @@ $(function () {
29
29
 
30
30
  editor.set(JSON.parse($current.val()));
31
31
  });
32
- });
32
+ });
@@ -1,4 +1,4 @@
1
- $(function () {
1
+ $(document).on(typeof Turbolinks === 'undefined' ? 'ready' : 'turbolinks:load', function() {
2
2
  let viewer;
3
3
  $('.administrate-jsoneditor-viewer').each(function (index) {
4
4
 
@@ -17,4 +17,4 @@ $(function () {
17
17
 
18
18
  viewer.set(JSON.parse($current.val()));
19
19
  });
20
- });
20
+ });
@@ -0,0 +1,54 @@
1
+ .administrate-field-jsonb-wrapper {
2
+ float: left;
3
+ width: 100%;
4
+ margin-top: 0;
5
+ border: 1px solid #ccc;
6
+ padding: 10px;
7
+ }
8
+
9
+ .administrate-field-jsonb-box {
10
+ margin-bottom: 0;
11
+ float: left;
12
+ width: 100%;
13
+ }
14
+
15
+ .administrate-field-jsonb-accordion {
16
+ width: 100%;
17
+ text-align: left;
18
+ padding: 5px 15px;
19
+ background-color: #eee;
20
+ color: #444;
21
+ cursor: pointer;
22
+ outline: none;
23
+ transition: 0.4s;
24
+ margin-bottom: 0;
25
+ margin-top: 15px;
26
+ }
27
+
28
+ .administrate-field-jsonb-accordion:first-child {
29
+ margin-top: 0;
30
+ }
31
+
32
+ .administrate-field-jsonb-active, .administrate-field-jsonb-accordion:hover {
33
+ background-color: #ccc;
34
+ }
35
+
36
+ .administrate-field-jsonb-panel {
37
+ padding: 0 18px 15px;
38
+ background-color: white;
39
+ display: none;
40
+ overflow: hidden;
41
+ border: 1px solid #ccc;
42
+ }
43
+
44
+ .administrate-field-jsonb-accordion:after {
45
+ content: '\02795'; /* Unicode character for "plus" sign (+) */
46
+ font-size: 13px;
47
+ color: #777;
48
+ float: right;
49
+ margin-left: 5px;
50
+ }
51
+
52
+ .administrate-field-jsonb-active:after {
53
+ content: "\2796"; /* Unicode character for "minus" sign (-) */
54
+ }
@@ -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,10 @@
1
+ <% label ||= nil %>
2
+
3
+ <% data.each.with_index(1) do |d, index| %>
4
+ <dt class='attribute-label administrate-field-jsonb-accordion'><%= "#{label || field.attribute.to_s.singularize.titleize} - #{index}" %></dt>
5
+ <dd class='administrate-field-jsonb-panel'>
6
+ <dl class='administrate-field-jsonb-box'>
7
+ <%= render field.to_partial_path('hash'), field: field, structure: structure, data: d %>
8
+ <dl>
9
+ </dd>
10
+ <% 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,17 @@
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 class='administrate-field-jsonb-box'>
6
+ <% if data[sk].is_a?(Array) %>
7
+ <%= render field.to_partial_path('array'),
8
+ field: field, structure: structure[sk], data: data[sk], label: sk.to_s.singularize.titleize %>
9
+ <% else %>
10
+ <%= render field.to_partial_path('hash'), field: field, structure: structure[sk], data: data[sk] %>
11
+ <% end %>
12
+ <dl>
13
+ </dd>
14
+ <% else %>
15
+ <%= render field.to_partial_path('field'), type: sv, key: sk, value: data[sk] %>
16
+ <% end %>
17
+ <% end %>
@@ -1 +1 @@
1
- <%= JSON.pretty_generate field.transform %>
1
+ <%= JSON.pretty_generate field.transform %>
@@ -1,3 +1,15 @@
1
- <div class="administrate-jsoneditor-viewer">
2
- <%= text_area_tag field.attribute, field.transform.to_json.html_safe.squish %>
3
- </div>
1
+ <% if field.transform.blank? %>
2
+ <%= field.blank_sign %>
3
+ <% elsif field.advanced_view? %>
4
+ <dl class='administrate-field-jsonb-wrapper'>
5
+ <% if field.array? %>
6
+ <%= render field.to_partial_path('array'), field: field, structure: field.advanced_view, data: field.transform %>
7
+ <% else %>
8
+ <%= render field.to_partial_path('hash'), field: field, structure: field.advanced_view, data: field.transform %>
9
+ <% end %>
10
+ </dl>
11
+ <% else %>
12
+ <div class='administrate-jsoneditor-viewer'>
13
+ <p><%= text_area_tag field.attribute, field.transform.to_json.html_safe.squish %></p>
14
+ </div>
15
+ <% end %>
Binary file
@@ -9,15 +9,36 @@ module Administrate
9
9
  class JSONB < Administrate::Field::Base
10
10
 
11
11
  def transform
12
+ return nil if data.blank?
12
13
  return data unless options[:transform].present? && options[:transform].is_a?(Array)
13
14
 
14
15
  result = data
15
16
  options[:transform].each do |method|
16
- result = data.public_send(method)
17
+ result = result.is_a?(Array) ? result.map(&method) : result.public_send(method)
17
18
  end
18
19
  result
19
20
  end
20
21
 
22
+ def array?
23
+ transform.is_a?(Array)
24
+ end
25
+
26
+ def advanced_view?
27
+ advanced_view.present? && advanced_view.is_a?(Hash)
28
+ end
29
+
30
+ def advanced_view
31
+ options[:advanced_view]
32
+ end
33
+
34
+ def to_partial_path(partial = page)
35
+ "/fields/jsonb/#{partial}"
36
+ end
37
+
38
+ def blank_sign
39
+ options[:blank_sign] || '-'
40
+ end
41
+
21
42
  class Engine < ::Rails::Engine
22
43
 
23
44
  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.4.2
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: 2020-08-03 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
@@ -107,8 +113,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
113
  - !ruby/object:Gem::Version
108
114
  version: '0'
109
115
  requirements: []
110
- rubyforge_project:
111
- rubygems_version: 2.6.14
116
+ rubygems_version: 3.0.3
112
117
  signing_key:
113
118
  specification_version: 4
114
119
  summary: JSONb field plugin for Administrate
Binary file
Binary file
Binary file