records_rip 0.1.0 → 0.2.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
  SHA256:
3
- metadata.gz: b9abf78b965f3dcea94acd4dcc7353d0c555ef3f85413a7914b04389da1e3e90
4
- data.tar.gz: 8df75b73944fbbb385b299e0af74b70560eb2bcb929bbc20a985dbc0556b05a9
3
+ metadata.gz: baa83553306abec0824d39c327f8bba54ec52c54355402c98d48a21a016d910b
4
+ data.tar.gz: 6c800f3969935c1b44f631ff98e580ba4d3925bfacad86ccfe822e9a1e1c95d8
5
5
  SHA512:
6
- metadata.gz: da333369e81fec108061b770d7de40997ec5daaa44a6c432e5a3f4cacec43099840837a0ea96d04723cdfc42329301b933489861f0229e42bd259fb6ed15700a
7
- data.tar.gz: 256db6428d9d92e457dcb4b47c30e35338c309c1fbc5407165d90c762bc23924a02c9444f9a2ee264ed7f25359d7edf8541a067aaed1cbf8cf7920e9545b533d
6
+ metadata.gz: c34fbe5e94d2aca6b8157700f4b3711eb5adcfcdfbe981240d11f6d2bac0b21fc56b2b6798d105f34f63dc372d37d65ba6ce3127d5be5bb357763ab2033324e8
7
+ data.tar.gz: d540c11b83e99f733192ac112868e2dfad019e4a069db1b8ea54baf43415d2e1160132024825d1693a7e29eddc6608e85ac35e5debf0405272acb3ed0aa1bff8
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- records_rip (0.1.0)
4
+ records_rip (0.2.0)
5
5
  activesupport
6
6
 
7
7
  GEM
@@ -0,0 +1,67 @@
1
+ require "rails/generators"
2
+ require "rails/generators/active_record"
3
+
4
+ module RecordsRip
5
+ class InstallGenerator < ::Rails::Generators::Base
6
+ include ::Rails::Generators::Migration
7
+
8
+ MYSQL_ADAPTERS = [
9
+ "ActiveRecord::ConnectionAdapters::MysqlAdapter",
10
+ "ActiveRecord::ConnectionAdapters::Mysql2Adapter"
11
+ ].freeze
12
+
13
+ source_root File.expand_path('templates', __dir__)
14
+
15
+ desc "Generates (but does not run) a migration to add a tombs table."
16
+
17
+ def self.next_migration_number(dirname)
18
+ ::ActiveRecord::Generators::Base.next_migration_number(dirname)
19
+ end
20
+
21
+ def create_migration_file
22
+ add_records_rip_migration("create_tomb",
23
+ item_type_options: item_type_options,
24
+ tombs_table_options: tombs_table_options)
25
+ end
26
+
27
+ private
28
+
29
+ def item_type_options
30
+ opt = {null: false}
31
+ opt[:limit] = 191 if mysql?
32
+ ", #{opt}"
33
+ end
34
+
35
+ def mysql?
36
+ MYSQL_ADAPTERS.include?(ActiveRecord::Base.connection.class.name)
37
+ end
38
+
39
+ def tombs_table_options
40
+ if mysql?
41
+ ', { options: "ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci" }'
42
+ else
43
+ ""
44
+ end
45
+ end
46
+
47
+ def add_records_rip_migration(template, extra_options = {})
48
+ migration_dir = File.expand_path("db/migrate")
49
+ if self.class.migration_exists?(migration_dir, template)
50
+ ::Kernel.warn "Migration already exists: #{template}"
51
+ else
52
+ migration_template(
53
+ "#{template}.rb.erb",
54
+ "db/migrate/#{template}.rb",
55
+ {migration_version: migration_version}.merge(extra_options)
56
+ )
57
+ end
58
+ end
59
+
60
+ def migration_version
61
+ major = ActiveRecord::VERSION::MAJOR
62
+ if major >= 5
63
+ "[#{major}.#{ActiveRecord::VERSION::MINOR}]"
64
+ end
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,21 @@
1
+ # This migration creates the `tombs` table.
2
+ class CreateTomb < ActiveRecord::Migration<%= migration_version %>
3
+
4
+ # The largest text column available in all supported RDBMS is
5
+ # 1024^3 - 1 bytes, roughly one gibibyte. We specify a size
6
+ # so that MySQL will use `longtext` instead of `text`. Otherwise,
7
+ # when serializing very large objects, `text` might not be big enough.
8
+ TEXT_BYTES = 1_073_741_823
9
+
10
+ def change
11
+ create_table :tombs<%= tombs_table_options %> do |t|
12
+ t.string :item_type<%= item_type_options %>
13
+ t.integer :item_id, null: false
14
+ t.text :object, limit: TEXT_BYTES
15
+
16
+ t.datetime :created_at
17
+
18
+ t.index %i(item_type item_id)
19
+ end
20
+ end
21
+ end
@@ -6,7 +6,16 @@ module RecordsRip
6
6
 
7
7
  class_methods do
8
8
  def rest_in_place
9
- scope :tomb, -> { Tomb.where(item_type: self.class.to_s) }
9
+ model_class = self
10
+
11
+ model_class.send(
12
+ "before_destroy",
13
+ lambda do |record|
14
+ ::RecordsRip::Tomb.create(item_id: record.id, item_type: record.class.name, object: '{}')
15
+ end
16
+ )
17
+
18
+ scope :tomb, -> {::RecordsRip::Tomb.where(item_type: model_class.to_s)}
10
19
  end
11
20
  end
12
21
  end
@@ -0,0 +1,4 @@
1
+ module RecordsRip
2
+ class Tomb < ::ActiveRecord::Base
3
+ end
4
+ end
@@ -1,3 +1,3 @@
1
1
  module RecordsRip
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: records_rip
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - ts-3156
@@ -109,8 +109,11 @@ files:
109
109
  - Rakefile
110
110
  - bin/console
111
111
  - bin/setup
112
+ - lib/generators/records_rip/install/install_generator.rb
113
+ - lib/generators/records_rip/install/templates/create_tomb.rb.erb
112
114
  - lib/records_rip.rb
113
115
  - lib/records_rip/model.rb
116
+ - lib/records_rip/tomb.rb
114
117
  - lib/records_rip/version.rb
115
118
  - records_rip.gemspec
116
119
  homepage: https://github.com/ts-3156/records_rip