kakurenbo-puti 0.0.2 → 0.0.3

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: 8f95a87b6823125992dedb70fa73d833862d8ade
4
- data.tar.gz: 58f452544e1489ce768f12b8513a5ad3246f44ab
3
+ metadata.gz: 4a224bebe0029c5a6c736176831dd549df38df0a
4
+ data.tar.gz: 6bac0bc291bd5318c582a18f435b63850bcc598d
5
5
  SHA512:
6
- metadata.gz: ad104efd23f3da59f8169d166a39f365db4269b263c3ceeba7ef0987ba71e034565b9179f820858346d273289743e69a8d66a657fbd7eda99d4c6941d1044dc7
7
- data.tar.gz: 221030479f483ee88584812f4279ac6f3cb3b7b95b167fa33ad0d323d18136d47e6b2c0eb357c5f7503ee5db3515e8724895dd8ddd95ef5ad267a5674e0949ad
6
+ metadata.gz: a8d590bfc647fbe0a63d2d743e354bed23480f89c4326def4f9c5663ccd56e9e973a8043841971360426d9b996fbb0ecbd832d78578fe9b0f03f58c4a2cceb0a
7
+ data.tar.gz: 32cac6cab3b8035e9983c53685809ea30c5eaec0ab2a26240c913cb76d21d8f4c50304f86d44acebe86ac6b2cb6bcd9f4f7dad1c10771fd25901617287c19473
@@ -4,30 +4,54 @@ module KakurenboPuti
4
4
  # Enable soft-delete.
5
5
  # @raise [StandardException] if Not found soft-deleted date column.
6
6
  #
7
- # @param [Symbol] column name of soft-deleted date column.
8
- # @param [Array<Symbol>] dependent_associations names of dependency association.
7
+ # @param [Symbol] column Name of soft-deleted date column.
8
+ # @param [Array<Symbol>] dependent_associations Names of dependency association.
9
9
  def soft_deletable(column: :soft_destroyed_at, dependent_associations: [])
10
- raise "`#{column}` is not defined in #{self.class.name}." unless column_names.include?(column.to_s)
10
+ Initializers.create_callbacks self
11
+ Initializers.create_column_name_accessors self, column
12
+ Initializers.create_scopes self, dependent_associations
11
13
 
12
- define_singleton_method(:soft_delete_column) { column }
13
- delegate :soft_delete_column, to: :class
14
+ include InstanceMethods
15
+ end
14
16
 
15
- define_model_callbacks :restore
16
- define_model_callbacks :soft_destroy
17
+ module Initializers
18
+ # Create callbacks.
19
+ # @param [Class] target_class Class of target.
20
+ def self.create_callbacks(target_class)
21
+ target_class.class_eval do
22
+ define_model_callbacks :restore
23
+ define_model_callbacks :soft_destroy
24
+ end
25
+ end
17
26
 
18
- scope :only_soft_destroyed, -> { where.not(id: without_soft_destroyed.select(:id)) }
19
- scope :without_soft_destroyed, (lambda do
20
- dependent_associations.inject(where(soft_delete_column => nil)) do |relation, name|
21
- association = relation.klass.reflect_on_all_associations.find{|a| a.name == name }
22
- if association.klass.method_defined?(:soft_delete_column)
23
- relation.joins(name).merge(association.klass.without_soft_destroyed).references(name)
24
- else
25
- relation.joins(name).references(name)
26
- end
27
+ # Create attribute_accessors of column_name.
28
+ # @param [Class] target_class Class of target.
29
+ # @param [Symbol] column Name of column.
30
+ def self.create_column_name_accessors(target_class, column)
31
+ target_class.class_eval do
32
+ define_singleton_method(:soft_delete_column) { column }
33
+ delegate :soft_delete_column, to: :class
27
34
  end
28
- end)
35
+ end
29
36
 
30
- include InstanceMethods
37
+ # Create scopes.
38
+ # @param [Class] target_class Class of target.
39
+ # @param [Array<Symbol>] dependent_associations Names of dependency association.
40
+ def self.create_scopes(target_class, dependent_associations)
41
+ target_class.class_eval do
42
+ scope :only_soft_destroyed, -> { where.not(id: without_soft_destroyed.select(:id)) }
43
+ scope :without_soft_destroyed, (lambda do
44
+ dependent_associations.inject(where(soft_delete_column => nil)) do |relation, name|
45
+ association = relation.klass.reflect_on_all_associations.find{|a| a.name == name }
46
+ if association.klass.method_defined?(:soft_delete_column)
47
+ relation.joins(name).merge(association.klass.without_soft_destroyed).references(name)
48
+ else
49
+ relation.joins(name).references(name)
50
+ end
51
+ end
52
+ end)
53
+ end
54
+ end
31
55
  end
32
56
 
33
57
  module InstanceMethods
@@ -1,3 +1,3 @@
1
1
  module KakurenboPuti
2
- VERSION = '0.0.2'
2
+ VERSION = '0.0.3'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kakurenbo-puti
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - alfa-jpn
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-04 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: activerecord