Module: LowCardTables::LowCardTable::Base::ClassMethods

Defined in:
lib/low_card_tables/low_card_table/base.rb

Instance Method Summary collapse

Instance Method Details

#_low_card_disable_save_when_needed!Object

See LowCardTables::HasLowCardTable::LowCardObjectsManager for more details. In short, you should never be saving low-card objects directly; you should rather let the low-card Gem create such rows for you automatically, based on the attributes you assign to the model.

This method is invoked only once, when #is_low_card_table is called.



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/low_card_tables/low_card_table/base.rb', line 98

def _low_card_disable_save_when_needed!
  send(:define_method, :save_low_card_row!) do |*args|
    begin
      @_low_card_saves_allowed = true
      save!(*args)
    ensure
      @_low_card_saves_allowed = false
    end
  end

  send(:define_method, :save_low_card_row) do |*args|
    begin
      @_low_card_saves_allowed = true
      save(*args)
    ensure
      @_low_card_saves_allowed = false
    end
  end

  %w{save save!}.each do |method_name|
    send(:define_method, method_name) do |*args|
      if @_low_card_saves_allowed
        super(*args)
      else
        raise LowCardTables::Errors::LowCardCannotSaveAssociatedLowCardObjectsError, %{You just tried to save a model that represents a row in a low-card table.
You can't do this, because the entire low-card system relies on the fact that low-card rows
are immutable once created. Changing this row would therefore change the logical state of
many, many rows that are associated with this one, and that is almost certainly not what
you want.

Instead, simply modify the low-card attributes directly -- typically on the associated object
(e.g., my_user.deleted = true), or on the low-card object (my_user.status.deleted = true),
and then save the associated object instead (my_user.save!). This will trigger the low-card
system to recompute which low-card row the object should be associated with, and update it
as needed, which is almost certainly what you actually want.

If you are absolutely certain you know what you're doing, you can call #save_low_card_row!
on this object, and it will save, but make sure you understand ALL the implications first.}
      end
    end
  end
end

#_low_card_row_managerObject

Returns the associated LowCardTables::LowCardTable::RowManager object for this class, which is where an awful lot of the real work happens.



167
168
169
# File 'lib/low_card_tables/low_card_table/base.rb', line 167

def _low_card_row_manager
  @_low_card_row_manager ||= LowCardTables::LowCardTable::RowManager.new(self)
end

#is_low_card_table(options = { }) ⇒ Object

Declares that this is a low-card table. This should only ever be used on tables that are, in fact, low-card tables.

options can contain:

:exclude_column_names

Excludes the specified Array of column names from being treated as low-card columns; this happens by default to created_at and updated_at. These columns will not be touched by the low-card code, meaning they have to be nullable or have defaults.

:max_row_count

The low-card system has a check built in to start raising errors if you appear to be storing data in a low-card table that is, in fact, not actually of low cardinality. The effect that doing this has is to explode the number of rows in the low-card table, so the check simply tests the total number of rows in the table. This defaults to 5,000 (in LowCardTables::LowCardTable::Cache::DEFAULT_MAX_ROW_COUNT). If you really do have a valid low-card table with more than this number of rows, you can override that limit here.



88
89
90
91
# File 'lib/low_card_tables/low_card_table/base.rb', line 88

def is_low_card_table(options = { })
  self.low_card_options = options
  _low_card_disable_save_when_needed!
end

#is_low_card_table?Boolean

Is this a low-card table? Since this module has been included into the class in question (which happens via #is_low_card_table), then the answer is always, ‘yes’.

Returns:

  • (Boolean)


143
144
145
# File 'lib/low_card_tables/low_card_table/base.rb', line 143

def is_low_card_table?
  true
end

#low_card_optionsObject

This returns the set of low-card options specified for this class in #is_low_card_table.



156
157
158
# File 'lib/low_card_tables/low_card_table/base.rb', line 156

def low_card_options
  @_low_card_options ||= { }
end

#low_card_options=(options) ⇒ Object

This sets the set of low-card options.



161
162
163
# File 'lib/low_card_tables/low_card_table/base.rb', line 161

def low_card_options=(options)
  @_low_card_options = options
end

#reset_column_informationObject

This is a method provided by ActiveRecord::Base. When the set of columns on a low-card table has changed, we need to tell the row manager, so that it can flush its caches.



149
150
151
152
153
# File 'lib/low_card_tables/low_card_table/base.rb', line 149

def reset_column_information
  out = super
  _low_card_row_manager.column_information_reset!
  out
end