Sha256: 66c30608a735d61eccde0f46a551b054111684eb7abf139f4a66d16f3d700d2c

Contents?: true

Size: 1.33 KB

Versions: 2

Compression:

Stored size: 1.33 KB

Contents

# frozen_string_literal: true

module InvisibleRecord
  # Helps other modules complete methods
  module Helper
    def self.define_actions(klass, deleted_ts_attr:)
      klass.define_method "restore" do |*_args|
        assign_timestamp = "#{deleted_ts_attr}="
        send(assign_timestamp, nil)
      end

      klass.define_method "restore!" do |*_args|
        restore
        save!
      end

      klass.define_method "soft_delete" do |*args|
        options = args.last || {}
        options[:datetime] ||= DateTime.now
        assign_timestamp = "#{deleted_ts_attr}="
        send(assign_timestamp, options[:datetime])
      end

      klass.define_method "soft_delete!" do |*args|
        options = args.last || {}
        options[:datetime] ||= DateTime.now
        soft_delete(datetime: options[:datetime])
        save!
      end
    end

    def self.define_hidden_attributes(klass, deleted_ts_attr:)
      klass.attribute_names.each do |attribute|
        klass.define_method attribute do |*_args|
          return attributes[deleted_ts_attr] if attribute == deleted_ts_attr

          if attributes[deleted_ts_attr].present?
            nil
          else
            attributes[attribute]
          end
        end

        klass.define_method "hidden_#{attribute}" do |*_args|
          attributes[attribute]
        end
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
invisible_record-0.1.4 lib/invisible_record/helper.rb
invisible_record-0.1.3 lib/invisible_record/helper.rb