administrate-field-jsontable 0.0.1 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +5 -5
  2. data/.gitignore +7 -0
  3. data/.rubocop.yml +96 -0
  4. data/Gemfile +9 -1
  5. data/Gemfile.lock +153 -118
  6. data/LICENSE.md +1 -1
  7. data/README.md +11 -23
  8. data/Rakefile +11 -0
  9. data/administrate-field-jsontable.gemspec +8 -6
  10. data/app/views/fields/jsontable/_index.html.erb +18 -0
  11. data/app/views/fields/jsontable/_json_table_field.html.erb +30 -0
  12. data/app/views/fields/jsontable/_show.html.erb +6 -33
  13. data/lib/administrate/field/jsontable.rb +2 -0
  14. data/spec/example_app/app/assets/config/manifest.js +2 -0
  15. data/spec/example_app/app/assets/javascripts/application.js +15 -0
  16. data/spec/example_app/app/assets/stylesheets/application.css +15 -0
  17. data/spec/example_app/app/controllers/application_controller.rb +7 -0
  18. data/spec/example_app/app/models/application_record.rb +5 -0
  19. data/spec/example_app/app/views/layouts/application.html.erb +31 -0
  20. data/spec/example_app/app/views/pages/.keep +0 -0
  21. data/spec/example_app/config.ru +6 -0
  22. data/spec/example_app/config/application.rb +39 -0
  23. data/spec/example_app/config/boot.rb +4 -0
  24. data/spec/example_app/config/database.yml +11 -0
  25. data/spec/example_app/config/environment.rb +7 -0
  26. data/spec/example_app/config/environments/development.rb +39 -0
  27. data/spec/example_app/config/environments/production.rb +78 -0
  28. data/spec/example_app/config/environments/staging.rb +3 -0
  29. data/spec/example_app/config/environments/test.rb +43 -0
  30. data/spec/example_app/config/initializers/assets.rb +13 -0
  31. data/spec/example_app/config/initializers/backtrace_silencers.rb +8 -0
  32. data/spec/example_app/config/initializers/cookies_serializer.rb +5 -0
  33. data/spec/example_app/config/initializers/disable_xml_params.rb +5 -0
  34. data/spec/example_app/config/initializers/errors.rb +20 -0
  35. data/spec/example_app/config/initializers/filter_parameter_logging.rb +6 -0
  36. data/spec/example_app/config/initializers/inflections.rb +18 -0
  37. data/spec/example_app/config/initializers/json_encoding.rb +3 -0
  38. data/spec/example_app/config/initializers/mime_types.rb +5 -0
  39. data/spec/example_app/config/initializers/session_store.rb +5 -0
  40. data/spec/example_app/config/initializers/wrap_parameters.rb +16 -0
  41. data/spec/example_app/config/routes.rb +4 -0
  42. data/spec/example_app/config/secrets.yml +15 -0
  43. data/spec/example_app/db/schema.rb +18 -0
  44. data/spec/example_app/db/seeds.rb +8 -0
  45. data/spec/example_app/public/robots.txt +5 -0
  46. data/spec/fixtures/jsontable/array_contains_hash.html +1 -0
  47. data/spec/fixtures/jsontable/hash_contains_array.html +31 -0
  48. data/spec/fixtures/jsontable/pure_hash.html +58 -0
  49. data/spec/lib/administrate/field/jsontable_index_spec.rb +95 -0
  50. data/spec/lib/administrate/field/jsontable_show_spec.rb +95 -0
  51. data/spec/lib/administrate/field/jsontable_spec.rb +16 -5
  52. data/spec/rails_helper.rb +6 -0
  53. data/spec/support/read_fixture.rb +7 -0
  54. metadata +93 -17
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'production'
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ # The test environment is used exclusively to run your application's
4
+ # test suite. You never need to work with it otherwise. Remember that
5
+ # your test database is "scratch space" for the test suite and is wiped
6
+ # and recreated between test runs. Don't rely on the data there!
7
+
8
+ Rails.application.configure do
9
+ config.cache_classes = true
10
+
11
+ # Do not eager load code on boot. This avoids loading your whole application
12
+ # just for the purpose of running a single test. If you are using a tool that
13
+ # preloads Rails for running tests, you may have to set it to true.
14
+ config.eager_load = false
15
+
16
+ # Configure public file server for tests with Cache-Control for performance.
17
+ if config.respond_to?(:public_file_server)
18
+ config.public_file_server.enabled = true
19
+ config.public_file_server.headers = {
20
+ 'Cache-Control' => 'public, max-age=3600'
21
+ }
22
+ else
23
+ config.serve_static_files = true
24
+ config.static_cache_control = 'public, max-age=3600'
25
+ end
26
+
27
+ # Show full error reports and disable caching.
28
+ config.consider_all_requests_local = true
29
+ config.action_controller.perform_caching = false
30
+ config.cache_store = :null_store
31
+
32
+ # Raise exceptions instead of rendering exception templates.
33
+ config.action_dispatch.show_exceptions = false
34
+
35
+ # Disable request forgery protection in test environment.
36
+ config.action_controller.allow_forgery_protection = false
37
+
38
+ # Print deprecation notices to the stderr.
39
+ config.active_support.deprecation = :stderr
40
+
41
+ # Raises error for missing translations.
42
+ config.action_view.raise_on_missing_translations = true
43
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Be sure to restart your server when you modify this file.
4
+
5
+ # Version of your assets, change this if you want to expire all your assets.
6
+ Rails.application.config.assets.version = (ENV['ASSETS_VERSION'] || '1.0')
7
+
8
+ # Add additional assets to the asset load path
9
+ # Rails.application.config.assets.paths << Emoji.images_path
10
+
11
+ # Precompile additional assets.
12
+ # application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
13
+ Rails.application.config.assets.precompile += %w[docs.css]
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+ # Be sure to restart your server when you modify this file.
3
+
4
+ # You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces.
5
+ # Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ }
6
+
7
+ # You can also remove all the silencers if you're trying to debug a problem that might stem from framework code.
8
+ # Rails.backtrace_cleaner.remove_silencers!
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Be sure to restart your server when you modify this file.
4
+
5
+ Rails.application.config.action_dispatch.cookies_serializer = :marshal
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Protect against injection attacks
4
+ # http://www.kb.cert.org/vuls/id/380039
5
+ ActionDispatch::ParamsParser::DEFAULT_PARSERS.delete(Mime::XML) if Rails::VERSION::MAJOR < 5
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+
5
+ # Example:
6
+ # begin
7
+ # some http call
8
+ # rescue *HTTP_ERRORS => error
9
+ # notify_hoptoad error
10
+ # end
11
+
12
+ HTTP_ERRORS = [
13
+ EOFError,
14
+ Errno::ECONNRESET,
15
+ Errno::EINVAL,
16
+ Net::HTTPBadResponse,
17
+ Net::HTTPHeaderSyntaxError,
18
+ Net::ProtocolError,
19
+ Timeout::Error
20
+ ].freeze
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Be sure to restart your server when you modify this file.
4
+
5
+ # Configure sensitive parameters which will be filtered from the log file.
6
+ Rails.application.config.filter_parameters += [:password]
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Be sure to restart your server when you modify this file.
4
+
5
+ # Add new inflection rules using the following format. Inflections
6
+ # are locale specific, and you may define rules for as many different
7
+ # locales as you wish. All of these examples are active by default:
8
+ ActiveSupport::Inflector.inflections(:en) do |inflect|
9
+ # inflect.plural /^(ox)$/i, '\1en'
10
+ # inflect.singular /^(ox)en/i, '\1'
11
+ # inflect.irregular 'person', 'people'
12
+ inflect.uncountable %w[series]
13
+ end
14
+
15
+ # These inflection rules are supported but not enabled by default:
16
+ # ActiveSupport::Inflector.inflections(:en) do |inflect|
17
+ # inflect.acronym 'RESTful'
18
+ # end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ ActiveSupport::JSON::Encoding.time_precision = 0
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+ # Be sure to restart your server when you modify this file.
3
+
4
+ # Add new mime types for use in respond_to blocks:
5
+ # Mime::Type.register "text/richtext", :rtf
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Be sure to restart your server when you modify this file.
4
+
5
+ Rails.application.config.session_store :cookie_store, key: '_administrate-field-json-to-table'
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Be sure to restart your server when you modify this file.
4
+
5
+ # This file contains settings for ActionController::ParamsWrapper which
6
+ # is enabled by default.
7
+
8
+ # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
9
+ ActiveSupport.on_load(:action_controller) do
10
+ wrap_parameters format: [:json]
11
+ end
12
+
13
+ # To enable root element in JSON for ActiveRecord objects.
14
+ # ActiveSupport.on_load(:active_record) do
15
+ # self.include_root_in_json = true
16
+ # end
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ Rails.application.routes.draw do
4
+ end
@@ -0,0 +1,15 @@
1
+ default: &default
2
+ secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>
3
+
4
+ development:
5
+ <<: *default
6
+ secret_key_base: 1a022e4f335d24af3d6bd622b9daef5a44808afbe16d4f1c8bed03675ab91ecd9c76ddc1a30f3fbb668b02c00abcba2ecc3714c95943b8f4af86289a2a46d5e1
7
+
8
+ test:
9
+ secret_key_base: test_secret
10
+
11
+ staging:
12
+ <<: *default
13
+
14
+ production:
15
+ <<: *default
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is auto-generated from the current state of the database. Instead
4
+ # of editing this file, please use the migrations feature of Active Record to
5
+ # incrementally modify your database, and then regenerate this schema definition.
6
+ #
7
+ # This file is the source Rails uses to define your schema when running `rails
8
+ # db:schema:load`. When creating a new database, `rails db:schema:load` tends to
9
+ # be faster and is potentially less error prone than running all of your
10
+ # migrations from scratch. Old migrations may fail to apply correctly if those
11
+ # migrations use external dependencies or application code.
12
+ #
13
+ # It's strongly recommended that you check this file into your version control system.
14
+
15
+ ActiveRecord::Schema.define(version: 20_200_326_202_615) do
16
+ # These are extensions that must be enabled in order to support this database
17
+ enable_extension 'plpgsql'
18
+ end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+ # This file should contain all the record creation needed to seed the database with its default values.
3
+ # The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
4
+ #
5
+ # Examples:
6
+ #
7
+ # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
8
+ # Mayor.create(name: 'Emanuel', city: cities.first)
@@ -0,0 +1,5 @@
1
+ # See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
2
+ #
3
+ # To ban all spiders from the entire site uncomment the next two lines:
4
+ # User-agent: *
5
+ # Disallow: /
@@ -0,0 +1 @@
1
+ [1, 2, 3, {:&quot;2019&quot;=&gt;{:start_date=&gt;&quot;2019-01-01&quot;, :end_date=&gt;&quot;2019-12-31&quot;, :id=&gt;&quot;123456_2019&quot;}}]
@@ -0,0 +1,31 @@
1
+ <table>
2
+ <tbody>
3
+ <tr>
4
+ <td>2019</td>
5
+ <td>
6
+ <table>
7
+ <tbody>
8
+ <tr>
9
+ <td>ids</td>
10
+ <td>
11
+ [1, 2, 3, 4]
12
+ </td>
13
+ </tr>
14
+ <tr>
15
+ <td>end_date</td>
16
+ <td>
17
+ 2019-12-31
18
+ </td>
19
+ </tr>
20
+ <tr>
21
+ <td>id</td>
22
+ <td>
23
+ 123456_2019
24
+ </td>
25
+ </tr>
26
+ <tbody>
27
+ </table>
28
+ </td>
29
+ </tr>
30
+ <tbody>
31
+ </table>
@@ -0,0 +1,58 @@
1
+ <table>
2
+ <tbody>
3
+ <tr>
4
+ <td>2019</td>
5
+ <td>
6
+ <table>
7
+ <tbody>
8
+ <tr>
9
+ <td>start_date</td>
10
+ <td>
11
+ 2019-01-01
12
+ </td>
13
+ </tr>
14
+ <tr>
15
+ <td>end_date</td>
16
+ <td>
17
+ 2019-12-31
18
+ </td>
19
+ </tr>
20
+ <tr>
21
+ <td>id</td>
22
+ <td>
23
+ 123456_2019
24
+ </td>
25
+ </tr>
26
+ <tbody>
27
+ </table>
28
+ </td>
29
+ </tr>
30
+ <tr>
31
+ <td>2020</td>
32
+ <td>
33
+ <table>
34
+ <tbody>
35
+ <tr>
36
+ <td>start_date</td>
37
+ <td>
38
+ 2020-01-01
39
+ </td>
40
+ </tr>
41
+ <tr>
42
+ <td>end_date</td>
43
+ <td>
44
+ 2020-12-31
45
+ </td>
46
+ </tr>
47
+ <tr>
48
+ <td>id</td>
49
+ <td>
50
+ 123456_2020
51
+ </td>
52
+ </tr>
53
+ <tbody>
54
+ </table>
55
+ </td>
56
+ </tr>
57
+ <tbody>
58
+ </table>
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'administrate/field/jsontable'
4
+ require File.expand_path('../../../support/read_fixture', __dir__)
5
+ require 'rails_helper'
6
+
7
+ describe 'fields/jsontable/_index', type: :view do
8
+ include ReadFixture
9
+
10
+ let(:pure_hash) do
11
+ instance_double(
12
+ 'Administrate::Field::Jsontable',
13
+ data: {
14
+ "2019": {
15
+ "start_date": '2019-01-01',
16
+ "end_date": '2019-12-31',
17
+ "id": '123456_2019'
18
+ },
19
+ "2020": {
20
+ "start_date": '2020-01-01',
21
+ "end_date": '2020-12-31',
22
+ "id": '123456_2020'
23
+ }
24
+ }
25
+ )
26
+ end
27
+
28
+ let(:pure_array) do
29
+ instance_double(
30
+ 'Administrate::Field::Jsontable',
31
+ data: [1, 2, 3, 4]
32
+ )
33
+ end
34
+
35
+ let(:hash_contains_array) do
36
+ instance_double(
37
+ 'Administrate::Field::Jsontable',
38
+ data: {
39
+ "2019": {
40
+ "ids": [1, 2, 3, 4],
41
+ "end_date": '2019-12-31',
42
+ "id": '123456_2019'
43
+ }
44
+ }
45
+ )
46
+ end
47
+
48
+ let(:array_contains_hash) do
49
+ instance_double(
50
+ 'Administrate::Field::Jsontable',
51
+ data: [1, 2, 3, { "2019": {
52
+ "start_date": '2019-01-01',
53
+ "end_date": '2019-12-31',
54
+ "id": '123456_2019'
55
+ } }]
56
+ )
57
+ end
58
+
59
+ it 'displays pure_hash' do
60
+ allow(view).to receive(:valid_action?).and_return(true)
61
+ render_jsontable_index(field: pure_hash)
62
+
63
+ expect(rendered.strip).to eq(read_fixture('pure_hash.html'))
64
+ end
65
+
66
+ it 'displays pure_array' do
67
+ allow(view).to receive(:valid_action?).and_return(true)
68
+ render_jsontable_index(field: pure_array)
69
+
70
+ expect(rendered.strip).to eq('[1, 2, 3, 4]')
71
+ end
72
+
73
+ it 'displays hash_contains_array' do
74
+ allow(view).to receive(:valid_action?).and_return(true)
75
+ render_jsontable_index(field: hash_contains_array)
76
+
77
+ expect(rendered.strip).to eq(read_fixture('hash_contains_array.html'))
78
+ end
79
+
80
+ it 'displays array_contains_hash' do
81
+ allow(view).to receive(:valid_action?).and_return(true)
82
+ render_jsontable_index(field: array_contains_hash)
83
+
84
+ expect(rendered.strip).to eq(read_fixture('array_contains_hash.html'))
85
+ end
86
+
87
+ private
88
+
89
+ def render_jsontable_index(field:)
90
+ render(
91
+ partial: 'fields/jsontable/index.html.erb',
92
+ locals: { field: field, namespace: 'admin' }
93
+ )
94
+ end
95
+ end
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'administrate/field/jsontable'
4
+ require File.expand_path('../../../support/read_fixture', __dir__)
5
+ require 'rails_helper'
6
+
7
+ describe 'fields/jsontable/_show', type: :view do
8
+ include ReadFixture
9
+
10
+ let(:pure_hash) do
11
+ instance_double(
12
+ 'Administrate::Field::Jsontable',
13
+ data: {
14
+ "2019": {
15
+ "start_date": '2019-01-01',
16
+ "end_date": '2019-12-31',
17
+ "id": '123456_2019'
18
+ },
19
+ "2020": {
20
+ "start_date": '2020-01-01',
21
+ "end_date": '2020-12-31',
22
+ "id": '123456_2020'
23
+ }
24
+ }
25
+ )
26
+ end
27
+
28
+ let(:pure_array) do
29
+ instance_double(
30
+ 'Administrate::Field::Jsontable',
31
+ data: [1, 2, 3, 4]
32
+ )
33
+ end
34
+
35
+ let(:hash_contains_array) do
36
+ instance_double(
37
+ 'Administrate::Field::Jsontable',
38
+ data: {
39
+ "2019": {
40
+ "ids": [1, 2, 3, 4],
41
+ "end_date": '2019-12-31',
42
+ "id": '123456_2019'
43
+ }
44
+ }
45
+ )
46
+ end
47
+
48
+ let(:array_contains_hash) do
49
+ instance_double(
50
+ 'Administrate::Field::Jsontable',
51
+ data: [1, 2, 3, { "2019": {
52
+ "start_date": '2019-01-01',
53
+ "end_date": '2019-12-31',
54
+ "id": '123456_2019'
55
+ } }]
56
+ )
57
+ end
58
+
59
+ it 'displays pure_hash' do
60
+ allow(view).to receive(:valid_action?).and_return(true)
61
+ render_jsontable_show(field: pure_hash)
62
+
63
+ expect(rendered.strip).to eq(read_fixture('pure_hash.html'))
64
+ end
65
+
66
+ it 'displays pure_array' do
67
+ allow(view).to receive(:valid_action?).and_return(true)
68
+ render_jsontable_show(field: pure_array)
69
+
70
+ expect(rendered.strip).to eq('[1, 2, 3, 4]')
71
+ end
72
+
73
+ it 'displays hash_contains_array' do
74
+ allow(view).to receive(:valid_action?).and_return(true)
75
+ render_jsontable_show(field: hash_contains_array)
76
+
77
+ expect(rendered.strip).to eq(read_fixture('hash_contains_array.html'))
78
+ end
79
+
80
+ it 'displays array_contains_hash' do
81
+ allow(view).to receive(:valid_action?).and_return(true)
82
+ render_jsontable_show(field: array_contains_hash)
83
+
84
+ expect(rendered.strip).to eq(read_fixture('array_contains_hash.html'))
85
+ end
86
+
87
+ private
88
+
89
+ def render_jsontable_show(field:)
90
+ render(
91
+ partial: 'fields/jsontable/show.html.erb',
92
+ locals: { field: field, namespace: 'admin' }
93
+ )
94
+ end
95
+ end