trax_controller 0.1.3 → 0.1.4
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.gitignore +2 -0
- data/Gemfile +0 -2
- data/lib/trax/controller/engine.rb +1 -1
- data/lib/trax/controller/permit_params_for.rb +54 -0
- data/lib/trax/controller/serialization/adapters/json.rb +4 -53
- data/lib/trax/controller.rb +1 -0
- data/lib/trax_controller/version.rb +1 -1
- data/spec/internal/app/controllers/categories_controller.rb +4 -0
- data/spec/internal/app/serializers/category_serializer.rb +1 -1
- data/spec/internal/app/serializers/category_with_subcategories_serializer.rb +4 -0
- data/spec/internal/app/serializers/product_with_category_serializer.rb +6 -0
- data/spec/internal/config/routes.rb +1 -0
- data/spec/trax/controller_spec.rb +1 -0
- data/spec/trax/controllers/categories_controller_spec.rb +26 -5
- data/spec/trax/serialization/adapters/json_spec.rb +3 -13
- data/trax_controller.gemspec +2 -0
- metadata +27 -9
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: ed00dea1fdfcffa650fe001af13c9903a9e8cea3f8bcfaa1c944440c2ef8d293
|
4
|
+
data.tar.gz: f34a3dcd374fb0adddb5b126cc5e1f9a5ab69520ac1902e8b5399a2bf1e475cc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ff8023e73a5c5d27b0f337fccdc0f337fb5f4f91ec3e3ebbd5a4bce75c9fefa4c3373159848e86867ee3686e662e0eda6cec8814ac2f8cb830a5632731f34eb
|
7
|
+
data.tar.gz: 658915adb99f4b46f3fdb160abe4b217dccad146584df53949ddc47708ac3f9761bd7eb1baaae46e30d2b3d5619d4e998aa45a1380b38d31814bad7be55ba5b2
|
data/.gitignore
CHANGED
data/Gemfile
CHANGED
@@ -2,8 +2,6 @@ source 'https://rubygems.org'
|
|
2
2
|
|
3
3
|
# Specify your gem's dependencies in trax_controller.gemspec
|
4
4
|
gemspec
|
5
|
-
# gem 'active_model_serializers', :github => 'rails-api/active_model_serializers', :branch => "0-10-stable"
|
6
|
-
gem 'active_model_serializers', :github => 'rails-api/active_model_serializers', :branch => "0-10-stable", :ref => "6aba26049154712d65176708ffad55fcfe512ca0"
|
7
5
|
|
8
6
|
group :test do
|
9
7
|
gem 'activerecord'
|
@@ -2,7 +2,7 @@ module Trax
|
|
2
2
|
module Controller
|
3
3
|
class Engine < ::Rails::Engine
|
4
4
|
initializer 'trax_controller.active_model_serializers' do |app|
|
5
|
-
::ActiveModel::Serializer.config.adapter = Trax::Controller::Serialization::Adapters::Json
|
5
|
+
::ActiveModel::Serializer.config.adapter = ::Trax::Controller::Serialization::Adapters::Json
|
6
6
|
end
|
7
7
|
end
|
8
8
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module Trax
|
2
|
+
module Controller
|
3
|
+
module PermitParamsFor
|
4
|
+
extend ::ActiveSupport::Concern
|
5
|
+
|
6
|
+
# Allows you to permit params by creating a Trax::Core::Types::Struct
|
7
|
+
# https://github.com/jasonayre/trax_core/blob/master/lib/trax/core/types/struct.rb
|
8
|
+
# permitted_params_for_action :create do
|
9
|
+
# string :title
|
10
|
+
# end
|
11
|
+
|
12
|
+
included do
|
13
|
+
class_attribute :trax_params_permitters
|
14
|
+
self.trax_params_permitters = {}
|
15
|
+
end
|
16
|
+
|
17
|
+
def params_permitter_for_action
|
18
|
+
action = params[:action]
|
19
|
+
return self.trax_params_permitters[action.to_sym] if self.trax_params_permitters.key?(action.to_sym)
|
20
|
+
return self.trax_params_permitters[:save] if self.trax_params_permitters.key?(:save) && (action == 'create' || action == 'update' || action == 'first_or_create')
|
21
|
+
raise StandardError.new("No param permitter defined for action #{action}")
|
22
|
+
end
|
23
|
+
|
24
|
+
def build_resource_params
|
25
|
+
_params = request.parameters[resource_request_name]
|
26
|
+
self.class.trax_params_permitters[action_name.to_sym].new(_params).to_serializable_hash
|
27
|
+
end
|
28
|
+
|
29
|
+
def build_resource
|
30
|
+
get_resource_ivar || set_resource_ivar(end_of_association_chain.send(method_for_build, resource_params))
|
31
|
+
end
|
32
|
+
|
33
|
+
def update_resource(object, attributes)
|
34
|
+
object.update(attributes)
|
35
|
+
end
|
36
|
+
|
37
|
+
module ClassMethods
|
38
|
+
def permitted_params_for_action(action_name, &block)
|
39
|
+
_klass = ::Trax::Core::NamedClass.new("#{self.name}::PermittedParamsForAction#{action_name.to_s.classify}", Trax::Core::Types::Struct, &block)
|
40
|
+
self.trax_params_permitters[action_name] = _klass
|
41
|
+
end
|
42
|
+
|
43
|
+
def permitted_params_for_save(&block)
|
44
|
+
_update_klass = ::Trax::Core::NamedClass.new("#{self.name}::PermittedParamsForActionUpdate", Trax::Core::Types::Struct, &block)
|
45
|
+
_create_klass = ::Trax::Core::NamedClass.new("#{self.name}::PermittedParamsForActionCreate", Trax::Core::Types::Struct, &block)
|
46
|
+
_first_or_create_klass = ::Trax::Core::NamedClass.new("#{self.name}::PermittedParamsForActionFirstOrCreate", Trax::Core::Types::Struct, &block)
|
47
|
+
self.trax_params_permitters[:update] = _update_klass
|
48
|
+
self.trax_params_permitters[:create] = _create_klass
|
49
|
+
self.trax_params_permitters[:first_or_create] = _first_or_create_klass
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -1,60 +1,11 @@
|
|
1
|
-
# This adapter is a workaround for
|
2
|
-
# https://github.com/rails-api/active_model_serializers/issues/835 until
|
3
|
-
# https://github.com/rails-api/active_model_serializers/pull/952 is merged
|
4
|
-
|
5
1
|
module Trax
|
6
2
|
module Controller
|
7
3
|
module Serialization
|
8
4
|
module Adapters
|
9
|
-
class Json <
|
10
|
-
def
|
11
|
-
|
12
|
-
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
|
17
|
-
def get_hash(serializer, options={})
|
18
|
-
self.class.new(serializer, :nested => true).serializable_hash(options)
|
19
|
-
end
|
20
|
-
|
21
|
-
def collection_serializable_hash(options={})
|
22
|
-
serializer.map { |s| get_hash(s, options) }
|
23
|
-
end
|
24
|
-
|
25
|
-
def is_collection?
|
26
|
-
serializer.respond_to?(:each)
|
27
|
-
end
|
28
|
-
|
29
|
-
def resource_serializable_hash(options={})
|
30
|
-
cached = cache_check(serializer){ serializer.attributes(options) }
|
31
|
-
resource_result = serialize_resource_associations
|
32
|
-
cached.merge(resource_result)
|
33
|
-
end
|
34
|
-
|
35
|
-
def serialize_resource_associations
|
36
|
-
resource_result = {}
|
37
|
-
|
38
|
-
serializer.associations.each do |association|
|
39
|
-
association_serializer = association.serializer
|
40
|
-
association_options = association.options
|
41
|
-
|
42
|
-
if association_serializer.respond_to?(:each)
|
43
|
-
array_serializer = association_serializer
|
44
|
-
|
45
|
-
resource_result[association.key] = array_serializer.map do |item|
|
46
|
-
cache_check(item) { get_hash(item) }
|
47
|
-
end
|
48
|
-
else
|
49
|
-
resource_result[association.key] = if association_serializer && association_serializer.object
|
50
|
-
cache_check(association_serializer) { get_hash(association_serializer) }
|
51
|
-
elsif association_options[:virtual_value]
|
52
|
-
association_options[:virtual_value]
|
53
|
-
end
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
resource_result
|
5
|
+
class Json < ::ActiveModelSerializers::Adapter::Json
|
6
|
+
def initialize(serializer, options = {})
|
7
|
+
super
|
8
|
+
options[:include] = "**"
|
58
9
|
end
|
59
10
|
end
|
60
11
|
end
|
data/lib/trax/controller.rb
CHANGED
@@ -33,6 +33,10 @@ class CategoriesController < ::ApplicationController
|
|
33
33
|
show(serializer: ::CategoriesSerializer, root: 'show_by_calling_original_action')
|
34
34
|
end
|
35
35
|
|
36
|
+
def product_with_category
|
37
|
+
render_resource(serializer: ::ProductWithCategorySerializer, root: 'product', resource: Product.find(params[:id]))
|
38
|
+
end
|
39
|
+
|
36
40
|
private
|
37
41
|
|
38
42
|
def category_params
|
@@ -2,11 +2,14 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
::RSpec.describe CategoriesController, :type => :controller do
|
4
4
|
before(:all) do
|
5
|
-
@shoes_category = ::Category.create(:name => 'mens shoes')
|
6
|
-
@watches_category = ::Category.create(:name => 'mens watches')
|
7
|
-
|
8
|
-
@
|
9
|
-
|
5
|
+
@shoes_category = ::Category.create!(:name => 'mens shoes')
|
6
|
+
@watches_category = ::Category.create!(:name => 'mens watches')
|
7
|
+
@apparels_category = ::Category.create!(:name => 'mens apparels')
|
8
|
+
@shirts_category = @apparels_category.subcategories.create(:name => 'mens shirts')
|
9
|
+
|
10
|
+
@product1 = ::Product.create!(:name => "DC Villan", :quantity => 0, :in_stock => false, :category => @shoes_category)
|
11
|
+
@product2 = ::Product.create!(:name => "Nixon Rotolog", :quantity => 2, :in_stock => true, :category => @watches_category)
|
12
|
+
@product3 = ::Product.create!(:name => "T-Shirt", :quantity => 3, :in_stock => true, :category => @apparels_category)
|
10
13
|
end
|
11
14
|
|
12
15
|
context '#index' do
|
@@ -69,6 +72,24 @@ require 'spec_helper'
|
|
69
72
|
expect(json["show_by_calling_original_action"]["name"]).to eq @shoes_category.name
|
70
73
|
end
|
71
74
|
end
|
75
|
+
|
76
|
+
describe "#product_with_category" do
|
77
|
+
it "deeply serializes" do
|
78
|
+
get :product_with_category, :id => @product3.id
|
79
|
+
json = ::JSON.parse(response.body)
|
80
|
+
expect(json["product"]).to eq(
|
81
|
+
"name" => @product3.name,
|
82
|
+
"category" => {
|
83
|
+
"name" => @apparels_category.name,
|
84
|
+
"subcategories" => [
|
85
|
+
{ "name" => @shirts_category.name,
|
86
|
+
"subcategories" => []
|
87
|
+
}
|
88
|
+
]
|
89
|
+
}
|
90
|
+
)
|
91
|
+
end
|
92
|
+
end
|
72
93
|
end
|
73
94
|
|
74
95
|
context '#create_with_modified_resource' do
|
@@ -1,15 +1,5 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
class NameCategorySerializer < ::ActiveModel::Serializer
|
4
|
-
attributes :name
|
5
|
-
has_many :subcategories, :serializer => NameCategorySerializer
|
6
|
-
end
|
7
|
-
|
8
|
-
class NameProductSerializer < ::ActiveModel::Serializer
|
9
|
-
attributes :name
|
10
|
-
has_one :category, :serializer => NameCategorySerializer
|
11
|
-
end
|
12
|
-
|
13
3
|
describe Trax::Controller::Serialization::Adapters::Json do
|
14
4
|
let(:category_flat_heads){ ::Category.new(:name => "flat heads") }
|
15
5
|
let(:category_screwdrivers){ ::Category.new(:name => "screwdrivers", :subcategories => [category_flat_heads]) }
|
@@ -17,7 +7,7 @@ describe Trax::Controller::Serialization::Adapters::Json do
|
|
17
7
|
let(:category_tools){ ::Category.new(:name => "tools", :subcategories => [category_screwdrivers, category_knives]) }
|
18
8
|
let(:product_tool){ ::Product.new(:name => "Generics Multi-Purpose Tool", :category => category_tools) }
|
19
9
|
let(:root_key){ 'root_key' }
|
20
|
-
let(:serializer){ ::
|
10
|
+
let(:serializer){ ::ProductWithCategorySerializer.new(product_tool, :root => root_key) }
|
21
11
|
let!(:expected_root_key){ root_key.to_sym }
|
22
12
|
let!(:expected_tool_hash){ {
|
23
13
|
:name => product_tool.name,
|
@@ -40,7 +30,7 @@ describe Trax::Controller::Serialization::Adapters::Json do
|
|
40
30
|
let(:category_cookware){ ::Category.new(:name => "cookware") }
|
41
31
|
let(:product_pot){ ::Product.new(:name => "Pot", :category => category_cookware) }
|
42
32
|
let(:collection){ [product_tool, product_pot] }
|
43
|
-
let
|
33
|
+
let(:root_key){ 'root_keys' }
|
44
34
|
let!(:expected_pot_hash){ {
|
45
35
|
:name => product_pot.name,
|
46
36
|
:category => {
|
@@ -48,7 +38,7 @@ describe Trax::Controller::Serialization::Adapters::Json do
|
|
48
38
|
:subcategories => []
|
49
39
|
}
|
50
40
|
}}
|
51
|
-
let(:serializer){ ::ActiveModel::Serializer::
|
41
|
+
let(:serializer){ ::ActiveModel::Serializer::CollectionSerializer.new(collection, :root => root_key, :serializer => ::ProductWithCategorySerializer) }
|
52
42
|
|
53
43
|
it { expect(serialized).to eq(expected_root_key => [expected_tool_hash, expected_pot_hash]) }
|
54
44
|
end
|
data/trax_controller.gemspec
CHANGED
@@ -12,12 +12,14 @@ Gem::Specification.new do |spec|
|
|
12
12
|
spec.description = %q{Resourceful, standardized controllers}
|
13
13
|
spec.homepage = "http://www.github.com/jasonayre"
|
14
14
|
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = '<= 3.0'
|
15
16
|
|
16
17
|
spec.files = `git ls-files -z`.split("\x0")
|
17
18
|
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
19
|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
20
|
spec.require_paths = ["lib"]
|
20
21
|
|
22
|
+
spec.add_dependency "active_model_serializers", "~> 0.10"
|
21
23
|
spec.add_dependency "trax_core"
|
22
24
|
spec.add_dependency "will_paginate"
|
23
25
|
spec.add_dependency "has_scope"
|
metadata
CHANGED
@@ -1,15 +1,29 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trax_controller
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jason Ayre
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2024-07-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: active_model_serializers
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.10'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.10'
|
13
27
|
- !ruby/object:Gem::Dependency
|
14
28
|
name: trax_core
|
15
29
|
requirement: !ruby/object:Gem::Requirement
|
@@ -349,6 +363,7 @@ files:
|
|
349
363
|
- lib/trax/controller/config.rb
|
350
364
|
- lib/trax/controller/engine.rb
|
351
365
|
- lib/trax/controller/inherit_resources.rb
|
366
|
+
- lib/trax/controller/permit_params_for.rb
|
352
367
|
- lib/trax/controller/resource.rb
|
353
368
|
- lib/trax/controller/resource/base.rb
|
354
369
|
- lib/trax/controller/resource/response_meta.rb
|
@@ -368,7 +383,9 @@ files:
|
|
368
383
|
- spec/internal/app/policies/widget_policy.rb
|
369
384
|
- spec/internal/app/serializers/categories_serializer.rb
|
370
385
|
- spec/internal/app/serializers/category_serializer.rb
|
386
|
+
- spec/internal/app/serializers/category_with_subcategories_serializer.rb
|
371
387
|
- spec/internal/app/serializers/product_serializer.rb
|
388
|
+
- spec/internal/app/serializers/product_with_category_serializer.rb
|
372
389
|
- spec/internal/app/serializers/widget_serializer.rb
|
373
390
|
- spec/internal/config/database.yml
|
374
391
|
- spec/internal/config/routes.rb
|
@@ -387,24 +404,23 @@ homepage: http://www.github.com/jasonayre
|
|
387
404
|
licenses:
|
388
405
|
- MIT
|
389
406
|
metadata: {}
|
390
|
-
post_install_message:
|
407
|
+
post_install_message:
|
391
408
|
rdoc_options: []
|
392
409
|
require_paths:
|
393
410
|
- lib
|
394
411
|
required_ruby_version: !ruby/object:Gem::Requirement
|
395
412
|
requirements:
|
396
|
-
- - "
|
413
|
+
- - "<="
|
397
414
|
- !ruby/object:Gem::Version
|
398
|
-
version: '0'
|
415
|
+
version: '3.0'
|
399
416
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
400
417
|
requirements:
|
401
418
|
- - ">="
|
402
419
|
- !ruby/object:Gem::Version
|
403
420
|
version: '0'
|
404
421
|
requirements: []
|
405
|
-
|
406
|
-
|
407
|
-
signing_key:
|
422
|
+
rubygems_version: 3.5.9
|
423
|
+
signing_key:
|
408
424
|
specification_version: 4
|
409
425
|
summary: Resourceful, standardized controllers
|
410
426
|
test_files:
|
@@ -419,7 +435,9 @@ test_files:
|
|
419
435
|
- spec/internal/app/policies/widget_policy.rb
|
420
436
|
- spec/internal/app/serializers/categories_serializer.rb
|
421
437
|
- spec/internal/app/serializers/category_serializer.rb
|
438
|
+
- spec/internal/app/serializers/category_with_subcategories_serializer.rb
|
422
439
|
- spec/internal/app/serializers/product_serializer.rb
|
440
|
+
- spec/internal/app/serializers/product_with_category_serializer.rb
|
423
441
|
- spec/internal/app/serializers/widget_serializer.rb
|
424
442
|
- spec/internal/config/database.yml
|
425
443
|
- spec/internal/config/routes.rb
|