ar_sync 1.0.3 → 1.0.4

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: 949fc166a975785ab522fe3a2714bf334a56c1f2d4e32931aced7b3ee1de65ac
4
- data.tar.gz: f9428cc4dbf7e2e7cea7c2f958198e628f7ad92241d71c351e8328b856a315de
3
+ metadata.gz: 1998952425317eff3faa4eb8511f2ad0e40a5f5025dbdc3fae6b5ca7916c454f
4
+ data.tar.gz: 2b5819d226d4f05892e6fda8a639d758d5579414e6e0ce7eb0fe2cddae978da4
5
5
  SHA512:
6
- metadata.gz: dff083ce7735e475c3104b7581da185e8bd872e76afd8addeecfd5ebc9d292cb299358edeb9d445c5aac268813d4bd358a886bd89dff761eab8dbbac2fa75593
7
- data.tar.gz: 54ce2594a8749284200924456188fc7b7a3836f8719cbaea4b2626823e9bbe1879cc6a8d7dad327e82f83d7f48d488620eef18f94bda6297aecf1c87d3893b85
6
+ metadata.gz: d8c730609dedb72287c26ae09b39dfbe75aeb2b9e9c41b029e2c90212bb4253383a08e96fa975d00f56eb24154ba34dbe4fc13620654314e7fe061b227465a5b
7
+ data.tar.gz: 5c5bed249fbc71e1c620aa1d0ef476efbb34492ee28554b86a158181d1f572e6449959b0e2fabd60af16f9b75e33b043e8d7d58dc8b50934949d88784cdc320a
data/Gemfile.lock CHANGED
@@ -1,46 +1,46 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ar_sync (1.0.3)
4
+ ar_sync (1.0.4)
5
5
  activerecord
6
- ar_serializer (= 1.0.0)
6
+ ar_serializer
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- activemodel (5.2.3)
12
- activesupport (= 5.2.3)
13
- activerecord (5.2.3)
14
- activemodel (= 5.2.3)
15
- activesupport (= 5.2.3)
16
- arel (>= 9.0)
11
+ activemodel (6.0.2.1)
12
+ activesupport (= 6.0.2.1)
13
+ activerecord (6.0.2.1)
14
+ activemodel (= 6.0.2.1)
15
+ activesupport (= 6.0.2.1)
17
16
  activerecord-import (1.0.2)
18
17
  activerecord (>= 3.2)
19
- activesupport (5.2.3)
18
+ activesupport (6.0.2.1)
20
19
  concurrent-ruby (~> 1.0, >= 1.0.2)
21
20
  i18n (>= 0.7, < 2)
22
21
  minitest (~> 5.1)
23
22
  tzinfo (~> 1.1)
24
- ar_serializer (1.0.0)
23
+ zeitwerk (~> 2.2)
24
+ ar_serializer (1.1.0)
25
25
  activerecord
26
26
  top_n_loader
27
- arel (9.0.0)
28
27
  coderay (1.1.2)
29
28
  concurrent-ruby (1.1.5)
30
- i18n (1.6.0)
29
+ i18n (1.7.0)
31
30
  concurrent-ruby (~> 1.0)
32
31
  method_source (0.9.2)
33
- minitest (5.11.3)
32
+ minitest (5.13.0)
34
33
  pry (0.12.2)
35
34
  coderay (~> 1.1.0)
36
35
  method_source (~> 0.9.0)
37
36
  rake (12.3.2)
38
- sqlite3 (1.3.13)
37
+ sqlite3 (1.4.2)
39
38
  thread_safe (0.3.6)
40
- top_n_loader (1.0.0)
39
+ top_n_loader (1.0.1)
41
40
  activerecord
42
- tzinfo (1.2.5)
41
+ tzinfo (1.2.6)
43
42
  thread_safe (~> 0.1)
43
+ zeitwerk (2.2.2)
44
44
 
45
45
  PLATFORMS
46
46
  ruby
@@ -53,4 +53,4 @@ DEPENDENCIES
53
53
  sqlite3
