snaps 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (70) hide show
  1. checksums.yaml +15 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +157 -0
  4. data/Rakefile +30 -0
  5. data/app/assets/javascripts/snaps/application.js +13 -0
  6. data/app/assets/stylesheets/snaps/application.css +15 -0
  7. data/app/controllers/snaps/application_controller.rb +4 -0
  8. data/app/helpers/snaps/application_helper.rb +4 -0
  9. data/app/models/concerns/snaps/suppressor.rb +55 -0
  10. data/app/models/snaps/tag.rb +45 -0
  11. data/app/views/layouts/snaps/application.html.erb +14 -0
  12. data/config/routes.rb +2 -0
  13. data/db/migrate/20150321131740_create_snaps_tags.rb +12 -0
  14. data/lib/snaps.rb +61 -0
  15. data/lib/snaps/engine.rb +13 -0
  16. data/lib/snaps/snapshot.rb +42 -0
  17. data/lib/snaps/version.rb +3 -0
  18. data/lib/tasks/snaps_tasks.rake +4 -0
  19. data/spec/dummy/README.rdoc +28 -0
  20. data/spec/dummy/Rakefile +6 -0
  21. data/spec/dummy/app/assets/javascripts/application.js +13 -0
  22. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  23. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  24. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  25. data/spec/dummy/app/models/post.rb +3 -0
  26. data/spec/dummy/app/models/post_with_sections.rb +7 -0
  27. data/spec/dummy/app/models/section.rb +5 -0
  28. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  29. data/spec/dummy/bin/bundle +3 -0
  30. data/spec/dummy/bin/rails +4 -0
  31. data/spec/dummy/bin/rake +4 -0
  32. data/spec/dummy/bin/setup +29 -0
  33. data/spec/dummy/config.ru +4 -0
  34. data/spec/dummy/config/application.rb +26 -0
  35. data/spec/dummy/config/boot.rb +5 -0
  36. data/spec/dummy/config/database.yml +25 -0
  37. data/spec/dummy/config/environment.rb +5 -0
  38. data/spec/dummy/config/environments/development.rb +41 -0
  39. data/spec/dummy/config/environments/production.rb +79 -0
  40. data/spec/dummy/config/environments/test.rb +42 -0
  41. data/spec/dummy/config/initializers/assets.rb +11 -0
  42. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  43. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  44. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  45. data/spec/dummy/config/initializers/inflections.rb +16 -0
  46. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  47. data/spec/dummy/config/initializers/session_store.rb +3 -0
  48. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  49. data/spec/dummy/config/locales/en.yml +23 -0
  50. data/spec/dummy/config/routes.rb +4 -0
  51. data/spec/dummy/config/secrets.yml +22 -0
  52. data/spec/dummy/db/development.sqlite3 +0 -0
  53. data/spec/dummy/db/migrate/20150321145225_create_posts.rb +11 -0
  54. data/spec/dummy/db/migrate/20150323173024_create_sections.rb +11 -0
  55. data/spec/dummy/db/migrate/20150324074047_create_snaps_tags.snaps.rb +13 -0
  56. data/spec/dummy/db/schema.rb +41 -0
  57. data/spec/dummy/db/test.sqlite3 +0 -0
  58. data/spec/dummy/log/development.log +36 -0
  59. data/spec/dummy/log/test.log +472 -0
  60. data/spec/dummy/public/404.html +67 -0
  61. data/spec/dummy/public/422.html +67 -0
  62. data/spec/dummy/public/500.html +66 -0
  63. data/spec/dummy/public/favicon.ico +0 -0
  64. data/spec/dummy/spec/factories/posts.rb +11 -0
  65. data/spec/dummy/spec/factories/sections.rb +8 -0
  66. data/spec/integration/revision_spec.rb +214 -0
  67. data/spec/integration/revision_spec.rb! +8 -0
  68. data/spec/spec_helper.rb +26 -0
  69. data/spec/support/config/timecop.rb +11 -0
  70. metadata +247 -0
