deleted_at 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f21d27f294c651f6dbae4c1fb608081a50e61a77
4
- data.tar.gz: 3891be06e90471eab8c7f2be5ec695f1f3dd068d
3
+ metadata.gz: b044ffad6653348c6202362b5894d01311dc3e16
4
+ data.tar.gz: d3f41c1fe35254b1b779d351330376a91ff0919a
5
5
  SHA512:
6
- metadata.gz: e0d01f586a884db030b9e51e2226a139a6ceb05a0e9763c0df5e1acfad0ec8dd86c59ce7d8310bcd9747e1f39abc9cc663a59541a8973622f919fd7023057f25
7
- data.tar.gz: a8aa9c11e0ddf3b4ab2f5f4ad4167ab5d0063803b04bc2c8187950095b9bc0346cfe7f2708a397d47cef7f059afd58e8f9f1ced5b79dfe564f1b5c9976d7b8b1
6
+ metadata.gz: ec94212c15a7538dfcde0a22ea4320944fc42d6332aae8b8bc5f850fd5f91a79694829eac276b4693167c88d52ccf4d400fec75011b2a073ce9d8beafe68938c
7
+ data.tar.gz: 0df63d894dcb07be2a8f460fd8b898b4cdbcd0f32d14d5f221fad52ebe40fe71019fcfe2eb104422ae396723b745d3ea7f2c7721ba9fd8c48b7e0919f1ab0d5a
@@ -30,11 +30,11 @@ Gem::Specification.new do |spec|
30
30
  spec.require_paths = ["lib"]
31
31
  spec.required_ruby_version = '>= 2.0'
32
32
 
33
- spec.add_dependency "activerecord", "~> 4.2.0"
34
- spec.add_dependency 'request_store', '>= 1.1.0'
33
+ spec.add_dependency "activerecord", "~> 4.2"
34
+ spec.add_dependency 'request_store', '~> 1.1', '>= 1.1'
35
35
 
36
36
  spec.add_development_dependency "bundler", "~> 1.13"
37
37
  spec.add_development_dependency "rake", "~> 10.0"
38
38
  spec.add_development_dependency "rspec", "~> 3.0"
39
- spec.add_development_dependency "pry", "~> 0.10.0"
39
+ spec.add_development_dependency "pry", "~> 0.10"
40
40
  end
@@ -3,14 +3,17 @@ require 'deleted_at/railtie' if defined? ::Rails::Railtie
3
3
 
4
4
  module DeletedAt
5
5
 
6
+
6
7
  def self.install(model)
7
- DeletedAt::Views.create_deleted_view(model)
8
- DeletedAt::Views.create_present_view(model)
8
+ DeletedAt::Views.install_present_view(model)
9
+ DeletedAt::Views.install_deleted_view(model)
9
10
  end
10
11
 
11
12
  def self.uninstall(model)
12
- DeletedAt::Views.destroy_deleted_view(model)
13
- DeletedAt::Views.destroy_present_view(model)
13
+ DeletedAt::Views.uninstall_deleted_view(model)
14
+ DeletedAt::Views.uninstall_present_view(model)
14
15
  end
15
16
 
17
+ private
18
+
16
19
  end
@@ -7,8 +7,8 @@ module DeletedAt
7
7
  extend ActiveSupport::Concern
8
8
 
9
9
  included do
10
- class_attribute :original_table_name,
11
- :deleted_at_column, :deleted_by_column, :deleted_by_class, :deleted_by_primary_key
10
+ class_attribute :deleted_at_column,
11
+ :deleted_by_column, :deleted_by_class, :deleted_by_primary_key
12
12
 
13
13
  class << self
14
14
  [:archive_with_deleted_at?, :archive_with_deleted_by?].each do |sym|
@@ -29,9 +29,9 @@ module DeletedAt
29
29
  # NOTE: This needs to happen regardless of whether the views have been installed
30
30
  # yet or not. This is because to install the views, this needs to have been done!
31
31
  primary_key = self.primary_key
32
- self.original_table_name = self.table_name
32
+ table_name = self.table_name
33
33
 
34
- unless ::DeletedAt::Views.present_view_exists?(self) && ::DeletedAt::Views.deleted_view_exists?(self)
34
+ unless ::DeletedAt::Views.all_table_exists?(self) && ::DeletedAt::Views.deleted_view_exists?(self)
35
35
  return warn("You're trying to use `with_deleted_at` on #{name} but you have not installed the views, yet.")
36
36
  end
37
37
 
@@ -50,8 +50,6 @@ module DeletedAt
50
50
  setup_class_views
51
51
  with_deleted_by
52
52
 
53
- self.table_name = ::DeletedAt::Views.present_view(self)
54
-
55
53
  end
56
54
 
57
55
  private
@@ -78,11 +76,20 @@ module DeletedAt
78
76
  end
79
77
  end
80
78
 
79
+ def refactor_validators
80
+ validators.each do |validator|
81
+ case validator
82
+ when ActiveRecord::Validations::UniquenessValidator
83
+
84
+ end
85
+ end
86
+ end
87
+
81
88
  def setup_class_views
82
89
 
83
90
  self.const_set(:All, Class.new(self) do |klass|
84
91
  class_eval <<-AAA
85
- self.table_name = '#{self.original_table_name}'
92
+ self.table_name = '#{::DeletedAt::Views.all_table(klass)}'
86
93
  AAA
87
94
  end)
