tramway-api 1.8 → 1.8.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/app/controllers/tramway/api/v1/records_controller.rb +36 -10
- data/lib/tramway/api.rb +2 -2
- data/lib/tramway/api/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3065ba5d3d8a135210084aae9fdeea5291bfbc25c195bffa97e544491b7c9776
|
4
|
+
data.tar.gz: 9e604ff996584c0982dc2abd7ce7da324060d89f53e55383cf2dc24caa47ed03
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: cd85298817523d5f697e9c4074f45bc258f004f8d07865ea2297248d62ba42327f20a2fbd9dde83d2d1d714769e8df0cfd6207da65309085ad804a3f4861de14
|
7
|
+
data.tar.gz: f101fd0601e24a0b96488daadad96d5dd8d8db666aa52d839e403444b4478f9d33325e8ef926b06a7a83277036b12478716ca2fff5b924706a50ac824252e2b9
|
@@ -3,13 +3,13 @@
|
|
3
3
|
module Tramway::Api::V1
|
4
4
|
class RecordsController < ::Tramway::Api::V1::ApplicationController
|
5
5
|
before_action :check_available_model_class
|
6
|
-
before_action :
|
6
|
+
before_action :check_available_model_action_for_record, only: [ :show, :update, :destroy ]
|
7
7
|
before_action :authenticate_user_if_needed
|
8
8
|
|
9
9
|
def index
|
10
|
-
|
11
|
-
|
12
|
-
render json:
|
10
|
+
collection = available_action_for_collection
|
11
|
+
|
12
|
+
render json: collection,
|
13
13
|
each_serializer: serializer_class,
|
14
14
|
include: '*',
|
15
15
|
status: :ok
|
@@ -63,6 +63,12 @@ module Tramway::Api::V1
|
|
63
63
|
end
|
64
64
|
end
|
65
65
|
|
66
|
+
def records
|
67
|
+
collection = model_class.active.order(id: :desc).send params[:scope] || :all
|
68
|
+
collection = collection.full_text_search params[:search] if params[:search]
|
69
|
+
collection
|
70
|
+
end
|
71
|
+
|
66
72
|
def check_available_model_class
|
67
73
|
unless model_class
|
68
74
|
head(:unauthorized) && return unless current_user
|
@@ -70,23 +76,43 @@ module Tramway::Api::V1
|
|
70
76
|
end
|
71
77
|
end
|
72
78
|
|
73
|
-
def
|
79
|
+
def check_available_model_action_for_record
|
80
|
+
action_is_available = check_action
|
81
|
+
action_is_available.tap do
|
82
|
+
head(:unprocessable_entity) && return if action_is_available.is_a?(Proc) && !action_is_available.call(record, current_user)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def available_action_for_collection
|
87
|
+
action_is_available = check_action
|
88
|
+
return records if action_is_available == true
|
89
|
+
action_is_available.call records, current_user if action_is_available.is_a?(Proc)
|
90
|
+
end
|
91
|
+
|
92
|
+
def check_action
|
74
93
|
action_is_available = checking_roles.map do |role|
|
75
|
-
Tramway::Api.action_is_available
|
76
|
-
record: record,
|
94
|
+
Tramway::Api.action_is_available(
|
77
95
|
action: action_name.to_sym,
|
78
96
|
project: (@application_engine || @application.name),
|
79
97
|
role: role,
|
80
98
|
model_name: params[:model],
|
81
99
|
current_user: current_user
|
82
100
|
)
|
83
|
-
end.
|
101
|
+
end.compact.uniq - [false]
|
102
|
+
|
103
|
+
if action_is_available.count > 1
|
104
|
+
Tramway::Error.raise_error(:tramway, :api, :api, :v1, :records_controller, :available_action_for_collection, :duplicate_actions)
|
105
|
+
end
|
106
|
+
|
107
|
+
action_is_available = action_is_available.first
|
84
108
|
|
85
|
-
|
109
|
+
action_is_available.tap do
|
110
|
+
head(:unprocessable_entity) && return unless action_is_available
|
111
|
+
end
|
86
112
|
end
|
87
113
|
|
88
114
|
def authenticate_user_if_needed
|
89
|
-
action_is_open = Tramway::Api.action_is_available
|
115
|
+
action_is_open = Tramway::Api.action_is_available(
|
90
116
|
action: action_name.to_sym,
|
91
117
|
project: (@application_engine || @application.name),
|
92
118
|
model_name: params[:model]
|
data/lib/tramway/api.rb
CHANGED
@@ -54,7 +54,7 @@ module Tramway
|
|
54
54
|
end&.flatten || []
|
55
55
|
end
|
56
56
|
|
57
|
-
def action_is_available
|
57
|
+
def action_is_available(project:, role: :open, model_name:, action:, current_user: nil)
|
58
58
|
actions = select_actions(project: project, role: role, model_name: model_name)
|
59
59
|
availability = actions&.select do |a|
|
60
60
|
if a.is_a? Symbol
|
@@ -67,7 +67,7 @@ module Tramway
|
|
67
67
|
return false unless availability.present?
|
68
68
|
return true if availability.is_a? Symbol
|
69
69
|
|
70
|
-
availability.values.first
|
70
|
+
availability.values.first
|
71
71
|
end
|
72
72
|
|
73
73
|
def select_actions(project:, role:, model_name:)
|
data/lib/tramway/api/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: tramway-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version:
|
4
|
+
version: 1.8.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Pavel Kalashnikov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-03-
|
11
|
+
date: 2020-03-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: active_model_serializers
|