54
54
 
55
55
  BUNDLED WITH
56
- 1.16.1
56
+ 2.1.2
data/ar_sync.gemspec CHANGED
@@ -21,7 +21,7 @@ Gem::Specification.new do |spec|
21
21
  spec.require_paths = ['lib']
22
22
 
23
23
  spec.add_dependency 'activerecord'
24
- spec.add_dependency 'ar_serializer', '1.0.0'
24
+ spec.add_dependency 'ar_serializer'
25
25
  %w[rake pry sqlite3 activerecord-import].each do |gem_name|
26
26
  spec.add_development_dependency gem_name
27
27
  end
@@ -21,7 +21,7 @@ module ArSync::ModelBase::ClassMethods
21
21
  end
22
22
 
23
23
  def _each_sync_parent(&block)
24
- _sync_parents_info.each(&block)
24
+ _sync_parents_info.each { |parent, options| block.call(parent, **options) }
25
25
  superclass._each_sync_parent(&block) if superclass < ActiveRecord::Base
26
26
  end
27
27
 
@@ -46,32 +46,32 @@ module ArSync::ModelBase::ClassMethods
46
46
  @_sync_self = true
47
47
  names.each do |name|
48
48
  _sync_children_info[name] = nil
49
- _sync_define name, option, &data_block
49
+ _sync_define name, **option, &data_block
50
50
  end
51
51
  end
52
52
 
53
53
  def sync_has_many(name, **option, &data_block)
54
54
  _sync_children_info[name] = [:many, option, data_block]
55
- _sync_has_many name, option, &data_block
55
+ _sync_has_many name, **option, &data_block
56
56
  end
57
57
 
58
58
  def sync_has_one(name, **option, &data_block)
59
59
  _sync_children_info[name] = [:one, option, data_block]
60
- _sync_define name, option, &data_block
60
+ _sync_define name, **option, &data_block
61
61
  end
62
62
 
63
63
  def _sync_has_many(name, order: :asc, limit: nil, preload: nil, association: nil, **option, &data_block)
64
64
  raise "order not in [:asc, :desc] : #{order}" unless %i[asc desc].include? order
65
65
  if data_block.nil? && preload.nil?
66
66
  underscore_name = name.to_s.underscore.to_sym
67
- preload = lambda do |records, _context, params|
67
+ preload = lambda do |records, _context, **params|
68
68
  ArSerializer::Field.preload_association(
69
69
  self, records, association || underscore_name,
70
70
  order: (!limit && params && params[:order]) || order,
71
71
  limit: [params && params[:limit]&.to_i, limit].compact.min
72
72
  )
73
73
  end
74
- data_block = lambda do |preloaded, _context, params|
74
+ data_block = lambda do |preloaded, _context, **params|
75
75
  records = preloaded ? preloaded[id] || [] : send(name)
76
76
  next records unless limit || order == :asc
77
77
  ArSync::CollectionWithOrder.new(
@@ -80,7 +80,7 @@ module ArSync::ModelBase::ClassMethods
80
80
  limit: [params && params[:limit]&.to_i, limit].compact.min
81
81
  )
82
82
  end
83
- serializer_data_block = lambda do |preloaded, _context, _params|
83
+ serializer_data_block = lambda do |preloaded, _context, **_params|
84
84
  preloaded ? preloaded[id] || [] : send(name)
85
85
  end
86
86
  params_type = { limit?: :int, order?: [{ :* => %w[asc desc] }, 'asc', 'desc'] }
@@ -150,12 +150,6 @@ module ArSync::ModelBase::ClassMethods
150
150
  @_sync_belongs_to_info_before_mutation ||= _sync_current_belongs_to_info
151
151
  end
152
152
 
153
- before_save on: :create do
154
- @_sync_parents_info_before_mutation ||= {}
155
- @_sync_watch_values_before_mutation ||= {}
156
- @_sync_belongs_to_info_before_mutation ||= {}
157
- end
158
-
159
153
  %i[create update destroy].each do |action|
