From 9e821041ea307661d2e1d55d1a20c145e3bdde92 Mon Sep 17 00:00:00 2001 From: Esop Date: Tue, 11 Dec 2012 14:51:24 -0500 Subject: [PATCH 1/4] panda complete --- README.md | 4 ++-- config/database.yml.sample | 2 +- db/migrate/201205031230_create_shows.rb | 14 +++++++------- db/migrate/201205031300_create_networks.rb | 20 ++++++++++---------- db/seed.rb | 3 +++ db/setup.rb | 2 +- models/network.rb | 8 ++++---- models/show.rb | 10 +++++----- rakefile.rb | 2 +- watchman.rb | 10 +++++----- 10 files changed, 39 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index fce7a09..56c21fe 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,8 @@ Eagle (Advanced) Level 4. Create a table (using the migrations) which represents a hobby of yours: Fishing, Sports, Cooking, etc. 3. When I run ruby watchman.rb - * Fill the table with 5 records (Recipe.create) - * Have it show me all the records, with a nicely implemented to_s method + * Fill the table with 5 records (Recipe.create) + * Have it show me all the records, with a nicely implemented to_s method * Ask me (the user) what I want to show. Example, if you have Recipe with :name and :ingredients: ``` diff --git a/config/database.yml.sample b/config/database.yml.sample index 44360d7..c1e753d 100644 --- a/config/database.yml.sample +++ b/config/database.yml.sample @@ -1,6 +1,6 @@ host: 'localhost' adapter: 'postgresql' database: 'watchman' -username: XXXXXXX +username: 'russell' encoding: 'utf8' pool: 5 diff --git a/db/migrate/201205031230_create_shows.rb b/db/migrate/201205031230_create_shows.rb index 8197b57..27a7809 100644 --- a/db/migrate/201205031230_create_shows.rb +++ b/db/migrate/201205031230_create_shows.rb @@ -1,9 +1,9 @@ class CreateShows < ActiveRecord::Migration - def change - create_table :shows do |t| - t.string :name - t.string :day_of_week - t.integer :hour_of_day - end - end + def change + create_table :shows do |t| + t.string :name + t.string :day_of_week + t.integer :hour_of_day + end + end end diff --git a/db/migrate/201205031300_create_networks.rb b/db/migrate/201205031300_create_networks.rb index 2ef327c..0ab916f 100644 --- a/db/migrate/201205031300_create_networks.rb +++ b/db/migrate/201205031300_create_networks.rb @@ -1,13 +1,13 @@ class CreateNetworks < ActiveRecord::Migration - def change - create_table :networks do |t| - t.string :name - t.timestamps - end + def change + create_table :networks do |t| + t.string :name + t.timestamps + end - change_table :shows do |t| - t.references :network - t.timestamps - end - end + change_table :shows do |t| + t.references :network + t.timestamps + end + end end diff --git a/db/seed.rb b/db/seed.rb index 3c028ff..c1c8dbe 100644 --- a/db/seed.rb +++ b/db/seed.rb @@ -3,5 +3,8 @@ Show.delete_all amc = Network.create(name: "AMC") nbc = Network.create(name: "NBC") +nat = Network.create(name: "NAT") Show.create(name: "Mad Men", day_of_week: "Sunday", hour_of_day: 22, network: amc) Show.create(name: "Community", day_of_week: "Thursday", hour_of_day: 20, network: nbc) +Show.create(name: "The Dog Whisperer", day_of_week: "Friday", hour_of_day: 10, network: nat) +Show.create(name: "My Cat From Hell", day_of_week: "Saturday", hour_of_day: 10, network: nat) diff --git a/db/setup.rb b/db/setup.rb index 0e80690..73df786 100644 --- a/db/setup.rb +++ b/db/setup.rb @@ -7,7 +7,7 @@ # Setup out connection details ActiveRecord::Base.establish_connection(connection_details.merge({'database'=> 'postgres', 'schema_search_path'=> 'public'})) # create the 'tv' database -ActiveRecord::Base.connection.drop_database (connection_details.fetch('database')) rescue nil +ActiveRecord::Base.connection.drop_database(connection_details.fetch('database')) rescue nil ActiveRecord::Base.connection.create_database(connection_details.fetch('database')) rescue nil # connect to it ActiveRecord::Base.establish_connection(connection_details) diff --git a/models/network.rb b/models/network.rb index 00b697c..bb61f13 100644 --- a/models/network.rb +++ b/models/network.rb @@ -1,7 +1,7 @@ class Network < ActiveRecord::Base - has_many :shows + has_many :shows - def to_s - name - end + def to_s + name + end end diff --git a/models/show.rb b/models/show.rb index 6c82f65..c9c160a 100644 --- a/models/show.rb +++ b/models/show.rb @@ -1,9 +1,9 @@ class Show < ActiveRecord::Base - belongs_to :network + belongs_to :network - validates_presence_of :name + validates_presence_of :name - def to_s - "#{name} airs at #{hour_of_day}:#{day_of_week}:00 on #{network} " - end + def to_s + "#{name} airs at #{hour_of_day}:#{day_of_week}:00 on #{network} " + end end diff --git a/rakefile.rb b/rakefile.rb index 0bcfd0f..76f9b41 100644 --- a/rakefile.rb +++ b/rakefile.rb @@ -13,5 +13,5 @@ end task :say_hi do - puts "hi" + puts "hi" end diff --git a/watchman.rb b/watchman.rb index ebe9be4..87fe800 100644 --- a/watchman.rb +++ b/watchman.rb @@ -1,15 +1,15 @@ require 'rubygems' require 'bundler/setup' - require "./db/setup" + Dir.glob('./models/*').each { |r| require r} require "./db/seed" puts "There are #{Show.count} in the database" Network.all.each do |network| - puts "Shows airing on #{network}" - network.shows.each do |show| - puts show - end + puts "Shows airing on #{network}" + network.shows.each do |show| + puts show + end end From bf2fa9de81fc236a0a7fd6dfeae1766acec3defe Mon Sep 17 00:00:00 2001 From: Esop Date: Tue, 11 Dec 2012 15:02:47 -0500 Subject: [PATCH 2/4] forgot to merge --- README.md | 4 ++-- watchman.rb | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index fce7a09..56c21fe 100644 --- a/README.md +++ b/README.md @@ -26,8 +26,8 @@ Eagle (Advanced) Level 4. Create a table (using the migrations) which represents a hobby of yours: Fishing, Sports, Cooking, etc. 3. When I run ruby watchman.rb - * Fill the table with 5 records (Recipe.create) - * Have it show me all the records, with a nicely implemented to_s method + * Fill the table with 5 records (Recipe.create) + * Have it show me all the records, with a nicely implemented to_s method * Ask me (the user) what I want to show. Example, if you have Recipe with :name and :ingredients: ``` diff --git a/watchman.rb b/watchman.rb index ebe9be4..8a28569 100644 --- a/watchman.rb +++ b/watchman.rb @@ -8,8 +8,8 @@ puts "There are #{Show.count} in the database" Network.all.each do |network| - puts "Shows airing on #{network}" - network.shows.each do |show| - puts show - end + puts "Shows airing on #{network}" + network.shows.each do |show| + puts show + end end From 44854871ccca087846aafb101ed87406b49949ea Mon Sep 17 00:00:00 2001 From: Esop Date: Tue, 11 Dec 2012 16:56:10 -0500 Subject: [PATCH 3/4] finished tiger --- db/seed.rb | 3 +++ watchman.rb | 10 +++++----- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/db/seed.rb b/db/seed.rb index c1c8dbe..ccbd1eb 100644 --- a/db/seed.rb +++ b/db/seed.rb @@ -4,7 +4,10 @@ amc = Network.create(name: "AMC") nbc = Network.create(name: "NBC") nat = Network.create(name: "NAT") +sci = Network.create(name: "SCI") Show.create(name: "Mad Men", day_of_week: "Sunday", hour_of_day: 22, network: amc) Show.create(name: "Community", day_of_week: "Thursday", hour_of_day: 20, network: nbc) Show.create(name: "The Dog Whisperer", day_of_week: "Friday", hour_of_day: 10, network: nat) Show.create(name: "My Cat From Hell", day_of_week: "Saturday", hour_of_day: 10, network: nat) +Show.create(name: "Battlestar Galactica", day_of_week: "Saturday", hour_of_day: 11, network: sci) +Show.create(name: "Dr. Who", day_of_week: "Saturday", hour_of_day: 13, network: sci) diff --git a/watchman.rb b/watchman.rb index 87fe800..c2033ee 100644 --- a/watchman.rb +++ b/watchman.rb @@ -5,11 +5,11 @@ Dir.glob('./models/*').each { |r| require r} require "./db/seed" -puts "There are #{Show.count} in the database" +puts "What day do you want to watch tv?" +user_input = gets.chomp -Network.all.each do |network| - puts "Shows airing on #{network}" - network.shows.each do |show| - puts show +Show.all.each do |s| + if user_input.casecmp(s.day_of_week.to_s) == 0 + puts s end end From 04d14fcee99b8053769bd4067d4d49cc48194d30 Mon Sep 17 00:00:00 2001 From: leToaster Date: Wed, 12 Dec 2012 23:20:09 -0500 Subject: [PATCH 4/4] Eagle Tiger Panda --- db/migrate/201205031231_create_books.rb | 11 +++++++++++ db/seed.rb | 10 ++++++++++ models/book.rb | 7 +++++++ watchman.rb | 19 +++++++++++++++---- 4 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 db/migrate/201205031231_create_books.rb create mode 100644 models/book.rb diff --git a/db/migrate/201205031231_create_books.rb b/db/migrate/201205031231_create_books.rb new file mode 100644 index 0000000..99c8c13 --- /dev/null +++ b/db/migrate/201205031231_create_books.rb @@ -0,0 +1,11 @@ +class CreateBooks < ActiveRecord::Migration + + def change + create_table :books do |t| + t.string :author + t.string :title + t.integer :rating + end + end + +end diff --git a/db/seed.rb b/db/seed.rb index ccbd1eb..42291c5 100644 --- a/db/seed.rb +++ b/db/seed.rb @@ -11,3 +11,13 @@ Show.create(name: "My Cat From Hell", day_of_week: "Saturday", hour_of_day: 10, network: nat) Show.create(name: "Battlestar Galactica", day_of_week: "Saturday", hour_of_day: 11, network: sci) Show.create(name: "Dr. Who", day_of_week: "Saturday", hour_of_day: 13, network: sci) + +Book.create(author: "Fyodor Dostoyevsky",title: "The Brothers karamazov", rating: 10) +Book.create(author: "Fyodor Dostoyevsky",title: "Crime And Punishment", rating: 10) +Book.create(author: "Charles Bukowski",title: "Ham On Rye", rating: 10) +Book.create(author: "Alexander Solzhenitsyn",title: "Cancer Ward", rating: 7) +Book.create(author: "China Mieville", title: "The Scar", rating: 9) +Book.create(author: "Colson Whitehead", title: "John Henry Days", rating: 3) +Book.create(author: "Junot Diaz", title: "The Brief Wondrous Life of Oscar Wao", rating: 8) +Book.create(author: "Richard Wright", title: "The Outsider", rating: 8) +Book.create(author: "Richard Wright", title: "Native Son", rating: 9) diff --git a/models/book.rb b/models/book.rb new file mode 100644 index 0000000..7ff7079 --- /dev/null +++ b/models/book.rb @@ -0,0 +1,7 @@ +class Book < ActiveRecord::Base + + def to_s + "#{title} was written by #{author} and I gave it a rating of " + + "#{rating} out of 10 stars" + end +end diff --git a/watchman.rb b/watchman.rb index c2033ee..f5831d0 100644 --- a/watchman.rb +++ b/watchman.rb @@ -1,15 +1,26 @@ require 'rubygems' require 'bundler/setup' require "./db/setup" - Dir.glob('./models/*').each { |r| require r} require "./db/seed" puts "What day do you want to watch tv?" user_input = gets.chomp -Show.all.each do |s| - if user_input.casecmp(s.day_of_week.to_s) == 0 - puts s +Show.all.each do |show| + if user_input.casecmp(show.day_of_week) == 0 + puts show end end + +2.times { puts "\n" } + +puts "Here is a partial list of books I own" +puts Book.all +2.times { puts "\n" } + +puts "What author or book would you like to know more about?" +user_input = gets.chomp + +book = Book.where(author: user_input).all +puts book