eac_rails_remotes 0.3.0 → 0.4.0

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: a6cc86581c903002e2e9913cb8d5177fcf6584335981aafe7f635a2e6bf91793
4
- data.tar.gz: 734e7de9b68fca2663e6ec6a932169ab7580e0597f5d12d17bf48f731b23e54b
3
+ metadata.gz: 20d00ca632f9889c6bb6158d9931d28de7d0fb41f5a803f9b375322c40f22185
4
+ data.tar.gz: 2549430c7a500b0faa2ba60ea86e110a081dcc27a30f24c501506163eeb66cdf
5
5
  SHA512:
6
- metadata.gz: bde88ca426dffcf997c6a79101d7165eb7fc3b15593650b3345772b7d8566d6355ed48fc3870d9e41160f5030a81ce558ad6adffb27b27ae5d0587df4d9f9ffb
7
- data.tar.gz: c57298ba0a909455c8b212dbabd35b9b353fae6cecc27abf320662574b85873eb43dd8780faba300bd2361fbf30726b64dfc3642353a2ebe1ece03db8769572a
6
+ metadata.gz: ca3bfc3d8dcbe6d927b8a660ec2b4ef8fe1b16cf3885a34e1bdf42fe7ab8bc2b12e577342b523dcd7c4fa21be6ed9fb170b30e27f3b9b74943f71afe4b5d7303
7
+ data.tar.gz: 8db027139ca17da7d9149cc9bd4ea22b3ba82051358e46ae1ddfe8587153928c1e48e06014fb07c344bd220ac15564c79a7e3b6cbeb9ece9937bac27de7e814f
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module EacRailsRemotes
6
+ class Instance < ::ActiveRecord::Base
7
+ class Export < ::SimpleDelegator
8
+ acts_as_instance_method
9
+
10
+ def result
11
+ Rails.logger.info("Exporting #{self}")
12
+ t = target || entity_class.new
13
+ t.attributes = target_attributes
14
+ if t.save
15
+ update!(export_status: EXPORT_STATUS_OK, export_message: '', target: t)
16
+ else
17
+ update!(export_status: EXPORT_STATUS_ERROR,
18
+ export_message: target_export_message(t))
19
+ end
20
+ end
21
+
22
+ protected
23
+
24
+ # @return [Hash]
25
+ def target_attributes
26
+ parsed_data.map { |k, v| export_target_attribute(k, v) }.compact_blank.to_h
27
+ end
28
+
29
+ # @return [String]
30
+ def target_export_message(target)
31
+ "ATTRIBUTES: #{target.attributes}\nERRORS: #{target.errors.messages}\n"
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,43 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'eac_ruby_utils/core_ext'
4
+
5
+ module EacRailsRemotes
6
+ class Instance < ::ActiveRecord::Base
7
+ class ExportTargetAttribute
8
+ acts_as_instance_method
9
+ enable_simple_cache
10
+ common_constructor :instance, :attribute, :value
11
+
12
+ # @return [Array]
13
+ def result
14
+ return nil unless valid?
15
+ return [attribute, value] unless entity_association_class
16
+
17
+ result_by_association
18
+ end
19
+
20
+ protected
21
+
22
+ # @return [ActiveRecord::Reflection::BelongsToReflection, nil]
23
+ def entity_association_class_uncached
24
+ instance.entity_class.reflect_on_all_associations(:belongs_to)
25
+ .find { |x| x.foreign_key.to_sym == attribute.to_sym }
26
+ end
27
+
28
+ # @return [Array]
29
+ def result_by_association
30
+ ri = instance.class.find_by(
31
+ source: instance.source, entity: entity_association_class.klass.name, code: value
32
+ )
33
+ [entity_association_class.name, ri&.assert_target]
34
+ end
35
+
36
+ # @return [Boolean]
37
+ def valid?
38
+ instance.entity_class.primary_key != attribute.to_s &&
39
+ instance.entity_class.new.respond_to?("#{attribute}=")
40
+ end
41
+ end
42
+ end
43
+ end
@@ -20,6 +20,16 @@ module EacRailsRemotes
20
20
  where.not(export_status: EXPORT_STATUS_OK)
21
21
  }
22
22
 
23
+ # @return [Object]
24
+ def parsed_data
25
+ data.if_present(nil) { |v| ::EacRubyUtils::Yaml.load(v) }
26
+ end
27
+
28
+ # @param value [Object]
29
+ def parsed_data=(value)
30
+ self.data = value.if_present(nil) { |v| ::EacRubyUtils::Yaml.dump(v) }
31
+ end
32
+
23
33
  def to_s
24
34
  "#{source}|#{entity}|#{code}"
25
35
  end
@@ -34,53 +44,18 @@ module EacRailsRemotes
34
44
  raise "Export failed (ID: #{id}, export message: \"#{export_message}\""
35
45
  end
36
46
 
