module Kakurenbo::Core
Public Class Methods
Extend ClassMethods
after include.
# File lib/kakurenbo/core.rb, line 4 def self.included(base_class) base_class.extend ClassMethods base_class.extend Callbacks base_class.extend Scopes base_class.extend Aliases end
Public Instance Methods
delete record.
@param options [Hash] options. @option options [Boolean] hard (false) if hard-delete.
# File lib/kakurenbo/core.rb, line 57 def delete(options = {:hard => false}) if options[:hard] self.class.delete(self.id, options) else return if new_record? or destroyed? update_column kakurenbo_column, current_time_from_proper_timezone end end
destroy record and run callbacks.
@param options [Hash] options. @option options [Boolean] hard (false) if hard-delete.
@return [Boolean, self] if action is cancelled, return false.
# File lib/kakurenbo/core.rb, line 72 def destroy(options = {:hard => false}) if options[:hard] with_transaction_returning_status do hard_destroy_associated_records self.reload.hard_destroy end else return true if destroyed? with_transaction_returning_status do destroy_at = Time.now run_callbacks(:destroy){ update_column kakurenbo_column, destroy_at; self } end end end
destroy record and run callbacks.
@param options [Hash] options. @option options [Boolean] hard (false) if hard-delete.
@return [self] self.
# File lib/kakurenbo/core.rb, line 93 def destroy!(options = {:hard => false}) destroy(options) || raise(ActiveRecord::RecordNotDestroyed) end
# File lib/kakurenbo/core.rb, line 97 def destroy_row(options = {:hard => true}) relation_for_destroy.delete_all(nil, options) end
# File lib/kakurenbo/core.rb, line 101 def destroyed? !send(kakurenbo_column).nil? end
# File lib/kakurenbo/core.rb, line 105 def kakurenbo_column self.class.kakurenbo_column end
# File lib/kakurenbo/core.rb, line 109 def persisted? !new_record? end
restore record.
@param options [Hash] options. @option options [Boolean] recursive (true) if restore recursive.
@return [Boolean, self] if action is cancelled, return false.
# File lib/kakurenbo/core.rb, line 119 def restore(options = {:recursive => true}) return false unless destroyed? with_transaction_returning_status do run_callbacks(:restore) do restore_associated_records if options[:recursive] update_column kakurenbo_column, nil self end end end
restore record.
@param options [Hash] options. @option options [Boolean] hard (false) if hard-delete.
@return [self] self.
# File lib/kakurenbo/core.rb, line 137 def restore!(options = {:recursive => true}) restore(options) || raise(ActiveRecord::RecordNotRestored) end
Private Instance Methods
All Scope of dependent association.
@return [Array<ActiveRecord::Relation>] array of dependent association.
# File lib/kakurenbo/core.rb, line 145 def dependent_association_scopes self.class.reflect_on_all_associations.select { |reflection| reflection.options[:dependent] == :destroy and reflection.klass.paranoid? }.map { |reflection| self.association(reflection.name).tap {|assoc| assoc.reset_scope }.scope } end
Hard-Destroy associated records.
# File lib/kakurenbo/core.rb, line 154 def hard_destroy_associated_records dependent_association_scopes.each do |scope| scope.with_deleted.destroy_all(nil, hard: true) end end
Restore associated records. @note Record will be restored if record#deleted_at is newer than parent_deleted_at.
# File lib/kakurenbo/core.rb, line 162 def restore_associated_records dependent_association_scopes.each do |scope| scope.restore_all end end