bitia-rails 0.4.0 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +3 -0
- data/Gemfile.lock +2 -1
- data/app/controllers/bitia/api_controller.rb +114 -124
- data/lib/bitia/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: 0202b94e37dfe57da1c1947fe34b5caf3c87e1ae795a543467b67b6e333b87a3
|
4
|
+
data.tar.gz: 601f294f1a4f04d77dfbf6b5a7bad48f83138506099571671d75a92ff48dcb98
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9264e3db125094bc32e43e96c2fd2134b2d74796fdf92c1e4651cf8c5d0f324412671a096b74d74891b71d3ab8bb2885f6eb3e0ddbea1fed7340102a8997c914
|
7
|
+
data.tar.gz: bedb44cd7f8bb4514f673b1a6df0cc5bdc56bdd0062cb0a85282a9011d6e1e8c08e0705bcb3257d7ec4f1e91b14d8b957c2c79171794b051879118a4fa29282d
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
bitia-rails (0.
|
4
|
+
bitia-rails (0.4.0)
|
5
5
|
rails (~> 5.2.4, >= 5.2.4.3)
|
6
6
|
responders
|
7
7
|
|
@@ -177,6 +177,7 @@ DEPENDENCIES
|
|
177
177
|
factory_bot_rails
|
178
178
|
jbuilder
|
179
179
|
pry
|
180
|
+
rack (~> 2.2.2)
|
180
181
|
responders
|
181
182
|
rspec-rails
|
182
183
|
rubocop
|
@@ -1,164 +1,154 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
|
4
|
-
|
3
|
+
module Bitia
|
4
|
+
class ApiController < ActionController::Base
|
5
|
+
include ActionController::MimeResponds
|
5
6
|
|
6
|
-
|
7
|
+
respond_to :json
|
7
8
|
|
8
|
-
|
9
|
+
include Resourceable
|
9
10
|
|
10
|
-
|
11
|
-
|
11
|
+
before_action :purable_initialize_metavars
|
12
|
+
before_action :purable_prepare_resources
|
12
13
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
render
|
24
|
-
end
|
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]
|
25
23
|
|
26
|
-
|
27
|
-
|
28
|
-
render
|
29
|
-
end
|
24
|
+
render
|
25
|
+
end
|
30
26
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
27
|
+
def new
|
28
|
+
authorize resource
|
29
|
+
render
|
30
|
+
end
|
35
31
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
32
|
+
def show
|
33
|
+
authorize resource
|
34
|
+
render
|
35
|
+
end
|
40
36
|
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
37
|
+
def create
|
38
|
+
pure_create_or_update
|
39
|
+
render
|
40
|
+
end
|
45
41
|
|
46
|
-
|
47
|
-
|
48
|
-
|
42
|
+
def update
|
43
|
+
pure_create_or_update
|
44
|
+
render
|
45
|
+
end
|
49
46
|
|
50
|
-
|
51
|
-
|
47
|
+
def destroy
|
48
|
+
authorize resource
|
49
|
+
resource.destroy!
|
52
50
|
|
53
|
-
|
54
|
-
|
51
|
+
render
|
52
|
+
end
|
55
53
|
|
56
|
-
|
57
|
-
|
54
|
+
def pure_filter(param)
|
55
|
+
return unless params[param].present?
|
58
56
|
|
59
|
-
|
57
|
+
instance_variable_get("@#{resources_name}").where!(param => params[param])
|
58
|
+
end
|
60
59
|
|
61
|
-
|
62
|
-
self.class.send(:purable_model)
|
63
|
-
end
|
60
|
+
helper_method :pure_filter
|
64
61
|
|
65
|
-
|
62
|
+
private
|
66
63
|
|
67
|
-
|
68
|
-
|
69
|
-
|
64
|
+
def purable_initialize_metavars
|
65
|
+
@resource = purable_model.name.underscore.to_sym
|
66
|
+
@metadata ||= {}
|
67
|
+
nil
|
68
|
+
end
|
70
69
|
|
71
|
-
|
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(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
|
72
89
|
|
73
|
-
|
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
|
74
96
|
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
97
|
+
def purable_param_id
|
98
|
+
purable_resource_params[:id]
|
99
|
+
rescue StandardError
|
100
|
+
nil
|
101
|
+
end
|
80
102
|
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
id = params[:id].present? ? params[:id] : purable_param_id
|
88
|
-
|
89
|
-
instance_variable_set(
|
90
|
-
"@#{resource_name}", id ? purable_relation.find(id) : purable_relation.new
|
91
|
-
)
|
92
|
-
elsif purable_resource_params.is_a?(Array)
|
93
|
-
resources = purable_resource_params.map do |params|
|
94
|
-
if params[:id].present? && params[:id] != 'self'
|
95
|
-
purable_relation.find(params[:id]).tap { |r| r.assign_attributes(params) }
|
96
|
-
else
|
97
|
-
purable_relation.new(params)
|
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
|
98
109
|
end
|
99
110
|
end
|
100
|
-
|
101
|
-
instance_variable_set("@#{resources_name}", resources)
|
102
|
-
else
|
103
|
-
resource = purable_relation.new(purable_resource_params)
|
104
|
-
instance_variable_set("@#{resource_name}", resource)
|
105
111
|
end
|
106
|
-
end
|
107
|
-
|
108
|
-
def purable_param_id
|
109
|
-
purable_resource_params[:id]
|
110
|
-
rescue StandardError
|
111
|
-
nil
|
112
|
-
end
|
113
112
|
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
m.find(params["#{m.to_s.underscore}_id"]).send(pm.to_sym) # TODO: uncovered
|
113
|
+
def pure_create_or_update
|
114
|
+
if resources.present?
|
115
|
+
purable_model.transaction { resources.each(&:save!) }
|
116
|
+
else
|
117
|
+
resource.save!
|
120
118
|
end
|
121
119
|
end
|
122
|
-
end
|
123
120
|
|
124
|
-
|
125
|
-
|
126
|
-
purable_model.transaction { resources.each(&:save!) }
|
127
|
-
else
|
128
|
-
resource.save!
|
121
|
+
def purable_resource_params
|
122
|
+
send :"#{resource_name}_params"
|
129
123
|
end
|
130
|
-
end
|
131
124
|
|
132
|
-
|
133
|
-
|
134
|
-
|
125
|
+
def purable_model
|
126
|
+
purable_model_chain.last.singularize.classify.constantize
|
127
|
+
end
|
135
128
|
|
136
|
-
|
137
|
-
|
129
|
+
def purable_model_chain
|
130
|
+
controller_path = self.class.controller_path.dup
|
138
131
|
|
139
|
-
|
140
|
-
|
141
|
-
|
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
|
142
135
|
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
controller_path.split('/')
|
147
|
-
end
|
136
|
+
controller_path.sub! "#{prefix_s}/", ''
|
137
|
+
end
|
148
138
|
|
149
|
-
|
139
|
+
controller_path.split('/')
|
140
|
+
end
|
150
141
|
|
151
|
-
|
152
|
-
|
153
|
-
end
|
142
|
+
def initialize
|
143
|
+
super
|
154
144
|
|
155
|
-
|
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
|
156
151
|
|
157
|
-
|
158
|
-
resource_accessor_name = klass.send(:purable_model).name.underscore.to_sym
|
159
|
-
klass.define_method(resource_accessor_name) { resource }
|
160
|
-
klass.helper_method resource_accessor_name
|
152
|
+
private_class_method :inherited
|
161
153
|
end
|
162
|
-
|
163
|
-
private_class_method :inherited
|
164
154
|
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.5.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-15 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|