active_snapshot 0.1.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.
Files changed (52) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +10 -0
  3. data/LICENSE +21 -0
  4. data/README.md +145 -0
  5. data/Rakefile +52 -0
  6. data/lib/active_snapshot.rb +17 -0
  7. data/lib/active_snapshot/models/concerns/snapshots_concern.rb +111 -0
  8. data/lib/active_snapshot/models/snapshot.rb +104 -0
  9. data/lib/active_snapshot/models/snapshot_item.rb +40 -0
  10. data/lib/active_snapshot/version.rb +3 -0
  11. data/lib/generators/active_snapshot/install/install_generator.rb +22 -0
  12. data/lib/generators/active_snapshot/install/templates/create_snapshots_tables.rb.erb +21 -0
  13. data/lib/generators/active_snapshot/migration_generator.rb +76 -0
  14. data/test/dummy_app/Rakefile +7 -0
  15. data/test/dummy_app/app/assets/config/manifest.js +3 -0
  16. data/test/dummy_app/app/assets/javascripts/application.js +0 -0
  17. data/test/dummy_app/app/assets/stylesheets/application.css +3 -0
  18. data/test/dummy_app/app/controllers/application_controller.rb +3 -0
  19. data/test/dummy_app/app/models/application_record.rb +3 -0
  20. data/test/dummy_app/app/models/comment.rb +5 -0
  21. data/test/dummy_app/app/models/post.rb +13 -0
  22. data/test/dummy_app/app/models/volatile_post.rb +5 -0
  23. data/test/dummy_app/app/views/layouts/application.html.erb +14 -0
  24. data/test/dummy_app/config.ru +4 -0
  25. data/test/dummy_app/config/application.rb +61 -0
  26. data/test/dummy_app/config/boot.rb +10 -0
  27. data/test/dummy_app/config/database.yml +20 -0
  28. data/test/dummy_app/config/environment.rb +5 -0
  29. data/test/dummy_app/config/environments/development.rb +30 -0
  30. data/test/dummy_app/config/environments/production.rb +60 -0
  31. data/test/dummy_app/config/environments/test.rb +41 -0
  32. data/test/dummy_app/config/initializers/backtrace_silencers.rb +7 -0
  33. data/test/dummy_app/config/initializers/inflections.rb +10 -0
  34. data/test/dummy_app/config/initializers/mime_types.rb +5 -0
  35. data/test/dummy_app/config/initializers/secret_token.rb +11 -0
  36. data/test/dummy_app/config/initializers/session_store.rb +8 -0
  37. data/test/dummy_app/config/initializers/wrap_parameters.rb +14 -0
  38. data/test/dummy_app/config/locales/en.yml +5 -0
  39. data/test/dummy_app/config/routes.rb +6 -0
  40. data/test/dummy_app/config/secrets.yml +22 -0
  41. data/test/dummy_app/db/migrate/20210128155312_set_up_test_tables.rb +19 -0
  42. data/test/dummy_app/db/migrate/20210306070749_create_snapshots_tables.rb +21 -0
  43. data/test/dummy_app/log/development.log +9 -0
  44. data/test/dummy_app/log/test.log +46812 -0
  45. data/test/generators/active_snapshot/install_generator_test.rb +31 -0
  46. data/test/models/snapshot_item_test.rb +75 -0
  47. data/test/models/snapshot_test.rb +109 -0
  48. data/test/models/snapshots_concern_test.rb +111 -0
  49. data/test/test_helper.rb +71 -0
  50. data/test/unit/active_snapshot_test.rb +60 -0
  51. data/test/unit/errors_test.rb +12 -0
  52. metadata +231 -0