160
154
  after_commit on: action do
161
155
  next if ArSync.skip_notification?
@@ -33,10 +33,10 @@ module ArSync::ModelBase::InstanceMethods
33
33
  def _serializer_field_value(name)
34
34
  field = self.class._serializer_field_info name
35
35
  preloadeds = field.preloaders.map do |preloader|
36
- args = [[self], nil, {}]
36
+ args = [[self], nil]
37
37
  preloader.call(*(preloader.arity < 0 ? args : args.take(preloader.arity)))
38
38
  end
39
- instance_exec(*preloadeds, nil, {}, &field.data_block)
39
+ instance_exec(*preloadeds, nil, &field.data_block)
40
40
  end
41
41
 
42
42
  def _sync_current_belongs_to_info
@@ -65,11 +65,13 @@ module ArSync::ModelBase::InstanceMethods
65
65
  parents_was = parents.map { nil }
66
66
  elsif action == :destroy
67
67
  parents_was = _sync_parents_info_before_mutation
68
+ return unless parents_was
68
69
  parents = parents_was.map { nil }
69
70
  else
70
71
  parents_was = _sync_parents_info_before_mutation
72
+ return unless parents_was
71
73
  parents = _sync_current_parents_info
72
- column_values_was = _sync_watch_values_before_mutation
74
+ column_values_was = _sync_watch_values_before_mutation || {}
73
75
  column_values = _sync_current_watch_values
74
76
  end
75
77
  parents_was.zip(parents).each do |(parent_was, info_was), (parent, info)|
@@ -109,6 +111,7 @@ module ArSync::ModelBase::InstanceMethods
109
111
 
110
112
  def _sync_notify_self
111
113
  belongs_was = _sync_belongs_to_info_before_mutation
114
+ return unless belongs_was
112
115
  belongs = _sync_current_belongs_to_info
113
116
  belongs.each do |name, info|
114
117
  next if belongs_was[name] == info
@@ -10,17 +10,14 @@ module ArSync::TypeScript
10
10
  end
11
11
 
12
12
  def self.generate_type_definition(api_class)
13
+ types = ArSerializer::TypeScript.related_serializer_types([api_class]).reject { |t| t.type == api_class }
13
14
  [
14
- ArSerializer::TypeScript.generate_type_definition(api_related_classes(api_class)),
15
+ types.map { |t| data_type_definition t },
16
+ types.map { |t| query_type_definition t },
15
17
  request_type_definition(api_class)
16
18
  ].join "\n"
17
19
  end
18
20
 
19
- def self.api_related_classes(api_class)
20
- classes = ArSerializer::TypeScript.related_serializer_types([api_class]).map(&:type)
21
- classes - [api_class]
22
- end
23
-
24
21
  def self.request_type_definition(api_class)
25
22
  type = ArSerializer::GraphQL::TypeClass.from api_class
26
23
  definitions = []
@@ -98,4 +95,42 @@ module ArSync::TypeScript
98
95
  }
99
96
  CODE
100
97
  end
98
+
99
+ def self.query_type_definition(type)
100
+ field_definitions = type.fields.map do |field|
101
+ association_type = field.type.association_type
102
+ if association_type
103
+ qname = "Type#{association_type.name}Query"
104
+ if field.args.empty?
105
+ "#{field.name}?: true | #{qname} | { attributes?: #{qname} }"
106
+ else
107
+ "#{field.name}?: true | #{qname} | { params: #{field.args_ts_type}; attributes?: #{qname} }"
108
+ end
109
+ else
110
+ "#{field.name}?: true"
111
+ end
112
+ end
113
+ field_definitions << "'*'?: true"
114
+ query_type_name = "Type#{type.name}Query"
115
+ base_query_type_name = "Type#{type.name}QueryBase"
116
+ <<~TYPE
117
+ export type #{query_type_name} = keyof (#{base_query_type_name}) | Readonly<(keyof (#{base_query_type_name}))[]> | #{base_query_type_name}
118
+ export interface #{base_query_type_name} {
119
+ #{field_definitions.map { |line| " #{line}" }.join("\n")}
120
+ }
121
+ TYPE
122
+ end
123
+
124
+ def self.data_type_definition(type)
125
+ field_definitions = []
126
+ type.fields.each do |field|
127
+ field_definitions << "#{field.name}: #{field.type.ts_type}"
128
+ end
129
+ field_definitions << "_meta?: { name: '#{type.name}'; query: Type#{type.name}QueryBase }"
130
+ <<~TYPE
131
+ export interface Type#{type.name} {
132
+ #{field_definitions.map { |line| " #{line}" }.join("\n")}
133
+ }
134
+ TYPE
135
+ end
101
136
  end