37
- def export
38
- Rails.logger.info("Exporting #{self}")
39
- t = target || entity_class.new
40
- t.attributes = target_attributes
41
- if t.save
42
- update!(export_status: EXPORT_STATUS_OK, export_message: '', target: t)
43
- else
44
- update!(export_status: EXPORT_STATUS_ERROR,
45
- export_message: target_export_message(t))
46
- end
47
- end
48
-
49
- private
50
-
51
- def target_attributes
52
- EacRubyUtils::Yaml.load(data).to_h { |k, v| target_attribute(k, v) }
53
- end
54
-
55
- def target_attribute(attr, value)
56
- a = entity_association_class(attr)
57
- return [attr, value] unless a
58
-
59
- ri = self.class.find_by(source: source, entity: a.klass.name, code: value)
60
- [a.name, ri&.target]
61
- end
62
-
63
- # @return [ActiveRecord::Reflection::BelongsToReflection, nil]
64
- def entity_association_class(attribute)
65
- entity_class.reflect_on_all_associations(:belongs_to)
66
- .find { |x| x.foreign_key.to_sym == attribute.to_sym }
67
- end
68
-
47
+ # @return [Class<ActiveRecord::Base>]
69
48
  def entity_class
70
49
  entity.constantize
71
50
  end
72
51
 
73
- def target_export_message(target)
74
- "ATTRIBUTES: #{target.attributes}\nERRORS: #{target.errors.messages}\n"
75
- end
76
-
77
52
  class << self
78
53
  # @param record [Hash]
79
54
  # @return [EacRailsRemotes::Instance]
80
55
  def import(record)
81
56
  ri = where(source: record.fetch(:source), entity: sanitize_entity(record.fetch(:entity)),
82
57
  code: record.fetch(:code)).first_or_initialize
83
- ri.data = record.fetch(:data).to_yaml
58
+ ri.parsed_data = record.fetch(:data)
84
59
  if ri.data_changed?
85
60
  ri.export_status = EXPORT_STATUS_NEW_DATA
86
61
  ri.save!
@@ -94,5 +69,7 @@ module EacRailsRemotes
94
69
  entity.is_a?(::Module) ? entity.name : entity.to_s
95
70
  end
96
71
  end
72
+
73
+ require_sub __FILE__, require_mode: :kernel
97
74
  end
98
75
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EacRailsRemotes
4
- VERSION = '0.3.0'
4
+ VERSION = '0.4.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: eac_rails_remotes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Put here the authors
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-09-05 00:00:00.000000000 Z
11
+ date: 2024-09-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: eac_active_scaffold
@@ -30,26 +30,6 @@ dependencies:
30
30
  - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 0.6.1
33
- - !ruby/object:Gem::Dependency
34
- name: eac_rails_gem_support
35
- requirement: !ruby/object:Gem::Requirement
36
- requirements:
37
- - - "~>"
38
- - !ruby/object:Gem::Version
39
- version: '0.10'
40
- - - ">="
41
- - !ruby/object:Gem::Version
42
- version: 0.10.1
43
- type: :runtime
44
- prerelease: false
45
- version_requirements: !ruby/object:Gem::Requirement
46
- requirements:
47
- - - "~>"
48
- - !ruby/object:Gem::Version
49
- version: '0.10'
50
- - - ">="
51
- - !ruby/object:Gem::Version
52
- version: 0.10.1
53
33
  - !ruby/object:Gem::Dependency
54
34
  name: eac_rails_utils
55
35
  requirement: !ruby/object:Gem::Requirement
@@ -70,34 +50,34 @@ dependencies:
70
50
  requirements:
71
51
  - - "~>"
72
52
  - !ruby/object:Gem::Version
73
- version: '0.122'
53
+ version: '0.123'
74
54
  type: :runtime
75
55
  prerelease: false
76
56
  version_requirements: !ruby/object:Gem::Requirement
77
57
  requirements:
78
58
  - - "~>"
79
59
  - !ruby/object:Gem::Version
80
- version: '0.122'
60
+ version: '0.123'
81
61
  - !ruby/object:Gem::Dependency
82
62
  name: eac_rails_gem_support
83
63
  requirement: !ruby/object:Gem::Requirement
84
64
  requirements:
85
65
  - - "~>"
86
66
  - !ruby/object:Gem::Version
87
- version: '0.9'
67
+ version: '0.10'
88
68
  - - ">="
89
69
  - !ruby/object:Gem::Version
90
- version: 0.9.1
70
+ version: 0.10.1
91
71
  type: :development
92
72
  prerelease: false
93
73
  version_requirements: !ruby/object:Gem::Requirement
94
74
  requirements:
95
75
  - - "~>"
96
76
  - !ruby/object:Gem::Version
97
- version: '0.9'
77
+ version: '0.10'
98
78
  - - ">="
99
79
  - !ruby/object:Gem::Version
100
- version: 0.9.1
80
+ version: 0.10.1
101
81
  description:
102
82
  email:
103
83
  executables: []
@@ -108,6 +88,8 @@ files:
108
88
  - app/assets/stylesheets/eac_rails_remotes/application.scss
109
89
  - app/controllers/eac_rails_remotes/instances_controller.rb
110
90
  - app/models/eac_rails_remotes/instance.rb
91
+ - app/models/eac_rails_remotes/instance/export.rb
92
+ - app/models/eac_rails_remotes/instance/export_target_attribute.rb
111
93
  - config/initializers/menus.rb
112
94
  - config/locales/en.yml
113
95
  - config/locales/pt-BR.yml