Permalink
Browse files

Builds scaffold and secure sign-up. Need to add sign-in.

  • Loading branch information...
1 parent 7f5855b commit c33d89381eb296847ff5b3263d72a9dc7bd858f5 @raghubetina committed Jul 18, 2012
Showing with 69 additions and 7 deletions.
  1. +2 −0 .gitignore
  2. +4 −0 app
  3. +62 −7 association_builder.rb
  4. +1 −0 layout_modifier.rb
View
@@ -0,0 +1,2 @@
+.DS_Store
+temp1/*
View
4 app
@@ -3,6 +3,7 @@ require './model_generator.rb'
require './bootstrap_installer.rb'
require './user_modifier.rb'
require './layout_modifier.rb'
+require './association_builder.rb'
app_name = ARGV[0] || "MyApp"
layout_name = ARGV[1] || "fixed"
@@ -36,4 +37,7 @@ l.expand_main_div
l.fix_nav_bar_links
l.fix_misc
+a = AssociationBuilder.new(app_name)
+a.build_associations
+
Dir.chdir(app_name) { `rails s` }
View
@@ -1,6 +1,11 @@
+require 'active_support/inflector'
require 'csv'
class AssociationBuilder
+ def initialize(app_name = "MyApp")
+ @app_name = app_name
+ end
+
def build_associations
puts "Building associations."
CSV.foreach("models.csv") do |row|
@@ -11,14 +16,64 @@ def build_associations
ending = parsed.last
if ending == "id"
foreign_key = parsed.first
- puts foreign_key
+ puts "Foreign key #{foreign_key} detected in #{model}."
+
+ puts "Adding belongs_to to #{model}."
+ model_file = IO.readlines("#{@app_name}/app/models/#{model.downcase}.rb")
+ model_file.insert(1, " belongs_to :#{foreign_key}")
+
+ f = File.new("#{@app_name}/app/models/#{model.downcase}.rb", 'w')
+ model_file.each { |line| f.puts line }
+ f.close
+
+ #TODO: Clean this up a lot. All CSV parsing needs to be done with headers.
+ descriptor = ""
+ CSV.foreach("models.csv") do |test_model_row|
+ test_model = test_model_row.shift.downcase
+ if test_model == foreign_key
+ descriptor = test_model_row.first.split(':').first
+ end
+ end
+
+ puts "Using #{descriptor} as descriptor for #{foreign_key.capitalize}."
+
+ puts "Fixing #{model} show template."
+
+ #TODO: Reading and/or writing need to be refactored into method.
+ model_show = IO.readlines("#{@app_name}/app/views/#{model.pluralize.downcase}/show.html.erb")
+ foreign_key_label_pos = model_show.map(&:strip).index("<dd><%= @#{model.downcase}.#{foreign_key}_id %></dd>")
+ model_show[foreign_key_label_pos] = " <dd><%= @#{model.downcase}.#{foreign_key}.#{descriptor} %></dd>"
+ f = File.new("#{@app_name}/app/views/#{model.pluralize.downcase}/show.html.erb", 'w')
+ model_show.each { |line| f.puts line }
+ f.close
+
+ puts "Fixing #{model} index template."
+
+ model_index = IO.readlines("#{@app_name}/app/views/#{model.pluralize.downcase}/index.html.erb")
+ foreign_key_td_pos = model_index.map(&:strip).index("<td><%= #{model.downcase}.#{foreign_key}_id %></td>")
+ model_index[foreign_key_td_pos] = " <td><%= #{model.downcase}.#{foreign_key}.#{descriptor} %></td>"
+ f = File.new("#{@app_name}/app/views/#{model.pluralize.downcase}/index.html.erb", 'w')
+ model_index.each { |line| f.puts line }
+ f.close
+
+ puts "Fixing #{model} form."
+
+ model_form = IO.readlines("#{@app_name}/app/views/#{model.pluralize.downcase}/_form.html.erb")
+ foreign_key_field_pos = model_form.map(&:strip).index("<%= f.number_field :#{foreign_key}_id, :class => 'number_field' %>")
+ model_form[foreign_key_field_pos] = " <%= f.collection_select :#{foreign_key}_id, #{foreign_key.capitalize}.all, :id, :#{descriptor} %>"
+ f = File.new("#{@app_name}/app/views/#{model.pluralize.downcase}/_form.html.erb", 'w')
+ model_form.each { |line| f.puts line }
+ f.close
+
+ puts "Adding has_many to #{foreign_key.capitalize}."
+ model_file = IO.readlines("#{@app_name}/app/models/#{foreign_key}.rb")
+ model_file.insert(1, " has_many :#{model.pluralize.downcase}")
+
+ f = File.new("#{@app_name}/app/models/#{foreign_key}.rb", 'w')
+ model_file.each { |line| f.puts line }
+ f.close
end
end
end
end
-
-
-end
-
-a = AssociationBuilder.new
-a.build_associations
+end
View
@@ -5,6 +5,7 @@ class LayoutModifier
def initialize(app_name = "MyApp")
@app_name = app_name
end
+
def remove_sidebar
puts "Removing sidebar."

0 comments on commit c33d893

Please sign in to comment.