Class: Generators::Hobo::Migration::HabtmModelShim

Inherits:
Struct
  • Object
show all
Defined in:
lib/generators/hobo/migration/migrator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#connectionObject

Returns the value of attribute connection

Returns:

  • (Object)

    the current value of connection



5
6
7
# File 'lib/generators/hobo/migration/migrator.rb', line 5

def connection
  @connection
end

#foreign_keysObject

Returns the value of attribute foreign_keys

Returns:

  • (Object)

    the current value of foreign_keys



5
6
7
# File 'lib/generators/hobo/migration/migrator.rb', line 5

def foreign_keys
  @foreign_keys
end

#join_tableObject

Returns the value of attribute join_table

Returns:

  • (Object)

    the current value of join_table



5
6
7
# File 'lib/generators/hobo/migration/migrator.rb', line 5

def join_table
  @join_table
end

Class Method Details

.from_reflection(refl) ⇒ Object



7
8
9
10
11
12
13
14
15
# File 'lib/generators/hobo/migration/migrator.rb', line 7

def self.from_reflection(refl)
  result = self.new
  result.join_table = refl.options[:join_table].to_s
  result.foreign_keys = [refl.foreign_key.to_s, refl.association_foreign_key.to_s].sort
  # this may fail in weird ways if HABTM is running across two DB connections (assuming that's even supported)
  # figure that anybody who sets THAT up can deal with their own migrations...
  result.connection = refl.active_record.connection
  result
end

Instance Method Details

#field_specsObject



25
26
27
28
29
30
31
32
33
# File 'lib/generators/hobo/migration/migrator.rb', line 25

def field_specs
  i = 0
  foreign_keys.inject({}) do |h, v|
    # some trickery to avoid an infinite loop when FieldSpec#initialize tries to call model.field_specs
    h[v] = HoboFields::Model::FieldSpec.new(self, v, :integer, :position => i)
    i += 1
    h
  end
end

#index_specsObject



39
40
41
# File 'lib/generators/hobo/migration/migrator.rb', line 39

def index_specs
  []
end

#primary_keyObject



35
36
37
# File 'lib/generators/hobo/migration/migrator.rb', line 35

def primary_key
  false
end

#table_exists?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'lib/generators/hobo/migration/migrator.rb', line 21

def table_exists?
  ActiveRecord::Migration.table_exists? table_name
end

#table_nameObject



17
18
19
# File 'lib/generators/hobo/migration/migrator.rb', line 17

def table_name
  self.join_table
end