Class: Annotator::InitialDescription::BelongsTo

Inherits:
Base
  • Object
show all
Defined in:
lib/annotator/initial_description/belongs_to.rb

Overview

Initial descriptions for columns associated with belongs_to

Instance Method Summary collapse

Methods inherited from Base

inherited, #initialize, providers

Constructor Details

This class inherits a constructor from Annotator::InitialDescription::Base

Instance Method Details

#checkObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/annotator/initial_description/belongs_to.rb', line 7

def check
  # check if there is belongs to association where this column is a foreign key
  @model.reflect_on_all_associations.each do |reflection|
    if (reflection.options[:foreign_key] || reflection.send(:derive_foreign_key)) == @column && reflection.macro == :belongs_to
      @reflection = reflection
      return true
    end
  end

  # Polymorphic association type column
  if @column.ends_with? '_type'
    return true if @reflection = @model.reflect_on_association(@column.match(/(.*?)_type$/)[1].to_sym) 
  end

  return false
end

#textObject



24
25
26
# File 'lib/annotator/initial_description/belongs_to.rb', line 24

def text
  "belongs to :#{@reflection.name}#{@reflection.options[:polymorphic] ? ' (polymorphic)' : ''}"
end