never_wastes 0.0.7 → 0.0.8

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: 1e0cc58ce583493943546b8be381efee31dd739f
4
- data.tar.gz: 39ed990d9e2afe0b9736a310bc5f8081fe61e434
3
+ metadata.gz: c85ab18840099a907e37896a9fbdf155c84720fb
4
+ data.tar.gz: 9bfc863a688d3193001729ec8f7a3935cd27172f
5
5
  SHA512:
6
- metadata.gz: f14b5a37fcb42b11bb6dc13a96a15fb2804f4c4b9010797e48c649bf5c7c7d6a41fbf439af7519e903aacf3675e541f728dbbe8c39204be86b8aed1080dd085e
7
- data.tar.gz: e25fc6d3963feba0e963f96318dc8563e93af1ff4589bdcd91e586076c03c1e6ec015c8e785596e316ef85fae966afcc9cccf02e39d4ebdcd260036c96d20c1a
6
+ metadata.gz: 654a6ce77cf9a29bff70389bc8acc7f862a340cc575573cae72b4294513686d3af5f6a512a5d5fdf27e5447c7c304a229c67c70019624a3c4ca37275b5741c02
7
+ data.tar.gz: 738da964f16210ba589c578e99d5ffa0f06de65a5a68191fe548e589adb9c721687c82fd3844ffcdbd0cb67b6f4d72e8f328a6612daa62b8246be60d03600573
data/README.md CHANGED
@@ -10,6 +10,8 @@ It's similar to acts_as_paranoid but simpler.
10
10
 
11
11
  ## Usage
12
12
 
13
+ ### Migrations
14
+
13
15
  First, add deleted column in your models.
14
16
 
15
17
  class AddDeletedToYourModels < ActiveRecord::Migration
@@ -29,12 +31,38 @@ If you need a timestamp, you can also add deleted_at column.
29
31
  end
30
32
  end
31
33
 
34
+ If you need to have unique index for that table, waste_id will help.
35
+
36
+ class AddDeletedToYourModels < ActiveRecord::Migration
37
+ def change
38
+ add_column :your_models, :deleted, :boolean, :null => false, :default => false
39
+ add_column :your_models, :deleted_at, :datetime
40
+ add_column :your_models, :waste_id, :integer, :null => false, :default => 0
41
+ end
42
+ end
43
+
44
+ The waste_id supposed to be 0 when it's not deleted.
45
+ When the record is softly deleted, its primary key is copied to waste_id to be unique in all deleted records.
46
+ This helps you add unique index for some typical column like 'name' as the following example;
47
+
48
+ class AddNameIndexToYourModels < ActiveRecord::Migration
49
+ def up
50
+ add_index :your_models, [:name, :waste_id], :unique => true
51
+ end
52
+
53
+ # down is needed
54
+ end
55
+
56
+ ### Declaration
57
+
32
58
  Next step is to specify never_wastes in your model which needs soft delete.
33
59
 
34
60
  class YourModel < ActiveRecord::Base
35
61
  never_wastes
36
62
  end
37
63
 
64
+ ### Use APIs
65
+
38
66
  Then you can use destroy for soft delete.
39
67
 
40
68
  model.destroy
data/lib/never_wastes.rb CHANGED
@@ -23,14 +23,10 @@ module NeverWastes
23
23
  module SoftDestroy
24
24
  def self.included(base)
25
25
  base.class_eval do
26
- class_attribute :never_wastes_boolean_column_name, :never_wastes_datetime_column_name
26
+ class_attribute :never_wastes_boolean_column_name, :never_wastes_datetime_column_name, :never_wastes_id_column_name
27
27
  self.never_wastes_boolean_column_name = :deleted
28
28
  self.never_wastes_datetime_column_name = :deleted_at
29
- def self.soft_destroy_stamps
30
- stamps = {never_wastes_boolean_column_name => true}
31
- stamps[never_wastes_datetime_column_name] = self.current_time if column_names.include?(never_wastes_datetime_column_name.to_s)
32
- stamps
33
- end
29
+ self.never_wastes_id_column_name = :waste_id
34
30
 
35
31
  alias_method :destroy!, :destroy
36
32
 
@@ -38,7 +34,7 @@ module NeverWastes
38
34
  @destroying_softly = true
39
35
  ret = with_transaction_returning_status do
40
36
  run_callbacks :destroy do
41
- stamps = self.class.soft_destroy_stamps
37
+ stamps = soft_destroy_stamps
42
38
  self.class.where(self.class.primary_key.to_sym => id).update_all(stamps)
43
39
  stamps.each {|key, value| send("#{key}=", value)}
44
40
  @destroyed = true
