Permalink
Browse files

Added seeds, refactored seeking into search_and_destroy method, finis…

…hed new versions of fix_index, fix_show, fix_form.
  • Loading branch information...
1 parent 31ca61a commit 2e96dc1e41a57dcd65803b52c3ffde5ea55cda89 @raghubetina committed Jul 23, 2012
Showing with 3,640 additions and 83 deletions.
  1. +8 −2 app
  2. +51 −77 association_builder.rb
  3. +4 −0 modeler/rails_model.rb
  4. +6 −4 models.csv
  5. +69 −0 seeds.rb
  6. +15 −0 test1/.gitignore
  7. +49 −0 test1/Gemfile
  8. +130 −0 test1/Gemfile.lock
  9. +261 −0 test1/README.rdoc
  10. +7 −0 test1/Rakefile
  11. BIN test1/app/assets/images/rails.png
  12. +3 −0 test1/app/assets/javascripts/actors.js.coffee
  13. +16 −0 test1/app/assets/javascripts/application.js
  14. +4 −0 test1/app/assets/javascripts/bootstrap.js.coffee
  15. +3 −0 test1/app/assets/javascripts/characters.js.coffee
  16. +3 −0 test1/app/assets/javascripts/directors.js.coffee
  17. +3 −0 test1/app/assets/javascripts/favorites.js.coffee
  18. +3 −0 test1/app/assets/javascripts/movies.js.coffee
  19. +3 −0 test1/app/assets/javascripts/users.js.coffee
  20. +13 −0 test1/app/assets/stylesheets/application.css
  21. +33 −0 test1/app/assets/stylesheets/bootstrap_and_overrides.css.less
  22. +83 −0 test1/app/controllers/actors_controller.rb
  23. +3 −0 test1/app/controllers/application_controller.rb
  24. +83 −0 test1/app/controllers/characters_controller.rb
  25. +83 −0 test1/app/controllers/directors_controller.rb
  26. +83 −0 test1/app/controllers/favorites_controller.rb
  27. +83 −0 test1/app/controllers/movies_controller.rb
  28. +83 −0 test1/app/controllers/users_controller.rb
  29. +2 −0 test1/app/helpers/actors_helper.rb
  30. +2 −0 test1/app/helpers/application_helper.rb
  31. +2 −0 test1/app/helpers/characters_helper.rb
  32. +2 −0 test1/app/helpers/directors_helper.rb
  33. +2 −0 test1/app/helpers/favorites_helper.rb
  34. +2 −0 test1/app/helpers/movies_helper.rb
  35. +2 −0 test1/app/helpers/users_helper.rb
  36. 0 test1/app/mailers/.gitkeep
  37. 0 test1/app/models/.gitkeep
  38. +7 −0 test1/app/models/actor.rb
  39. +8 −0 test1/app/models/character.rb
  40. +7 −0 test1/app/models/director.rb
  41. +8 −0 test1/app/models/favorite.rb
  42. +8 −0 test1/app/models/movie.rb
  43. +9 −0 test1/app/models/user.rb
  44. +20 −0 test1/app/views/actors/_form.html.erb
  45. +6 −0 test1/app/views/actors/edit.html.erb
  46. +75 −0 test1/app/views/actors/index.html.erb
  47. +6 −0 test1/app/views/actors/new.html.erb
  48. +89 −0 test1/app/views/actors/show.html.erb
  49. +26 −0 test1/app/views/characters/_form.html.erb
  50. +6 −0 test1/app/views/characters/edit.html.erb
  51. +79 −0 test1/app/views/characters/index.html.erb
  52. +6 −0 test1/app/views/characters/new.html.erb
  53. +97 −0 test1/app/views/characters/show.html.erb
  54. +20 −0 test1/app/views/directors/_form.html.erb
  55. +6 −0 test1/app/views/directors/edit.html.erb
  56. +75 −0 test1/app/views/directors/index.html.erb
  57. +6 −0 test1/app/views/directors/new.html.erb
  58. +89 −0 test1/app/views/directors/show.html.erb
  59. +20 −0 test1/app/views/favorites/_form.html.erb
  60. +6 −0 test1/app/views/favorites/edit.html.erb
  61. +75 −0 test1/app/views/favorites/index.html.erb
  62. +6 −0 test1/app/views/favorites/new.html.erb
  63. +89 −0 test1/app/views/favorites/show.html.erb
  64. +67 −0 test1/app/views/layouts/application.html.erb
  65. +26 −0 test1/app/views/movies/_form.html.erb
  66. +6 −0 test1/app/views/movies/edit.html.erb
  67. +79 −0 test1/app/views/movies/index.html.erb
  68. +6 −0 test1/app/views/movies/new.html.erb
  69. +97 −0 test1/app/views/movies/show.html.erb
  70. +32 −0 test1/app/views/users/_form.html.erb
  71. +6 −0 test1/app/views/users/edit.html.erb
  72. +75 −0 test1/app/views/users/index.html.erb
  73. +6 −0 test1/app/views/users/new.html.erb
  74. +89 −0 test1/app/views/users/show.html.erb
  75. +4 −0 test1/config.ru
  76. +62 −0 test1/config/application.rb
  77. +6 −0 test1/config/boot.rb
  78. +25 −0 test1/config/database.yml
  79. +5 −0 test1/config/environment.rb
  80. +37 −0 test1/config/environments/development.rb
  81. +67 −0 test1/config/environments/production.rb
  82. +37 −0 test1/config/environments/test.rb
  83. +7 −0 test1/config/initializers/backtrace_silencers.rb
  84. +15 −0 test1/config/initializers/inflections.rb
  85. +5 −0 test1/config/initializers/mime_types.rb
  86. +7 −0 test1/config/initializers/secret_token.rb
  87. +8 −0 test1/config/initializers/session_store.rb
  88. +14 −0 test1/config/initializers/wrap_parameters.rb
  89. +5 −0 test1/config/locales/en.yml
  90. +71 −0 test1/config/routes.rb
  91. +10 −0 test1/db/migrate/20120723232047_create_directors.rb
  92. +11 −0 test1/db/migrate/20120723232049_create_movies.rb
  93. +11 −0 test1/db/migrate/20120723232052_create_characters.rb
  94. +10 −0 test1/db/migrate/20120723232054_create_actors.rb
  95. +11 −0 test1/db/migrate/20120723232057_create_users.rb
  96. +10 −0 test1/db/migrate/20120723232059_create_favorites.rb
  97. +61 −0 test1/db/schema.rb
  98. +69 −0 test1/db/seeds.rb
  99. +2 −0 test1/doc/README_FOR_APP
  100. 0 test1/lib/assets/.gitkeep
  101. 0 test1/lib/tasks/.gitkeep
  102. 0 test1/log/.gitkeep
  103. +26 −0 test1/public/404.html
  104. +26 −0 test1/public/422.html
  105. +25 −0 test1/public/500.html
  106. 0 test1/public/favicon.ico
  107. +5 −0 test1/public/robots.txt
  108. +6 −0 test1/script/rails
  109. 0 test1/test/fixtures/.gitkeep
  110. +9 −0 test1/test/fixtures/actors.yml
  111. +11 −0 test1/test/fixtures/characters.yml
  112. +9 −0 test1/test/fixtures/directors.yml
  113. +9 −0 test1/test/fixtures/favorites.yml
  114. +11 −0 test1/test/fixtures/movies.yml
  115. +11 −0 test1/test/fixtures/users.yml
  116. 0 test1/test/functional/.gitkeep
  117. +49 −0 test1/test/functional/actors_controller_test.rb
  118. +49 −0 test1/test/functional/characters_controller_test.rb
  119. +49 −0 test1/test/functional/directors_controller_test.rb
  120. +49 −0 test1/test/functional/favorites_controller_test.rb
  121. +49 −0 test1/test/functional/movies_controller_test.rb
  122. +49 −0 test1/test/functional/users_controller_test.rb
  123. 0 test1/test/integration/.gitkeep
  124. +12 −0 test1/test/performance/browsing_test.rb
  125. +13 −0 test1/test/test_helper.rb
  126. 0 test1/test/unit/.gitkeep
  127. +7 −0 test1/test/unit/actor_test.rb
  128. +7 −0 test1/test/unit/character_test.rb
  129. +7 −0 test1/test/unit/director_test.rb
  130. +7 −0 test1/test/unit/favorite_test.rb
  131. +4 −0 test1/test/unit/helpers/actors_helper_test.rb
  132. +4 −0 test1/test/unit/helpers/characters_helper_test.rb
  133. +4 −0 test1/test/unit/helpers/directors_helper_test.rb
  134. +4 −0 test1/test/unit/helpers/favorites_helper_test.rb
  135. +4 −0 test1/test/unit/helpers/movies_helper_test.rb
  136. +4 −0 test1/test/unit/helpers/users_helper_test.rb
  137. +7 −0 test1/test/unit/movie_test.rb
  138. +7 −0 test1/test/unit/user_test.rb
  139. 0 test1/vendor/assets/javascripts/.gitkeep
  140. 0 test1/vendor/assets/stylesheets/.gitkeep
  141. 0 test1/vendor/plugins/.gitkeep
