Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Eagle Tiger Panda #16

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

```
Expand Down
2 changes: 1 addition & 1 deletion config/database.yml.sample
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
host: 'localhost'
adapter: 'postgresql'
database: 'watchman'
username: XXXXXXX
username: 'russell'
encoding: 'utf8'
pool: 5
14 changes: 7 additions & 7 deletions db/migrate/201205031230_create_shows.rb
Original file line number Diff line number Diff line change
@@ -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
11 changes: 11 additions & 0 deletions db/migrate/201205031231_create_books.rb
Original file line number Diff line number Diff line change
@@ -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
20 changes: 10 additions & 10 deletions db/migrate/201205031300_create_networks.rb
Original file line number Diff line number Diff line change
@@ -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
16 changes: 16 additions & 0 deletions db/seed.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,21 @@
Show.delete_all
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)

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)
2 changes: 1 addition & 1 deletion db/setup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
7 changes: 7 additions & 0 deletions models/book.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions models/network.rb
Original file line number Diff line number Diff line change
@@ -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
10 changes: 5 additions & 5 deletions models/show.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion rakefile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
end

task :say_hi do
puts "hi"
puts "hi"
end
25 changes: 18 additions & 7 deletions watchman.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
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"
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
end
Show.all.each do |show|
if user_input.casecmp(show.day_of_week) == 0
puts show
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather you used ActiveRecord to limit the number of shows returned.

Show.where(day_of_week: user_input).each do |show|
  puts show
end

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