administrate-field-json_array 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +89 -0
- data/administrate-field-json_array.gemspec +21 -0
- data/app/assets/javascripts/administrate-field-json_array/application.js +69 -0
- data/app/assets/stylesheets/administrate-field-json_array/application.css +12 -0
- data/app/views/fields/json_array/_form.html.erb +34 -0
- data/app/views/fields/json_array/_header.html.erb +3 -0
- data/app/views/fields/json_array/_index.html.erb +1 -0
- data/app/views/fields/json_array/_row.html.erb +14 -0
- data/app/views/fields/json_array/_show.html.erb +14 -0
- data/lib/administrate/field/json_array.rb +46 -0
- metadata +104 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 38560ba496736377787310288687799a8a963c56091035bc7d3ab9e59cbeed40
|
4
|
+
data.tar.gz: 90e15cf3dac4330f5395d93eedd265ae8c3e82997023de76a1afebf49ec73e7d
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 37b1cf2269f4d3525b22d4259304d99b258f9cd47b7594ab26355f04ece02efa74e85c8b21be6273b2b2531e413c5c08a352bb2d6f47f2e4c0631bda89c90b3d
|
7
|
+
data.tar.gz: 2f5c91a95d601de3c53e10d0ac47566d5449760b9ce41208f7a76cdd97e3ad01aa80d5714e384825917a69f082c9d8ed504068344c78807a25239fe2391d0f98
|
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Dmitry Davydov
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,89 @@
|
|
1
|
+
# Administrate::Field::JSONArray
|
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).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby
|
10
|
+
gem 'administrate-field-json_array'
|
11
|
+
```
|
12
|
+
|
13
|
+
And then execute:
|
14
|
+
|
15
|
+
```bash
|
16
|
+
bundle
|
17
|
+
```
|
18
|
+
|
19
|
+
If you are using asset pipeline, add the following lines to your `manifest.js`:
|
20
|
+
|
21
|
+
```js
|
22
|
+
//= link administrate-field-json_array/application.css
|
23
|
+
//= link administrate-field-json_array/application.js
|
24
|
+
```
|
25
|
+
|
26
|
+
The manifest file is at `app/assets/config` by default.
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
|
30
|
+
You shoud pass `schema` to describe what inputs will be generate.
|
31
|
+
Currently supported `:number`, `:text`(textarea) and `:string`.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
ATTRIBUTE_TYPES = {
|
35
|
+
# ...
|
36
|
+
details: Field::JSONArray.with_options(schema: { recipe: :number, description: :text }),
|
37
|
+
}.freeze
|
38
|
+
```
|
39
|
+
|
40
|
+
## How it looks like
|
41
|
+
|
42
|
+
### Form
|
43
|
+
|
44
|
+
<img src="docs/images/form.png">
|
45
|
+
|
46
|
+
### Show
|
47
|
+
|
48
|
+
<img src="docs/images/show.png">
|
49
|
+
|
50
|
+
## Recipes
|
51
|
+
|
52
|
+
### Store in jsonb
|
53
|
+
|
54
|
+
If you want to store your JSON in hash format and not a string add this to your model.
|
55
|
+
|
56
|
+
```ruby
|
57
|
+
def your_field_name=(value)
|
58
|
+
self[:your_field_name] = value.is_a?(String) ? JSON.parse(value) : value
|
59
|
+
end
|
60
|
+
```
|
61
|
+
|
62
|
+
Example:
|
63
|
+
|
64
|
+
```ruby
|
65
|
+
def details=(value)
|
66
|
+
self[:details] = value.is_a?(String) ? JSON.parse(value) : value
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
### I18n
|
71
|
+
|
72
|
+
You can translate table headers like this
|
73
|
+
|
74
|
+
```yaml
|
75
|
+
ru:
|
76
|
+
activerecord:
|
77
|
+
attributes:
|
78
|
+
your_model:
|
79
|
+
recipe_field:
|
80
|
+
add: Добавить строку
|
81
|
+
keys:
|
82
|
+
rank: Номер
|
83
|
+
description: Описание
|
84
|
+
```
|
85
|
+
|
86
|
+
|
87
|
+
## License
|
88
|
+
|
89
|
+
[MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,21 @@
|
|
1
|
+
$LOAD_PATH.push File.expand_path('lib', __dir__)
|
2
|
+
|
3
|
+
Gem::Specification.new do |gem|
|
4
|
+
gem.name = 'administrate-field-json_array'
|
5
|
+
gem.version = '0.1.1'
|
6
|
+
gem.authors = ['Dmitry Davydov']
|
7
|
+
gem.email = ['haudvd@gmail.com']
|
8
|
+
gem.homepage = 'https://github.com/haukot/administrate-field-json_array'
|
9
|
+
gem.summary = 'Field plugin for Administrate to edit JSON array as table of inputs'
|
10
|
+
gem.description = gem.summary
|
11
|
+
gem.license = 'MIT'
|
12
|
+
|
13
|
+
gem.require_paths = ['lib']
|
14
|
+
gem.files = `git ls-files`.split("\n")
|
15
|
+
gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
16
|
+
|
17
|
+
gem.add_runtime_dependency 'administrate', '< 1.0.0'
|
18
|
+
gem.add_runtime_dependency 'rails', '>= 4.2', '< 7'
|
19
|
+
|
20
|
+
gem.add_development_dependency 'rspec', '~> 3.7'
|
21
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
// This is a manifest file that'll be compiled into application.js, which will include all the files
|
2
|
+
// listed below.
|
3
|
+
//
|
4
|
+
// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
|
5
|
+
// or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
|
6
|
+
//
|
7
|
+
// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
|
8
|
+
// the compiled file.
|
9
|
+
//
|
10
|
+
// WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
|
11
|
+
// GO AFTER THE REQUIRES BELOW.
|
12
|
+
//
|
13
|
+
//= require_tree .
|
14
|
+
|
15
|
+
$(function() {
|
16
|
+
function processJsonArrayData($field, callback) {
|
17
|
+
var $base = $field.parents('.json_array_field__base')
|
18
|
+
var $dataField = $base.find('.json_array_field__data_field')
|
19
|
+
var fieldValue = $dataField.val()
|
20
|
+
var parsed = JSON.parse(fieldValue)
|
21
|
+
|
22
|
+
var inputKey = $field.data('key')
|
23
|
+
var index = $field.parents('tr').index() - 1 // 1 - because template row is first
|
24
|
+
|
25
|
+
callback(parsed, index, inputKey)
|
26
|
+
|
27
|
+
$dataField.val(JSON.stringify(parsed))
|
28
|
+
}
|
29
|
+
|
30
|
+
// Json array field interactions
|
31
|
+
$(document).on('click', '.json_array_field__remove_row', function() {
|
32
|
+
var $field = $(this)
|
33
|
+
processJsonArrayData($field, (data, index) => {
|
34
|
+
data.splice(index, 1)
|
35
|
+
})
|
36
|
+
|
37
|
+
$field.parents('tr').remove()
|
38
|
+
|
39
|
+
return false
|
40
|
+
})
|
41
|
+
|
42
|
+
$(document).on('click', '.json_array_field__add_row', function(e) {
|
43
|
+
var $base = $(this).parents('.json_array_field__base');
|
44
|
+
var $templateRow = $base.find('.json_array_field__template_row')
|
45
|
+
var $field = $templateRow.find('input') // FIXME: Bad, to save interface processJsonArrayData.
|
46
|
+
var template = $templateRow.html()
|
47
|
+
$base.find('tbody').append('<tr>' + template + '</tr>')
|
48
|
+
|
49
|
+
processJsonArrayData($field, (data) => {
|
50
|
+
data.push({}) // default values?..
|
51
|
+
})
|
52
|
+
|
53
|
+
e.preventDefault()
|
54
|
+
return false
|
55
|
+
})
|
56
|
+
|
57
|
+
$(document).on('input', '.json_array_field__input', function() {
|
58
|
+
var $field = $(this)
|
59
|
+
processJsonArrayData($field, (data, index, inputKey) => {
|
60
|
+
var value = $field.val()
|
61
|
+
// TODO: more types? Maybe send a schema to js?
|
62
|
+
if ($field.attr('type') === 'number') {
|
63
|
+
value = parseFloat(value)
|
64
|
+
}
|
65
|
+
data[index][inputKey] = value
|
66
|
+
})
|
67
|
+
})
|
68
|
+
|
69
|
+
})
|
@@ -0,0 +1,34 @@
|
|
1
|
+
<div class="field-unit__label">
|
2
|
+
<%= f.label field.attribute %>
|
3
|
+
</div>
|
4
|
+
|
5
|
+
<div class="field-unit__field json_array_field__base">
|
6
|
+
<table>
|
7
|
+
<thead>
|
8
|
+
|
9
|
+
<%= render 'fields/json_array/header', field: field %>
|
10
|
+
<th></th>
|
11
|
+
|
12
|
+
</thead>
|
13
|
+
|
14
|
+
<tbody>
|
15
|
+
<tr class="json_array_field__template_row">
|
16
|
+
<%= render 'fields/json_array/row', row: {}, field: field %>
|
17
|
+
</tr>
|
18
|
+
|
19
|
+
<% field.values.each do |row| %>
|
20
|
+
<tr>
|
21
|
+
<%= render 'fields/json_array/row', row: row, field: field %>
|
22
|
+
</tr>
|
23
|
+
<% end %>
|
24
|
+
|
25
|
+
</tbody>
|
26
|
+
</table>
|
27
|
+
|
28
|
+
<button class="json_array_field__add_row" type='button'>
|
29
|
+
<%= I18n.t("activerecord.attributes.#{field.resource.class.name.underscore}.#{field.attribute.to_s}_field.add", default: 'Add') %>
|
30
|
+
</button>
|
31
|
+
|
32
|
+
|
33
|
+
<%= f.hidden_field field.attribute, value: field.values.to_json, class: 'json_array_field__data_field' %>
|
34
|
+
</div>
|
@@ -0,0 +1 @@
|
|
1
|
+
<%= field.to_s %>
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<% field.schema.each.with_index do |(k, type), key_i| %>
|
2
|
+
<td>
|
3
|
+
<%= field.input type, nil, row[k], class: 'json_array_field__input', data: { key: k } %>
|
4
|
+
</td>
|
5
|
+
|
6
|
+
<% if key_i == field.schema.size - 1 %>
|
7
|
+
<td>
|
8
|
+
<a class="text-color-red json_array_field__remove_row" href="#">
|
9
|
+
<%= I18n.t('administrate.actions.destroy') %>
|
10
|
+
</a>
|
11
|
+
</td>
|
12
|
+
<% end %>
|
13
|
+
|
14
|
+
<% end %>
|
@@ -0,0 +1,14 @@
|
|
1
|
+
<table>
|
2
|
+
<thead>
|
3
|
+
<%= render 'fields/json_array/header', field: field %>
|
4
|
+
</thead>
|
5
|
+
<tbody>
|
6
|
+
<% field.values.each do |row| %>
|
7
|
+
<tr>
|
8
|
+
<% field.schema.keys.each do |k| %>
|
9
|
+
<td><%= row[k] %></td>
|
10
|
+
<% end %>
|
11
|
+
</tr>
|
12
|
+
<% end %>
|
13
|
+
</tbody>
|
14
|
+
</table>
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
require 'administrate/engine'
|
3
|
+
require 'administrate/field/base'
|
4
|
+
require 'rails'
|
5
|
+
|
6
|
+
module Administrate
|
7
|
+
module Field
|
8
|
+
class JSONArray < Administrate::Field::Base
|
9
|
+
include ActionView::Helpers::FormTagHelper
|
10
|
+
|
11
|
+
def to_s
|
12
|
+
return '-' unless data
|
13
|
+
|
14
|
+
data.to_s
|
15
|
+
end
|
16
|
+
|
17
|
+
def values
|
18
|
+
return [] unless data.is_a? Array
|
19
|
+
|
20
|
+
data
|
21
|
+
end
|
22
|
+
|
23
|
+
def input(schema_type, *args)
|
24
|
+
case schema_type
|
25
|
+
when :number
|
26
|
+
number_field_tag(*args)
|
27
|
+
when :text
|
28
|
+
text_area_tag(*args)
|
29
|
+
else
|
30
|
+
text_field_tag(*args)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def schema
|
35
|
+
raise 'Schema is required' unless options || options[:schema].empty?
|
36
|
+
|
37
|
+
@_json_array_schema ||= options[:schema].map { |k, v| [k.to_s, v] }.to_h
|
38
|
+
end
|
39
|
+
|
40
|
+
class Engine < ::Rails::Engine
|
41
|
+
Administrate::Engine.add_javascript 'administrate-field-json_array/application'
|
42
|
+
Administrate::Engine.add_stylesheet 'administrate-field-json_array/application'
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
metadata
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: administrate-field-json_array
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dmitry Davydov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-03-09 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: administrate
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "<"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 1.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "<"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 1.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rails
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '4.2'
|
34
|
+
- - "<"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '7'
|
37
|
+
type: :runtime
|
38
|
+
prerelease: false
|
39
|
+
version_requirements: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '4.2'
|
44
|
+
- - "<"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '7'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '3.7'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '3.7'
|
61
|
+
description: Field plugin for Administrate to edit JSON array as table of inputs
|
62
|
+
email:
|
63
|
+
- haudvd@gmail.com
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- Gemfile
|
69
|
+
- LICENSE
|
70
|
+
- README.md
|
71
|
+
- administrate-field-json_array.gemspec
|
72
|
+
- app/assets/javascripts/administrate-field-json_array/application.js
|
73
|
+
- app/assets/stylesheets/administrate-field-json_array/application.css
|
74
|
+
- app/views/fields/json_array/_form.html.erb
|
75
|
+
- app/views/fields/json_array/_header.html.erb
|
76
|
+
- app/views/fields/json_array/_index.html.erb
|
77
|
+
- app/views/fields/json_array/_row.html.erb
|
78
|
+
- app/views/fields/json_array/_show.html.erb
|
79
|
+
- lib/administrate/field/json_array.rb
|
80
|
+
homepage: https://github.com/haukot/administrate-field-json_array
|
81
|
+
licenses:
|
82
|
+
- MIT
|
83
|
+
metadata: {}
|
84
|
+
post_install_message:
|
85
|
+
rdoc_options: []
|
86
|
+
require_paths:
|
87
|
+
- lib
|
88
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
89
|
+
requirements:
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: '0'
|
93
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
94
|
+
requirements:
|
95
|
+
- - ">="
|
96
|
+
- !ruby/object:Gem::Version
|
97
|
+
version: '0'
|
98
|
+
requirements: []
|
99
|
+
rubyforge_project:
|
100
|
+
rubygems_version: 2.7.8
|
101
|
+
signing_key:
|
102
|
+
specification_version: 4
|
103
|
+
summary: Field plugin for Administrate to edit JSON array as table of inputs
|
104
|
+
test_files: []
|