upframework 0.1.1 → 0.2.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/{crud_extensions.rb → crud_endpoint.rb} +1 -1
- data/app/controllers/upframework/api_controller.rb +2 -2
- data/app/controllers/upframework/searches_controller.rb +30 -0
- data/app/searches/upframework/base_search.rb +23 -0
- data/app/services/concerns/upframework/{service_action_controller.rb → service_endpoint.rb} +1 -1
- data/app/services/upframework/base_service.rb +52 -0
- data/config/routes.rb +0 -1
- data/lib/upframework/searches/routes.rb +14 -0
- data/lib/upframework/services/routes.rb +13 -4
- data/lib/upframework/version.rb +1 -1
- data/lib/upframework.rb +1 -0
- metadata +8 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 01aa46c287d2a52c8fb32619543e90f984269b7186ad4f358665a84428618b4e
|
4
|
+
data.tar.gz: 9c310448029653953ee6fb7cda77ff9da622fd2e6e711db9eed4d4038bed1504
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e0abf86d1dda28bda9e7224307af334f2a26c5e7da4065ca749b4fa6fccead7518a173e7e9cc4b7c17d6e08be0cc3589e866d49238f6b767cbc4118e80cc0783
|
7
|
+
data.tar.gz: 919879cab48a3800d1bacf8040a69d4961366cef98699362a28a407f4a34d1a89ff9160efcea3063f284711fafc28aa6a0ce5d64a6c86674e150e74ce12af250
|
@@ -3,9 +3,9 @@ module Upframework
|
|
3
3
|
include ::DeviseTokenAuth::Concerns::SetUserByToken
|
4
4
|
include ::Upframework::ErrorHandler
|
5
5
|
include ::Upframework::TransformParamKeys
|
6
|
-
include ::Upframework::
|
6
|
+
include ::Upframework::CrudEndpoint
|
7
|
+
include ::Upframework::ServiceEndpoint
|
7
8
|
include ::Upframework::RenderExtensions
|
8
|
-
include ::Upframework::ServiceActionController
|
9
9
|
|
10
10
|
before_action :authenticate!
|
11
11
|
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Upframework
|
2
|
+
class SearchesController < Upframework::ApiController
|
3
|
+
skip_authorize_resource
|
4
|
+
|
5
|
+
authorize_resource class: :search
|
6
|
+
|
7
|
+
skip_before_action :set_base_resource
|
8
|
+
|
9
|
+
def index
|
10
|
+
args = { current_ability: current_ability, current_user: current_user }
|
11
|
+
args.merge!(permitted_params)
|
12
|
+
|
13
|
+
resource_scope = search_class.run(args).result
|
14
|
+
render_serialized resource_scope
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def search_class
|
20
|
+
"#{params[:resource].classify}Search".constantize
|
21
|
+
end
|
22
|
+
|
23
|
+
def permitted_params
|
24
|
+
excluded_keys = %w[resource format controller action search]
|
25
|
+
permitted_keys = params.keys - excluded_keys
|
26
|
+
|
27
|
+
params.slice(*permitted_keys).permit(permitted_keys)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
class Upframework::BaseSearch < Upframework::BaseService
|
2
|
+
DEFAULT_PAGE = 1
|
3
|
+
DEFAULT_PER_PAGE = 12
|
4
|
+
|
5
|
+
def result
|
6
|
+
@model_scope
|
7
|
+
end
|
8
|
+
|
9
|
+
def query(field)
|
10
|
+
@model_scope = yield if present_value?(field)
|
11
|
+
end
|
12
|
+
|
13
|
+
def paginate_scope
|
14
|
+
@model_scope = @model_scope.
|
15
|
+
page(@page || DEFAULT_PAGE).
|
16
|
+
per(@per_page || DEFAULT_PER_PAGE).
|
17
|
+
order(created_at: :desc)
|
18
|
+
end
|
19
|
+
|
20
|
+
def present_value?(field)
|
21
|
+
instance_variable_get("@#{field}").present?
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,52 @@
|
|
1
|
+
module Upframework
|
2
|
+
class BaseService
|
3
|
+
attr_reader :errors
|
4
|
+
|
5
|
+
class << self
|
6
|
+
def run(attributes = {})
|
7
|
+
attributes = Hash[attributes.to_h.map { |k, v| [k.to_sym, v] }]
|
8
|
+
new(**attributes).tap(&:execute)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def initialize(**attributes)
|
13
|
+
@errors = []
|
14
|
+
|
15
|
+
self.class.send(:attr_reader, *attributes.keys)
|
16
|
+
|
17
|
+
#TODO: Remove. this should be done on the child class.
|
18
|
+
attributes.each do |key, value|
|
19
|
+
instance_variable_set("@#{key}", value)
|
20
|
+
end
|
21
|
+
|
22
|
+
post_initialize(**attributes)
|
23
|
+
end
|
24
|
+
|
25
|
+
def post_initialize(**attributes)
|
26
|
+
end
|
27
|
+
|
28
|
+
def execute
|
29
|
+
end
|
30
|
+
|
31
|
+
def result
|
32
|
+
end
|
33
|
+
|
34
|
+
def success?
|
35
|
+
@errors.blank?
|
36
|
+
end
|
37
|
+
|
38
|
+
def error?
|
39
|
+
@errors.present?
|
40
|
+
end
|
41
|
+
|
42
|
+
def error_messages
|
43
|
+
@errors.join('. ')
|
44
|
+
end
|
45
|
+
|
46
|
+
protected
|
47
|
+
|
48
|
+
def add_error(error)
|
49
|
+
@errors << error
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
data/config/routes.rb
CHANGED
@@ -0,0 +1,14 @@
|
|
1
|
+
module Upframework
|
2
|
+
module Searches
|
3
|
+
module Routes
|
4
|
+
def self.load(namespace: nil, **options)
|
5
|
+
Rails.application.routes.draw do
|
6
|
+
# Create a route for searches
|
7
|
+
# ex.
|
8
|
+
# GET searches
|
9
|
+
get [namespace, "search"].compact.join("/"), to: "searches#index"
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -1,17 +1,26 @@
|
|
1
1
|
module Upframework
|
2
2
|
module Services
|
3
3
|
module Routes
|
4
|
-
def self.load
|
4
|
+
def self.load(namespace: nil, **options)
|
5
|
+
scope_name = namespace
|
6
|
+
|
5
7
|
Rails.application.routes.draw do
|
6
8
|
source_path = Rails.root.join('app', 'services')
|
7
9
|
|
8
|
-
|
10
|
+
service_routes = proc do
|
9
11
|
Dir.glob("#{source_path}/*/").map{ |e| File.basename e }.each do |resource|
|
10
|
-
#
|
11
|
-
#
|
12
|
+
# Create a post route for services
|
13
|
+
# ex.
|
14
|
+
# POST users/service/my_custom_service
|
12
15
|
post "#{resource}/service/:service_name", to: "#{resource}#service"
|
13
16
|
end
|
14
17
|
end
|
18
|
+
|
19
|
+
if scope_name
|
20
|
+
namespace scope_name, defaults: { format: :json }, &service_routes
|
21
|
+
else
|
22
|
+
service_routes.call
|
23
|
+
end
|
15
24
|
end
|
16
25
|
end
|
17
26
|
end
|
data/lib/upframework/version.rb
CHANGED
data/lib/upframework.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.2.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-04-
|
11
|
+
date: 2020-04-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -42,21 +42,25 @@ files:
|
|
42
42
|
- Rakefile
|
43
43
|
- app/assets/config/upframework_manifest.js
|
44
44
|
- app/assets/stylesheets/upframework/application.css
|
45
|
-
- app/controllers/concerns/upframework/
|
45
|
+
- app/controllers/concerns/upframework/crud_endpoint.rb
|
46
46
|
- app/controllers/concerns/upframework/error_handler.rb
|
47
47
|
- app/controllers/concerns/upframework/render_extensions.rb
|
48
48
|
- app/controllers/concerns/upframework/transform_param_keys.rb
|
49
49
|
- app/controllers/upframework/api_controller.rb
|
50
|
+
- app/controllers/upframework/searches_controller.rb
|
50
51
|
- app/helpers/upframework/application_helper.rb
|
51
52
|
- app/jobs/upframework/application_job.rb
|
52
53
|
- app/mailers/upframework/application_mailer.rb
|
53
54
|
- app/models/upframework/application_record.rb
|
54
|
-
- app/
|
55
|
+
- app/searches/upframework/base_search.rb
|
56
|
+
- app/services/concerns/upframework/service_endpoint.rb
|
57
|
+
- app/services/upframework/base_service.rb
|
55
58
|
- app/views/layouts/upframework/application.html.erb
|
56
59
|
- config/routes.rb
|
57
60
|
- lib/tasks/upframework_tasks.rake
|
58
61
|
- lib/upframework.rb
|
59
62
|
- lib/upframework/engine.rb
|
63
|
+
- lib/upframework/searches/routes.rb
|
60
64
|
- lib/upframework/services/routes.rb
|
61
65
|
- lib/upframework/version.rb
|
62
66
|
homepage: https://gitlab.com/disruptors/upframework
|