Class: ActiveRecord::BaseWithoutTable

Inherits:
Base
  • Object
show all
Defined in:
lib/active_record/base_without_table.rb

Overview

Get the power of ActiveRecord models, including validation, without having a table in the database.

Usage

class Contact < ActiveRecord::BaseWithoutTable
  column :name, :text
  column :email_address, :text
  column :message, :text
end

validates_presence_of :name, :email_address, :string

This model can be used just like a regular model based on a table, except it will never be saved to the database.

Class Method Summary collapse

Class Method Details

.attribute_namesObject



47
48
49
# File 'lib/active_record/base_without_table.rb', line 47

def attribute_names
  _default_attributes.keys.map(&:to_s)
end

.attributes_builderObject



32
33
34
# File 'lib/active_record/base_without_table.rb', line 32

def attributes_builder
  AttributesBuilderWithoutTable.new(super)
end

.column(name, sql_type = nil, default = nil, null = true) ⇒ Object



51
52
53
54
55
56
# File 'lib/active_record/base_without_table.rb', line 51

def column(name, sql_type = nil, default = nil, null = true)
  cast_type = lookup_attribute_type(sql_type)
  decorated_type = attribute_type_decorations.apply(name, cast_type)

  define_attribute(name.to_s, decorated_type, default: default)
end

.connectionObject



28
29
30
# File 'lib/active_record/base_without_table.rb', line 28

def connection
  ConnectionAdapters::NullAdapter.new(super)
end

.gettext_translation_for_attribute_name(attribute) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/active_record/base_without_table.rb', line 76

def gettext_translation_for_attribute_name(attribute)
  # `rake gettext:store_model_attributes` processes our BaseWithoutTable models, but we have our own rake task
  # for that. Return right away if calling gettext_translation_for_attribute_name on BaseWithoutTable
  return "BaseWithoutTable" if self == BaseWithoutTable

  attribute = attribute.to_s
  if attribute.ends_with?("_id")
    humanize_class_name(attribute)
  else
    "#{self}|#{attribute.split('.').map!(&:humanize).join('|')}"
  end
end

.inherited(subclass) ⇒ Object



40
41
42
43
44
45
# File 'lib/active_record/base_without_table.rb', line 40

def inherited(subclass)
  subclass.define_singleton_method(:table_name) do
    "activerecord_base_without_table_#{subclass.name}"
  end
  super(subclass)
end

.lookup_attribute_type(sql_type) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/active_record/base_without_table.rb', line 58

def lookup_attribute_type(sql_type)
  # This is an emulation of the Rails 4.1 runtime behaviour.
  # Please consider rewriting once we move to Rails 5.1.
  mapped_sql_type =
    case sql_type
    when :datetime
      :date_time
    when :datetime_point
      :integer
    when :enumerable
      :value
    else
      sql_type
    end.to_s.camelize

  "::ActiveRecord::Type::#{mapped_sql_type}".constantize.new
end

.table_exists?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/active_record/base_without_table.rb', line 36

def table_exists?
  false
end