@@ -0,0 +1,40 @@
1
+ module ActiveSnapshot
2
+ class SnapshotItem < ActiveRecord::Base
3
+ self.table_name = "snapshot_items"
4
+
5
+ if defined?(ProtectedAttributes)
6
+ attr_accessible :object, :item_id, :item_type, :child_group_name
7
+ end
8
+
9
+ belongs_to :snapshot, class_name: 'ActiveSnapshot::Snapshot'
10
+ belongs_to :item, polymorphic: true
11
+
12
+ validates :snapshot_id, presence: true
13
+ validates :item_id, presence: true, uniqueness: { scope: [:snapshot_id, :item_type] }
14
+ validates :item_type, presence: true, uniqueness: { scope: [:snapshot_id, :item_id] }
15
+
16
+ def object
17
+ @object ||= self[:object].with_indifferent_access
18
+ end
19
+
20
+ def object=(val)
21
+ @object = nil
22
+ self[:object] = val
23
+ end
24
+
25
+ def restore_item!
26
+ ### Add any custom logic here
27
+
28
+ if !item
29
+ item_klass = item_type.constantize
30
+
31
+ self.item = item_klass.new
32
+ end
33
+
34
+ item.assign_attributes(object)
35
+
36
+ item.save!(validate: false, touch: false)
37
+ end
38
+
39
+ end
40
+ end
@@ -0,0 +1,3 @@
1
+ module ActiveSnapshot
2
+ VERSION = "0.1.0".freeze
3
+ end
@@ -0,0 +1,22 @@
1
+ require "generators/active_snapshot/migration_generator"
2
+
3
+ module ActiveSnapshot
4
+ class InstallGenerator < MigrationGenerator
5
+
6
+ source_root File.expand_path("templates", __dir__)
7
+
8
+ desc "Generates a migration to add a the `snapshots` and `snapshot_items` tables"
9
+
10
+ def create_migration_file
11
+ add_migration(
12
+ MIGRATION_NAME,
13
+ {
14
+ table_options: table_options,
15
+ }
16
+ )
17
+ end
18
+
19
+ MIGRATION_NAME = "create_snapshots_tables".freeze
20
+
21
+ end
22
+ end
@@ -0,0 +1,21 @@
1
+ class <%= migration_name %> < ActiveRecord::Migration<%= migration_version %>
2
+
3
+ def change
4
+ create_table :snapshots<%= table_options %> do |t|
5
+ t.belongs_to :item, polymorphic: true, null: false, index: true
6
+ t.string :identifier, null: false, unique: true, index: true
7
+ t.belongs_to :user, polymorphic: true
8
+ t.json :metadata
9
+ t.datetime :created_at, null: false
10
+ end
11
+
12
+ create_table :snapshot_items<%= table_options %> do |t|
13
+ t.belongs_to :snapshot, null: false, index: true
14
+ t.belongs_to :item, polymorphic: true, null: false, unique: [:snapshot_id], index: true
15
+ t.json :object, null: false
16
+ t.datetime :created_at, null: false
17
+ t.string :child_group_name
18
+ end
19
+ end
20
+
21
+ end
@@ -0,0 +1,76 @@
1
+ require "rails/generators"
2
+ require "rails/generators/active_record"
3
+
4
+ module ActiveSnapshot
5
+ # Basic structure to support a generator that builds a migration
6
+ class MigrationGenerator < ::Rails::Generators::Base
7
+ include ::Rails::Generators::Migration
8
+
9
+ def self.next_migration_number(dirname)
10
+ ::ActiveRecord::Generators::Base.next_migration_number(dirname)
11
+ end
12
+
13
+ protected
14
+
15
+ def add_migration(template_name, opts = {})
16
+ @template_name = template_name
17
+
18
+ @migration_path ||= "db/migrate"
19
+
20
+ if self.class.migration_exists?(@migration_path, template_name)
21
+ Kernel.warn "Migration already exists: #{template_name}"
22
+ else
23
+ migration_template(
24
+ "#{template_name}.rb.erb",
25
+ File.join(@migration_path, "#{template_name}.rb"),
26
+ {
27
+ migration_version: migration_version,
28
+ }.merge(opts)
29
+ )
30
+ end
31
+ end
32
+
33
+ def migration_version
34
+ "[#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}]"
35
+ end
36
+
37
+ def migration_name
38
+ @template_name.titleize.gsub(' ', '')
39
+ end
40
+
41
+ private
42
+
43
+ MYSQL_ADAPTERS = [
44
+ "ActiveRecord::ConnectionAdapters::MysqlAdapter", # Used by gems: `mysql`, `activerecord-jdbcmysql-adapter`.
45
+ "ActiveRecord::ConnectionAdapters::Mysql2Adapter", # Used by `mysql2` gem.
46
+ ].freeze
47
+
48
+ def mysql?
49
+ MYSQL_ADAPTERS.include?(ActiveRecord::Base.connection.class.name)
50
+ end
51
+
52
+ # Even modern versions of MySQL still use `latin1` as the default character
53
+ # encoding. Many users are not aware of this, and run into trouble when they
54
+ # try to use PaperTrail in apps that otherwise tend to use UTF-8. Postgres, by
55
+ # comparison, uses UTF-8 except in the unusual case where the OS is configured
56
+ # with a custom locale.
57
+ #
58
+ # - https://dev.mysql.com/doc/refman/5.7/en/charset-applications.html
59
+ # - http://www.postgresql.org/docs/9.4/static/multibyte.html
60
+ #
61
+ # Furthermore, MySQL's original implementation of UTF-8 was flawed, and had
62
+ # to be fixed later by introducing a new charset, `utf8mb4`.
63
+ #
64
+ # - https://mathiasbynens.be/notes/mysql-utf8mb4
65
+ # - https://dev.mysql.com/doc/refman/5.5/en/charset-unicode-utf8mb4.html
66
+ #
67
+ def table_options
68
+ if mysql?
69
+ ', { options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci" }'
70
+ else
71
+ ""
72
+ end
73
+ end
74
+
75
+ end
76
+ end
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
3
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
4
+
5
+ require File.expand_path('../config/application', __FILE__)
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,3 @@
1
+ //= link_tree ../images
2
+ //= link_directory ../javascripts .js
3
+ //= link_directory ../stylesheets .css .scss
@@ -0,0 +1,3 @@
1
+ /*
2
+ *= require_self
3
+ */
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,3 @@
1
+ class ApplicationRecord < ActiveRecord::Base
2
+ self.abstract_class = true
3
+ end
@@ -0,0 +1,5 @@
1
+ class Comment < ActiveRecord::Base
2
+ include ActiveSnapshot
3
+
4
+ belongs_to :post
5
+ end
@@ -0,0 +1,13 @@
1
+ class Post < ActiveRecord::Base
2
+ include ActiveSnapshot
3
+
4
+ has_many :comments
5
+
6
+ has_snapshot_children do
7
+ instance = self.class.includes(:comments).find(id)
8
+
9
+ {
10
+ comments: instance.comments,
11
+ }
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ class VolatilePost < ActiveRecord::Base
2
+ self.table_name = "posts"
3
+
4
+ include ActiveSnapshot
5
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy App</title>
5
+ <%= stylesheet_link_tag "application" %>
6
+ <%= javascript_include_tag "application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,61 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require 'rails/all'
4
+
5
+ Bundler.require
6
+
7
+ module Dummy
8
+ class Application < Rails::Application
9
+ # Settings in config/environments/* take precedence over those specified here.
10
+ # Application configuration should go into files in config/initializers
11
+ # -- all .rb files in that directory are automatically loaded.
12
+
13
+ # Custom directories with classes and modules you want to be autoloadable.
14
+ # config.autoload_paths += %W(#{config.root}/extras)
15
+
16
+ # Only load the plugins named here, in the order given (default is alphabetical).
17
+ # :all can be used as a placeholder for all plugins not explicitly named.
18
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
19
+
20
+ # Activate observers that should always be running.
21
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
22
+
23
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
24
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
25
+ # config.time_zone = 'Central Time (US & Canada)'
26
+
27
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
28
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
29
+ # config.i18n.default_locale = :de
30
+
31
+ # Configure the default encoding used in templates for Ruby 1.9.
32
+ config.encoding = "utf-8"
33
+
34
+ # Configure sensitive parameters which will be filtered from the log file.
35
+ config.filter_parameters += [:password]
36
+
37
+ # Enable the asset pipeline
38
+ config.assets.enabled = true
39
+
40
+ config.assets.quiet = true
41
+
42
+ # Version of your assets, change this if you want to expire all your assets
43
+ config.assets.version = '1.0'
44
+
45
+ config.generators.test_framework = false
46
+ config.generators.helper = false
47
+ config.generators.stylesheets = false
48
+ config.generators.javascripts = false
49
+
50
+ config.after_initialize do
51
+ ActiveRecord::Migration.migrate(Rails.root.join("db/migrate/*").to_s)
52
+ end
53
+
54
+ if ActiveRecord.respond_to?(:gem_version)
55
+ gem_version = ActiveRecord.gem_version
56
+ if gem_version.to_s.start_with?("5.2.")
57
+ config.active_record.sqlite3.represent_boolean_as_integer = true
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ if File.exist?(gemfile)
5
+ ENV['BUNDLE_GEMFILE'] = gemfile
6
+ require 'bundler'
7
+ Bundler.setup
8
+ end
9
+
10
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,20 @@
1
+ default: &default
2
+ <% if defined?(SQLite3) %>
3
+ adapter: sqlite3
4
+ database: ':memory:'
5
+
6
+ <% elsif defined?(Mysql2) %>
7
+ adapter: mysql2
8
+ database: active_snapshot_test
9
+
10
+ <% elsif defined?(PG) %>
11
+ adapter: postgresql
12
+ database: active_snapshot_test
13
+
14
+ <% end %>
15
+
16
+ development:
17
+ <<: *default
18
+
19
+ test:
20
+ <<: *default
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!
@@ -0,0 +1,30 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the web server when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.consider_all_requests_local = true
14
+ config.action_controller.perform_caching = false
15
+
16
+ # Don't care if the mailer can't send
17
+ config.action_mailer.raise_delivery_errors = false
18
+
19
+ # Print deprecation notices to the Rails logger
20
+ config.active_support.deprecation = :log
21
+
22
+ # Only use best-standards-support built into browsers
23
+ config.action_dispatch.best_standards_support = :builtin
24
+
25
+ # Do not compress assets
26
+ config.assets.compress = false
27
+
28
+ # Expands the lines which load the assets
29
+ config.assets.debug = true
30
+ end
@@ -0,0 +1,60 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # Code is not reloaded between requests
5
+ config.cache_classes = true
6
+
7
+ # Full error reports are disabled and caching is turned on
8
+ config.consider_all_requests_local = false
9
+ config.action_controller.perform_caching = true
10
+
11
+ # Disable Rails's static asset server (Apache or nginx will already do this)
12
+ config.serve_static_files = false
13
+
14
+ # Compress JavaScripts and CSS
15
+ config.assets.compress = true
16
+
17
+ # Don't fallback to assets pipeline if a precompiled asset is missed
18
+ config.assets.compile = false
19
+
20
+ # Generate digests for assets URLs
21
+ config.assets.digest = true
22
+
23
+ # Defaults to Rails.root.join("public/assets")
24
+ # config.assets.manifest = YOUR_PATH
25
+
26
+ # Specifies the header that your server uses for sending files
27
+ # config.action_dispatch.x_sendfile_header = "X-Sendfile" # for apache
28
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for nginx
29
+
30
+ # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies.
31
+ # config.force_ssl = true
32
+
33
+ # See everything in the log (default is :info)
34
+ # config.log_level = :debug
35
+
36
+ # Use a different logger for distributed setups
37
+ # config.logger = SyslogLogger.new
38
+
39
+ # Use a different cache store in production
40
+ # config.cache_store = :mem_cache_store
41
+
42
+ # Enable serving of images, stylesheets, and JavaScripts from an asset server
43
+ # config.action_controller.asset_host = "http://assets.example.com"
44
+
45
+ # Precompile additional assets (application.js, application.css, and all non-JS/CSS are already added)
46
+ # config.assets.precompile += %w( search.js )
47
+
48
+ # Disable delivery errors, bad email addresses will be ignored
49
+ # config.action_mailer.raise_delivery_errors = false
50
+
51
+ # Enable threaded mode
52
+ # config.threadsafe!
53
+
54
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
55
+ # the I18n.default_locale when a translation can not be found)
56
+ config.i18n.fallbacks = true
57
+
58
+ # Send deprecation notices to registered listeners
59
+ config.active_support.deprecation = :notify
60
+ end