@@ -59,12 +55,28 @@ module NeverWastes
59
55
 
60
56
  alias_method :delete_all!, :delete_all
61
57
  def delete_all_softly
62
- update_all(deleted: true, deleted_at: self.current_time)
58
+ updates = sanitize_sql_for_assignment(never_wastes_boolean_column_name => true)
59
+ if column_names.include?(never_wastes_datetime_column_name.to_s)
60
+ updates << ","
61
+ updates << sanitize_sql_for_assignment(never_wastes_datetime_column_name => current_time)
62
+ end
63
+ if column_names.include?(never_wastes_id_column_name.to_s)
64
+ updates << ","
65
+ updates << "#{never_wastes_id_column_name.to_s} = #{primary_key}"
66
+ end
67
+ update_all(updates)
63
68
  end
64
69
  alias_method :delete_all, :delete_all_softly
65
70
  end
66
71
 
67
72
  private
73
+ def soft_destroy_stamps
74
+ stamps = {self.class.never_wastes_boolean_column_name => true}
75
+ stamps[self.class.never_wastes_datetime_column_name] = self.class.current_time if self.class.column_names.include?(self.class.never_wastes_datetime_column_name.to_s)
76
+ stamps[self.class.never_wastes_id_column_name] = id if self.class.column_names.include?(self.class.never_wastes_id_column_name.to_s)
77
+ stamps
78
+ end
79
+
68
80
  # useful in callbacks
69
81
  def destroying_softly?
70
82
  @destroying_softly
@@ -1,3 +1,3 @@
1
1
  module NeverWastes
2
- VERSION = "0.0.7"
2
+ VERSION = "0.0.8"
3
3
  end
data/spec/fake_app.rb CHANGED
@@ -8,7 +8,13 @@ ActiveRecord::Base.establish_connection('test')
8
8
  #migrations
9
9
  class CreateAllTables < ActiveRecord::Migration
10
10
  def self.up
11
- create_table(:users) {|t| t.string :name; t.boolean :deleted; t.datetime :deleted_at }
11
+ create_table(:users) do |t|
12
+ t.string :name
13
+ t.boolean :deleted, :null => false, :default => false
14
+ t.datetime :deleted_at
15
+ t.integer :waste_id, :null => false, :default => 0
16
+ end
17
+ add_index :users, [:name, :waste_id], :unique => true
12
18
  end
13
19
  end
14
20
 
@@ -19,6 +19,12 @@ describe User do
19
19
  subject { User.new(name: user.name) }
20
20
  it { should be_valid }
21
21
  end
22
+
23
+ context "when the same name user exists and is deleted, and the other is going to be deleted, when there is an unique index" do
24
+ before { user.destroy }
25
+ let(:other) { User.create!(name: user.name) }
26
+ it { expect { other.destroy }.not_to raise_error }
27
+ end
22
28
  end
23
29
  end
24
30
 
@@ -28,13 +34,15 @@ describe User do
28
34
 
29
35
  context 'when user is not deleted' do
30
36
  its(:deleted) { should be_false }
31
- its(:deleted_at) { should be_nil}
37
+ its(:deleted_at) { should be_nil }
38
+ its(:waste_id) { should eq(0) }
32
39
  end
33
40
 
34
41
  context 'when user is deleted' do
35
42
  before { user.destroy }
36
43
  its(:deleted) { should be_true }
37
44
  its(:deleted_at) { should_not be_nil }
45
+ its(:waste_id) { should eq(user.id)}
38
46
  end
39
47
  end
40
48
 
@@ -92,12 +100,14 @@ describe User do
92
100
  subject { undelete_user.reload }
93
101
  its(:deleted) { should be_false }
94
102
  its(:deleted_at) { should be_nil}
103
+ its(:waste_id) { should eq(0) }
95
104
  end
96
105
 
97
106
  context 'when user is deleted' do
98
107
  subject { delete_users.first.reload }
99
108
  its(:deleted) { should be_true }
100
109
  its(:deleted_at) { should_not be_nil }
110
+ its(:waste_id) { should eq(delete_users.first.id) }
101
111
  end
102
112
  end
103
113
  end
data/spec/spec_helper.rb CHANGED
@@ -6,6 +6,7 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
6
6
  # Merge keys is often used in mongoid.yml
7
7
  # See: http://redmine.ruby-lang.org/issues/show/4300
8
8
  if RUBY_VERSION >= '1.9.2'
9
+ require 'yaml'
9
10
  YAML::ENGINE.yamler = 'syck'
10
11
  end
11
12
  require 'active_record'
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: never_wastes
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.7
4
+ version: 0.0.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - nay3
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-24 00:00:00.000000000 Z
11
+ date: 2013-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport