class SoftDeletablePetit::Builder

Attributes

klass[R]
options[R]

Public Class Methods

build(klass, options = {}) click to toggle source
# File lib/soft_deletable_petit/builder.rb, line 7
def self.build(klass, options = {})
  new(klass, options).build
end
new(klass, options = {}) click to toggle source
# File lib/soft_deletable_petit/builder.rb, line 11
def initialize(klass, options = {})
  @klass = klass
  @options = options
end

Public Instance Methods

build() click to toggle source
# File lib/soft_deletable_petit/builder.rb, line 16
def build
  define_column_name_accessor(options.column)

  define_instance_methods(options.soft_delete_method_name, options.restore_method_name)
  define_scopes(options.soft_deleted_scope, options.without_soft_deleted_scope)
end

Private Instance Methods

define_column_name_accessor(col) click to toggle source
# File lib/soft_deletable_petit/builder.rb, line 25
def define_column_name_accessor(col)
  klass.class_eval do
    define_singleton_method(:soft_delete_column){ col }
    define_method(:soft_delete_column){ col }
  end
end
define_instance_methods(soft_delete_method_name, restore_method_name) click to toggle source
# File lib/soft_deletable_petit/builder.rb, line 32
def define_instance_methods(soft_delete_method_name, restore_method_name)
  klass.class_eval do
    define_model_callbacks soft_delete_method_name
    define_model_callbacks restore_method_name

    define_method "#{soft_delete_method_name}!" do
      run_callbacks(soft_delete_method_name) { touch soft_delete_column; self }
    end
    define_method soft_delete_method_name do
      !! send("#{soft_delete_method_name}!") rescue false
    end

    define_method "#{restore_method_name}!" do
      run_callbacks(restore_method_name) { update_column soft_delete_column, nil; self  }
    end
    define_method restore_method_name do
      !! send("#{restore_method_name}!") rescue false
    end
  end
end
define_scopes(soft_deleted_scope, without_soft_deleted_scope) click to toggle source
# File lib/soft_deletable_petit/builder.rb, line 53
def define_scopes(soft_deleted_scope, without_soft_deleted_scope)
  klass.class_eval do
    scope soft_deleted_scope, ->{ where.not(soft_delete_column => nil) }
    scope without_soft_deleted_scope, ->{ where(soft_delete_column => nil) }

    define_method "#{soft_deleted_scope}?" do
      send("#{soft_delete_column}?")
    end

    define_method "#{without_soft_deleted_scope}?" do
      !send("#{soft_delete_column}?")
    end
  end
end