Sha256: 9698d89ea50f3c184ce82706affdfb2b51c091c14b45f3c174ede98d9c406f2c

Contents?: true

Size: 912 Bytes

Versions: 5

Compression:

Stored size: 912 Bytes

Contents

module Annotator
  module InitialDescription

    # Initial descriptions for columns associated with belongs_to
    class BelongsTo < Base

      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

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
annotator-0.0.10 lib/annotator/initial_description/belongs_to.rb
annotator-0.0.9 lib/annotator/initial_description/belongs_to.rb
annotator-0.0.8.1 lib/annotator/initial_description/belongs_to.rb
annotator-0.0.8 lib/annotator/initial_description/belongs_to.rb
annotator-0.0.7 lib/annotator/initial_description/belongs_to.rb