88
95
 
@@ -1,3 +1,3 @@
1
1
  module DeletedAt
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -1,31 +1,33 @@
1
1
  module DeletedAt
2
2
  module Views
3
3
 
4
- def self.create_present_view(model)
5
- table_name = present_view(model)
4
+ def self.install_present_view(model)
5
+ uninstall_present_view(model)
6
+ all_table_name = all_table(model)
7
+ present_table_name = present_view(model)
8
+
9
+ model.connection.execute("ALTER TABLE \"#{present_table_name}\" RENAME TO \"#{all_table_name}\"")
6
10
  model.connection.execute <<-eos
7
- CREATE OR REPLACE VIEW "#{table_name}"
8
- AS SELECT * FROM "#{model.original_table_name}" WHERE #{model.deleted_at_column} IS NULL;
11
+ CREATE OR REPLACE VIEW "#{present_table_name}"
12
+ AS SELECT * FROM "#{all_table_name}" WHERE #{model.deleted_at_column} IS NULL;
9
13
  eos
10
- # AS SELECT #{cols.join(', ')} FROM "#{model.original_table_name}" WHERE #{model.deleted_at_column} IS NULL;
11
- return table_name
12
14
  end
13
15
 
14
- def self.create_deleted_view(model)
16
+ def self.install_deleted_view(model)
17
+ return warn("You must install the all/present tables/views first!") unless all_table_exists?(model)
15
18
  table_name = deleted_view(model)
16
19
  model.connection.execute <<-eos
17
20
  CREATE OR REPLACE VIEW "#{table_name}"
18
- AS SELECT * FROM "#{model.original_table_name}" WHERE #{model.deleted_at_column} IS NOT NULL;
21
+ AS SELECT * FROM "#{all_table(model)}" WHERE #{model.deleted_at_column} IS NOT NULL;
19
22
  eos
20
- return table_name
21
23
  end
22
24
 
23
- def self.present_view_exists?(model)
25
+ def self.all_table_exists?(model)
24
26
  query = model.connection.execute <<-eos
25
27
  SELECT EXISTS (
26
28
  SELECT 1
27
29
  FROM information_schema.tables
28
- WHERE table_name = '#{present_view(model)}'
30
+ WHERE table_name = '#{all_table(model)}'
29
31
  );
30
32
  eos
31
33
  query.first['exists'] == 't'
@@ -43,18 +45,27 @@ module DeletedAt
43
45
  end
44
46
 
45
47
  def self.present_view(model)
46
- "#{model.original_table_name}/present"
48
+ "#{model.table_name}"
47
49
  end
48
50
 
49
51
  def self.deleted_view(model)
50
- "#{model.original_table_name}/deleted"
52
+ "#{model.table_name}/deleted"
53
+ end
54
+
55
+ def self.all_table(model)
56
+ "#{model.table_name}/all"
51
57
  end
52
58
 
53
- def self.destroy_present_view(model)
59
+ def self.uninstall_present_view(model)
60
+ # Legacy
61
+ model.connection.execute("DROP VIEW IF EXISTS \"#{model.table_name}/present\"")
62
+ # New
63
+ return unless all_table_exists?(model)
54
64
  model.connection.execute("DROP VIEW IF EXISTS \"#{present_view(model)}\"")
65
+ model.connection.execute("ALTER TABLE \"#{all_table(model)}\" RENAME TO \"#{present_view(model)}\"")
55
66
  end
56
67
 
57
- def self.destroy_deleted_view(model)
68
+ def self.uninstall_deleted_view(model)
58
69
  model.connection.execute("DROP VIEW IF EXISTS \"#{deleted_view(model)}\"")
59
70
  end
60
71
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deleted_at
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dale Stevens
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-01-31 00:00:00.000000000 Z
11
+ date: 2017-02-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -16,28 +16,34 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 4.2.0
19
+ version: '4.2'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: 4.2.0
26
+ version: '4.2'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: request_store
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.1'
31
34
  - - ">="
32
35
  - !ruby/object:Gem::Version
33
- version: 1.1.0
36
+ version: '1.1'
34
37
  type: :runtime
35
38
  prerelease: false
36
39
  version_requirements: !ruby/object:Gem::Requirement
37
40
  requirements:
41
+ - - "~>"
42
+ - !ruby/object:Gem::Version
43
+ version: '1.1'
38
44
  - - ">="
39
45
  - !ruby/object:Gem::Version
40
- version: 1.1.0
46
+ version: '1.1'
41
47
  - !ruby/object:Gem::Dependency
42
48
  name: bundler
43
49
  requirement: !ruby/object:Gem::Requirement
@@ -86,14 +92,14 @@ dependencies:
86
92
  requirements:
87
93
  - - "~>"
88
94
  - !ruby/object:Gem::Version
89
- version: 0.10.0
95
+ version: '0.10'
90
96
  type: :development
91
97
  prerelease: false
92
98
  version_requirements: !ruby/object:Gem::Requirement
93
99
  requirements:
94
100
  - - "~>"
95
101
  - !ruby/object:Gem::Version
96
- version: 0.10.0
102
+ version: '0.10'
97
103
  description: Default scopes are bad. Don't delete your data. DeletedAt, I choose you!
98
104
  email:
99
105
  - dale@twilightcoders.net