Class: Commento::Adapters::ActiveRecord

Inherits:
Object
  • Object
show all
Defined in:
lib/commento/adapters/active_record.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ ActiveRecord

Public: Initialize a new ActiveRecord adapter instance.

name - The Symbol name for this adapter. Optional (default :active_record)



14
15
16
# File 'lib/commento/adapters/active_record.rb', line 14

def initialize(options={})
  @name = options.fetch(:name, :active_record)
end

Instance Attribute Details

#nameObject (readonly)

Public: The name of the adapter.



9
10
11
# File 'lib/commento/adapters/active_record.rb', line 9

def name
  @name
end

Instance Method Details

#fetch_column_comment(table_name, column_name) ⇒ Object

Public: Returns comment for table’s column.



39
40
41
42
43
# File 'lib/commento/adapters/active_record.rb', line 39

def fetch_column_comment(table_name, column_name)
  sql = columns_comments_sql(table_name)
  column_data = execute(sql).to_a.find { |data| data['column_name'].to_sym == column_name }
  column_data.present? ? column_data['col_description'] : nil
end

#fetch_comments_for_databaseObject

Public: Returns comments for tables and columns.



46
47
48
49
50
51
52
# File 'lib/commento/adapters/active_record.rb', line 46

def fetch_comments_for_database
  execute(tables_comments_sql).to_a.filter_map do |table_data|
    next if configuration.skip_table_names.include?(table_data['table_name'])

    parse_data(table_data)
  end
end

#fetch_table_comment(table_name) ⇒ Object

Public: Returns comment for table.



26
27
28
29
# File 'lib/commento/adapters/active_record.rb', line 26

def fetch_table_comment(table_name)
  table_data = execute(tables_comments_sql).to_a.find { |data| data['table_name'] == table_name }
  table_data.present? ? table_data['obj_description'] : nil
end

#set_column_comment(table_name, column_name, value) ⇒ Object

Public: Sets comment for table’s column.



32
33
34
35
36
# File 'lib/commento/adapters/active_record.rb', line 32

def set_column_comment(table_name, column_name, value)
  sql = "COMMENT ON COLUMN #{table_name}.#{column_name} IS "
  sql += value ? "'#{value}'" : 'NULL'
  execute(sql)
end

#set_table_comment(table_name, value) ⇒ Object

Public: Sets comment for table.



19
20
21
22
23
# File 'lib/commento/adapters/active_record.rb', line 19

def set_table_comment(table_name, value)
  sql = "COMMENT ON TABLE #{table_name} IS "
  sql += value ? "'#{value}'" : 'NULL'
  execute(sql)
end