Module: ActiveRecord::Tablefree::SingletonMethods

Defined in:
lib/activerecord/tablefree.rb

Instance Method Summary collapse

Instance Method Details

#add_columns(sql_type, *args) ⇒ Object

Register a set of columns with the same SQL type



101
102
103
104
105
# File 'lib/activerecord/tablefree.rb', line 101

def add_columns(sql_type, *args)
  args.each do |col|
    column col, sql_type
  end
end

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

Register a new column.

Raises:



89
90
91
92
93
94
95
96
97
98
# File 'lib/activerecord/tablefree.rb', line 89

def column(name, sql_type = nil, default = nil, null = true, cast_class_params = nil)
  cast_class = if sql_type.is_a?(Class) && sql_type < ActiveModel::Type::Value
                 sql_type
               else
                 "ActiveRecord::Type::#{sql_type.to_s.camelize}".constantize rescue nil
               end
  raise InvalidColumnType, "sql_type is #{sql_type} (#{sql_type.class}), which is not supported" unless cast_class.respond_to?(:new)
  cast_type = cast_class.new(*cast_class_params)
  tablefree_options[:columns_hash][name.to_s] = ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default, cast_type, null)
end

#destroy(*_args) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/activerecord/tablefree.rb', line 107

def destroy(*_args)
  case tablefree_options[:database]
  when :pretend_success
    new
  when :fail_fast
    raise NoDatabase, "Can't #destroy on Tablefree class"
  end
end

#destroy_all(*_args) ⇒ Object



116
117
118
119
120
121
122
123
# File 'lib/activerecord/tablefree.rb', line 116

def destroy_all(*_args)
  case tablefree_options[:database]
  when :pretend_success
    []
  when :fail_fast
    raise NoDatabase, "Can't #destroy_all on Tablefree class"
  end
end

#load_schema!Object

Used internally by ActiveRecord 5. This is the special hook that makes everything else work.



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

def load_schema!
  @columns_hash = tablefree_options[:columns_hash].except(*ignored_columns)
  @columns_hash.each do |name, column|
    define_attribute(
      name,
      connection.lookup_cast_type_from_column(column),
      default: column.default,
      user_provided_default: false
    )
  end
end

#table_exists?Boolean

Returns:

  • (Boolean)


153
154
155
# File 'lib/activerecord/tablefree.rb', line 153

def table_exists?
  false
end

#tablefree?Boolean

Returns:

  • (Boolean)


149
150
151
# File 'lib/activerecord/tablefree.rb', line 149

def tablefree?
  true
end

#transactionObject



139
140
141
142
143
144
145
146
147
# File 'lib/activerecord/tablefree.rb', line 139

def transaction
  #        case tablefree_options[:database]
  #        when :pretend_success
  @_current_transaction_records ||= []
  yield
  #        when :fail_fast
  #          raise NoDatabase.new("Can't #transaction on Tablefree class")
  #        end
end