menilite 0.5.7 → 0.5.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/docs/articles/controller.md +5 -0
- data/lib/menilite/client/http.rb +1 -1
- data/lib/menilite/controller.rb +4 -9
- data/lib/menilite/model.rb +37 -20
- data/lib/menilite/model/association.rb +2 -0
- data/lib/menilite/server/router.rb +1 -1
- data/lib/menilite/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 764faea5732c888bde98d4755617fdbc908f493bce35b3d2d5fca1fee388d8a6
|
4
|
+
data.tar.gz: 75a9e0e3ca6773ed3bc62e0d8e9433e91addffaa7cb1f37a19ed396fab5d1947
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 130512b2b357eaae83cd8cc0dec07d0e74ec0353e1f289a20338bc1fce8e87106513dad1c10c458fd9e91fe702ecfb6e1843718f4500513cc1a1663acaceb204
|
7
|
+
data.tar.gz: '09602d8bfa2a2ae0482ad0edf6ce7657e0ce9cc44b8b82b781fbe9bf41db5372038880697c1abdac9880c4fdae016d023117a320ae6418def6c4f9b59db9cd4d'
|
data/lib/menilite/client/http.rb
CHANGED
data/lib/menilite/controller.rb
CHANGED
@@ -5,20 +5,15 @@ end
|
|
5
5
|
module Menilite
|
6
6
|
class Controller
|
7
7
|
unless RUBY_ENGINE == 'opal'
|
8
|
-
|
8
|
+
attr_reader :settings, :session, :request
|
9
|
+
|
10
|
+
def initialize(session, settings, request)
|
9
11
|
@settings = settings
|
10
12
|
@session = session
|
13
|
+
@request = request
|
11
14
|
end
|
12
15
|
end
|
13
16
|
|
14
|
-
def session
|
15
|
-
@session
|
16
|
-
end
|
17
|
-
|
18
|
-
def settings
|
19
|
-
@settings
|
20
|
-
end
|
21
|
-
|
22
17
|
class << self
|
23
18
|
def action_info
|
24
19
|
@action_info ||= {}
|
data/lib/menilite/model.rb
CHANGED
@@ -206,9 +206,9 @@ module Menilite
|
|
206
206
|
return unless params[:server]
|
207
207
|
end
|
208
208
|
|
209
|
-
field_info[name] = FieldInfo.new(name, type, params)
|
209
|
+
field_info[name] = FieldInfo.new(self, name, type, params)
|
210
210
|
if type == :reference
|
211
|
-
field_info["#{name}_id".to_sym] = FieldInfo.new("#{name}_id", :id, {})
|
211
|
+
field_info["#{name}_id".to_sym] = FieldInfo.new(self, "#{name}_id", :id, {})
|
212
212
|
end
|
213
213
|
|
214
214
|
self.instance_eval do
|
@@ -258,7 +258,7 @@ module Menilite
|
|
258
258
|
end
|
259
259
|
else
|
260
260
|
method = Proc.new do |model, *args, &callback| # todo: should adopt keyword parameters
|
261
|
-
action_url = options[:save] || options[:class] ? "api/#{self}/#{name}" : "api/#{self}/#{model.id}/#{name}"
|
261
|
+
action_url = options[:save] || options[:class] ? "/api/#{self}/#{name}" : "/api/#{self}/#{model.id}/#{name}"
|
262
262
|
post_data = { args: args }
|
263
263
|
|
264
264
|
if options[:save]
|
@@ -278,10 +278,13 @@ module Menilite
|
|
278
278
|
|
279
279
|
on :failure do |res|
|
280
280
|
if callback
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
281
|
+
res.json.then do |json|
|
282
|
+
json = JSON.from_object(json)
|
283
|
+
if json[:result] == 'validation_error'
|
284
|
+
callback.call(:validation_error, Menilite::ValidationError.new(json[:messages]))
|
285
|
+
else
|
286
|
+
callback.call(:failure, json)
|
287
|
+
end
|
285
288
|
end
|
286
289
|
end
|
287
290
|
end
|
@@ -300,10 +303,10 @@ module Menilite
|
|
300
303
|
def validation(field_name, params = {}, &block)
|
301
304
|
params.each do |k, v|
|
302
305
|
if validator = Validators[k, v]
|
303
|
-
|
306
|
+
self.field_info[field_name].validators << validator.new(self, field_name)
|
304
307
|
end
|
305
308
|
end
|
306
|
-
|
309
|
+
self.field_info[field_name].validators << Validator.new(self, field_name, &block) if block
|
307
310
|
end
|
308
311
|
|
309
312
|
def permit(privileges)
|
@@ -406,14 +409,11 @@ module Menilite
|
|
406
409
|
end
|
407
410
|
|
408
411
|
def validate(name, value)
|
409
|
-
|
410
|
-
if validators
|
411
|
-
messages = validators.select(&:enabled?).map {|validator| validator.validate(value) }.compact
|
412
|
-
end
|
412
|
+
self.class.field_info[name].valudate(self, value)
|
413
413
|
end
|
414
414
|
|
415
415
|
def validate_all
|
416
|
-
messages = self.class.field_info.flat_map {|k, info| validate(
|
416
|
+
messages = self.class.field_info.flat_map {|k, info| info.validate(self, self.fields[k]) }.compact
|
417
417
|
messages.empty? or raise ValidationError.new(messages)
|
418
418
|
end
|
419
419
|
|
@@ -472,7 +472,23 @@ module Menilite
|
|
472
472
|
end
|
473
473
|
end
|
474
474
|
|
475
|
-
class FieldInfo < Struct.new(:name, :type, :params)
|
475
|
+
class FieldInfo < Struct.new(:model_class, :name, :type, :params)
|
476
|
+
attr_reader :validators
|
477
|
+
|
478
|
+
def initialize(model_class, name, type, params)
|
479
|
+
@validators = []
|
480
|
+
params.each do |k, v|
|
481
|
+
if validator = Validators[k, v]
|
482
|
+
@validators << validator.new(model_class, name)
|
483
|
+
end
|
484
|
+
end
|
485
|
+
super
|
486
|
+
end
|
487
|
+
|
488
|
+
def validate(obj, value)
|
489
|
+
@validators.select(&:enabled?).map {|validator| validator.validate(obj, value) }.compact
|
490
|
+
end
|
491
|
+
|
476
492
|
def default
|
477
493
|
params[:default] if params.has_key?(:default)
|
478
494
|
end
|
@@ -486,8 +502,8 @@ module Menilite
|
|
486
502
|
@proc = block
|
487
503
|
end
|
488
504
|
|
489
|
-
def validate(value)
|
490
|
-
@proc.call(value)
|
505
|
+
def validate(obj, value)
|
506
|
+
@proc.call(obj, value)
|
491
507
|
end
|
492
508
|
|
493
509
|
def enabled?
|
@@ -509,14 +525,15 @@ module Menilite
|
|
509
525
|
|
510
526
|
class PresenceValidator < Validator
|
511
527
|
def initialize(klass, name)
|
512
|
-
super(klass, name) {|value| "#{name} must not be empty" if value.nil? || value == "" }
|
528
|
+
super(klass, name) {|obj, value| "#{name} must not be empty" if value.nil? || value == "" }
|
513
529
|
end
|
514
530
|
end
|
515
531
|
|
516
532
|
class UniqueValidator < Validator
|
517
533
|
def initialize(klass, name)
|
518
|
-
super(klass, name) do |value|
|
519
|
-
|
534
|
+
super(klass, name) do |obj, value|
|
535
|
+
pp obj
|
536
|
+
"#{name}: '#{value}' already exist #{klass.fetch(filter: { name => value })}" unless klass.fetch(filter: { name => value }).reject{|x| x.id == obj.id }.empty?
|
520
537
|
end
|
521
538
|
end
|
522
539
|
|
@@ -162,7 +162,7 @@ module Menilite
|
|
162
162
|
PrivilegeService.init
|
163
163
|
router.before_action_handlers(klass, action.name).each {|h| self.instance_eval(&h[:proc]) }
|
164
164
|
data = JSON.parse(request.body.read)
|
165
|
-
controller = klass.new(session, settings)
|
165
|
+
controller = klass.new(session, settings, request)
|
166
166
|
result = controller.send(action.name, *data["args"])
|
167
167
|
json result
|
168
168
|
end
|
data/lib/menilite/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: menilite
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.8
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- youchan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-05-
|
11
|
+
date: 2019-05-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -84,6 +84,7 @@ files:
|
|
84
84
|
- Rakefile
|
85
85
|
- bin/console
|
86
86
|
- bin/setup
|
87
|
+
- docs/articles/controller.md
|
87
88
|
- docs/articles/getting-started.md
|
88
89
|
- docs/articles/list.yaml
|
89
90
|
- docs/articles/model.md
|