Sha256: c3a4a89f850bd33470773b8b96b927bf848a4d4323564f0eb3378648eaabeb77
Contents?: true
Size: 1.25 KB
Versions: 19
Compression:
Stored size: 1.25 KB
Contents
module RestrictiveDestroyer extend ActiveSupport::Concern included do before_destroy :destroyable? end module ClassMethods def acts_as_restrictive_destroyer(options={}) raise ArgumentError, "Hash expected, got #{options.class.name}" unless options.is_a?(Hash) class_attribute :restrictive_destroyer_config, :restrictive_destroyer_col_ref self.restrictive_destroyer_config = { column: "deleted_at" }.merge!(options) self.restrictive_destroyer_col_ref = "#{self.table_name}.#{restrictive_destroyer_config[:column]}" scope :undeleted, -> { where("#{restrictive_destroyer_col_ref} IS NULL") } scope :deleted, -> { where("#{restrictive_destroyer_col_ref} IS NOT NULL") } end end def readonly? is_destroyed? end def restrictive_destroy update_attributes("#{restrictive_destroyer_attr_ref}" => Time.current) end def restrictive_destroy_with_api save_and_manage_api("#{restrictive_destroyer_attr_ref}" => Time.current) end def is_destroyed? !send("#{restrictive_destroyer_attr_ref}_changed?") && send("#{restrictive_destroyer_attr_ref}?") end private def restrictive_destroyer_attr_ref self.class.restrictive_destroyer_config[:column] end def destroyable? false end end
Version data entries
19 entries across 19 versions & 1 rubygems