rails-archiver 0.1.4 → 0.1.5

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: b647fb72150d65930552bb7c012b90df5c3f8d74
4
- data.tar.gz: ba295dee7a18aec3b628c73d6e9cf4836e38aa20
3
+ metadata.gz: e89e5ca01be0d05982ac7fa26975124829306cae
4
+ data.tar.gz: 6780627670479f1c424524a8deda72fa1c929469
5
5
  SHA512:
6
- metadata.gz: ed11fad708c175d2c6f8bcb5fe557a722ae0b414da057c9ec7fed6aead8b919cb383d910fc13d7aca9e15252fb84cbfe64e1dc0396b39ef9ed2be9f58edb6e71
7
- data.tar.gz: 44c2c0a496b2301d57a3738ce6d59c7dec05d3e339c56e261fd20b06a822c3d38c4f80f31ae3e75b6c20783518f72fcb47f1c5cd8168a9de1edcaa609f55aa25
6
+ metadata.gz: bba0013c0242a752fa52e28c25c3514a531bec16b0d50562897db64c932ab021aa4e9466d770eed74b12bb20d753e2d71bc2028001ef5f032ecfb098075f99fc
7
+ data.tar.gz: 244cb0624a39829ac8ecc00cc5e335ea4bcd27c86590b1e29de418bd022f2e80b97e1b0bce343ae72bf13fdc2639434577d264b2f6ce6ef5f7a5370e4ac9b5e6
@@ -39,18 +39,54 @@ module RailsArchiver
39
39
  def archive
40
40
  @logger.info("Starting archive of #{@model.class.name} #{@model.id}")
41
41
  @hash = {}
42
- _visit_association(@model)
42
+ visit_nodes(@model)
43
+ save_additional_data
43
44
  @logger.info('Completed loading data')
44
45
  @archive_location = @transport.store_archive(@hash)
45
46
  if @model.attribute_names.include?('archived')
46
47
  @model.update_attribute(:archived, true)
47
48
  end
48
- @logger.info('Deleting rows')
49
- _delete_records if @options[:delete_records]
50
- @logger.info('All records deleted')
49
+ if @options[:delete_records]
50
+ @logger.info('Deleting rows')
51
+ _delete_records
52
+ @logger.info('All records deleted')
53
+ end
51
54
  @hash
52
55
  end
53
56
 
57
+ # Use this method to add more data into the archive hash which isn't
58
+ # directly accessible from the parent model. Call `visit_nodes` on each
59
+ # set of data you want to add. To be overridden by subclasses.
60
+ def save_additional_data
61
+ end
62
+
63
+ # Used to visit an association, and recursively calls down to
64
+ # all child objects through all other allowed associations.
65
+ # @param node [ActiveRecord::Base|Array<ActiveRecord::Base>]
66
+ # any object(s) that inherits from ActiveRecord::Base
67
+ def visit_nodes(node)
68
+ return if node.blank?
69
+ if node.respond_to?(:each) # e.g. a list of nodes from a has_many association
70
+ node.each { |n| visit_nodes(n) }
71
+ else
72
+ class_name = node.class.name
73
+ @hash[class_name] ||= Set.new
74
+ @hash[class_name] << visit(node)
75
+ get_associations(node).each do |assoc|
76
+ @logger.debug("Visiting #{assoc.name}")
77
+ new_nodes = node.send(assoc.name)
78
+ next if new_nodes.blank?
79
+
80
+ if new_nodes.respond_to?(:find_each)
81
+ new_nodes.find_each { |n| visit_nodes(n) }
82
+ else
83
+ visit_nodes(new_nodes)
84
+ end
85
+ end
86
+
87
+ end
88
+ end
89
+
54
90
  # Returns a single object in the database represented as a hash.
55
91
  # Does not account for any associations, only prints out the columns
56
92
  # associated with the object as they relate to the current schema.
@@ -92,6 +128,13 @@ module RailsArchiver
92
128
  @logger.info("Finished deleting from #{table}")
93
129
  end
94
130
 
131
+ # @return [RailsArchiver::Unarchiver]
132
+ def unarchiver
133
+ unarchiver = RailsArchiver::Unarchiver.new(@model, :logger => @logger)
134
+ unarchiver.transport = self.transport
135
+ unarchiver
136
+ end
137
+
95
138
  protected
