Module: DeletedAt::ActiveRecord::Base::ClassMethods

Defined in:
lib/deleted_at/active_record/base.rb

Instance Method Summary collapse

Instance Method Details

#create_with_deleted_at(attributes = nil, &block) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/deleted_at/active_record/base.rb', line 27

def create_with_deleted_at(attributes = nil, &block)
  if archive_with_deleted_at?
    const_get(:All).create_without_deleted_at(attributes, &block).becomes(self)
  else
    create_without_deleted_at(attributes, &block)
  end
end

#create_with_deleted_at!(attributes = nil, &block) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/deleted_at/active_record/base.rb', line 35

def create_with_deleted_at!(attributes = nil, &block)
  if archive_with_deleted_at?
    const_get(:All).create_without_deleted_at!(attributes, &block).becomes(self)
  else
    create_without_deleted_at!(attributes, &block)
  end
end

#with_deleted_at(options = {}) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/deleted_at/active_record/base.rb', line 43

def with_deleted_at(options={})

  parse_options(options)

  unless ::DeletedAt::Views.all_table_exists?(self) && ::DeletedAt::Views.deleted_view_exists?(self)
    return warn("You're trying to use `with_deleted_at` on #{name} but you have not installed the views, yet.")
  end

  unless columns.map(&:name).include?(deleted_at_column)
    return warn("Missing `#{deleted_at_column}` in `#{name}` when trying to employ `deleted_at`")
  end

  [:archive_with_deleted_at?, :archive_with_deleted_by?].each do |sym|
    class_eval <<-BBB
      def self.#{sym}
        true
      end
    BBB
  end


  # We are confident at this point that the tables and views have been setup.
  # We need to do a bit of wizardy by setting the table name to the actual table
  # (at this point: model/all), such that the model has all the information
  # regarding its structure and intended behavior. Calling primary_key loads the
  # table data into the class.
  self.original_table_name = self.table_name
  self.table_name = ::DeletedAt::Views.all_table(self)
  primary_key = self.primary_key
  self.table_name = self.original_table_name

  setup_class_views
  with_deleted_by

end