View
10 app
@@ -42,5 +42,11 @@ l.fix_misc
a = AssociationBuilder.new(app_name)
a.build_associations
a.fix_index
-# a.fix_show
-# Dir.chdir(app_name) { `rails s` }
+a.fix_show
+a.fix_form
+
+`cp seeds.rb #{app_name}/db/`
+Dir.chdir(app_name) { `rake db:seed` }
+Dir.chdir(app_name) { `rails s` }
+
+
View
@@ -25,9 +25,15 @@ def modify_file(path)
puts lines if debug_mode?
end
- def association_line(model, relationship)
+ def association_lines(model, relationship)
+ associations = []
symbols = model.send(relationship)
- " #{relationship} #{symbols.map { |x| ":#{x}" }.join(', ')}" if symbols.any?
+ if symbols.any?
+ symbols.each do |symbol|
+ associations << " #{relationship} :#{symbol}"
+ end
+ end
+ return associations
end
def build_associations
@@ -36,94 +42,62 @@ def build_associations
models.each do |model|
modify_file model.class_path(@app_name) do |model_file|
[:belongs_to, :has_many].each do |assoc|
- model_file.insert(-2, association_line(model, assoc))
+ model_file.insert(-2, association_lines(model, assoc)).flatten
end
end
end
end
+ def search_and_destroy(lines, target, replacement)
+ pos = lines.index { |line| line.index(target) }
+ lines[pos].sub!(target, replacement) if pos
+ end
+
def fix_index
puts "Fixing associated index templates."
models.each do |model|
modify_file model.index_path(@app_name) do |model_index|
model.belongs_to.each do |bt|
descriptor = models[bt].columns.first.name
- bt_pos = model_index.map(&:strip).index("<td><%= #{model.name.downcase}.#{bt}_id %></td>")
- model_index[bt_pos] = " <td><%= #{model.name.downcase}.#{bt}.#{descriptor} if #{model.name.downcase}.#{bt} %></td>"
+
+ target = "#{model.name.downcase}.#{bt}_id"
+ replacement = "link_to(#{model.name.downcase}.#{bt}.#{descriptor}, #{model.name.downcase}.#{bt}) if #{model.name.downcase}.#{bt}"
+ search_and_destroy(model_index, target, replacement)
end
end
end
end
- #
- # CSV.foreach("models.csv") do |row|
- # model = row.shift
- #
- # row.compact.each do |column|
- # parsed = column.split(':').first.split('_')
- # ending = parsed.last
- # if ending == "id"
- # foreign_key = parsed.first
- # 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} ? @#{model.downcase}.#{foreign_key}.#{descriptor} : nil %></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} ? #{model.downcase}.#{foreign_key}.#{descriptor} : nil %></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
-
-# AssociationBuilder.new("hockey").build_associations
+
+ def fix_show
+ puts "Fixing associated show templates."
+
+ models.each do |model|
+ modify_file model.show_path(@app_name) do |model_show|
+ model.belongs_to.each do |bt|
+ descriptor = models[bt].columns.first.name
+
+ target = "@#{model.name.downcase}.#{bt}_id"
+ replacement = "link_to(@#{model.name.downcase}.#{bt}.#{descriptor}, @#{model.name.downcase}.#{bt}) if @#{model.name.downcase}.#{bt}"
+ search_and_destroy(model_show, target, replacement)
+ end
+ end
+ end
+ end
+
+ def fix_form
+ puts "Fixing associated form templates."
+
+ models.each do |model|
+ modify_file model.form_path(@app_name) do |model_form|
+ model.belongs_to.each do |bt|
+ descriptor = models[bt].columns.first.name
+
+ target = "<%= f.number_field :#{bt}_id, :class => 'number_field' %>"
+ replacement = "<%= f.collection_select :#{bt}_id, #{bt.capitalize}.all, :id, :#{descriptor} %>"
+ search_and_destroy(model_form, target, replacement)
+ end
+ end
+ end
+ end
+end
View
@@ -27,6 +27,10 @@ def show_path(app_root)
@show_path ||= File.join(app_root, "app", "views", name.pluralize.underscore, "show.html.erb")
end
+ def form_path(app_root)
+ @show_path ||= File.join(app_root, "app", "views", name.pluralize.underscore, "_form.html.erb")
+ end
+
def initialize(name, columns)
@name = name.strip.classify
@columns = columns.compact.map { |c| RailsColumn.new(c) }
View
@@ -1,4 +1,6 @@
-User,email,password_digest,first_name,last_name
-Lesson,name,embed_code,user_id:integer,
-Question,content:text,lesson_id:integer,,
-Vote,question_id:integer,user_id:integer,,
+Director,name,dob:date,
+Movie,title,year,director_id:integer
+Character,name,movie_id:integer,actor_id:integer
+Actor,name,dob:date,
+User,name,email,password_digest
+Favorite,user_id:integer,movie_id:integer,
View
@@ -0,0 +1,69 @@
+# This file should contain all the record creation needed to seed the database with its default values.
+# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup).
+#
+# Examples:
+#
+# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }])
+# Mayor.create(name: 'Emanuel', city: cities.first)
+
+Director.destroy_all
+Movie.destroy_all
+Actor.destroy_all
+Character.destroy_all
+
+directors = [
+ { name: "Francis Ford Coppola", dob: "04/07/1939" },
+ { name: "Christopher Nolan", dob: "07/30/1970" },
+ { name: "Frank Darabont", dob: "01/29/1959" }
+ ]
+
+movies = [
+ { title: "The Godfather", year: "1972", director: "Francis Ford Coppola" },
+ { title: "The Godfather: Part II", year: "1974", director: "Francis Ford Coppola" },
+ { title: "The Shawshank Redemption", year: "1994", director: "Frank Darabont" },
+ { title: "The Dark Knight", year: "1972", director: "Christopher Nolan" }
+ ]
+
+characters = [
+ { name: "Bruce Wayne", movie: "The Dark Knight", actor: "Christian Bale"},
+ { name: "Michael Corleone", movie: "The Godfather", actor: "Al Pacino" },
+ { name: "Andy Dufresne", movie: "The Shawshank Redemption", actor: "Tim Robbins"},
+ { name: "Red Redding", movie: "The Shawshank Redemption", actor: "Morgan Freeman"}
+ ]
+
+actors = [
+ { name: "Christian Bale", dob: "01/30/1974" },
+ { name: "Al Pacino", dob: "04/25/1940" },
+ { name: "Tim Robbins", dob: "10/16/1958" },
+ { name: "Morgan Freeman", dob: "06/01/1937" }
+ ]
+
+directors.each do |director|
+ d = Director.new
+ d.name = director[:name]
+ d.dob = Date.strptime director[:dob], '%m/%d/%Y'
+ d.save
+end
+
+movies.each do |movie|
+ m = Movie.new
+ m.title = movie[:title]
+ m.year = movie[:year]
+ m.director = Director.find_by_name(movie[:director])
+ m.save
+end
+
+actors.each do |actor|
+ a = Actor.new
+ a.name = actor[:name]
+ a.dob = Date.strptime actor[:dob], '%m/%d/%Y'
+ a.save
+end
+
+characters.each do |character|
+ c = Character.new
+ c.name = character[:name]
+ c.movie = Movie.find_by_title(character[:movie])
+ c.actor = Actor.find_by_name(character[:actor])
+ c.save
+end
View
@@ -0,0 +1,15 @@
+# See http://help.github.com/ignore-files/ for more about ignoring files.
+#
+# If you find yourself ignoring temporary files generated by your text editor
+# or operating system, you probably want to add a global ignore instead:
+# git config --global core.excludesfile ~/.gitignore_global
+
+# Ignore bundler config
+/.bundle
+
+# Ignore the default SQLite database.
+/db/*.sqlite3
+
+# Ignore all logfiles and tempfiles.
+/log/*.log
+/tmp
View
@@ -0,0 +1,49 @@
+source 'https://rubygems.org'
+
+gem 'twitter-bootstrap-rails', '2.1.0'
+
+group :development do
+ gem 'sqlite3'
+end
+
+group :production do
+ gem 'pg'
+end
+
+gem 'bcrypt-ruby', '~> 3.0.0'
+
+gem 'rails', '3.2.6'
+
+# Bundle edge Rails instead:
+# gem 'rails', :git => 'git://github.com/rails/rails.git'
+
+
+
+# Gems used only for assets and not required
+# in production environments by default.
+group :assets do
+ gem 'sass-rails', '~> 3.2.3'
+ gem 'coffee-rails', '~> 3.2.1'
+
+ # See https://github.com/sstephenson/execjs#readme for more supported runtimes
+ # gem 'therubyracer', :platforms => :ruby
+
+ gem 'uglifier', '>= 1.0.3'
+end
+
+gem 'jquery-rails'
+
+# To use ActiveModel has_secure_password
+# gem 'bcrypt-ruby', '~> 3.0.0'
+
+# To use Jbuilder templates for JSON
+# gem 'jbuilder'
+
+# Use unicorn as the app server
+# gem 'unicorn'
+
+# Deploy with Capistrano
+# gem 'capistrano'
+
+# To use debugger
+# gem 'debugger'
Oops, something went wrong.

0 comments on commit 2e96dc1

Please sign in to comment.