upframework 0.2.8 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/concerns/upframework/error_handler.rb +2 -0
- data/app/controllers/concerns/upframework/render_extensions.rb +12 -18
- data/app/controllers/upframework/api_extensions.rb +1 -1
- data/app/controllers/upframework/resources_controller.rb +5 -5
- data/app/lib/serializer/collection.rb +37 -0
- data/app/lib/serializer/item.rb +32 -0
- data/app/lib/serializer/pagination.rb +26 -0
- data/app/lib/serializer/sort.rb +20 -0
- data/app/searches/upframework/base_search.rb +0 -18
- data/app/services/upframework/base_service.rb +1 -0
- data/lib/upframework/version.rb +1 -1
- metadata +6 -3
- data/app/controllers/concerns/upframework/crud_endpoint.rb +0 -98
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3d78ce308cddeb5a32b7797a7e26d25cfdf1f0709a17e81a600861d8b82076e8
|
4
|
+
data.tar.gz: c31b0a99dc060af46a8bf1f7d749f129df8064fd8e56c46a3226a6d215f94ebf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8ca6e587887c644c1d3067ac8bd50b0b85a55e52c73cd5e3734f052bc93b8f5c68e9135ce45695b9937b75b6bc6dfc66bee00ef8484ae093ab9cf3aa12f9a519
|
7
|
+
data.tar.gz: 918fe786f0fcd0364d306623e1d5c06be4ebcf4fe2fc342f79542c0da6ec7f8ff42074939b30154cd4f3652f8e525a16c1f1ad8911eeba5cc7cbb9b46f07ad59
|
@@ -11,8 +11,11 @@ module Upframework
|
|
11
11
|
# format => format can be :full(returns all default) or :minimal(returns only ids an types)
|
12
12
|
# compound_opts => options, e.g: meta or links
|
13
13
|
# opts => extra options from render
|
14
|
-
def render_serialized(resource,
|
15
|
-
|
14
|
+
def render_serialized(resource, options)
|
15
|
+
options = options.is_a?(ActionController::Parameters) ? options.to_unsafe_h.symbolize_keys : options
|
16
|
+
options[:current_controller] = self
|
17
|
+
|
18
|
+
render json: serialize_resource(resource, options).as_json, **options.except(:format)
|
16
19
|
end
|
17
20
|
|
18
21
|
# Args
|
@@ -22,10 +25,7 @@ module Upframework
|
|
22
25
|
# format => format can be :full(returns all default) or :minimal(returns only ids an types)
|
23
26
|
# compound_opts => options, e.g: meta or links
|
24
27
|
# opts => extra options from render
|
25
|
-
def broadcast_serialized(channel, resource:,
|
26
|
-
includes << :events
|
27
|
-
resource.add_event(event, creator: current_member)
|
28
|
-
|
28
|
+
def broadcast_serialized(channel, resource:, includes: [], format: :full, compound_opts: {})
|
29
29
|
channel_klass = "::#{channel.class.name}Channel".constantize
|
30
30
|
channel_klass.broadcast_to(channel, serialize_resource(resource, includes, format, compound_opts))
|
31
31
|
end
|
@@ -36,20 +36,14 @@ module Upframework
|
|
36
36
|
|
37
37
|
protected
|
38
38
|
|
39
|
-
def serialize_resource(resource,
|
39
|
+
def serialize_resource(resource, options)
|
40
40
|
resource_klass = resource.class.name
|
41
|
-
if resource_klass == 'ActiveRecord::Relation'
|
42
|
-
resource_klass = resource.klass.name
|
43
|
-
end
|
44
|
-
# TODO: support empty arrays
|
45
|
-
resource_klass = resource.first.class.name if resource.is_a? Array
|
46
41
|
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
serializer_klass.new(resource, include: relations, format: format, current_controller: self, **compound_opts).serializable_hash
|
42
|
+
if resource.respond_to? :each
|
43
|
+
::Serializer::Collection.new(resource, **options)
|
44
|
+
else
|
45
|
+
::Serializer::Item.new(resource, **options)
|
46
|
+
end
|
53
47
|
end
|
54
48
|
end
|
55
49
|
end
|
@@ -15,7 +15,7 @@ module Upframework
|
|
15
15
|
|
16
16
|
rescue_from CanCan::AccessDenied do |exception|
|
17
17
|
respond_to do |format|
|
18
|
-
format.json { render json: { success: false, error: exception.message }, status: :forbidden }
|
18
|
+
format.json { render json: { success: false, error: exception.message, status_code: 403 }, status: :forbidden }
|
19
19
|
format.html { redirect_to main_app.root_url, alert: exception.message }
|
20
20
|
end
|
21
21
|
end
|
@@ -6,13 +6,13 @@ module Upframework
|
|
6
6
|
base_resource
|
7
7
|
|
8
8
|
yield if block_given?
|
9
|
-
render_serialized base_resource
|
9
|
+
render_serialized base_resource, params
|
10
10
|
end
|
11
11
|
|
12
12
|
def update
|
13
13
|
if base_resource.update(base_resource_params)
|
14
14
|
yield if block_given?
|
15
|
-
render_serialized base_resource
|
15
|
+
render_serialized base_resource, params
|
16
16
|
else
|
17
17
|
render_errors base_resource.errors.full_messages
|
18
18
|
end
|
@@ -22,14 +22,14 @@ module Upframework
|
|
22
22
|
base_resource
|
23
23
|
|
24
24
|
yield if block_given?
|
25
|
-
render_serialized base_resource,
|
25
|
+
render_serialized base_resource, params
|
26
26
|
end
|
27
27
|
|
28
28
|
def create
|
29
29
|
if base_resource.save
|
30
30
|
|
31
31
|
yield if block_given?
|
32
|
-
render_serialized base_resource
|
32
|
+
render_serialized base_resource, params
|
33
33
|
else
|
34
34
|
render_errors base_resource.errors.full_messages
|
35
35
|
end
|
@@ -81,7 +81,7 @@ module Upframework
|
|
81
81
|
end
|
82
82
|
|
83
83
|
def accessors
|
84
|
-
current_user
|
84
|
+
super || current_user
|
85
85
|
end
|
86
86
|
|
87
87
|
def base_resource_name
|
@@ -0,0 +1,37 @@
|
|
1
|
+
class Serializer::Collection
|
2
|
+
attr_accessor :records, :includes, :compound_opts, :format, :current_controller
|
3
|
+
|
4
|
+
def initialize(records, includes: [], compound_opts: {}, **params)
|
5
|
+
@records = ::Serializer::Pagination.run(records: records, page: params[:page], per_page: params[:per_page], **params).result
|
6
|
+
@records = ::Serializer::Sort.run(records: @records, sort: params[:sort], **params).result
|
7
|
+
|
8
|
+
@includes = includes
|
9
|
+
@compound_opts = compound_opts
|
10
|
+
@format = params[:format]
|
11
|
+
@current_controller = params[:current_controller]
|
12
|
+
end
|
13
|
+
|
14
|
+
def as_json
|
15
|
+
serializer_klass.new(records, include: relation_includes, format: format, current_controller: current_controller, **compound_opts).serializable_hash
|
16
|
+
end
|
17
|
+
|
18
|
+
def serializer_klass
|
19
|
+
resource_klass = records.klass if records.is_a? ActiveRecord::Relation
|
20
|
+
resource_klass = records.first.class if records.is_a? Array
|
21
|
+
|
22
|
+
"#{resource_klass.name}Serializer".constantize
|
23
|
+
end
|
24
|
+
|
25
|
+
def relation_includes
|
26
|
+
relations = serializer_klass.default_includes
|
27
|
+
|
28
|
+
@includes =
|
29
|
+
if includes.is_a? String
|
30
|
+
includes.split(",").map(&:underscore).map(&:to_sym)
|
31
|
+
else
|
32
|
+
Array(includes).map(&:to_sym)
|
33
|
+
end
|
34
|
+
|
35
|
+
relations.concat(includes.compact)
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
class Serializer::Item
|
2
|
+
attr_accessor :record, :includes, :compound_opts, :current_controller, :format
|
3
|
+
|
4
|
+
def initialize(item, includes: [], compound_opts: {}, **params)
|
5
|
+
@record = item
|
6
|
+
@includes = includes
|
7
|
+
@compound_opts = compound_opts
|
8
|
+
@current_controller = params[:current_controller]
|
9
|
+
@format = params[:format]
|
10
|
+
end
|
11
|
+
|
12
|
+
def serializer_klass
|
13
|
+
"#{record.class.name}Serializer".constantize
|
14
|
+
end
|
15
|
+
|
16
|
+
def as_json
|
17
|
+
serializer_klass.new(record, include: relation_includes, format: format, current_controller: current_controller, **compound_opts).serializable_hash
|
18
|
+
end
|
19
|
+
|
20
|
+
def relation_includes
|
21
|
+
relations = serializer_klass.default_includes
|
22
|
+
|
23
|
+
@includes =
|
24
|
+
if includes.is_a? String
|
25
|
+
includes.split(",").map(&:underscore).map(&:to_sym)
|
26
|
+
else
|
27
|
+
Array(includes).map(&:to_sym)
|
28
|
+
end
|
29
|
+
|
30
|
+
relations.concat(includes.compact)
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
class Serializer::Pagination < Upframework::BaseService
|
2
|
+
DEFAULT_PAGE = 1
|
3
|
+
DEFAULT_PER_PAGE = 20
|
4
|
+
|
5
|
+
attr_accessor :records, :page, :per_page
|
6
|
+
|
7
|
+
def initialize(records: [], page:, per_page:, **params)
|
8
|
+
@records = records
|
9
|
+
@page = page
|
10
|
+
@per_page = per_page
|
11
|
+
end
|
12
|
+
|
13
|
+
def execute
|
14
|
+
paginate_scope
|
15
|
+
end
|
16
|
+
|
17
|
+
def result
|
18
|
+
records
|
19
|
+
end
|
20
|
+
|
21
|
+
def paginate_scope
|
22
|
+
@records = records.
|
23
|
+
page(page || DEFAULT_PAGE).
|
24
|
+
per(per_page || DEFAULT_PER_PAGE)
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
class Serializer::Sort < Upframework::BaseService
|
2
|
+
attr_accessor :records
|
3
|
+
|
4
|
+
def initialize(records: [], sort: nil, **params)
|
5
|
+
@records = records
|
6
|
+
@sort = sort
|
7
|
+
end
|
8
|
+
|
9
|
+
def execute
|
10
|
+
sort_scope
|
11
|
+
end
|
12
|
+
|
13
|
+
def result
|
14
|
+
records
|
15
|
+
end
|
16
|
+
|
17
|
+
def sort_scope
|
18
|
+
#TODO sort scope here
|
19
|
+
end
|
20
|
+
end
|
@@ -9,22 +9,4 @@ class Upframework::BaseSearch < Upframework::BaseService
|
|
9
9
|
def query(field)
|
10
10
|
@model_scope = yield if field.present?
|
11
11
|
end
|
12
|
-
|
13
|
-
def paginate_scope
|
14
|
-
return if @model_scope.nil?
|
15
|
-
|
16
|
-
@model_scope = @model_scope.
|
17
|
-
page(@page || DEFAULT_PAGE).
|
18
|
-
per(@per_page || DEFAULT_PER_PAGE).
|
19
|
-
order(created_at: :desc)
|
20
|
-
end
|
21
|
-
|
22
|
-
module ExecuteWrapper
|
23
|
-
def execute
|
24
|
-
super
|
25
|
-
paginate_scope
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
include ExecuteWrapper
|
30
12
|
end
|
data/lib/upframework/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: upframework
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- jude_cali
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-09-30 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -70,7 +70,6 @@ files:
|
|
70
70
|
- Rakefile
|
71
71
|
- app/assets/config/upframework_manifest.js
|
72
72
|
- app/assets/stylesheets/upframework/application.css
|
73
|
-
- app/controllers/concerns/upframework/crud_endpoint.rb
|
74
73
|
- app/controllers/concerns/upframework/error_handler.rb
|
75
74
|
- app/controllers/concerns/upframework/render_extensions.rb
|
76
75
|
- app/controllers/concerns/upframework/transform_param_keys.rb
|
@@ -79,6 +78,10 @@ files:
|
|
79
78
|
- app/controllers/upframework/searches_controller.rb
|
80
79
|
- app/helpers/upframework/application_helper.rb
|
81
80
|
- app/jobs/upframework/application_job.rb
|
81
|
+
- app/lib/serializer/collection.rb
|
82
|
+
- app/lib/serializer/item.rb
|
83
|
+
- app/lib/serializer/pagination.rb
|
84
|
+
- app/lib/serializer/sort.rb
|
82
85
|
- app/mailers/upframework/application_mailer.rb
|
83
86
|
- app/models/upframework/application_record.rb
|
84
87
|
- app/searches/upframework/base_search.rb
|
@@ -1,98 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
module Upframework::CrudEndpoint
|
3
|
-
extend ::ActiveSupport::Concern
|
4
|
-
|
5
|
-
included do
|
6
|
-
before_action :set_base_resource
|
7
|
-
end
|
8
|
-
|
9
|
-
def show
|
10
|
-
base_resource
|
11
|
-
|
12
|
-
yield if block_given?
|
13
|
-
render_serialized base_resource
|
14
|
-
end
|
15
|
-
|
16
|
-
def update
|
17
|
-
if base_resource.update(base_resource_params)
|
18
|
-
yield if block_given?
|
19
|
-
render_serialized base_resource
|
20
|
-
else
|
21
|
-
render_errors base_resource.errors.full_messages
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def index
|
26
|
-
base_resource
|
27
|
-
|
28
|
-
yield if block_given?
|
29
|
-
render_serialized base_resource, includes: params[:includes]
|
30
|
-
end
|
31
|
-
|
32
|
-
def create
|
33
|
-
if base_resource.save
|
34
|
-
|
35
|
-
yield if block_given?
|
36
|
-
render_serialized base_resource
|
37
|
-
else
|
38
|
-
render_errors base_resource.errors.full_messages
|
39
|
-
end
|
40
|
-
end
|
41
|
-
|
42
|
-
def destroy
|
43
|
-
base_resource.destroy
|
44
|
-
head :no_content
|
45
|
-
end
|
46
|
-
|
47
|
-
private
|
48
|
-
|
49
|
-
def base_resource
|
50
|
-
if params[:id]
|
51
|
-
instance_variable_get("@#{base_resource_name}")
|
52
|
-
else
|
53
|
-
instance_variable_get("@#{base_resource_name.pluralize}")
|
54
|
-
end
|
55
|
-
end
|
56
|
-
|
57
|
-
def set_base_resource
|
58
|
-
data =
|
59
|
-
if params[:id]
|
60
|
-
base_resource_class.find(params[:id])
|
61
|
-
elsif action_name == 'index'
|
62
|
-
base_resource_class.accessible_by(current_ability).where(base_resource_params || {})
|
63
|
-
elsif action_name == 'create'
|
64
|
-
base_resource_class.new(base_resource_params)
|
65
|
-
end
|
66
|
-
|
67
|
-
resource_name = %w[index create].include?(action_name) \
|
68
|
-
? base_resource_name.pluralize \
|
69
|
-
: base_resource_name
|
70
|
-
|
71
|
-
instance_variable_set("@#{resource_name}", data) if data
|
72
|
-
end
|
73
|
-
|
74
|
-
def base_resource_params
|
75
|
-
base_name = base_resource_name
|
76
|
-
|
77
|
-
base_name = base_name.pluralize unless params.key?(base_name)
|
78
|
-
|
79
|
-
params.fetch(base_name).permit(*permitted_columns)
|
80
|
-
end
|
81
|
-
|
82
|
-
def permitted_columns
|
83
|
-
columns = base_resource_class.accessible_fields_by(accessors)
|
84
|
-
columns.concat(extra_params)
|
85
|
-
end
|
86
|
-
|
87
|
-
def accessors
|
88
|
-
current_user
|
89
|
-
end
|
90
|
-
|
91
|
-
def base_resource_name
|
92
|
-
controller_name.singularize
|
93
|
-
end
|
94
|
-
|
95
|
-
def base_resource_class
|
96
|
-
base_resource_name.classify.safe_constantize
|
97
|
-
end
|
98
|
-
end
|