snfoil-rails 0.5.5

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: d48c445c4545a06e5d8e76b7efff38cc25397ee250a0cecf742c662e35b85c0e
4
+ data.tar.gz: 91b1a44d6fe259a22148bfcbe7e70ae6d5114e1253ab65eecb906032e6b17216
5
+ SHA512:
6
+ metadata.gz: 53bef1d3d8eaf8f7e46dd5e9d161bb9ff6e83628e0cf0682519aaee965cfcafb575e87031aa2760d4fcb47693670f3c505b83ee828cae9a4efc28906fb2ad982
7
+ data.tar.gz: 9c6cee13f1ba22c291469b9339c655a5a797fc31666f92ab60eaec8c1229e51aa10648f3beb13a9b29505d1b69e9a61a20c3f56a9b52968fb2d97e4fc85e507d
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at howeszy@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
@@ -0,0 +1,20 @@
1
+ Copyright 2019 Matthew Howes
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,28 @@
1
+ # Sn::Foil::Rails
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'sn-foil-rails'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install sn-foil-rails
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ begin
4
+ require 'bundler/setup'
5
+ rescue LoadError
6
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
7
+ end
8
+
9
+ require 'rdoc/task'
10
+
11
+ RDoc::Task.new(:rdoc) do |rdoc|
12
+ rdoc.rdoc_dir = 'rdoc'
13
+ rdoc.title = 'SnFoil::Rails'
14
+ rdoc.options << '--line-numbers'
15
+ rdoc.rdoc_files.include('README.md')
16
+ rdoc.rdoc_files.include('lib/**/*.rb')
17
+ end
18
+
19
+ require 'bundler/gem_tasks'
20
+
21
+ require 'rake/testtask'
22
+
23
+ Rake::TestTask.new(:test) do |t|
24
+ t.libs << 'test'
25
+ t.pattern = 'test/**/*_test.rb'
26
+ t.verbose = false
27
+ end
28
+
29
+ task default: :test
@@ -0,0 +1,47 @@
1
+ # frozen_string_literal: true
2
+
3
+ class SnFoilGenerator < Rails::Generators::Base
4
+ source_root File.expand_path('templates', __dir__)
5
+ argument :model, type: :string
6
+
7
+ def generate_sn_foil
8
+ generate_model
9
+ generate_controller
10
+ generate_searcher
11
+ generate_serializer
12
+ generate_deserializer
13
+ generate_policy
14
+ generate_context
15
+ Rails.logger.info 'In order to expose your model, it must be added to the config/routes.rb file'
16
+ end
17
+
18
+ private
19
+
20
+ def generate_context
21
+ template 'context.erb', "app/contexts/#{model.singularize.underscore}_context.rb"
22
+ end
23
+
24
+ def generate_controller
25
+ template 'controller.erb', "app/controllers/#{model.pluralize.underscore}_controller.rb"
26
+ end
27
+
28
+ def generate_deserializer
29
+ template 'jsonapi_deserializer.erb', "app/deserializers/#{model.singularize.underscore}_deserializer.rb"
30
+ end
31
+
32
+ def generate_serializer
33
+ template 'jsonapi_serializer.erb', "app/serializers/#{model.singularize.underscore}_jsonapi_serializer.rb"
34
+ end
35
+
36
+ def generate_model
37
+ generate('model', model.underscore) unless File.file? "app/models/#{model.singularize.underscore}.rb"
38
+ end
39
+
40
+ def generate_searcher
41
+ template 'searcher.erb', "app/searchers/#{model.pluralize.underscore}_searcher.rb"
42
+ end
43
+
44
+ def generate_policy
45
+ template 'policy.erb', "app/policies/#{model.singularize.underscore}_policy.rb"
46
+ end
47
+ end
@@ -0,0 +1,88 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'action_controller/api'
4
+ require_relative 'base'
5
+
6
+ module SnFoil
7
+ module Controller
8
+ class API < SnFoil::Controller::Base
9
+ class << self
10
+ attr_reader :i_serializer, :i_deserializer
11
+
12
+ def serializer(klass = nil)
13
+ @i_serializer = klass
14
+ end
15
+
16
+ def deserializer(klass = nil)
17
+ @i_deserializer = klass
18
+ end
19
+ end
20
+
21
+ def serializer(**options)
22
+ options[:serializer] || self.class.i_serializer
23
+ end
24
+
25
+ def deserializer(**options)
26
+ options[:deserializer] || self.class.i_deserializer
27
+ end
28
+
29
+ def setup_options(**options)
30
+ inject_deserialized_params(super)
31
+ end
32
+
33
+ def setup_create(**options)
34
+ super(**options.merge(deserialize: true))
35
+ end
36
+
37
+ def setup_update(**options)
38
+ super(**options.merge(deserialize: true))
39
+ end
40
+
41
+ def render_change(model, **options)
42
+ if model.errors.empty?
43
+ params
44
+ render json: serializer(**options).new(model,
45
+ **options,
46
+ params: (options[:controller_params] || options[:params] || {})
47
+ .merge(current_entity: current_entity)).serializable_hash
48
+ else
49
+ render json: model.errors, status: :unprocessable_entity
50
+ end
51
+ end
52
+
53
+ def render_destroy(model, **_options)
54
+ if model.errors.empty?
55
+ render json: {}, status: :no_content
56
+ else
57
+ render json: model.errors, status: :unprocessable_entity
58
+ end
59
+ end
60
+
61
+ def render_index(results, **options)
62
+ render json: serializer(**options).new(paginate(results, **options),
63
+ **options,
64
+ params: (options[:controller_params] || options[:params] || {})
65
+ .merge(current_entity: current_entity),
66
+ meta: meta(results, **options))
67
+ .serializable_hash
68
+ end
69
+
70
+ def render_show(model, **options)
71
+ render json: serializer(**options).new(model,
72
+ **options,
73
+ params: (options[:controller_params] || options[:params] || {})
74
+ .merge(current_entity: current_entity)).serializable_hash
75
+ end
76
+
77
+ private
78
+
79
+ def inject_deserialized_params(**options)
80
+ return options unless options[:params].present? && options[:deserialize] == true
81
+ return options unless deserializer(**options)
82
+
83
+ options[:params] = deserializer(**options).new(options[:params], **options).to_h
84
+ options
85
+ end
86
+ end
87
+ end
88
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'concerns/create_controller_concern'
4
+ require_relative 'concerns/destroy_controller_concern'
5
+ require_relative 'concerns/index_controller_concern'
6
+ require_relative 'concerns/show_controller_concern'
7
+ require_relative 'concerns/update_controller_concern'
8
+
9
+ module SnFoil
10
+ module Controller
11
+ class Base < ActionController::Base # rubocop:disable Rails/ApplicationController
12
+ include Concerns::CreateControllerConcern
13
+ include Concerns::DestroyControllerConcern
14
+ include Concerns::IndexControllerConcern
15
+ include Concerns::ShowControllerConcern
16
+ include Concerns::UpdateControllerConcern
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+
5
+ module SnFoil
6
+ module Controller
7
+ module Concerns
8
+ module ChangeControllerConcern
9
+ extend ActiveSupport::Concern
10
+
11
+ def render_change(model, **_options)
12
+ if model.errors.empty?
13
+ render model
14
+ else
15
+ render model.errors, status: :unprocessable_entity
16
+ end
17
+ end
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require_relative 'setup_controller_concern'
5
+ require_relative 'change_controller_concern'
6
+
7
+ module SnFoil
8
+ module Controller
9
+ module Concerns
10
+ module CreateControllerConcern
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ include SetupControllerConcern
15
+ include ChangeControllerConcern
16
+ end
17
+
18
+ def create(**options)
19
+ options = setup_create(**options)
20
+ model = process_create(**options)
21
+ render_create(model, **options)
22
+ end
23
+
24
+ def setup_create(**options)
25
+ setup_options(**options)
26
+ end
27
+
28
+ def process_create(**options)
29
+ current_context(**options).create(**options)
30
+ end
31
+
32
+ def render_create(model, **options)
33
+ render_change(model, **options)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require_relative 'setup_controller_concern'
5
+
6
+ module SnFoil
7
+ module Controller
8
+ module Concerns
9
+ module DestroyControllerConcern
10
+ extend ActiveSupport::Concern
11
+
12
+ included do
13
+ include SetupControllerConcern
14
+ end
15
+
16
+ def destroy(**options)
17
+ options = setup_destroy(**options)
18
+ model = process_destroy(**options)
19
+ render_destroy(model, **options)
20
+ end
21
+
22
+ def setup_destroy(**options)
23
+ setup_options(**options)
24
+ end
25
+
26
+ def process_destroy(**options)
27
+ current_context(**options).destroy(**options)
28
+ end
29
+
30
+ def render_destroy(model, **_options)
31
+ if model.errors.empty?
32
+ render nil
33
+ else
34
+ render model.errors, status: :unprocessable_entity
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,67 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require_relative 'setup_controller_concern'
5
+
6
+ module SnFoil
7
+ module Controller
8
+ module Concerns
9
+ module IndexControllerConcern
10
+ extend ActiveSupport::Concern
11
+
12
+ included do
13
+ include SetupControllerConcern
14
+ end
15
+
16
+ def index(**options)
17
+ options = setup_index(**options)
18
+ results = process_index(**options)
19
+ render_index(results, **options)
20
+ end
21
+
22
+ def setup_index(**options)
23
+ setup_options(**options)
24
+ end
25
+
26
+ def process_index(**options)
27
+ current_context(**options).index(options)
28
+ end
29
+
30
+ def render_index(results, **options)
31
+ render paginate(results, **options), meta: meta(results, options)
32
+ end
33
+
34
+ def paginate(results, **options)
35
+ return results unless results.respond_to?(:page)
36
+
37
+ results.page(page(**options))
38
+ .per(per_page(**options))
39
+ end
40
+
41
+ def page(**options)
42
+ (options.dig(:params, :page) || 1).to_i
43
+ end
44
+
45
+ def per_page(**options)
46
+ per_page_param = (options.dig(:params, :per_page) || 10).to_i
47
+ return 1000 if per_page_param.zero? || per_page_param > 1000
48
+
49
+ per_page_param
50
+ end
51
+
52
+ def meta(results, **options)
53
+ results = paginate(results, **options)
54
+ total_pages = results.respond_to?(:total_pages) ? results.total_pages : nil
55
+ total_count = results.respond_to?(:total_count) ? results.total_count : nil
56
+
57
+ {
58
+ page: page(**options),
59
+ pages: total_pages,
60
+ total: total_count,
61
+ per: per_page(**options)
62
+ }
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+
5
+ module SnFoil
6
+ module Controller
7
+ module Concerns
8
+ module SetupControllerConcern
9
+ extend ActiveSupport::Concern
10
+
11
+ class_methods do
12
+ attr_reader :i_context
13
+
14
+ def context(klass = nil)
15
+ @i_context = klass
16
+ end
17
+ end
18
+
19
+ def context(**options)
20
+ options[:context] || self.class.i_context
21
+ end
22
+
23
+ def setup_options(**options)
24
+ options = inject_params(**options)
25
+ options = inject_id(**options)
26
+ options = inject_includes(**options)
27
+ inject_controller_action(**options)
28
+ end
29
+
30
+ def current_context(**options)
31
+ @current_context ||= context(**options).new(context_entity)
32
+ end
33
+
34
+ protected
35
+
36
+ def pundit_not_authorized
37
+ head :forbidden
38
+ end
39
+
40
+ private
41
+
42
+ # Grab the rails params and inject them into the options
43
+ def inject_params(**options)
44
+ return options if options[:params]
45
+ return options unless params
46
+
47
+ options[:params] = params.to_unsafe_h.deep_symbolize_keys
48
+ options[:controller_params] = options[:params]
49
+ options
50
+ end
51
+
52
+ def inject_id(**options)
53
+ return options if options[:id]
54
+
55
+ options[:id] = id if defined? id
56
+ options[:id] ||= options[:params][:id]
57
+ options
58
+ end
59
+
60
+ def inject_includes(**options)
61
+ return options if options[:include]
62
+ return options unless options.dig(:params, :include)
63
+
64
+ options[:include] = options.dig(:params, :include)
65
+ .split(',')
66
+ .map { |item| item.underscore.to_sym }
67
+ options
68
+ end
69
+
70
+ def inject_controller_action(**options)
71
+ return options if options[:controller_action]
72
+ return options unless options.dig(:params, :action)
73
+
74
+ options[:controller_action] = options[:params][:action]
75
+ options
76
+ end
77
+
78
+ def context_entity
79
+ return current_entity if defined? current_entity
80
+ return current_user if defined? current_user
81
+ end
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require_relative 'setup_controller_concern'
5
+
6
+ module SnFoil
7
+ module Controller
8
+ module Concerns
9
+ module ShowControllerConcern
10
+ extend ActiveSupport::Concern
11
+
12
+ included do
13
+ include SetupControllerConcern
14
+ end
15
+
16
+ def show(**options)
17
+ options = setup_show(**options)
18
+ model = process_show(**options)
19
+ render_show(model, **options)
20
+ end
21
+
22
+ def setup_show(**options)
23
+ setup_options(**options)
24
+ end
25
+
26
+ def process_show(**options)
27
+ current_context(**options).show(**options)
28
+ end
29
+
30
+ def render_show(model, **_options)
31
+ render model
32
+ end
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require_relative 'setup_controller_concern'
5
+ require_relative 'change_controller_concern'
6
+
7
+ module SnFoil
8
+ module Controller
9
+ module Concerns
10
+ module UpdateControllerConcern
11
+ extend ActiveSupport::Concern
12
+
13
+ included do
14
+ include SetupControllerConcern
15
+ include ChangeControllerConcern
16
+ end
17
+
18
+ def update(**options)
19
+ options = setup_update(**options)
20
+ model = process_update(**options)
21
+ render_update(model, **options)
22
+ end
23
+
24
+ def setup_update(**options)
25
+ setup_options(**options)
26
+ end
27
+
28
+ def process_update(**options)
29
+ current_context(**options).update(**options)
30
+ end
31
+
32
+ def render_update(model, **options)
33
+ render_change(model, **options)
34
+ end
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,153 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+
5
+ module SnFoil
6
+ module JsonapiDeserializer # rubocop:disable Metrics/ModuleLength
7
+ extend ActiveSupport::Concern
8
+
9
+ class_methods do
10
+ attr_reader :i_attribute_fields, :i_attribute_transforms
11
+
12
+ def attributes(*fields)
13
+ @i_attribute_fields ||= []
14
+ @i_attribute_fields |= fields
15
+ end
16
+
17
+ def attribute(key, **options)
18
+ @i_attribute_transforms ||= {}
19
+ @i_attribute_transforms[key] = options.merge(transform_type: :attribute)
20
+ end
21
+
22
+ def has_one(key, deserializer:, **options) # rubocop:disable Naming/PredicateName
23
+ @i_attribute_transforms ||= {}
24
+ @i_attribute_transforms[key] = options.merge(deserializer: deserializer, transform_type: :has_one)
25
+ end
26
+ alias_method :belongs_to, :has_one
27
+
28
+ def has_many(key, deserializer:, **options) # rubocop:disable Naming/PredicateName
29
+ @i_attribute_transforms ||= {}
30
+ @i_attribute_transforms[key] = options.merge(deserializer: deserializer, transform_type: :has_many)
31
+ end
32
+ end
33
+
34
+ attr_reader :object, :included, :options
35
+ def initialize(object, included: nil, **options)
36
+ @object = object
37
+ @included = included || object[:included]
38
+ @options = options
39
+ end
40
+
41
+ def attribute_fields
42
+ self.class.i_attribute_fields || []
43
+ end
44
+
45
+ def attribute_transforms
46
+ self.class.i_attribute_transforms || {}
47
+ end
48
+
49
+ def attributes
50
+ @attributes ||= attribute_fields | attribute_transforms.map { |k, v| v[:key] || k }
51
+ end
52
+
53
+ def parse
54
+ parse_data
55
+ end
56
+ alias to_h parse
57
+
58
+ private
59
+
60
+ def parse_data
61
+ if object[:data].is_a? Array
62
+ object[:data].map { |d| build_attributes(d) }
63
+ else
64
+ build_attributes(object[:data])
65
+ end
66
+ end
67
+
68
+ def build_attributes(data)
69
+ attributes = data_id({}, data)
70
+ attributes = parse_standard_attributes(attributes, data) if data[:attributes]
71
+ attribute_transforms.each do |key, opts|
72
+ attributes = apply_attribute_transform(attributes, data, key, **opts)
73
+ end
74
+ attributes
75
+ end
76
+
77
+ def data_id(attributes, data)
78
+ if data[:id]
79
+ attributes[:id] = data[:id]
80
+ elsif data[:'local:id']
81
+ attributes[:lid] = data[:'local:id']
82
+ end
83
+ attributes
84
+ end
85
+
86
+ def parse_standard_attributes(attributes, data)
87
+ attributes.merge!(data[:attributes].select { |k, _| attribute_fields.include? k })
88
+ end
89
+
90
+ def apply_attribute_transform(attributes, data, key, transform_type:, **opts)
91
+ if transform_type == :attribute
92
+ parse_attribute_transform(attributes, data, key, **opts)
93
+ elsif transform_type == :has_one
94
+ parse_has_one_relationship(attributes, data, key, **opts)
95
+ elsif transform_type == :has_many
96
+ parse_has_many_relationship(attributes, data, key, **opts)
97
+ end
98
+ end
99
+
100
+ def parse_attribute_transform(attributes, data, key, **opts)
101
+ return attributes unless data.dig(:attributes, key)
102
+
103
+ attributes.merge(Hash[opts.fetch(:key) { key }, data[:attributes][key]])
104
+ end
105
+
106
+ def parse_relationships(attributes, data)
107
+ self.class.has_one_relationships.each do |key, opts|
108
+ attributes = has_one_relationship(attributes, data, key, **opts)
109
+ end
110
+ self.class.has_many_relationships.each do |key, opts|
111
+ attributes = has_many_relationship(attributes, data, key, **opts)
112
+ end
113
+ attributes
114
+ end
115
+
116
+ def parse_has_one_relationship(attributes, data, key, deserializer:, **opts)
117
+ resource_data = data.dig(:relationships, key, :data)
118
+ return attributes unless resource_data
119
+
120
+ resource_data = data_id(resource_data, resource_data)
121
+ attribute_data = lookup_relationship(resource_data)
122
+ relationship_data = { data: attribute_data || resource_data }
123
+ attributes[opts.fetch(:key) { key }] = deserializer.new(relationship_data, **options, included: included).parse
124
+ attributes
125
+ end
126
+
127
+ def parse_has_many_relationship(attributes, data, key, deserializer:, **opts)
128
+ array_data = data.dig(:relationships, key, :data)
129
+ return attributes unless array_data
130
+
131
+ attributes[opts.fetch(:key) { key }] = array_data.map do |resource_data|
132
+ resource_data = data_id(resource_data, resource_data)
133
+ attribute_data = lookup_relationship(resource_data)
134
+ relationship_data = { data: attribute_data || resource_data }
135
+ deserializer.new(relationship_data, **options, included: included).parse
136
+ end
137
+ attributes
138
+ end
139
+
140
+ def lookup_relationship(type:, id: nil, lid: nil, **_opts)
141
+ raise ::ArgumentError, "missing keyword: id or lid for type: #{type}" unless id || lid
142
+
143
+ included&.find do |x|
144
+ x[:type].eql?(type) &&
145
+ if id
146
+ x[:id].eql?(id)
147
+ elsif lid
148
+ x[:'local:id'].eql?(lid)
149
+ end
150
+ end
151
+ end
152
+ end
153
+ end
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require 'fast_jsonapi/object_serializer'
5
+
6
+ module SnFoil
7
+ module JsonapiSerializer
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ include FastJsonapi::ObjectSerializer
12
+
13
+ set_key_transform :dash
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SnFoil
4
+ module Rails
5
+ if defined?(Rails)
6
+ require 'sn_foil/rails/engine'
7
+ else
8
+ require 'sn_foil'
9
+ require_relative '../searcher'
10
+ require_relative '../jsonapi_serializer'
11
+ require_relative '../jsonapi_deserializer'
12
+ require_relative '../controller/concerns/change_controller_concern'
13
+ require_relative '../controller/concerns/create_controller_concern'
14
+ require_relative '../controller/concerns/destroy_controller_concern'
15
+ require_relative '../controller/concerns/index_controller_concern'
16
+ require_relative '../controller/concerns/setup_controller_concern'
17
+ require_relative '../controller/concerns/show_controller_concern'
18
+ require_relative '../controller/concerns/update_controller_concern'
19
+ require_relative '../controller/api'
20
+ require_relative '../controller/base'
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SnFoil
4
+ module Rails
5
+ class Engine < ::Rails::Engine
6
+ require 'sn_foil'
7
+ require_relative '../searcher'
8
+ require_relative '../jsonapi_serializer'
9
+ require_relative '../jsonapi_deserializer'
10
+ require_relative '../controller/concerns/change_controller_concern'
11
+ require_relative '../controller/concerns/create_controller_concern'
12
+ require_relative '../controller/concerns/destroy_controller_concern'
13
+ require_relative '../controller/concerns/index_controller_concern'
14
+ require_relative '../controller/concerns/setup_controller_concern'
15
+ require_relative '../controller/concerns/show_controller_concern'
16
+ require_relative '../controller/concerns/update_controller_concern'
17
+ require_relative '../controller/api'
18
+ require_relative '../controller/base'
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SnFoil
4
+ module Rails
5
+ VERSION = '0.5.5'
6
+ end
7
+ end
@@ -0,0 +1,123 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support/concern'
4
+ require 'sn_foil/searcher'
5
+
6
+ module SnFoil
7
+ module Searcher
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+ module_eval do
12
+ alias_method :base_search, :search
13
+
14
+ # patch the additional search capabilities into the method
15
+ def search(params = {})
16
+ filtered_scope = base_search(params)
17
+ additional_search(filtered_scope, params)
18
+ end
19
+ end
20
+
21
+ ASC ||= 'ASC'
22
+ DESC ||= 'DESC'
23
+ end
24
+
25
+ class_methods do
26
+ attr_reader :i_include_params, :i_order_method, :i_order_block, :i_order_by_attr, :i_order_by_direction, :i_is_distinct
27
+
28
+ def order(method = nil, &block)
29
+ @i_order_method = method
30
+ @i_order_block = block
31
+ end
32
+
33
+ def order_by(attr, direction = nil)
34
+ @i_order_by_attr = attr
35
+ @i_order_by_direction = direction
36
+ end
37
+
38
+ def distinct(bool = true)
39
+ @i_is_distinct = bool
40
+ end
41
+
42
+ def includes(*array)
43
+ @i_include_params ||= [] # create new array if none exists
44
+ @i_include_params |= array # combine unique elements of both arrays
45
+ end
46
+ end
47
+
48
+ def distinct?
49
+ self.class.i_is_distinct || false
50
+ end
51
+
52
+ def included_params
53
+ self.class.i_include_params
54
+ end
55
+
56
+ def order_by(params = {})
57
+ if params[:order_by].present?
58
+ params[:order_by] = params[:order_by].to_s.underscore
59
+ return params[:order_by].to_sym if model.attribute_names.include?(params[:order_by])
60
+ end
61
+
62
+ self.class.i_order_by_attr || :id
63
+ end
64
+
65
+ def order(params = {})
66
+ if params[:order].present?
67
+ params[:order] = params[:order].to_s.upcase
68
+ return params[:order] if params[:order].eql?(ASC) || params[:order].eql?(DESC)
69
+ end
70
+
71
+ self.class.i_order_by_direction || ASC
72
+ end
73
+
74
+ private
75
+
76
+ def additional_search(filtered_scope, params = {})
77
+ filtered_scope = apply_order(filtered_scope, params)
78
+ filtered_scope = apply_includes(filtered_scope)
79
+ apply_distinct(filtered_scope, params)
80
+ end
81
+
82
+ def apply_includes(filtered_scope)
83
+ return filtered_scope unless included_params
84
+
85
+ filtered_scope.includes(*included_params)
86
+ end
87
+
88
+ def apply_order(filtered_scope, params)
89
+ return apply_default_order(filtered_scope, params) if params[:order_by].blank? && params[:order].blank?
90
+
91
+ filtered_scope.order(order_by(params) => order(params))
92
+ end
93
+
94
+ def apply_default_order(filtered_scope, params)
95
+ return order_method(filtered_scope, params) if order_method?
96
+ return order_block(filtered_scope, params) if order_block?
97
+
98
+ filtered_scope.order(order_by => order)
99
+ end
100
+
101
+ def order_method(filtered_scope, params)
102
+ send(self.class.i_order_method, filtered_scope, params)
103
+ end
104
+
105
+ def order_method?
106
+ self.class.i_order_method.present?
107
+ end
108
+
109
+ def order_block(filtered_scope, params)
110
+ instance_exec filtered_scope, params, &self.class.i_order_block
111
+ end
112
+
113
+ def order_block?
114
+ self.class.i_order_block.present?
115
+ end
116
+
117
+ def apply_distinct(filtered_scope, params)
118
+ return filtered_scope unless distinct? || params[:distinct] == true
119
+
120
+ filtered_scope.distinct
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'sn_foil/rails'
4
+ module SnFoil
5
+ end
metadata ADDED
@@ -0,0 +1,234 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snfoil-rails
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.5.5
5
+ platform: ruby
6
+ authors:
7
+ - Matthew Howes
8
+ - Danny Murphy
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2020-07-21 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: activesupport
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ">="
19
+ - !ruby/object:Gem::Version
20
+ version: 5.2.4.3
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ version: 5.2.4.3
28
+ - !ruby/object:Gem::Dependency
29
+ name: jsonapi-serializer
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - "~>"
33
+ - !ruby/object:Gem::Version
34
+ version: '2.0'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - "~>"
40
+ - !ruby/object:Gem::Version
41
+ version: '2.0'
42
+ - !ruby/object:Gem::Dependency
43
+ name: snfoil
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - "~>"
47
+ - !ruby/object:Gem::Version
48
+ version: '0.5'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - "~>"
54
+ - !ruby/object:Gem::Version
55
+ version: '0.5'
56
+ - !ruby/object:Gem::Dependency
57
+ name: bundler
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '2.0'
63
+ type: :development
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '2.0'
70
+ - !ruby/object:Gem::Dependency
71
+ name: pry-byebug
72
+ requirement: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - "~>"
75
+ - !ruby/object:Gem::Version
76
+ version: '3.0'
77
+ type: :development
78
+ prerelease: false
79
+ version_requirements: !ruby/object:Gem::Requirement
80
+ requirements:
81
+ - - "~>"
82
+ - !ruby/object:Gem::Version
83
+ version: '3.0'
84
+ - !ruby/object:Gem::Dependency
85
+ name: rails
86
+ requirement: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '='
89
+ - !ruby/object:Gem::Version
90
+ version: 5.2.4.3
91
+ type: :development
92
+ prerelease: false
93
+ version_requirements: !ruby/object:Gem::Requirement
94
+ requirements:
95
+ - - '='
96
+ - !ruby/object:Gem::Version
97
+ version: 5.2.4.3
98
+ - !ruby/object:Gem::Dependency
99
+ name: rake
100
+ requirement: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - "~>"
103
+ - !ruby/object:Gem::Version
104
+ version: '13.0'
105
+ type: :development
106
+ prerelease: false
107
+ version_requirements: !ruby/object:Gem::Requirement
108
+ requirements:
109
+ - - "~>"
110
+ - !ruby/object:Gem::Version
111
+ version: '13.0'
112
+ - !ruby/object:Gem::Dependency
113
+ name: rspec-rails
114
+ requirement: !ruby/object:Gem::Requirement
115
+ requirements:
116
+ - - "~>"
117
+ - !ruby/object:Gem::Version
118
+ version: '3.9'
119
+ type: :development
120
+ prerelease: false
121
+ version_requirements: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - "~>"
124
+ - !ruby/object:Gem::Version
125
+ version: '3.9'
126
+ - !ruby/object:Gem::Dependency
127
+ name: rubocop
128
+ requirement: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - "~>"
131
+ - !ruby/object:Gem::Version
132
+ version: 0.76.0
133
+ type: :development
134
+ prerelease: false
135
+ version_requirements: !ruby/object:Gem::Requirement
136
+ requirements:
137
+ - - "~>"
138
+ - !ruby/object:Gem::Version
139
+ version: 0.76.0
140
+ - !ruby/object:Gem::Dependency
141
+ name: rubocop-rails
142
+ requirement: !ruby/object:Gem::Requirement
143
+ requirements:
144
+ - - "~>"
145
+ - !ruby/object:Gem::Version
146
+ version: '2.0'
147
+ type: :development
148
+ prerelease: false
149
+ version_requirements: !ruby/object:Gem::Requirement
150
+ requirements:
151
+ - - "~>"
152
+ - !ruby/object:Gem::Version
153
+ version: '2.0'
154
+ - !ruby/object:Gem::Dependency
155
+ name: rubocop-rspec
156
+ requirement: !ruby/object:Gem::Requirement
157
+ requirements:
158
+ - - "~>"
159
+ - !ruby/object:Gem::Version
160
+ version: 1.36.0
161
+ type: :development
162
+ prerelease: false
163
+ version_requirements: !ruby/object:Gem::Requirement
164
+ requirements:
165
+ - - "~>"
166
+ - !ruby/object:Gem::Version
167
+ version: 1.36.0
168
+ - !ruby/object:Gem::Dependency
169
+ name: sqlite3
170
+ requirement: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ type: :development
176
+ prerelease: false
177
+ version_requirements: !ruby/object:Gem::Requirement
178
+ requirements:
179
+ - - ">="
180
+ - !ruby/object:Gem::Version
181
+ version: '0'
182
+ description:
183
+ email:
184
+ - howeszy@gmail.com
185
+ - dmurph24@gmail.com
186
+ executables: []
187
+ extensions: []
188
+ extra_rdoc_files: []
189
+ files:
190
+ - CODE_OF_CONDUCT.md
191
+ - MIT-LICENSE
192
+ - README.md
193
+ - Rakefile
194
+ - lib/generators/sn_foil/sn_foil_generator.rb
195
+ - lib/sn_foil/controller/api.rb
196
+ - lib/sn_foil/controller/base.rb
197
+ - lib/sn_foil/controller/concerns/change_controller_concern.rb
198
+ - lib/sn_foil/controller/concerns/create_controller_concern.rb
199
+ - lib/sn_foil/controller/concerns/destroy_controller_concern.rb
200
+ - lib/sn_foil/controller/concerns/index_controller_concern.rb
201
+ - lib/sn_foil/controller/concerns/setup_controller_concern.rb
202
+ - lib/sn_foil/controller/concerns/show_controller_concern.rb
203
+ - lib/sn_foil/controller/concerns/update_controller_concern.rb
204
+ - lib/sn_foil/jsonapi_deserializer.rb
205
+ - lib/sn_foil/jsonapi_serializer.rb
206
+ - lib/sn_foil/rails.rb
207
+ - lib/sn_foil/rails/engine.rb
208
+ - lib/sn_foil/rails/version.rb
209
+ - lib/sn_foil/searcher.rb
210
+ - lib/snfoil-rails.rb
211
+ homepage: https://github.com/howeszy/snfoil-rails
212
+ licenses:
213
+ - MIT
214
+ metadata: {}
215
+ post_install_message:
216
+ rdoc_options: []
217
+ require_paths:
218
+ - lib
219
+ required_ruby_version: !ruby/object:Gem::Requirement
220
+ requirements:
221
+ - - ">="
222
+ - !ruby/object:Gem::Version
223
+ version: '0'
224
+ required_rubygems_version: !ruby/object:Gem::Requirement
225
+ requirements:
226
+ - - ">="
227
+ - !ruby/object:Gem::Version
228
+ version: '0'
229
+ requirements: []
230
+ rubygems_version: 3.0.3
231
+ signing_key:
232
+ specification_version: 4
233
+ summary: Additional functionality gem for using SnFoil with Rails
234
+ test_files: []