Module: KakurenboPuti::ActiveRecordBase::Initializers
- Defined in:
- lib/kakurenbo_puti/active_record_base.rb
Class Method Summary collapse
-
.create_callbacks(target_class) ⇒ Object
Create callbacks.
-
.create_column_name_accessors(target_class, column) ⇒ Object
Create attribute_accessors of column_name.
-
.create_scopes(target_class, dependent_associations) ⇒ Object
Create scopes.
Class Method Details
.create_callbacks(target_class) ⇒ Object
Create callbacks.
20 21 22 23 24 25 |
# File 'lib/kakurenbo_puti/active_record_base.rb', line 20 def self.create_callbacks(target_class) target_class.class_eval do define_model_callbacks :restore define_model_callbacks :soft_destroy end end |
.create_column_name_accessors(target_class, column) ⇒ Object
Create attribute_accessors of column_name.
30 31 32 33 34 35 |
# File 'lib/kakurenbo_puti/active_record_base.rb', line 30 def self.create_column_name_accessors(target_class, column) target_class.class_eval do define_singleton_method(:soft_delete_column) { column } delegate :soft_delete_column, to: :class end end |
.create_scopes(target_class, dependent_associations) ⇒ Object
Create scopes.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/kakurenbo_puti/active_record_base.rb', line 40 def self.create_scopes(target_class, dependent_associations) target_class.class_eval do scope :only_soft_destroyed, -> { where.not(id: without_soft_destroyed.select(:id)) } scope :without_soft_destroyed, (lambda do dependent_associations.inject(where(soft_delete_column => nil)) do |relation, name| association = relation.klass.reflect_on_all_associations.find{|a| a.name == name } if association.klass.method_defined?(:soft_delete_column) relation.joins(name).merge(association.klass.without_soft_destroyed).references(name) else relation.joins(name).references(name) end end end) end end |