web-connect 0.1.7 → 0.1.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
  SHA1:
3
- metadata.gz: e6587764f67f4e6ce9e10f6d86edb1bf89c916c4
4
- data.tar.gz: 952c9040de0758ae2e546f932f04887d59e84b6a
3
+ metadata.gz: 9bff09894e31923c611c0c8cb82f423cef9dd2b3
4
+ data.tar.gz: ce9cc08844cefd19669865fff7c19b7941278e9f
5
5
  SHA512:
6
- metadata.gz: cfe5853e869e8910bad896aa3eeddbdccb479bbf122160f717df5c63488abbb4d1c57e0c61d36287b9bd74db380a63a452c5668ad00bce23c12c8d47304c5981
7
- data.tar.gz: e785afeea27467e52e926906b3ea2d1ee7819d9ca03d55ec25a875e95476dc7b045c0e8cd894ecd243e57e31ec9852fb5b97d6adbf4b0b0bdfdba50b85b1bcf7
6
+ metadata.gz: 14373030230ccba50232126e8a26e4c82f9e2a68e90d47e7864607bb11d187696c7f7a8bc9afab925746993e77c48e69f1bfd25afc5e88ab676f00d5a199185b
7
+ data.tar.gz: 2cf04972fcdfcd7526b90eee50528e9e5f6f9f55198ebd533e3e0cd5c6e3d25832076bab0f13b661aefc32facb87a2709d795f0cfdf60d3442ae96527ec702ee
@@ -11,4 +11,9 @@ module Netfira::WebConnect
11
11
  end
12
12
  end
13
13
 
14
+ def self.handles_event?(event)
15
+ target = :"on_#{event}"
16
+ !!event_delegates.find { |x| x.respond_to? target }
17
+ end
18
+
14
19
  end
@@ -28,6 +28,7 @@ module Netfira::WebConnect
28
28
 
29
29
  def dispatch_destroy
30
30
  as_readonly { Netfira::WebConnect.dispatch_event "delete_#{self.class.single_name}", self }
31
+ true # Returned so we don't cancel the before_destroy callback chain
31
32
  end
32
33
 
33
34
  end
@@ -72,6 +72,9 @@ module Netfira::WebConnect
72
72
  @schema ||= Netfira::WebConnect.schema[self]
73
73
  end
74
74
 
75
+ def related_classes
76
+ @related_classes ||= []
77
+ end
75
78
  end
76
79
 
77
80
  after_initialize :init
@@ -41,6 +41,9 @@ module Netfira::WebConnect
41
41
  through: relation_name,
42
42
  after_add: :after_add_relation
43
43
 
44
+ # Tell the class it's related to the other class (used by Schema::Table)
45
+ related_class.related_classes << opposite_class
46
+
44
47
  end
45
48
  end
46
49
 
@@ -8,15 +8,24 @@ module Netfira::WebConnect
8
8
  records.each do |class_name, attribute_sets|
9
9
  klass = class_for_record_type(class_name)
10
10
  attribute_sets.each do |attributes|
11
- update_record klass, attributes
11
+ modify_record klass, attributes
12
12
  end
13
13
  end
14
14
  end
15
15
 
16
- def update_record(klass, attributes)
17
- origin_key = attributes[klass.origin_key.to_s.camelize(:lower)]
18
- raise BadRequest, "#{klass.name.demodulize} sent with no #{klass.origin_key}" unless origin_key
19
- record = klass.find_or_initialize_by_origin_id(shop, origin_key)
16
+ def modify_record(klass, attributes)
17
+ record = find_record(klass, attributes)
18
+ if attributes['@delete']
19
+ delete_record record
20
+ else
21
+ update_record record, attributes
22
+ end
23
+ complete_record record
24
+ end
25
+
26
+ def update_record(record, attributes)
27
+ klass = record.class
28
+ origin_key = klass.origin_key.to_s
20
29
  attributes.each do |key, value|
21
30
  key = key.underscore
22
31
  next if key == origin_key
@@ -24,12 +33,13 @@ module Netfira::WebConnect
24
33
  request_file klass, attributes['fileName'] if record.checksum != value
25
34
  next
26
35
  end
27
- record.write_with_type_casting(key.to_sym, value) or raise BadRequest, "#{klass.name.demodulize.pluralize} don't have #{key} fields"
36
+ record.write_with_type_casting(key.to_sym, value) or unknown_attribute! klass, key
28
37
  end
29
- record.save or
30
- raise BadRequest.new "#{klass.name.demodulize} record could not be saved",
31
- errors: record.errors.messages.map { |k, v| [k.to_s.camelize(:lower), v] }.to_h
32
- complete_record record
38
+ record.save or could_not_save! record
39
+ end
40
+
41
+ def delete_record(record)
42
+ record.destroy! if record.persisted?
33
43
  end
34
44
 
35
45
  def complete_record(record)
@@ -42,6 +52,23 @@ module Netfira::WebConnect
42
52
  list << file_name unless list.include? file_name
43
53
  end
44
54
 
55
+ def find_record(klass, attributes)
56
+ origin_key = klass.origin_key.to_s.camelize :lower
57
+ origin_id = attributes[origin_key]
58
+ raise BadRequest, "#{klass.name.demodulize} sent with no #{klass.origin_key}" unless origin_id
59
+ klass.find_or_initialize_by_origin_id(shop, origin_id.to_s)
60
+ end
61
+
62
+ def could_not_save!(record)
63
+ message = "#{record.class.name.demodulize} record could not be saved"
64
+ errors = record.errors.messages.map { |k, v| [k.to_s.camelize(:lower), v] }.to_h
65
+ raise BadRequest.new message, errors: errors
66
+ end
67
+
68
+ def unknown_attribute!(klass, key)
69
+ raise BadRequest, "#{klass.name.demodulize.pluralize} don't have #{key} fields"
70
+ end
71
+
45
72
  end
46
73
  end
47
74
  end
@@ -0,0 +1,39 @@
1
+ module Netfira::WebConnect
2
+ class RackApp
3
+ module Action::Version8
4
+ class Records < Action
5
+
6
+ def call
7
+ case verb
8
+ when :delete then purge!
9
+ else raise MethodNotAllowed
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def purge!
16
+ raise NotFound unless path.count == 1 && Models.const_defined?(path.first)
17
+ klass = Models.const_get(path.first)
18
+ raise NotFound unless Class === klass && klass < Model::Record
19
+ scope = klass.where shop_id: shop.id
20
+ method = delete_handler_exists? ? :destroy_all : :delete_all
21
+ scope.__send__ method
22
+ {}
23
+ end
24
+
25
+ def delete_handler_name
26
+ @destroy_handler_name ||= :"delete_#{path.first.underscore}"
27
+ end
28
+
29
+ def delete_handler_exists?
30
+ Netfira::WebConnect.handles_event? delete_handler_name
31
+ end
32
+
33
+ end
34
+ end
35
+ end
36
+ end
37
+
38
+ require_relative 'commit/records'
39
+ require_relative 'commit/relations'
@@ -24,6 +24,10 @@ module Netfira::WebConnect
24
24
  @localize ||= l10n_columns.keys.map(&:to_sym)
25
25
  end
26
26
 
27
+ def relations
28
+ @relations = klass.related_classes
29
+ end
30
+
27
31
  IGNORE_COLUMNS = %w[id shop_id created_at updated_at digest]
28
32
 
29
33
  def columns
@@ -33,6 +37,7 @@ module Netfira::WebConnect
33
37
  def as_json(options)
34
38
  @json ||= {
35
39
  columns: columns.map { |name, type| [name.to_s.camelize(:lower), type] }.to_h,
40
+ relations: relations.map{ |klass| klass.name.demodulize.pluralize },
36
41
  singular: singular,
37
42
  primaryKey: [primary_key.to_s.camelize(:lower)],
38
43
  localize: l10n_columns.keys.map{ |k| k.camelize :lower }
@@ -1,5 +1,5 @@
1
1
  module Netfira
2
2
  module WebConnect
3
- VERSION = '0.1.7'
3
+ VERSION = '0.1.8'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: web-connect
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.7
4
+ version: 0.1.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Neil E. Pearson
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-06-20 00:00:00.000000000 Z
12
+ date: 2014-07-07 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -212,6 +212,7 @@ files:
212
212
  - lib/netfira/web_connect/rack_app/actions/version_8/commit/records.rb
213
213
  - lib/netfira/web_connect/rack_app/actions/version_8/commit/relations.rb
214
214
  - lib/netfira/web_connect/rack_app/actions/version_8/files.rb
215
+ - lib/netfira/web_connect/rack_app/actions/version_8/records.rb
215
216
  - lib/netfira/web_connect/rack_app/actions/version_8/settings.rb
216
217
  - lib/netfira/web_connect/rack_app/exceptions.rb
217
218
  - lib/netfira/web_connect/rack_app/exceptions/http_exception.rb