Class: ApspGenerator
- Inherits:
-
ActiveRecord::Generators::Base
- Object
- ActiveRecord::Generators::Base
- ApspGenerator
- Defined in:
- lib/generators/apsp/apsp_generator.rb
Instance Method Summary collapse
-
#create_apsp_file ⇒ Object
name_path_arry = name.split(‘/’) binding.pry.
- #create_connection ⇒ Object
Instance Method Details
#create_apsp_file ⇒ Object
name_path_arry = name.split(‘/’) binding.pry
9 10 11 12 13 14 15 16 17 18 |
# File 'lib/generators/apsp/apsp_generator.rb', line 9 def create_apsp_file #name_path_arry = name.split('/') #create_file "config/initializers/apsp.rb", "# Add initialization content here" #name_path_arry.pop rescue [] #migration_template "migration.rb", "db/migrate/create_#{table_name}.rb" #migration_template "connection.rb", "db/migrate/create"+name_path_arry.join('_')+"_connections.rb" #invoke "active_record:model", [name, attributes.collect{|m| m.name.to_s+":"+m.type.to_s}], migration: false end |
#create_connection ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/generators/apsp/apsp_generator.rb', line 20 def create_connection name_path_arry = name.split('/') name_path_arry.pop rescue [] #invoke "active_record:model", ['connection', 'station_id:integer connect_id:integer'], migration: true # binding.pry conection_path = name_path_arry.length>0 ? name_path_arry.join('/')+'/connection' : 'connection' Rails::Generators.invoke("active_record:model", [conection_path, [name.split('/').last+':references', 'connect_id:integer']], migration: false, behavior: behavior) Rails::Generators.invoke("active_record:model", [name, attributes.collect{|m| m.name.to_s+":"+m.type.to_s}], migration: false, behavior: behavior) #begin model_file_path = "app/models/#{name.underscore}.rb" m = File.open(model_file_path, &:gets) n = " has_many :connections\n def add_connects(station_ids_array)\nstation_ids_array.each do |id|\n self.connections.build(:connect_id=>id)\nend\nself.save\n end\nend \n" aFile = File.new(model_file_path, "w") if aFile # binding.pry aFile.puts(m+n) else puts "Unable to open file!" end model_file_path = "app/models/#{name.underscore}.rb" m = File.open(model_file_path, &:gets) #rescue #end end |