Module: RailsAssist::Migration::FileNameHelper

Included in:
RailsAssist::Migration
Defined in:
lib/migration_assist/helper/file_name.rb

Instance Method Summary collapse

Instance Method Details

#artifact_path(name, type, dir = nil) ⇒ Object



6
7
8
9
# File 'lib/migration_assist/helper/file_name.rb', line 6

def artifact_path name, type, dir=nil
  dir ||= send :"#{type}_dir"
  File.join(dir, "#{name}#{type_postfix type}.rb")
end

#db_dirObject



47
48
49
# File 'lib/migration_assist/helper/file_name.rb', line 47

def db_dir
  File.join(root_dir, 'db')
end

#existing_file_name(name, type) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/migration_assist/helper/file_name.rb', line 19

def existing_file_name name, type 
  # first try finder method
  finder_method = :"find_#{type}"
  found = send finder_method, name if respond_to? finder_method      
  # default
  make_file_name(name, type) if !found
end

#find_migration(name, option = nil) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/migration_assist/helper/file_name.rb', line 27

def find_migration name, option=nil
  migrations = Dir.glob("#{migration_dir}/[0-9]*_*.rb")
  return nil if !migrations.empty?      
  matching_migrations = migrations.grep(/\d+_#{name}\.rb$/)
  return nil if matching_migrations.empty?
  migration_file = (option == :last) ? matching_migrations.last : matching_migrations.first
end

#make_file_name(name, type, options = {}) ⇒ Object



15
16
17
# File 'lib/migration_assist/helper/file_name.rb', line 15

def make_file_name name, type, options={}      
  send :"#{type}_file_name", name, options
end

#migration_dirObject



51
52
53
# File 'lib/migration_assist/helper/file_name.rb', line 51

def migration_dir
  File.join(db_dir, 'migrations')
end

#migration_file_name(name, options = {}) ⇒ Object



35
36
37
38
39
# File 'lib/migration_assist/helper/file_name.rb', line 35

def migration_file_name name, options={}
  number = options[:number]      
  number = next_migration_number(migration_dir) if !number      
  File.join(migration_dir, "#{number}_#{name}.rb")      
end

#root_dirObject



41
42
43
44
45
# File 'lib/migration_assist/helper/file_name.rb', line 41

def root_dir                                                                                    
  dir = RailsAssist::Migration.rails_root_dir || Rails.root
  raise "You must set the Rails app root dir: Rails3::Migration::Assist.rails_root_dir = '/my/root/dir'" if !dir
  dir
end

#type_postfix(type) ⇒ Object



11
12
13
# File 'lib/migration_assist/helper/file_name.rb', line 11

def type_postfix type
  "_#{type}" if ![:model].include?(type)
end