Module: ActiveRecord::Comments::AbstractAdapterExt

Defined in:
lib/activerecord-comments/abstract_adapter_ext.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/activerecord-comments/abstract_adapter_ext.rb', line 3

def self.included base
  puts "including abstract adapter extensions in #{ base.inspect }"

  base.instance_eval {
    alias_method_chain :columns, :table_name # this is evil!!!  how to fix?  column needs to know its table  :(
  }
end

Instance Method Details

#column_comment(column, table) ⇒ Object

Get the database comment (if any) defined for a column

Parameters

column<~to_s>

The name of the column to get the comment for

table<~to_s>

The name of the table to get the column comment for

Returns

String

The comment for the given column (or nil if no comment)

:api: public



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/activerecord-comments/abstract_adapter_ext.rb', line 59

def column_comment column, table
  adapter = adapter_name.downcase
  database_specific_method_name = "#{ adapter }_column_comment"
  
  if self.respond_to? database_specific_method_name
    send database_specific_method_name, column.to_s, table.to_s
  else

    # try requiring 'activerecord-comments/[name-of-adapter]_adapter'
    begin

      # see if there right method exists after requiring
      require "activerecord-comments/#{ adapter }_adapter"
      if self.respond_to? database_specific_method_name
        send database_specific_method_name, column.to_s, table.to_s
      else
        raise ActiveRecord::Comments::UnsupportedDatabase.new("#{adapter} unsupported by ActiveRecord::Comments")
      end

    rescue LoadError
      raise ActiveRecord::Comments::UnsupportedDatabase.new("#{adapter} unsupported by ActiveRecord::Comments")
    end
  end
end

#columns_with_table_name(*args) ⇒ Object

Extends #columns, setting @table_name as an instance variable on each of the column instances that are returned

Returns

Array

Returns an Array of column objects, each with @table_name set

:api: private



92
93
94
95
96
97
98
99
100
101
# File 'lib/activerecord-comments/abstract_adapter_ext.rb', line 92

def columns_with_table_name *args
  puts "\n\n HELLO ???? HELLO ????? \n\n"
  puts "asking adapter for columns for #{ args.inspect }"
  columns = columns_without_table_name *args
  table = self.table_name # make table_name available as variable in instanve_eval closure
  columns.each do |column|
    column.instance_eval { @table_name = table }
  end
  columns
end

#comment(table) ⇒ Object

Get the database comment (if any) defined for a table

Parameters

table<~to_s>

The name of the table to get the comment for

Returns

String

The comment for the given table (or nil if no comment)

:api: public



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/activerecord-comments/abstract_adapter_ext.rb', line 21

def comment table
  adapter = adapter_name.downcase
  database_specific_method_name = "#{ adapter }_comment"
  
  if self.respond_to? database_specific_method_name
    send database_specific_method_name, table.to_s
  else

    # try requiring 'activerecord-comments/[name-of-adapter]_adapter'
    begin

      # see if there right method exists after requiring
      require "activerecord-comments/#{ adapter }_adapter"
      if self.respond_to? database_specific_method_name
        send database_specific_method_name, table.to_s
      else
        raise ActiveRecord::Comments::UnsupportedDatabase.new("#{adapter} unsupported by ActiveRecord::Comments")
      end

    rescue LoadError
      raise ActiveRecord::Comments::UnsupportedDatabase.new("#{adapter} unsupported by ActiveRecord::Comments")
    end
  end
end