Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/activerecord/tablelocks/activerecord.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.locking_enabledObject (readonly)

Returns the value of attribute locking_enabled.



5
6
7
# File 'lib/activerecord/tablelocks/activerecord.rb', line 5

def locking_enabled
  @locking_enabled
end

Class Method Details

.enable_locking(options = {}) ⇒ Object



7
8
9
10
11
12
13
# File 'lib/activerecord/tablelocks/activerecord.rb', line 7

def enable_locking(options = {})
  @locking_enabled = true
  @lock_targets = {
    :class_names => [*options[:class_names]].compact,
    :table_names => [*options[:table_names]].compact
  }
end

.lock_targetsObject



15
16
17
18
19
20
# File 'lib/activerecord/tablelocks/activerecord.rb', line 15

def lock_targets
  @lock_targets ||= {
    :class_names => [],
    :table_names => []
  }
end

.tables_to_lockObject



22
23
24
25
26
27
# File 'lib/activerecord/tablelocks/activerecord.rb', line 22

def tables_to_lock
  return [] unless @locking_enabled
  [quoted_table_name,
    *@lock_targets[:table_names].map{|table_name| connection.quote_table_name(table_name) },
    *@lock_targets[:class_names].map{|class_name| class_name.constantize.quoted_table_name }].sort.uniq
end

Instance Method Details

#add_to_transactionObject



30
31
32
33
34
35
# File 'lib/activerecord/tablelocks/activerecord.rb', line 30

def add_to_transaction
  if self.class.locking_enabled
    self.class.connection.lock_tables self.class.tables_to_lock
  end
  super
end