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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4fc23c86e73dc266b4778000012b7fac48fa54baa55634d2b2624d55d9087626
4
- data.tar.gz: c9f3ed1dbf225c237364d3a4df99551eb4f4e82b0032f246a743e9e05a7b5c88
3
+ metadata.gz: 764faea5732c888bde98d4755617fdbc908f493bce35b3d2d5fca1fee388d8a6
4
+ data.tar.gz: 75a9e0e3ca6773ed3bc62e0d8e9433e91addffaa7cb1f37a19ed396fab5d1947
5
5
  SHA512:
6
- metadata.gz: 5d038488bf0af42feddac643a53823cb1ea5ba9166d143ff919d2f5bba1aa8e2296fa18216a9b33bba8f23f0a7b8ce78ca89c8031f19d27082ef6d4ed736a6a7
7
- data.tar.gz: 2baf9395816f90d34ac6ee43db0a6209c18ea65be726e704908bd3aa1a7e5d8cc1dfa0f19539dc85624399735ef5f62e7d350e44a4f5f67bdb52470759d935ad
6
+ metadata.gz: 130512b2b357eaae83cd8cc0dec07d0e74ec0353e1f289a20338bc1fce8e87106513dad1c10c458fd9e91fe702ecfb6e1843718f4500513cc1a1663acaceb204
7
+ data.tar.gz: '09602d8bfa2a2ae0482ad0edf6ce7657e0ce9cc44b8b82b781fbe9bf41db5372038880697c1abdac9880c4fdae016d023117a320ae6418def6c4f9b59db9cd4d'
@@ -0,0 +1,5 @@
1
+ # Controller
2
+
3
+ ## before action
4
+
5
+
@@ -73,7 +73,7 @@ module Menilite
73
73
  @listeners[:success].call(res)
74
74
  end
75
75
 
76
- def failure
76
+ def failure(res)
77
77
  @listeners[:failure].call(res)
78
78
  end
79
79
  end
@@ -5,20 +5,15 @@ end
5
5
  module Menilite
6
6
  class Controller
7
7
  unless RUBY_ENGINE == 'opal'
8
- def initialize(session, settings)
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 ||= {}
@@ -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
- if res.json[:result] == 'validation_error'
282
- callback.call(:validation_error, Menilite::ValidationError.new(res.json[:messages]))
283
- else
284
- callback.call(:failure, res)
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
- (validators[field_name] ||= []) << validator.new(self, field_name)
306
+ self.field_info[field_name].validators << validator.new(self, field_name)
304
307
  end
305
308
  end
306
- (validators[field_name] ||= []) << Validator.new(self, field_name, &block) if block
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
- validators = self.class.validators[name]
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(k, self.fields[k]) }.compact
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
- "#{name}: '#{value}' already exist" unless klass.fetch(filter: { name => value }).empty?
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
 
@@ -36,6 +36,8 @@ module Menilite
36
36
  if field
37
37
  raise UnacquiredDataAccess.new unless @model
38
38
  @model.send(method_sym, *args)
39
+ elsif @model && @model.respond_to?(method_sym)
40
+ @model.send(method_sym, *args)
39
41
  else
40
42
  super
41
43
  end
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Menilite
2
- VERSION = "0.5.7"
2
+ VERSION = "0.5.8"
3
3
  end
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.7
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-09 00:00:00.000000000 Z
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