has_archive 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: ea41cc24909aa00d12d670bb87b110c61b0c26d0
4
- data.tar.gz: afdb00510af34d31e7e74520202af15fcd827010
3
+ metadata.gz: 2bfae8a910ef0352a264b4bccfe65f706b8d7193
4
+ data.tar.gz: 76135d10842de579e7e205ab65625b3709e73d53
5
5
  SHA512:
6
- metadata.gz: 152d2c9f84f6ffd3d2fdab88344bd4bda502760fb3e25e88154bc5fdd7c7841ab9d4f169df1471869874b00b126913b07ec8baaa88d9df5f6c0cd632e1d12c0b
7
- data.tar.gz: 24f58131b74c3087aaa406eb3d3291885564e18304ec71017644ed3bab7baeb82a6381b9d3af11f75eda888198e91d69ce8ab4dd9652e949664dd5ce67e6a804
6
+ metadata.gz: 9cf4bdf0569b5e0f5cada23bc7e6d6bb5c57e5a0f4a6166bb6ab60ac1f7021c1271ad4cbf8538cb03942127439d4355cf87e9423769e00a8ba39ffd15ebe4797
7
+ data.tar.gz: 1025e1e9bfffb0206ac101c90bfb70c6df26f8c8f6947cda5fb75b9354ce030f229f946b112eef84c362f7ce57f532c9d9b459ab82fbf7b76305c8f86a55dc81
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # HasArchive
2
2
 
3
- This project was born out of frustration with all the options I've found so far for archiving non-current ActiveRecord backed records, so I'm going to give it a go. The initial target will be Rails 4 (point two-ish) on Postgres, and if it goes somewhere, I will begin looking at what else we can support.
3
+ This project was born out of frustration with all the options I've found so far for archiving non-current ActiveRecord backed models, so I'm going to give it a go. The initial target will be Rails 4 (point two-ish) on Postgres, and if it goes somewhere, I will begin looking at what else we can support.
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/has_archive.svg)](https://badge.fury.io/rb/has_archive)
6
+
7
+ Please halp. This is far from ready, but if this seems like a useful/worthwhile project to you, please feel free to dig and lend a hand.
4
8
 
5
9
  ## Installation
6
10
 
@@ -20,7 +24,19 @@ Or install it yourself as:
20
24
 
21
25
  ## Usage
22
26
 
23
- TODO: Halp. This is ready for nothing, so far, but if this seems like a useful/worthwhile project to you, please feel free to dig and lend a hand.
27
+ ### Setup
28
+
29
+ 1. First, create your archive tables for any models you wish to make archivable. There is a utility method (`HasArchive::MigrationManager.create_archive_for`) to help generate the starting migrations. It must match the columns for the archivable model, plus add an `archived_at` datetime field.
30
+
31
+ 2. Call `has_archive` in your models.
32
+
33
+ ### Archive Controls
34
+
35
+ Access the archive model directly with `ModelName::Archive`, or use the class method `archived` to union live and archived records.
36
+
37
+ Archive a record by calling `archive` on it. Optionally, `destroy` may be overridden to work as a "soft delete" by setting `Rails.configuration.has_archive.override_destroy` to `true`.
38
+
39
+ An archived record may be returned to the main table by calling `restore` on it.
24
40
 
25
41
  ## Development
26
42
 
data/lib/has_archive.rb CHANGED
@@ -11,13 +11,22 @@ module HasArchive
11
11
  eval <<-EVAL
12
12
  class #{base}::Archive < #{base}
13
13
  self.table_name = "#{base.to_s.underscore}_archives"
14
+
15
+ def restore
16
+ attrs = self.attributes
17
+ attrs.delete('archived_at')
18
+ restored = self.class.parent.new(attrs)
19
+ restored.save && self.destroy(for_real: true)
20
+ self
21
+ end
14
22
  end
15
23
  EVAL
16
24
  end
17
25
 
18
26
  module ClassMethods
19
- def archive
20
- self::Archive
27
+ def archived
28
+ union = self.unscoped.union(self::Archive.unscoped.select(self.attribute_names.map(&:to_sym)))
29
+ self.from(self.arel_table.create_table_alias(union, self.table_name.to_sym).to_sql)
21
30
  end
22
31
  end
23
32
 
@@ -31,7 +40,6 @@ module HasArchive
31
40
 
32
41
  def destroy(for_real: false)
33
42
  if !for_real && Rails.configuration.has_archive.override_destroy
34
- # puts "destroy has been overridden..."
35
43
  archive
36
44
  else
37
45
  super()
@@ -1,3 +1,3 @@
1
1
  module HasArchive
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_archive
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
  - Matt Morris
@@ -93,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
93
93
  version: '0'
94
94
  requirements: []
95
95
  rubyforge_project:
96
- rubygems_version: 2.2.0
96
+ rubygems_version: 2.4.5
97
97
  signing_key:
98
98
  specification_version: 4
99
99
  summary: Add archives to your ActiveRecord models