@@ -0,0 +1,15 @@
1
+ ---
2
+ !binary "U0hBMQ==":
3
+ metadata.gz: !binary |-
4
+ ODEzZWE0YzA0MjY2NTVhYmMxOTE3MGMwNWRjZmM3ZTNlMjcyYjIyYg==
5
+ data.tar.gz: !binary |-
6
+ M2RjYmJhZmUxYjE1Njc1YjY1YWQ5N2VhOGEyZGIyOTIxMDAxNWFjYw==
7
+ SHA512:
8
+ metadata.gz: !binary |-
9
+ MTU2NGY4ODcxOTcxZTJkYjI5NDFkZTlmNDJmMTgwMmVjMGVmNzZlMmQ2N2Zj
10
+ ODhmOTFlZThkZDZmYzk2NzI1ZDI2Njg0YWIzMWUyNWQyYjlmNDRiZTdiZWIw
11
+ MTVhOWQxNWMzNzg5YmI0NGE3YjBmNDNiNDE5ZDgzMGE5MzcwOGQ=
12
+ data.tar.gz: !binary |-
13
+ Nzc3MTAwMGMyNjE4Y2JmNjc4MDdkM2I3YWM1ZjgyYjI4NjE5YTkxNGUwMWY2
14
+ MjNhNzkwYzk3ZGM3MzY4MjNmMWFjNTEwZTZlZTE2NzNmZDU1YzlkNzlmZjIx
15
+ ODdlNmIwOGM1NTM2MTM3NTU0NzA2MGY5YzZkNWNlN2I4YzgwYjM=
@@ -0,0 +1,20 @@
1
+ Copyright 2015 Vangelis Tsoumenis
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,157 @@
1
+ # Snaps
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/snaps.svg)](http://badge.fury.io/rb/snaps)
4
+ [![Build Status](https://travis-ci.org/codevise/snaps.svg?branch=master)](https://travis-ci.org/codevise/snaps)
5
+ [![Test Coverage](https://codeclimate.com/github/codevise/snaps/badges/coverage.svg)](https://codeclimate.com/github/codevise/snaps)
6
+ [![Code Climate](https://codeclimate.com/github/codevise/snaps/badges/gpa.svg)](https://codeclimate.com/github/codevise/snaps)
7
+
8
+ Revisioning and tagging of ActiveRecord models.
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ gem 'snaps'
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Run the install generator:
21
+
22
+ $ rake snaps:install:migrations
23
+
24
+ Migrate your database to create the `snaps_tags` table:
25
+
26
+ $ rake db:migrate
27
+
28
+ ## Basics
29
+
30
+ Snaps maintains a table of "named pointers" called Tags to other models. Implemented as a polymorphic association. Revisions are kept in the original table and identified by a `perma_id` field.
31
+
32
+ Snaps provides methods to create copies (snapshots) of models and tag these snapshots as needed.
33
+
34
+ ## Usage Examples
35
+
36
+ ### Basic Usage
37
+
38
+ A model that is to be revisioned with snaps needs an integer field called `perma_id` and must include the mixin created by `Snaps.revision`.
39
+
40
+ # db/migrate/xxx_create_posts.rb
41
+ create_table :posts do |t|
42
+ t.integer(:perma_id)
43
+
44
+ t.string(:title)
45
+ t.text(:body)
46
+ end
47
+
48
+
49
+ # app/models/post.rb
50
+ class Post < ActiveRecord::Base
51
+ include Snaps.revision
52
+ end
53
+
54
+
55
+ Now you can create snapshots of your post instances.
56
+
57
+ post = Posts.create(body: 'Some text')
58
+ post_snapshot = post.snapshot!
59
+
60
+ post.perma_id == post_snapshot.perma_id # => true
61
+
62
+ Using Snaps Tags you can assign one revision to be a 'draft'.
63
+
64
+ post.perma_id # => 25
65
+ post.snaps_tag!(:draft)
66
+
67
+ draft = Post.with_snaps_tag(:draft).find_by_perma_id(25)
68
+
69
+ ### Using a default Tag
70
+
71
+ It's easy to create a workflow in which a new post will be tagged by default.
72
+ Also it's convenient to hide calls to `with_snaps_tag` in a scope on your domain models.
73
+
74
+
75
+ # app/models/post.rb
76
+ class Post < ActiveRecord::Base
77
+ include Snaps.revision(default_tag: :draft)
78
+
79
+ scope :drafts, -> { with_snaps_tag(:draft) }
80
+ end
81
+
82
+ # in controller
83
+ draft = Post.create(body: 'Some Text')
84
+
85
+ existing_draft = Post.drafts.find_by_perma_id(25)
86
+
87
+ draft.update(body: "New text")
88
+ draft.snapshot!
89
+
90
+ ### Managing Lifecycle of records with tags
91
+
92
+ # app/models/post.rb
93
+ class Post < ActiveRecord::Base
94
+ include Snaps.revision(default_tag: :draft)
95
+
96
+ scope :drafts, -> { with_snaps_tag(:draft) }
97
+ scope :published, -> { with_snaps_tag(:published) }
98
+
99
+ def publish
100
+ snapshot!(tag: :published)
101
+ end
102
+ end
103
+
104
+ # in controller
105
+ draft = Post.drafts.find_by_perma_id(25)
106
+ draft.publish
107
+
108
+ all_published_posts = Post.published
109
+
110
+ ### Revisioning composite models
111
+
112
+
113
+ # db/migrate/xxx_create_sections.rb
114
+ create_table :sections do |t|
115
+ t.integer(:perma_id)
116
+ t.references(:post)
117
+
118
+ t.text(:body)
119
+ end
120
+
121
+ # app/models/section.rb
122
+ class Section < ActiveRecord::Base
123
+ include Snaps.revision
124
+
125
+ belongs_to :post
126
+ end
127
+
128
+ # app/models/post.rb
129
+ class Post < ActiveRecord::Base
130
+ include Snaps.revision(default_tag: :draft,
131
+ components: [:sections])
132
+
133
+ has_many :sections
134
+
135
+ scope :drafts, -> { with_snaps_tag(:draft) }
136
+ scope :published, -> { with_snaps_tag(:published) }
137
+
138
+ def publish
139
+ snapshot!(tag: :published)
140
+ end
141
+ end
142
+
143
+ # in controller
144
+
145
+ draft = Post.drafts.find_by_perma_id(25)
146
+ draft.sections.create(body: "Section text")
147
+ post = draft.snapshot!
148
+
149
+ # snapshots of sections have been created
150
+
151
+ draft.sections.first.id != post.sections.first.id
152
+ draft.sections.first.body == post.sections.first.body
153
+
154
+
155
+ ### Accessing other revisions of a record
156
+
157
+ post.snaps_revisions.where('created_at < ?', post.created_at)
@@ -0,0 +1,30 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Snaps'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+ load 'rails/tasks/statistics.rake'
21
+
22
+
23
+ Bundler::GemHelper.install_tasks
24
+
25
+ # rspec
26
+ require 'rspec/core'
27
+ require 'rspec/core/rake_task'
28
+ desc "Run all specs in spec directory (excluding plugin specs)"
29
+ RSpec::Core::RakeTask.new(:spec => 'app:db:test:prepare')
30
+ task :default => :spec
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module Snaps
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Snaps
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,55 @@
1
+ module Snaps
2
+ # ActiveRecord::Suppressor prevents the receiver from being saved during
3
+ # a given block.
4
+ #
5
+ # For example, here's a pattern of creating notifications when new comments
6
+ # are posted. (The notification may in turn trigger an email, a push
7
+ # notification, or just appear in the UI somewhere):
8
+ #
9
+ # class Comment < ActiveRecord::Base
10
+ # belongs_to :commentable, polymorphic: true
11
+ # after_create -> { Notification.create! comment: self,
12
+ # recipients: commentable.recipients }
13
+ # end
14
+ #
15
+ # That's what you want the bulk of the time. New comment creates a new
16
+ # Notification. But there may well be off cases, like copying a commentable
17
+ # and its comments, where you don't want that. So you'd have a concern
18
+ # something like this:
19
+ #
20
+ # module Copyable
21
+ # def copy_to(destination)
22
+ # Notification.suppress do
23
+ # # Copy logic that creates new comments that we do not want
24
+ # # triggering notifications.
25
+ # end
26
+ # end
27
+ # end
28
+ module Suppressor
29
+ extend ActiveSupport::Concern
30
+
31
+ module ClassMethods
32
+ def suppress(&block)
33
+ SuppressorRegistry.suppressed[name] = true
34
+ yield
35
+ ensure
36
+ SuppressorRegistry.suppressed[name] = false
37
+ end
38
+ end
39
+
40
+ # Ignore saving events if we're in suppression mode.
41
+ def save!(*args) # :nodoc:
42
+ SuppressorRegistry.suppressed[self.class.name] ? true : super
43
+ end
44
+ end
45
+
46
+ class SuppressorRegistry # :nodoc:
47
+ extend ActiveSupport::PerThreadRegistry
48
+
49
+ attr_reader :suppressed
50
+
51
+ def initialize
52
+ @suppressed = {}
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,45 @@
1
+ module Snaps
2
+ class Tag < ActiveRecord::Base
3
+ include Suppressor
4
+
5
+ belongs_to :record, polymorphic: true
6
+
7
+ before_save do |tag|
8
+ tag.record.snaps_untag!(tag.tag)
9
+ end
10
+
11
+ scope :current, -> do
12
+ where(superseded_at: nil)
13
+ end
14
+
15
+ scope :with_name, ->(name) do
16
+ where(tag: name)
17
+ end
18
+
19
+ def self.supersede!
20
+ current.update_all(superseded_at: Time.now)
21
+ end
22
+
23
+ def self.for_all_revisions_of(record)
24
+ joins(<<-SQL)
25
+ INNER JOIN #{record.class.table_name}
26
+ ON #{record.class.table_name}.id = snaps_tags.record_id
27
+ AND snaps_tags.record_type = '#{record.class.name}'
28
+ AND perma_id = #{record.perma_id}
29
+ SQL
30
+ end
31
+
32
+ def self.all_revisions_with_tag(model, tag)
33
+ model.joins(<<-SQL)
34
+ INNER JOIN snaps_tags
35
+ ON #{model.table_name}.id = snaps_tags.record_id
36
+ AND snaps_tags.record_type = '#{model.name}'
37
+ AND snaps_tags.tag = '#{tag}'
38
+ SQL
39
+ end
40
+
41
+ def self.current_revisions_with_tag(model, tag)
42
+ all_revisions_with_tag(model, tag).where('snaps_tags.superseded_at IS NULL')
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Snaps</title>
5
+ <%= stylesheet_link_tag "snaps/application", media: "all" %>
6
+ <%= javascript_include_tag "snaps/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,2 @@
1
+ Snaps::Engine.routes.draw do
2
+ end
@@ -0,0 +1,12 @@
1
+ class CreateSnapsTags < ActiveRecord::Migration
2
+ def change
3
+ create_table :snaps_tags do |t|
4
+ t.integer :record_id
5
+ t.string :record_type
6
+ t.string :tag
7
+ t.timestamp :superseded_at
8
+
9
+ t.timestamps null: false
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,61 @@
1
+ require "snaps/engine"
2
+ require "snaps/snapshot"
3
+
4
+ module Snaps
5
+ def self.revision(options={})
6
+ Module.new do
7
+ extend ActiveSupport::Concern
8
+
9
+ included do
10
+ before_save :ensure_perma_id
11
+ if options[:default_tag]
12
+ after_create do
13
+ snaps_tag!(options[:default_tag])
14
+ end
15
+ end
16
+ end
17
+
18
+ def snapshot!(options = {}, &block)
19
+ Snapshot.create(self,
20
+ options.merge(self.class.snaps_options),
21
+ &block)
22
+ end
23
+
24
+ def snaps_tag!(tag)
25
+ Tag.create!(
26
+ record: self,
27
+ tag: tag
28
+ )
29
+ end
30
+
31
+ def snaps_untag!(tag)
32
+ Tag
33
+ .for_all_revisions_of(self)
34
+ .with_name(tag)
35
+ .supersede!
36
+ end
37
+
38
+ def snaps_revisions
39
+ self.class.where(perma_id: perma_id)
40
+ end
41
+
42
+ def ensure_perma_id
43
+ self.perma_id ||= (self.class.maximum(:perma_id) || 0) + 1
44
+ end
45
+
46
+ class_methods do
47
+ def with_snaps_tag(tag, options={})
48
+ if options[:all_revisions]
49
+ Tag.all_revisions_with_tag(self, tag)
50
+ else
51
+ Tag.current_revisions_with_tag(self, tag)
52
+ end
53
+ end
54
+
55
+ define_method :snaps_options do
56
+ options
57
+ end
58
+ end
59
+ end
60
+ end
61
+ end