dry_crud_jsonapi 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1490f7eb09162ab497f07102570cb567d8cc78a65cadef8355ec15035ec4ed9c
4
+ data.tar.gz: 8b276f30b7e1d35b7beea7b45e5b2f0a948dd840c5666856df8830b463fac9d0
5
+ SHA512:
6
+ metadata.gz: 3a0fb63797bc84039904c6746beac40d3d45442c111b8315e3fc4c09db2576fb4545e8a95ff5ff9e49063dcf20617bd2d021c7404b764263a4b4789fa5fe4f0e
7
+ data.tar.gz: 0cfc9a0403b4f8e635b064a1d1901bd35dd009d33a2f20caab7121af86ddd38c81b5608dde3d6f819deca8651b389c65b077ac4a6ffe3b4bf5791597f8e744f7
@@ -0,0 +1,39 @@
1
+ # DryCrudJsonapi
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/dry_crud_jsonapi`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'dry_crud_jsonapi'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install dry_crud_jsonapi
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/dzubi/dry_crud_jsonapi.
36
+
37
+ ## License
38
+
39
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,7 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require "bundler/gem_tasks"
@@ -0,0 +1,39 @@
1
+ module DryCrudJsonapi
2
+ class Pager
3
+
4
+ include Rails.application.routes.url_helpers
5
+ attr_reader :scope, :model_class, :params
6
+
7
+ delegate :current_page, :next_page, :total_pages, to: :scope
8
+
9
+ def initialize(scope, model_class, params = {})
10
+ @scope = scope
11
+ @model_class = model_class
12
+ @params = params.except(:controller, :action, :format)
13
+ end
14
+
15
+ def render
16
+ [first_page, last_page, prev_page, next_page].compact.to_h
17
+ end
18
+
19
+ def first_page
20
+ [:first, path(page: 1)]
21
+ end
22
+
23
+ def last_page
24
+ [:last, path(page: total_pages)]
25
+ end
26
+
27
+ def next_page
28
+ [:next, path(page: current_page + 1)] unless current_page >= total_pages
29
+ end
30
+
31
+ def prev_page
32
+ [:prev, path(page: current_page - 1)] unless current_page <= 1
33
+ end
34
+
35
+ def path(params = {})
36
+ polymorphic_path(model_class, params.merge(params))
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,94 @@
1
+ module DryCrudJsonapi
2
+ class Serializer
3
+ attr_reader :model_class, :serializer_class, :define_conditionals
4
+
5
+ def initialize(model_class)
6
+ @model_class = model_class.to_s.constantize
7
+ @serializer_class = Class.new(JSONAPI::Serializable::Resource)
8
+ @serializer_class.send :extend, JSONAPI::Serializable::Resource::ConditionalFields
9
+
10
+ @define_conditionals = {
11
+ vmware_license: { if: -> { @current_user.license? } },
12
+ discoverer: { if: -> { @object.try(:discoverer_id).present? } }
13
+ }
14
+ end
15
+
16
+ def build
17
+ define_type
18
+ define_attributes
19
+ define_link_to_self
20
+ define_has_one_relations
21
+ define_belongs_to_relations
22
+ define_has_many_relations(:through)
23
+ define_has_many_relations(:has_many)
24
+
25
+ serializer_class
26
+ end
27
+
28
+ private
29
+
30
+ def define_type
31
+ serializer_class.type model_class.model_name.param_key
32
+ end
33
+
34
+ def define_attributes
35
+ attribute_names.each do |name|
36
+ define_conditional(:attribute, name)
37
+ end
38
+ end
39
+
40
+ def define_link_to_self
41
+ serializer_class.link(:self) do
42
+ @controller.rescued_polymorphic_path(@object)
43
+ end
44
+ end
45
+
46
+ def define_has_one_relations
47
+ reflections(for_type(:has_one)).each do |reflection|
48
+ define_conditional(:has_one, reflection.name.to_s) do
49
+ data { @object.send(reflection.name) }
50
+ end
51
+ end
52
+ end
53
+
54
+ def define_belongs_to_relations
55
+ reflections(for_type(:belongs_to)).each do |reflection|
56
+ define_conditional(:belongs_to, reflection.name.to_s) do
57
+ data { @object.send(reflection.name) }
58
+ end
59
+ end
60
+ end
61
+
62
+ def define_has_many_relations(type)
63
+ reflections(for_type(type)).each do |reflection|
64
+ define_conditional(:has_many, reflection.name) do
65
+ link(:related) do
66
+ @controller.rescued_polymorphic_path([@object, reflection.name])
67
+ end
68
+ end
69
+ end
70
+ end
71
+
72
+ def define_conditional(method, name, &block)
73
+ condition = define_conditionals.find(->{[{}]}) { |key, _| name.to_s =~ /#{key}/ }.last
74
+ serializer_class.send(method, name, **condition, &block)
75
+ end
76
+
77
+ def attribute_names
78
+ model_class.column_names.reject do |column|
79
+ %w[passwd].include?(column)
80
+ end
81
+ end
82
+
83
+ def for_type(type)
84
+ "ActiveRecord::Reflection::#{type.to_s.camelcase}Reflection".constantize
85
+ end
86
+
87
+ def reflections(type)
88
+ @reflections ||= {}
89
+ @reflections[type] ||= model_class.reflect_on_all_associations.select do |reflection|
90
+ reflection.is_a?(type)
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1 @@
1
+ Mime::Type.register "application/vnd.api+json", :jsonapi
@@ -0,0 +1,75 @@
1
+ require "dry_crud_jsonapi/engine"
2
+
3
+ module DryCrudJsonapi
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ delegate :model_serializer, to: 'self.class'
8
+ prepend Prepends
9
+ end
10
+
11
+ module Prepends
12
+ def index
13
+ respond_to do |format|
14
+ format.jsonapi { jsonapi_render(entries) }
15
+ format.all { super }
16
+ end
17
+ end
18
+
19
+ def show
20
+ respond_to do |format|
21
+ format.jsonapi { jsonapi_render(entry) }
22
+ format.all { super }
23
+ end
24
+ end
25
+ end
26
+
27
+ def jsonapi_pagination(resources)
28
+ return unless action_name == 'index' && resources.present?
29
+ Pager.new(resources, model_class, params).render
30
+ end
31
+
32
+ def jsonapi_class
33
+ @jsonapi_class ||= Hash.new do |hash, class_name|
34
+ hash[class_name] = model_serializer || Serializer.new(class_name).build
35
+ end
36
+ end
37
+
38
+ def jsonapi_expose
39
+ { controller: self, current_user: current_user }
40
+ end
41
+
42
+ def rescued_polymorphic_path(*objects)
43
+ polymorphic_path(*objects) rescue nil
44
+ end
45
+
46
+ private
47
+
48
+ def json_render_entries
49
+ jsonapi_render(entries)
50
+ end
51
+
52
+ def json_render_entry
53
+ jsonapi_render(entry)
54
+ end
55
+
56
+ def jsonapi_render(object)
57
+ render jsonapi: object, include: jsonapi_include, fields: jsonapi_fields, expose: jsonapi_expose
58
+ end
59
+
60
+ def jsonapi_include
61
+ params.permit(:include)[:include] || []
62
+ end
63
+
64
+ def jsonapi_fields
65
+ params.permit(fields: {}).fetch('fields', []).to_h.collect do |model, string|
66
+ [model, string.split(',')]
67
+ end.to_h
68
+ end
69
+
70
+ module ClassMethods
71
+ def model_serializer
72
+ @model_serializer ||= "#{model_class.name}Serializer".constantize rescue nil
73
+ end
74
+ end
75
+ end
@@ -0,0 +1,5 @@
1
+ require "jsonapi/rails"
2
+
3
+ module DryCrudJsonapi
4
+ class Engine < Rails::Engine; end
5
+ end
@@ -0,0 +1,3 @@
1
+ module DryCrudJsonapi
2
+ VERSION = '0.1.0'
3
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: dry_crud_jsonapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Daniel Illi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-01-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.17'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.17'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: jsonapi-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.3.1
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.3.1
69
+ description:
70
+ email:
71
+ - illi@puzzle.ch
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - README.md
77
+ - Rakefile
78
+ - app/domain/dry_crud_jsonapi/pager.rb
79
+ - app/domain/dry_crud_jsonapi/serializer.rb
80
+ - config/initializers/mime_types.rb
81
+ - lib/dry_crud_jsonapi.rb
82
+ - lib/dry_crud_jsonapi/engine.rb
83
+ - lib/dry_crud_jsonapi/version.rb
84
+ homepage: https://rubygems.org/gems/dry_crud_jsonapi
85
+ licenses:
86
+ - MIT
87
+ metadata: {}
88
+ post_install_message:
89
+ rdoc_options: []
90
+ require_paths:
91
+ - lib
92
+ required_ruby_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ required_rubygems_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ requirements: []
103
+ rubyforge_project:
104
+ rubygems_version: 2.7.6
105
+ signing_key:
106
+ specification_version: 4
107
+ summary: Automatic JSON:API for dry_crud applications
108
+ test_files: []