kamikakushi 0.0.4 → 0.0.5

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: 3d57943be6dcfaafeaa6e40c9428d3ff0707c2da
4
- data.tar.gz: ea56754ca04bfefb15b68e45e434e1ce78ec6362
3
+ metadata.gz: fe1f9c0b54645b29b9914c3ecb90d7344fbbf7a9
4
+ data.tar.gz: a1525f1eabbfa2628a22ec705140bf49203cfa57
5
5
  SHA512:
6
- metadata.gz: 498d8b86b29e8280eb58aee1f759aafa8ed1b8e83d5af097a58562854ac076b864ca02937b7ff1be650b05d9ed592756c57f143173f067203a00fcaaaf023c9e
7
- data.tar.gz: 29073f0dceb08ffbf73e7e3f5b1d7cd192c70b71ddc5b0510141a84d51f446a930731ce771d56500450730ac8e5b60580c296cb7134b218c2dc2fe4eb1f31791
6
+ metadata.gz: 6d078eeef09c06e42e45ef9047722c30c126b4c7c834c31a0b5d3ee0fda9b5337746d9beb401505b00987a17e12530395c3a738de12f36b31c89b38b3343bf9c
7
+ data.tar.gz: 945f0fd48cb350473bb37ae02cb26ec58605b2a6b36f6b591c8ef4baeb90fdf90770cc6ef651645218ac08a221fb1d28f958fce0f4d212eb5deea7988eae3c14
@@ -1,39 +1,3 @@
1
- module Kamikakushi
2
- extend ActiveSupport::Concern
3
-
4
- included do
5
- default_scope { without_deleted }
6
- alias_method_chain :destroy, :kamikakushi
7
- alias_method_chain :destroyed?, :kamikakushi
8
-
9
- scope :with_deleted, -> {
10
- unscope(where: :deleted_at)
11
- }
12
-
13
- scope :without_deleted, -> {
14
- where(deleted_at: nil)
15
- }
16
-
17
- scope :only_deleted, -> {
18
- with_deleted.where.not(deleted_at: nil)
19
- }
20
- end
21
-
22
- def destroy_with_kamikakushi
23
- run_callbacks(:destroy) do
24
- update_column(:deleted_at, Time.current)
25
- end
26
- end
27
-
28
- def destroyed_with_kamikakushi?
29
- self.deleted_at? || destroyed_without_kamikakushi?
30
- end
31
-
32
- def restore
33
- update_column(:deleted_at, self.deleted_at = nil)
34
- end
35
-
36
- def purge
37
- destroy_without_kamikakushi
38
- end
39
- end
1
+ require 'kamikakushi/kamikakushi'
2
+ require 'kamikakushi/kaonashi'
3
+ require 'kamikakushi/active_record/base'
@@ -0,0 +1,6 @@
1
+ module ActiveRecord
2
+ class Base
3
+ include Kamikakushi::Kamikakushi
4
+ include Kamikakushi::Kaonashi
5
+ end
6
+ end
@@ -0,0 +1,51 @@
1
+ module Kamikakushi
2
+ module Kamikakushi
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ end
7
+
8
+ module ClassMethods
9
+ def kamikakushi
10
+ class_eval do
11
+ include InstanceMethods
12
+ default_scope { without_deleted }
13
+ alias_method_chain :destroy, :kamikakushi
14
+ alias_method_chain :destroyed?, :kamikakushi
15
+
16
+ scope :with_deleted, -> {
17
+ unscope(where: :deleted_at)
18
+ }
19
+
20
+ scope :without_deleted, -> {
21
+ where(deleted_at: nil)
22
+ }
23
+
24
+ scope :only_deleted, -> {
25
+ with_deleted.where.not(deleted_at: nil)
26
+ }
27
+ end
28
+ end
29
+ end
30
+
31
+ module InstanceMethods
32
+ def destroy_with_kamikakushi
33
+ run_callbacks(:destroy) do
34
+ touch(:deleted_at)
35
+ end
36
+ end
37
+
38
+ def destroyed_with_kamikakushi?
39
+ self.deleted_at? || destroyed_without_kamikakushi?
40
+ end
41
+
42
+ def restore
43
+ update_column(:deleted_at, self.deleted_at = nil)
44
+ end
45
+
46
+ def purge
47
+ destroy_without_kamikakushi
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,50 @@
1
+ module Kamikakushi
2
+ module Kaonashi
3
+ extend ActiveSupport::Concern
4
+
5
+ module ClassMethods
6
+ def kaonashi(options = {})
7
+ define_singleton_method(:dependent_parent_name) { options[:parent] }
8
+ return unless dependent_parent_name
9
+
10
+ class_eval do
11
+ include InstanceMethods
12
+ default_scope { without_deleted }
13
+ alias_method_chain :destroyed?, :kaonashi
14
+
15
+ scope :with_deleted, -> {
16
+ join_with_dependent_parent(dependent_parent_name, :with_deleted)
17
+ }
18
+
19
+ scope :without_deleted, -> {
20
+ join_with_dependent_parent(dependent_parent_name, :without_deleted)
21
+ }
22
+
23
+ scope :only_deleted, -> {
24
+ join_with_dependent_parent(dependent_parent_name, :only_deleted)
25
+ }
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def join_with_dependent_parent(dependent_parent_name, scope_name)
32
+ association = reflect_on_all_associations.find { |a| a.name == dependent_parent_name }
33
+ dependent_parent_class = association.klass
34
+
35
+ parent_arel = dependent_parent_class.arel_table
36
+ joins_conditions = arel_table.join(parent_arel)
37
+ .on(parent_arel[:id].eq arel_table[association.foreign_key])
38
+ .join_sources
39
+ joins(joins_conditions).merge(dependent_parent_class.__send__(scope_name))
40
+ end
41
+ end
42
+
43
+ module InstanceMethods
44
+ def destroyed_with_kaonashi?
45
+ association = self.class.reflect_on_all_associations.find { |a| a.name == self.class.dependent_parent_name }
46
+ association.klass.with_deleted.find(__send__(association.foreign_key)).destroyed?
47
+ end
48
+ end
49
+ end
50
+ end
@@ -1,3 +1,3 @@
1
1
  module Kamikakushi
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kamikakushi
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - ppworks
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-30 00:00:00.000000000 Z
11
+ date: 2015-02-07 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -48,6 +48,9 @@ files:
48
48
  - MIT-LICENSE
49
49
  - Rakefile
50
50
  - lib/kamikakushi.rb
51
+ - lib/kamikakushi/active_record/base.rb
52
+ - lib/kamikakushi/kamikakushi.rb
53
+ - lib/kamikakushi/kaonashi.rb
51
54
  - lib/kamikakushi/version.rb
52
55
  homepage: http://ppworks.jp
53
56
  licenses: