archivist 1.0.5.1 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. data/MIT-LICENSE +17 -15
  2. data/README.rdoc +3 -0
  3. data/Rakefile +36 -0
  4. data/lib/archivist.rb +0 -1
  5. data/lib/archivist/archive.rb +10 -4
  6. data/lib/archivist/base.rb +59 -51
  7. data/lib/archivist/migration.rb +1 -1
  8. data/lib/archivist/version.rb +3 -0
  9. data/lib/tasks/archivist_tasks.rake +4 -0
  10. data/test/archive_test.rb +16 -0
  11. data/test/archivist_test.rb +17 -0
  12. data/test/base_test.rb +342 -0
  13. data/test/db/migrate/01_add_to_some_models.rb +11 -0
  14. data/test/db_test.rb +42 -0
  15. data/test/dummy/README.rdoc +261 -0
  16. data/test/dummy/Rakefile +7 -0
  17. data/test/dummy/app/assets/javascripts/application.js +15 -0
  18. data/test/dummy/app/assets/stylesheets/application.css +13 -0
  19. data/test/dummy/app/controllers/application_controller.rb +3 -0
  20. data/test/dummy/app/helpers/application_helper.rb +2 -0
  21. data/test/dummy/app/models/another_model.rb +4 -0
  22. data/test/dummy/app/models/some_model.rb +12 -0
  23. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  24. data/test/dummy/config.ru +4 -0
  25. data/test/dummy/config/application.rb +61 -0
  26. data/test/dummy/config/boot.rb +10 -0
  27. data/test/dummy/config/database.yml +19 -0
  28. data/test/dummy/config/environment.rb +5 -0
  29. data/test/dummy/config/environments/development.rb +37 -0
  30. data/test/dummy/config/environments/production.rb +67 -0
  31. data/test/dummy/config/environments/test.rb +37 -0
  32. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  33. data/test/dummy/config/initializers/inflections.rb +15 -0
  34. data/test/dummy/config/initializers/mime_types.rb +5 -0
  35. data/test/dummy/config/initializers/secret_token.rb +7 -0
  36. data/test/dummy/config/initializers/session_store.rb +8 -0
  37. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  38. data/test/dummy/config/locales/en.yml +5 -0
  39. data/test/dummy/config/routes.rb +58 -0
  40. data/test/dummy/db/schema.rb +41 -0
  41. data/test/dummy/db/test.sqlite3 +0 -0
  42. data/test/dummy/lib/this_module.rb +3 -0
  43. data/test/dummy/log/development.log +7 -0
  44. data/test/dummy/log/test.log +35228 -0
  45. data/test/dummy/public/404.html +26 -0
  46. data/test/dummy/public/422.html +26 -0
  47. data/test/dummy/public/500.html +25 -0
  48. data/test/dummy/public/favicon.ico +0 -0
  49. data/test/dummy/script/rails +6 -0
  50. data/test/dummy/test/fixtures/another_models.yml +9 -0
  51. data/test/dummy/test/fixtures/some_models.yml +13 -0
  52. data/test/dummy/test/unit/another_model_test.rb +7 -0
  53. data/test/dummy/test/unit/some_model_test.rb +7 -0
  54. data/test/factories/some_model_factory.rb +8 -0
  55. data/test/migration_test.rb +69 -0
  56. data/test/test_helper.rb +27 -0
  57. metadata +210 -16
  58. data/README.md +0 -162
data/README.md DELETED
@@ -1,162 +0,0 @@
1
- README.md
2
- =================
3
-
4
- This gem is intended as a direct replacement for acts\_as\_archive (AAA)
5
- in Rails 3 apps with most of the same functionality and wrapping AAA's
6
- methods in aliases to maintain compatibility for some time. Thanks to
7
- [Winton Welsh](https://github.com/winton "Winton on github") for his
8
- original work on AAA, it is good solution to a problem that makes
9
- maintaining audit records a breeze.
10
-
11
- TOC
12
- ---
13
- 1. <a href="#requirements">Requirements</a>
14
- 2. <a href="#install">Install</a>
15
- 2. <a href="#update_models">Update models</a>
16
- 2. <a href="#add_archive_tables">Add Archive tables</a>
17
- 1. <a href="#basic_usage">Basic Usage</a>
18
- 1. <a href="#additional_options">Additional Options</a>
19
- 1. <a href="#multiple_archives">Allowing multiple archived copies</a>
20
- 1. <a href="#associating">Associating archive to original</a>
21
- 1. <a href="#including">Including External modules</a>
22
- 1. <a href="#customizing">Customizing `copy_self_to_archive`</a>
23
- 1. <a href="#contributing">Contributing</a>
24
- 1. <a href="#todo">TODO</a>
25
-
26
- <a name="requirements"></a>
27
- Requirements
28
- ------------
29
- This gem is intended to be used with ActiveRecord/ActiveSupport 3.0.1 and later.
30
-
31
- <a name="install"></a>
32
- Install
33
- -------
34
- **Gemfile**:
35
- gem 'archivist'
36
-
37
- <a name="update_models"></a>
38
- Update models
39
- -------------
40
- add `has_archive` to your models:
41
- class SomeModel < ActiveRecord::Base
42
- has_archive
43
- end
44
-
45
- N.B. if you have any serialized attributes the has\_archive declaration *MUST* be after the serialization declarations or they will not be preserved and things will break when you try to deserialize the attributes. This is an unfortunate side effect of the declarative nature of both `serialize` and `has_archive` and I couldn't think of any way of getting around it that wouldn't make the entire gem really slow, any suggestions on this are welcome.
46
-
47
- i.e.
48
- class AnotherModel < ActiveRecord::Base
49
- serialize(:some_array,Array)
50
- has_archive
51
- end
52
-
53
- *NOT*
54
- class ThisModel < ActiveRecord::Base
55
- has_archive
56
- serialize(:a_hash,Hash)
57
- end
58
-
59
- <a name="add_archive_tables"></a>
60
- Add Archive tables
61
- ------------------
62
- There are two ways to do this, the first is to use the built in updater like acts as archive.
63
- `Archivist.update SomeModel`
64
- Currently this doesn't support adding indexes automatically (AAA does) but I'm working on doing multi column indexes (any help is greatly appreciated)
65
-
66
- The second way of adding archive tables is to build a migration yourself, if you're wanting to keep track of who triggered the archive or inject some other information you'll have to add those columns manually and pass a block into `copy_self_to_archive` before calling `delete!` or `destroy!`.
67
-
68
- <a name="basic_usage"></a>
69
- Basic Usage
70
- -----------
71
- Use `destroy`, `delete`, `destroy_all` as usual and the data will get moved to the archive table. If you really want the data to go away you can still do so by simply calling `destroy!` etc. This bypasses the archiving step but leaves your callback chain intact where appropriate.
72
-
73
- Migrations affecting the columns on the original model's table will be applied to the archive table as well.
74
-
75
- <a name="additional_options"></a>
76
- Additional Options
77
- ------------------
78
- <a name="multiple_archives"></a>
79
- ###Allowing multiple archived copies
80
- By default `copy_self_to_archive` just keeps updating a single instance of the archived record, this behavior is find if you're just trying to keep your main working table clean but can be problematic if you need a history of changes to a record.
81
- This behavior can be changed to allow multiple copies of a archived record to be created by setting the `:allow_multiple_archives` to true in the options hash when calling `has_archive`.
82
-
83
- ####Example:
84
- <pre>
85
- class SpecialModel &lt; AR::Base
86
- has_archive :allow_multiple_archives=&gt; true
87
- end
88
- </pre>
89
-
90
- <a name="associating"></a>
91
- ### Associating archive to original
92
- The default here is to not associate the archived records in any way to the originals. But, if you're keeping a history of changes to a record the archived copies can be associated automatically with the 'original' by setting the `associate_with_original` option to true.
93
-
94
- *N.B.* Using this option automatically sets `allow_multiple_archives` to true
95
-
96
- ####Example
97
- <pre>
98
- class SpecialModel &lt; AR::Base
99
- has_archive :associate_with_original=&gt;true
100
- end
101
- </pre>
102
-
103
- allows for calls like:
104
-
105
- `SpecialModel.first.archived_special_models`
106
-
107
- or
108
-
109
- `SpecialModel::Archive.first.special_model`
110
-
111
- <a name="including"></a>
112
- ###Including External modules
113
- If you want to include additional functionality in the `Archive` class you can pass in an array of module constants, this will allow adding scopes and methods to this class without any monkey patching.
114
-
115
- ####Example
116
- <pre>
117
- class MyModel &lt; AR:Base
118
- has\_archive :included\_modules=&gt;BigBadModule
119
- end
120
- </pre>
121
- or if multiple modules are needed
122
- <pre>
123
- class MyModel &lt; AR:Base
124
- has\_archive :included\_modules=&gt;[BigBadModule,MyScopes,MyArchiveMethods]
125
- end
126
- </pre>
127
-
128
- <a name="customizing"></a>
129
- ###Customizing copy\_self\_to\_archive
130
- A block can be passed into `copy_self_to_archive` which takes a single argument (the new archived record)
131
-
132
- ####Example:
133
- Supposing we have added an archiver\_id column to our archive table we can pass a block into the `copy_self_to_archive` method setting this value. The block gets called immediately before saving the archived record so all of the attributes have been copied over from the original and are available for use in the block.
134
- <pre>
135
- class SpecialModel &lt; AR:Base
136
- has_archive
137
- def archive!(user)
138
- self.copy_self_to_archive do |archive|
139
- archive.archiver_id = user.id
140
- end
141
- end
142
- end
143
- </pre>
144
-
145
- <a name="contributing"></a>
146
- Contributing
147
- ------------
148
- If you'd like to help out please feel free to fork and browse the TODO list below or add a feature that you're in need of. Then send a pull request my way and I'll happily merge in well tested changes.
149
-
150
- Also, I use autotest and MySQL but [nertzy (Grant Hutchins)](https://github.com/nertzy "Grant on github") was kind enough to add support for testing against Postgres using the pg gem.
151
-
152
- <a name="todo"></a>
153
- TODO
154
- ----
155
-
156
- * <del>Maintain seralized attributes from original model</del>
157
- * <del>allow passing of a block into copy\_to\_archive</del>
158
- * give Archive scopes from parent (may only work w/ 1.9 since scopes are Procs)
159
- * <del>give subclass Archive its parent's methods (method\_missing?)</del>
160
- * <del>associate SomeModel::Archive with SomeModel (if archiving more than one copy)</del>
161
- * associate Archive with other models (SomeModel.reflect\_on\_all\_associations?)
162
- * make archive\_all method chain-able with scopes and other finder type items