96
139
 
97
140
  # Callback that runs after deletion is finished.
@@ -131,31 +174,5 @@ module RailsArchiver
131
174
  end
132
175
  end
133
176
 
134
- # Used to visit an association, and recursively calls down to
135
- # all child objects through all other allowed associations.
136
- # @param node [ActiveRecord::Base|Array<ActiveRecord::Base>]
137
- # any object(s) that inherits from ActiveRecord::Base
138
- def _visit_association(node)
139
- return if node.blank?
140
- if node.respond_to?(:each) # e.g. a list of nodes from a has_many association
141
- node.each { |n| _visit_association(n) }
142
- else
143
- class_name = node.class.name
144
- @hash[class_name] ||= Set.new
145
- @hash[class_name] << visit(node)
146
- get_associations(node).each do |assoc|
147
- @logger.debug("Visiting #{assoc.name}")
148
- new_nodes = node.send(assoc.name)
149
- next if new_nodes.blank?
150
-
151
- if new_nodes.respond_to?(:find_each)
152
- new_nodes.find_each { |n| _visit_association(n) }
153
- else
154
- _visit_association(new_nodes)
155
- end
156
- end
157
-
158
- end
159
- end
160
177
  end
161
178
  end
@@ -32,15 +32,18 @@ module RailsArchiver
32
32
  # model) depending on the transport being used.
33
33
  def unarchive(location=nil)
34
34
  @errors = []
35
+ source = @model ? @model.class.name : location
35
36
  @logger.info('Downloading JSON file')
36
37
  hash = @transport.retrieve_archive(location)
37
- @logger.info("Loading #{@model.class.name}")
38
+ @logger.info("Loading #{source}")
38
39
  load_classes(hash)
39
- @model.reload
40
- if @model.attribute_names.include?('archived')
41
- @model.update_attribute(:archived, false)
40
+ if @model
41
+ @model.reload
42
+ if @model.attribute_names.include?('archived')
43
+ @model.update_attribute(:archived, false)
44
+ end
42
45
  end
43
- @logger.info("#{@model.class.name} load complete!")
46
+ @logger.info("#{source} load complete!")
44
47
  end
45
48
 
46
49
  # Load a list of general classes that were saved as JSON.
@@ -98,12 +101,12 @@ module RailsArchiver
98
101
  model = klass.where(klass.primary_key => hash[klass.primary_key]).first
99
102
  if model.nil?
100
103
  model = klass.new
101
- model.send(:attributes=, attrs, false)
104
+ _assign_attributes(model, attrs)
102
105
  # can't set this in the attribute hash, it'll be overridden. Need
103
106
  # to set it manually.
104
107
  model[klass.primary_key] = hash[klass.primary_key]
105
108
  else
106
- model.send(:attributes=, attrs, false)
109
+ _assign_attributes(model, attrs)
107
110
  end
108
111
 
109
112
  model
@@ -111,6 +114,16 @@ module RailsArchiver
111
114
 
112
115
  private
113
116
 
117
+ # @param model [ActiveRecord::Base]
118
+ # @param attrs [Hash]
119
+ def _assign_attributes(model, attrs)
120
+ if model.method(:attributes=).arity == 1
121
+ model.attributes = attrs
122
+ else
123
+ model.send(:attributes=, attrs, false)
124
+ end
125
+ end
126
+
114
127
  def _get_transport(symbol_or_object)
115
128
  if symbol_or_object.is_a?(Symbol)
116
129
  klass = if symbol_or_object.present?
@@ -1,8 +1,8 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'rails-archiver'
3
3
  s.require_paths = %w(. lib lib/rails-archiver)
4
- s.version = '0.1.4'
5
- s.date = '2018-06-01'
4
+ s.version = '0.1.5'
5
+ s.date = '2018-06-04'
6
6
  s.summary = 'Fully archive a Rails model'
7
7
  s.description = <<-EOF
8
8
  EOF
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails-archiver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Orner
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-06-01 00:00:00.000000000 Z
11
+ date: 2018-06-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails