bitia-rails 0.6.0 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/app/controllers/concerns/bitia/purable.rb +187 -0
- data/app/controllers/concerns/bitia/resourceable.rb +53 -0
- data/lib/bitia/version.rb +1 -1
- metadata +5 -5
- data/app/controllers/bitia/api_controller.rb +0 -154
- data/app/controllers/concerns/resourceable.rb +0 -41
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '089def456cc0795e8c3d2850968d8eeee89ee4c172624bbf661bab22bdfe2e4b'
|
4
|
+
data.tar.gz: 67c3f9650bc2efafa15fa2c7aff17d19e554a641bd92c12348b4228a9882dd92
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8d7dd18aba5eae87f3194ab103bb3c8f34914fb3e277cb83ca000c00c26f4730879ba198c196d9a5177d4b45b28f716120dfe80e00c5f1ea9af6a28b1f63b179
|
7
|
+
data.tar.gz: 45748385d63ebf2ffe2627efe36276074c29f42c8d96daab0a620ec9d186670d30fd57b4d8e2c1d39ca027629551ce87073de62f5c1c3b0c73030f9457f57d8a
|
data/Gemfile.lock
CHANGED
@@ -0,0 +1,187 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Bitia
|
4
|
+
module Purable
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
include ActionController::MimeResponds
|
7
|
+
include Bitia::Resourceable
|
8
|
+
|
9
|
+
included do
|
10
|
+
respond_to :json
|
11
|
+
|
12
|
+
before_action :purable_initialize_metavars
|
13
|
+
before_action :purable_prepare_resources
|
14
|
+
|
15
|
+
def index
|
16
|
+
check_resource_prepared
|
17
|
+
|
18
|
+
all = resources.count
|
19
|
+
if params.include? :offset
|
20
|
+
instance_variable_set("@#{resources_name}", resources.offset(params[:offset]))
|
21
|
+
end
|
22
|
+
if params.include? :limit
|
23
|
+
instance_variable_set("@#{resources_name}", resources.limit(Integer(params[:limit])))
|
24
|
+
end
|
25
|
+
@metadata[:all] = all unless @metadata[:all]
|
26
|
+
|
27
|
+
render template: 'bitia/api/index'
|
28
|
+
end
|
29
|
+
|
30
|
+
def new
|
31
|
+
check_resource_prepared
|
32
|
+
|
33
|
+
render template: 'bitia/api/new'
|
34
|
+
end
|
35
|
+
|
36
|
+
def show
|
37
|
+
check_resource_prepared
|
38
|
+
|
39
|
+
render template: 'bitia/api/show'
|
40
|
+
end
|
41
|
+
|
42
|
+
def create
|
43
|
+
check_resource_prepared
|
44
|
+
|
45
|
+
pure_create_or_update
|
46
|
+
render template: 'bitia/api/create'
|
47
|
+
end
|
48
|
+
|
49
|
+
def update
|
50
|
+
check_resource_prepared
|
51
|
+
|
52
|
+
pure_create_or_update
|
53
|
+
render template: 'bitia/api/update'
|
54
|
+
end
|
55
|
+
|
56
|
+
def destroy
|
57
|
+
check_resource_prepared
|
58
|
+
|
59
|
+
resource.destroy!
|
60
|
+
|
61
|
+
render template: 'bitia/api/destroy'
|
62
|
+
end
|
63
|
+
|
64
|
+
def pure_filter(param)
|
65
|
+
return unless params[param].present?
|
66
|
+
|
67
|
+
resource.where!(param => params[param])
|
68
|
+
end
|
69
|
+
|
70
|
+
helper_method :pure_filter
|
71
|
+
|
72
|
+
private
|
73
|
+
|
74
|
+
def purable_initialize_metavars
|
75
|
+
@resource = purable_model.name.underscore.to_sym
|
76
|
+
@metadata ||= {}
|
77
|
+
nil
|
78
|
+
end
|
79
|
+
|
80
|
+
def purable_prepare_resources
|
81
|
+
if params[:action] == 'index'
|
82
|
+
instance_variable_set("@#{resources_name}", purable_relation.all)
|
83
|
+
elsif params[:action] == 'show'
|
84
|
+
id = params[:id].present? ? params[:id] : purable_param_id
|
85
|
+
|
86
|
+
instance_variable_set(
|
87
|
+
"@#{resource_name}", id ? purable_relation.find(id) : purable_relation.new
|
88
|
+
)
|
89
|
+
elsif purable_resource_params.is_a?(Array)
|
90
|
+
resources = purable_resource_params.map do |params|
|
91
|
+
if params[:id].present? && params[:id] != 'self'
|
92
|
+
purable_relation.find(params[:id]).tap { |r| r.assign_attributes(params) }
|
93
|
+
else
|
94
|
+
purable_relation.new(params)
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
instance_variable_set("@#{resources_name}", resources)
|
99
|
+
else
|
100
|
+
resource = purable_relation.new(purable_resource_params)
|
101
|
+
instance_variable_set("@#{resource_name}", resource)
|
102
|
+
end
|
103
|
+
rescue ActiveRecord::RecordNotFound => e
|
104
|
+
self._prepare_resources_exception = e
|
105
|
+
end
|
106
|
+
|
107
|
+
def purable_param_id
|
108
|
+
purable_resource_params[:id]
|
109
|
+
rescue StandardError
|
110
|
+
nil
|
111
|
+
end
|
112
|
+
|
113
|
+
def purable_relation
|
114
|
+
purable_model_names_chain.inject(nil) do |m, pm|
|
115
|
+
if m.nil?
|
116
|
+
pm.singularize.classify.constantize
|
117
|
+
elsif params.include?("#{m.to_s.underscore}_id")
|
118
|
+
m.find(params["#{m.to_s.underscore}_id"]).send(pm.to_sym) # TODO: uncovered
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def pure_create_or_update
|
124
|
+
if resources.present?
|
125
|
+
purable_model.transaction { resources.each(&:save!) }
|
126
|
+
else
|
127
|
+
resource.save!
|
128
|
+
end
|
129
|
+
end
|
130
|
+
|
131
|
+
def purable_resource_params
|
132
|
+
send :"#{resource_name}_params"
|
133
|
+
end
|
134
|
+
|
135
|
+
def purable_model
|
136
|
+
purable_model_chain.last
|
137
|
+
end
|
138
|
+
|
139
|
+
def purable_model_names_chain
|
140
|
+
controller_path = self.class.controller_path.dup
|
141
|
+
|
142
|
+
if defined?(self.class.controller_prefix) and self.class.controller_prefix.present?
|
143
|
+
prefix_s = self.class.controller_prefix.map(&:to_s).join('/')
|
144
|
+
raise StandardError, 'Bad prefix' if controller_path.index(prefix_s) != 0
|
145
|
+
|
146
|
+
controller_path.sub! "#{prefix_s}/", ''
|
147
|
+
end
|
148
|
+
|
149
|
+
controller_path.split('/')
|
150
|
+
end
|
151
|
+
|
152
|
+
def purable_model_chain
|
153
|
+
purable_model_names_chain.map { |p| p.singularize.classify.constantize }
|
154
|
+
end
|
155
|
+
|
156
|
+
# rubocop:disable Style/MissingRespondToMissing
|
157
|
+
def method_missing(method_name)
|
158
|
+
resource_accessor_name = purable_model.name.underscore.to_sym
|
159
|
+
|
160
|
+
return resource if method_name == resource_accessor_name
|
161
|
+
|
162
|
+
super
|
163
|
+
end
|
164
|
+
# rubocop:enable Style/MissingRespondToMissing
|
165
|
+
|
166
|
+
def check_resource_prepared
|
167
|
+
if _prepare_resources_exception.present? and
|
168
|
+
self.class._prepare_resources_fallback.present? and
|
169
|
+
resource.nil? and
|
170
|
+
resources.nil?
|
171
|
+
raise _prepare_resources_exception
|
172
|
+
end
|
173
|
+
end
|
174
|
+
|
175
|
+
class_attribute :_prepare_resources_fallback
|
176
|
+
attr_accessor :_prepare_resources_exception
|
177
|
+
end
|
178
|
+
|
179
|
+
module ClassMethods
|
180
|
+
def prepare_resources_fallback(fallback, *args, &block)
|
181
|
+
self._prepare_resources_fallback = fallback
|
182
|
+
|
183
|
+
before_action fallback, *args, &block
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
187
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
module Bitia
|
2
|
+
module Resourceable
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
helper_method :resource_name
|
7
|
+
|
8
|
+
def resource_name
|
9
|
+
@resource
|
10
|
+
end
|
11
|
+
|
12
|
+
helper_method :resources_name
|
13
|
+
|
14
|
+
def resources_name
|
15
|
+
@resource.to_s.pluralize.to_sym
|
16
|
+
end
|
17
|
+
|
18
|
+
helper_method :resource
|
19
|
+
|
20
|
+
def resource
|
21
|
+
instance_variable_get(:"@#{resource_name}")
|
22
|
+
end
|
23
|
+
|
24
|
+
helper_method :resources
|
25
|
+
|
26
|
+
def resources
|
27
|
+
instance_variable_get(:"@#{resources_name}")
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def resource=(resource)
|
33
|
+
instance_variable_set(:"@#{resource_name}", resource)
|
34
|
+
end
|
35
|
+
|
36
|
+
def resources=(resources)
|
37
|
+
instance_variable_set(:"@#{resources_name}", resources)
|
38
|
+
end
|
39
|
+
|
40
|
+
def controller_prefix
|
41
|
+
self.class.controller_prefix
|
42
|
+
end
|
43
|
+
|
44
|
+
class_attribute :controller_prefix, default: Array.new
|
45
|
+
end
|
46
|
+
|
47
|
+
module ClassMethods
|
48
|
+
def controller_prefix_push(prefix)
|
49
|
+
controller_prefix.push(*prefix.to_s.split('/').map(&:to_sym))
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
data/lib/bitia/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bitia-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Artem Levenkov
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-07-
|
11
|
+
date: 2020-07-28 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -102,8 +102,8 @@ files:
|
|
102
102
|
- Gemfile.lock
|
103
103
|
- MIT-LICENSE
|
104
104
|
- Rakefile
|
105
|
-
- app/controllers/bitia/
|
106
|
-
- app/controllers/concerns/resourceable.rb
|
105
|
+
- app/controllers/concerns/bitia/purable.rb
|
106
|
+
- app/controllers/concerns/bitia/resourceable.rb
|
107
107
|
- app/views/bitia/api/create.json.jbuilder
|
108
108
|
- app/views/bitia/api/destroy.json.jbuilder
|
109
109
|
- app/views/bitia/api/index.json.jbuilder
|
@@ -135,7 +135,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
135
135
|
- !ruby/object:Gem::Version
|
136
136
|
version: '0'
|
137
137
|
requirements: []
|
138
|
-
rubygems_version: 3.
|
138
|
+
rubygems_version: 3.1.2
|
139
139
|
signing_key:
|
140
140
|
specification_version: 4
|
141
141
|
summary: Common modules for Bitia’s Rails projects
|
@@ -1,154 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Bitia
|
4
|
-
class ApiController < ActionController::Base
|
5
|
-
include ActionController::MimeResponds
|
6
|
-
|
7
|
-
respond_to :json
|
8
|
-
|
9
|
-
include Resourceable
|
10
|
-
|
11
|
-
before_action :purable_initialize_metavars
|
12
|
-
before_action :purable_prepare_resources
|
13
|
-
|
14
|
-
def index
|
15
|
-
all = resources.count
|
16
|
-
if params.include? :offset
|
17
|
-
instance_variable_set("@#{resources_name}", resources.offset(params[:offset]))
|
18
|
-
end
|
19
|
-
if params.include? :limit
|
20
|
-
instance_variable_set("@#{resources_name}", resources.limit(Integer(params[:limit])))
|
21
|
-
end
|
22
|
-
@metadata[:all] = all unless @metadata[:all]
|
23
|
-
|
24
|
-
render
|
25
|
-
end
|
26
|
-
|
27
|
-
def new
|
28
|
-
authorize resource
|
29
|
-
render
|
30
|
-
end
|
31
|
-
|
32
|
-
def show
|
33
|
-
authorize resource
|
34
|
-
render
|
35
|
-
end
|
36
|
-
|
37
|
-
def create
|
38
|
-
pure_create_or_update
|
39
|
-
render
|
40
|
-
end
|
41
|
-
|
42
|
-
def update
|
43
|
-
pure_create_or_update
|
44
|
-
render
|
45
|
-
end
|
46
|
-
|
47
|
-
def destroy
|
48
|
-
authorize resource
|
49
|
-
resource.destroy!
|
50
|
-
|
51
|
-
render
|
52
|
-
end
|
53
|
-
|
54
|
-
def pure_filter(param)
|
55
|
-
return unless params[param].present?
|
56
|
-
|
57
|
-
instance_variable_get("@#{resources_name}").where!(param => params[param])
|
58
|
-
end
|
59
|
-
|
60
|
-
helper_method :pure_filter
|
61
|
-
|
62
|
-
private
|
63
|
-
|
64
|
-
def purable_initialize_metavars
|
65
|
-
@resource = purable_model.name.underscore.to_sym
|
66
|
-
@metadata ||= {}
|
67
|
-
nil
|
68
|
-
end
|
69
|
-
|
70
|
-
def purable_prepare_resources
|
71
|
-
@current_user = User.find(params[:user_id]) if params.include? :user_id
|
72
|
-
|
73
|
-
if params[:action] == 'index'
|
74
|
-
instance_variable_set("@#{resources_name}", purable_relation.all)
|
75
|
-
elsif params[:action] == 'show'
|
76
|
-
id = params[:id].present? ? params[:id] : purable_param_id
|
77
|
-
|
78
|
-
instance_variable_set(
|
79
|
-
"@#{resource_name}", id ? purable_relation.find_by(id: id) : purable_relation.new
|
80
|
-
)
|
81
|
-
elsif purable_resource_params.is_a?(Array)
|
82
|
-
resources = purable_resource_params.map do |params|
|
83
|
-
if params[:id].present? && params[:id] != 'self'
|
84
|
-
purable_relation.find(params[:id]).tap { |r| r.assign_attributes(params) }
|
85
|
-
else
|
86
|
-
purable_relation.new(params)
|
87
|
-
end
|
88
|
-
end
|
89
|
-
|
90
|
-
instance_variable_set("@#{resources_name}", resources)
|
91
|
-
else
|
92
|
-
resource = purable_relation.new(purable_resource_params)
|
93
|
-
instance_variable_set("@#{resource_name}", resource)
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
def purable_param_id
|
98
|
-
purable_resource_params[:id]
|
99
|
-
rescue StandardError
|
100
|
-
nil
|
101
|
-
end
|
102
|
-
|
103
|
-
def purable_relation
|
104
|
-
purable_model_chain.inject(nil) do |m, pm|
|
105
|
-
if m.nil?
|
106
|
-
pm.singularize.classify.constantize
|
107
|
-
elsif params.include?("#{m.to_s.underscore}_id")
|
108
|
-
m.find(params["#{m.to_s.underscore}_id"]).send(pm.to_sym) # TODO: uncovered
|
109
|
-
end
|
110
|
-
end
|
111
|
-
end
|
112
|
-
|
113
|
-
def pure_create_or_update
|
114
|
-
if resources.present?
|
115
|
-
purable_model.transaction { resources.each(&:save!) }
|
116
|
-
else
|
117
|
-
resource.save!
|
118
|
-
end
|
119
|
-
end
|
120
|
-
|
121
|
-
def purable_resource_params
|
122
|
-
send :"#{resource_name}_params"
|
123
|
-
end
|
124
|
-
|
125
|
-
def purable_model
|
126
|
-
purable_model_chain.last.singularize.classify.constantize
|
127
|
-
end
|
128
|
-
|
129
|
-
def purable_model_chain
|
130
|
-
controller_path = self.class.controller_path.dup
|
131
|
-
|
132
|
-
if defined?(self.class.controller_prefix) and self.class.controller_prefix.present?
|
133
|
-
prefix_s = self.class.controller_prefix.map(&:to_s).join('/')
|
134
|
-
raise StandardError, 'Bad prefix' if controller_path.index(prefix_s) != 0
|
135
|
-
|
136
|
-
controller_path.sub! "#{prefix_s}/", ''
|
137
|
-
end
|
138
|
-
|
139
|
-
controller_path.split('/')
|
140
|
-
end
|
141
|
-
|
142
|
-
def initialize
|
143
|
-
super
|
144
|
-
|
145
|
-
resource_accessor_name = send(:purable_model).name.underscore.to_sym
|
146
|
-
define_singleton_method(resource_accessor_name) { resource }
|
147
|
-
# rubocop:disable Style/AccessModifierDeclarations
|
148
|
-
singleton_class.class_eval { private resource_accessor_name }
|
149
|
-
# rubocop:enable Style/AccessModifierDeclarations
|
150
|
-
end
|
151
|
-
|
152
|
-
private_class_method :inherited
|
153
|
-
end
|
154
|
-
end
|
@@ -1,41 +0,0 @@
|
|
1
|
-
module Resourceable
|
2
|
-
extend ActiveSupport::Concern
|
3
|
-
|
4
|
-
included do
|
5
|
-
helper_method :resource_name
|
6
|
-
|
7
|
-
def resource_name
|
8
|
-
@resource
|
9
|
-
end
|
10
|
-
|
11
|
-
helper_method :resources_name
|
12
|
-
|
13
|
-
def resources_name
|
14
|
-
@resource.to_s.pluralize.to_sym
|
15
|
-
end
|
16
|
-
|
17
|
-
helper_method :resource
|
18
|
-
|
19
|
-
def resource
|
20
|
-
instance_variable_get(:"@#{resource_name}")
|
21
|
-
end
|
22
|
-
|
23
|
-
helper_method :resources
|
24
|
-
|
25
|
-
def resources
|
26
|
-
instance_variable_get(:"@#{resources_name}")
|
27
|
-
end
|
28
|
-
|
29
|
-
def self.controller_prefix
|
30
|
-
@@controller_prefix ||= [] # rubocop:disable Style/ClassVars
|
31
|
-
end
|
32
|
-
|
33
|
-
def self.controller_prefix_push(prefix)
|
34
|
-
controller_prefix.push(*prefix.to_s.split('/').map(&:to_sym))
|
35
|
-
end
|
36
|
-
|
37
|
-
def controller_prefix
|
38
|
-
self.class.controller_prefix
|
39
|
-
end
|
40
|
-
end
|
41
|
-
end
|