upframework 0.2.91 → 0.4.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 801f75afeb73f9ebbaa429e0699f8e808f258a58debe206ed5cda954fb97fdb8
4
- data.tar.gz: b40be74dab664d2c3fcd5997bafd797a578c71a767bb15db07f93e01f8bf5cba
3
+ metadata.gz: 41060abef09824ee8f64069c82ed5e9f5fb2c98dbfc1fd300d8296f57df9d21c
4
+ data.tar.gz: '0924d6300add50bceea64910fe1434e76e35602ab1680433225b0996c2cf62dc'
5
5
  SHA512:
6
- metadata.gz: 7d79cfd3ca644a503593cdaeaad0692074261cddc045960a33190bd33c199c7b993f60386fc88b892754cc3711b6a52f47a9ec6c666ca90cf50e7cf53f3338bf
7
- data.tar.gz: 05a7471d324141b0db6c68ef55498a8ed016eb90b1634e89dc40a0f0415bb96016eef220d89709c7d3c72416bfcbc7acbee5ca1acaabfd4ffe07db1a563b97c1
6
+ metadata.gz: eec6e1dcca760aa0f663960dbdb497ccec4564a58ff48fdf4720a0186981b38d9d4879260e577b704f9e862b341b037c5821d9bfa9ae39b242f5e99e9bbfb7fe
7
+ data.tar.gz: 659b193e91f2b46ac00a7897201de3269aa092e56d2462178e5474e9db831cfa15993e799db2fd840c0181084fc144d6b903a1f7b162524350214ba944154fc1
@@ -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, includes: [], format: :full, compound_opts: {}, **opts)
15
- render json: serialize_resource(resource, includes, format, compound_opts), **opts
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,9 +25,9 @@ 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:, includes: [], format: :full, compound_opts: {})
28
+ def broadcast_serialized(channel, resource:, **options)
26
29
  channel_klass = "::#{channel.class.name}Channel".constantize
27
- channel_klass.broadcast_to(channel, serialize_resource(resource, includes, format, compound_opts))
30
+ channel_klass.broadcast_to(channel, serialize_resource(resource, options).as_json)
28
31
  end
29
32
 
30
33
  def render_errors(error_messages, status: :unprocessable_entity)
@@ -33,20 +36,14 @@ module Upframework
33
36
 
34
37
  protected
35
38
 
36
- def serialize_resource(resource, includes, format, compound_opts)
39
+ def serialize_resource(resource, options)
37
40
  resource_klass = resource.class.name
38
- if resource_klass == 'ActiveRecord::Relation'
39
- resource_klass = resource.klass.name
40
- end
41
- # TODO: support empty arrays
42
- resource_klass = resource.first.class.name if resource.is_a? Array
43
-
44
- serializer_klass = "#{resource_klass}Serializer".constantize
45
41
 
46
- relations = serializer_klass.default_includes
47
- relations.concat(Array(includes).map(&:to_sym)) if includes
48
-
49
- 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
50
47
  end
51
48
  end
52
49
  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, includes: params[:includes]
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
@@ -27,6 +27,7 @@ module Upframework
27
27
 
28
28
  def execute
29
29
  end
30
+ alias_method :run, :execute
30
31
 
31
32
  def result
32
33
  end
@@ -1,7 +1,13 @@
1
1
  require "upframework/engine"
2
2
 
3
3
  module Upframework
4
- # Your code goes here...
4
+ def self.configuration
5
+ @configuration ||= {}
6
+ end
7
+
8
+ def self.configure
9
+ yield(configuration)
10
+ end
5
11
  end
6
12
 
7
13
  require "upframework/services/routes"
@@ -1,3 +1,3 @@
1
1
  module Upframework
2
- VERSION = '0.2.91'
2
+ VERSION = '0.4.2'
3
3
  end
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.2.91
4
+ version: 0.4.2
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-08-03 00:00:00.000000000 Z
11
+ date: 2020-10-03 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
@@ -111,7 +114,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
111
114
  - !ruby/object:Gem::Version
112
115
  version: '0'
113
116
  requirements: []
114
- rubygems_version: 3.0.3
117
+ rubygems_version: 3.1.2
115
118
  signing_key:
116
119
  specification_version: 4
117
120
  summary: Rails framework extensions
@@ -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