Class: Localtower::Generators::ServiceObjects::InsertForeignKeys

Inherits:
Object
  • Object
show all
Defined in:
lib/localtower/generators/service_objects/insert_foreign_keys.rb

Instance Method Summary collapse

Constructor Details

#initialize(attributes) ⇒ InsertForeignKeys

Returns a new instance of InsertForeignKeys.



5
6
7
# File 'lib/localtower/generators/service_objects/insert_foreign_keys.rb', line 5

def initialize(attributes)
  @attributes = attributes
end

Instance Method Details

#callObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/localtower/generators/service_objects/insert_foreign_keys.rb', line 9

def call
  attributes.each do |attribute|
    attribute.each do |attr_key, options|
      line_str = Localtower::Tools.line_for_attribute(attr_key)[0]
      if line_str.present?
        if options['foreign_key'].present?
          if line_str['foreign_key: true']
            content = File.read(Localtower::Tools.last_migration_pending)
          else
            content = File.read(Localtower::Tools.last_migration_pending).gsub(line_str, "#{line_str}, foreign_key: true")
          end
        else
          content = File.read(Localtower::Tools.last_migration_pending).gsub(line_str, line_str.gsub(', foreign_key: true', ''))
        end
        File.write(Localtower::Tools.last_migration_pending, content)
      end
    end
  end
end