module ActsAsRemovable::ClassMethods
Public Class Methods
after_remove(*args, &block)
click to toggle source
# File lib/acts_as_removable.rb, line 37 def self.after_remove(*args, &block) set_callback(:remove, :after, *args, &block) end
after_unremove(*args, &block)
click to toggle source
# File lib/acts_as_removable.rb, line 45 def self.after_unremove(*args, &block) set_callback(:unremove, :after, *args, &block) end
before_remove(*args, &block)
click to toggle source
# File lib/acts_as_removable.rb, line 33 def self.before_remove(*args, &block) set_callback(:remove, :before, *args, &block) end
before_unremove(*args, &block)
click to toggle source
# File lib/acts_as_removable.rb, line 41 def self.before_unremove(*args, &block) set_callback(:unremove, :before, *args, &block) end
Public Instance Methods
_acts_as_removable_options()
click to toggle source
# File lib/acts_as_removable.rb, line 86 def _acts_as_removable_options @_acts_as_removable_options ||= { column_name: 'removed_at' } end
_update_remove_attribute(callback, value, with_bang = false, options = {})
click to toggle source
# File lib/acts_as_removable.rb, line 69 def _update_remove_attribute(callback, value, with_bang = false, options = {}) self.class.transaction do run_callbacks callback.to_sym do send("#{self.class._acts_as_removable_options[:column_name]}=", value) # workaround for new argument handling if RUBY_VERSION.to_i < 3 with_bang ? save!(options) : save(options) else with_bang ? save!(**options) : save(**options) end end end end
acts_as_removable(options = {})
click to toggle source
Add ability to remove ActiveRecord instances
acts_as_removable acts_as_removable column_name: 'other_column_name'
Options¶ ↑
-
:column_name
- A symbol or string with the column to use for removal timestamp.
# File lib/acts_as_removable.rb, line 19 def acts_as_removable(options = {}) _acts_as_removable_options.merge!(options) scope :removed, lambda { where(all.table[_acts_as_removable_options[:column_name]].not_eq(nil).to_sql) } scope :actives, lambda { where(all.table[_acts_as_removable_options[:column_name]].eq(nil).to_sql) } define_model_callbacks :remove, :unremove class_eval do def self.before_remove(*args, &block) set_callback(:remove, :before, *args, &block) end def self.after_remove(*args, &block) set_callback(:remove, :after, *args, &block) end def self.before_unremove(*args, &block) set_callback(:unremove, :before, *args, &block) end def self.after_unremove(*args, &block) set_callback(:unremove, :after, *args, &block) end def removed? send(self.class._acts_as_removable_options[:column_name]).present? end def remove(options = {}) _update_remove_attribute(:remove, Time.now, false, options) end def remove!(options = {}) _update_remove_attribute(:remove, Time.now, true, options) end def unremove(options = {}) _update_remove_attribute(:unremove, nil, false, options) end def unremove!(options = {}) _update_remove_attribute(:unremove, nil, true, options) end def _update_remove_attribute(callback, value, with_bang = false, options = {}) self.class.transaction do run_callbacks callback.to_sym do send("#{self.class._acts_as_removable_options[:column_name]}=", value) # workaround for new argument handling if RUBY_VERSION.to_i < 3 with_bang ? save!(options) : save(options) else with_bang ? save!(**options) : save(**options) end end end end end end
remove(options = {})
click to toggle source
# File lib/acts_as_removable.rb, line 53 def remove(options = {}) _update_remove_attribute(:remove, Time.now, false, options) end
remove!(options = {})
click to toggle source
# File lib/acts_as_removable.rb, line 57 def remove!(options = {}) _update_remove_attribute(:remove, Time.now, true, options) end
removed?()
click to toggle source
# File lib/acts_as_removable.rb, line 49 def removed? send(self.class._acts_as_removable_options[:column_name]).present? end
unremove(options = {})
click to toggle source
# File lib/acts_as_removable.rb, line 61 def unremove(options = {}) _update_remove_attribute(:unremove, nil, false, options) end
unremove!(options = {})
click to toggle source
# File lib/acts_as_removable.rb, line 65 def unremove!(options = {}) _update_remove_attribute(:unremove, nil, true, options) end