Class: HasHelpers::EraseDenormalizedBelongsToColumnTrigger

Inherits:
PsqlHq::Trigger
  • Object
show all
Defined in:
app/triggers/has_helpers/erase_denormalized_belongs_to_column_trigger.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(owner_table, owner_column, affected_table, update_fields, column_to_evaluate, **options) ⇒ EraseDenormalizedBelongsToColumnTrigger

owner_table -> table where the trigger resides owner_column -> column where the trigger resides affected_table -> table where the update it is made update_fields -> columns to be changed in affected_table - can be an array or not column_to_evaluate -> condition that evaluates if the change has to be made or not (eg. writing advisor that belongs to an opportunity has to have opportunity_id not null)



12
13
14
15
16
17
18
19
# File 'app/triggers/has_helpers/erase_denormalized_belongs_to_column_trigger.rb', line 12

def initialize(owner_table, owner_column, affected_table, update_fields, column_to_evaluate, **options)
  @owner_table = owner_table
  @owner_column = owner_column
  @affected_table = affected_table
  @update_fields = update_fields
  @column_to_evaluate = column_to_evaluate
  super(owner_table, update_fields, options)
end

Instance Attribute Details

#affected_tableObject (readonly)

Returns the value of attribute affected_table.



5
6
7
# File 'app/triggers/has_helpers/erase_denormalized_belongs_to_column_trigger.rb', line 5

def affected_table
  @affected_table
end

#column_to_evaluateObject (readonly)

Returns the value of attribute column_to_evaluate.



5
6
7
# File 'app/triggers/has_helpers/erase_denormalized_belongs_to_column_trigger.rb', line 5

def column_to_evaluate
  @column_to_evaluate
end

#owner_columnObject (readonly)

Returns the value of attribute owner_column.



5
6
7
# File 'app/triggers/has_helpers/erase_denormalized_belongs_to_column_trigger.rb', line 5

def owner_column
  @owner_column
end

#owner_tableObject (readonly)

Returns the value of attribute owner_table.



5
6
7
# File 'app/triggers/has_helpers/erase_denormalized_belongs_to_column_trigger.rb', line 5

def owner_table
  @owner_table
end

#update_fieldsObject (readonly)

Returns the value of attribute update_fields.



5
6
7
# File 'app/triggers/has_helpers/erase_denormalized_belongs_to_column_trigger.rb', line 5

def update_fields
  @update_fields
end

Class Method Details

.prefixObject



21
22
23
# File 'app/triggers/has_helpers/erase_denormalized_belongs_to_column_trigger.rb', line 21

def self.prefix
  "HQD"
end

Instance Method Details

#affected_fieldsObject

if array then use this



26
27
28
# File 'app/triggers/has_helpers/erase_denormalized_belongs_to_column_trigger.rb', line 26

def affected_fields
  update_fields.kind_of?(Array) ? update_fields.join(" = NULL, ") : update_fields.to_s
end

#createObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/triggers/has_helpers/erase_denormalized_belongs_to_column_trigger.rb', line 39

def create
  ::ActiveRecord::Base.connection.execute sql_to_add_function
  after_or_before = trigger_after? ? "AFTER" : "BEFORE"
  ::ActiveRecord::Base.connection.execute <<-SQL
    CREATE TRIGGER "#{trigger_name}"
    #{after_or_before} DELETE
    ON #{owner_table}
    FOR EACH ROW
    EXECUTE PROCEDURE "#{function_name}"();
  SQL
  puts "-- Created trigger #{trigger_name} and function #{function_name}."
end

#dropObject



35
36
37
# File 'app/triggers/has_helpers/erase_denormalized_belongs_to_column_trigger.rb', line 35

def drop
  self.drop_by_function_name(function_name)
end

#nameObject



30
31
32
33
# File 'app/triggers/has_helpers/erase_denormalized_belongs_to_column_trigger.rb', line 30

def name
  fields_name = update_fields.kind_of?(Array) ? update_fields.join("_") : update_fields.to_s
  "#{fields_name}_on_#{affected_table}_#{owner_table.downcase}"
end