@@ -1,3 +1,3 @@
1
1
  module ArSync
2
- VERSION = '1.0.3'
2
+ VERSION = '1.0.4'
3
3
  end
@@ -78,7 +78,7 @@ module ArSync
78
78
  [
79
79
  '//= require ar_sync',
80
80
  '//= require action_cable',
81
- '//= require ar_sync_actioncable_adapter',
81
+ '//= require ar_sync_action_cable_adapter',
82
82
  'ArSyncModel.setConnectionAdapter(new ArSyncActionCableAdapter(ActionCable))'
83
83
  ].join("\n") + "\n",
84
84
  before: '//= require_tree .'
@@ -2,6 +2,6 @@
2
2
  var modules = { actioncable: window.ActionCable }
3
3
  var exports = {}
4
4
  function require(name) { return modules[name] }
5
- <%= File.read File.dirname(__FILE__) + '/../../../core/ActioncableAdapter.js' %>
5
+ <%= File.read File.dirname(__FILE__) + '/../../../core/ActionCableAdapter.js' %>
6
6
  window.ArSyncActionCableAdapter = ActionCableAdapter
7
7
  }())
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ar_sync
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.3
4
+ version: 1.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - tompng
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-12-16 00:00:00.000000000 Z
11
+ date: 2020-01-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -28,16 +28,16 @@ dependencies:
28
28
  name: ar_serializer
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - '='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 1.0.0
33
+ version: '0'
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - '='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
- version: 1.0.0
40
+ version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
@@ -111,8 +111,8 @@ files:
111
111
  - ar_sync.gemspec
112
112
  - bin/console
113
113
  - bin/setup
114
- - core/ActioncableAdapter.d.ts
115
- - core/ActioncableAdapter.js
114
+ - core/ActionCableAdapter.d.ts
115
+ - core/ActionCableAdapter.js
116
116
  - core/ArSyncApi.d.ts
117
117
  - core/ArSyncApi.js
118
118
  - core/ArSyncModel.d.ts
@@ -143,7 +143,7 @@ files:
143
143
  - lib/generators/ar_sync/types/types_generator.rb
144
144
  - package-lock.json
145
145
  - package.json
146
- - src/core/ActioncableAdapter.ts
146
+ - src/core/ActionCableAdapter.ts
147
147
  - src/core/ArSyncApi.ts
148
148
  - src/core/ArSyncModel.ts
149
149
  - src/core/ArSyncStore.ts
@@ -154,7 +154,7 @@ files:
154
154
  - src/index.ts
155
155
  - tsconfig.json
156
156
  - vendor/assets/javascripts/ar_sync.js.erb
157
- - vendor/assets/javascripts/ar_sync_actioncable_adapter.js.erb
157
+ - vendor/assets/javascripts/ar_sync_action_cable_adapter.js.erb
158
158
  homepage: https://github.com/tompng/ar_sync
159
159
  licenses:
160
160
  - MIT
@@ -174,8 +174,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
174
174
  - !ruby/object:Gem::Version
175
175
  version: '0'
176
176
  requirements: []
177
- rubyforge_project:
178
- rubygems_version: 2.7.3
177
+ rubygems_version: 3.1.2
179
178
  signing_key:
180
179
  specification_version: 4
181
180
  summary: ActiveRecord - JavaScript Sync