Class: Trimodel::NewGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/trimodel/new_generator.rb

Instance Method Summary collapse

Instance Method Details

#check_parametersObject



12
13
14
15
16
17
18
19
# File 'lib/generators/trimodel/new_generator.rb', line 12

def check_parameters
  if options[:models].class == NilClass
    puts "error: You need to give the models you want to associate"
    puts "e.g. rails g trimodel:new --models a b c"
  elsif options[:models].size != 3
    puts "error: Wrong number of models, they should be 3"
  end
end

#create_migration_filesObject



21
22
23
24
25
26
27
# File 'lib/generators/trimodel/new_generator.rb', line 21

def create_migration_files
  create_migration_file options[:models][0], options[:models][1]
  #need to wait so the timestamp has a different value
  #and forms a correct migration file name
  sleep(1)
  create_migration_file options[:models][1], options[:models][2]
end

#create_trimodel_fileObject



33
34
35
36
37
38
39
40
# File 'lib/generators/trimodel/new_generator.rb', line 33

def create_trimodel_file
  File.open(Rails.root + "config/initializers/trimodel.rb",
    File::CREAT|File::RDWR) do |f|
      add_one_asoc_to_model f, options[:models][0], options[:models][1], options[:models][2]
      add_two_asocs_to_model f, options[:models][1], options[:models][0], options[:models][2]
      add_one_asoc_to_model f, options[:models][2], options[:models][1], options[:models][0]
  end
end

#perform_migrationsObject



29
30
31
# File 'lib/generators/trimodel/new_generator.rb', line 29

def perform_migrations
  %x[rake db:migrate]
end