Skip to content

Panda,tiger and eagle #28

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

Open
wants to merge 4 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
10 changes: 10 additions & 0 deletions db/migrate/201301020814_create_recipes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateRecipes < ActiveRecord::Migration
def change
create_table :recipes do |t|
t.string :name
t.text :ingredients
t.text :steps
t.timestamps
end
end
end
8 changes: 8 additions & 0 deletions db/seed.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
# Cleaning Out
Network.delete_all
Show.delete_all
Recipe.delete_all

amc = Network.create(name: "AMC")
nbc = Network.create(name: "NBC")
showtime = Network.create(name: "Showtime")
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: "Hannibal", day_of_week: "Monday", hour_of_day: 20, network: nbc)
Show.create(name: "Homeland", day_of_week: "Thursday", hour_of_day: 20, network: showtime)

Recipe.create(name: "Hummus", ingredients: "chickpeas,tahini,olive oil,salt,lemon juice,garlic,cumin,water", steps: "Blend tahini and lemon juice.Add and blend olive oil, garlic, cumin and salt.Add and blend chickpeas.Add and blend water to desired consistency.")
Recipe.create(name: "Biscuits", ingredients: "flour,baking powder,salt,butter,milk", steps: "Mix flour, salt, and baking soda.Add butter and mix till butter is pea sized.Add milk and mix lightly.Flip onto a floured surface and pat down to desired thickness.Cut biscuits out of dough, reforming dough when necessary.Bake at 400 degrees for 12 minutes.")
14 changes: 14 additions & 0 deletions models/recipe.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Recipe < ActiveRecord::Base
validates_presence_of :name, :ingredients, :steps

def pretty_steps
step_out = ""
steps.split(".").each_with_index { |step, step_num| step_out += "#{step_num+1}: #{step}\n"}
return step_out
end


def to_s
"#{name}\nIngredients: #{ingredients.split(",")}\nSteps:\n#{pretty_steps}"
end
end
2 changes: 2 additions & 0 deletions models/show.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,6 @@ class Show < ActiveRecord::Base
def to_s
"#{name} airs at #{hour_of_day}:#{day_of_week}:00 on #{network} "
end


end
12 changes: 12 additions & 0 deletions spec/show_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
require './db/setup'


describe '#search_by_day' do
it "should return Thursday shows" do
STDIN.stub(:gets).and_return("")
Copy link
Member

Choose a reason for hiding this comment

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

Nicely done!

Though it sort of shows we should be using an abstraction for STDIN, like highline or similar

require_relative '../watchman'
shows = search_by_day('Thursday').map { |show| show.name }
expect(shows).to eq(['Community','Homeland'])
end
end

28 changes: 23 additions & 5 deletions watchman.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,33 @@
require 'rubygems'
require 'bundler/setup'
# require 'rubygems'
# require 'bundler/setup'

require "./db/setup"
Dir.glob('./models/*').each { |r| require r}
require "./db/seed"

def search_by_day(day)
Show.where(day_of_week: day)
end

puts "There are #{Show.count} in the database"

puts "What day of the week to search?"
day = STDIN.gets.chomp.capitalize
results = search_by_day(day)



Network.all.each do |network|
puts "Shows airing on #{network}"
network.shows.each do |show|
puts show
end
results.each do |show|
puts show if show.network == network
Copy link
Member

Choose a reason for hiding this comment

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

This is perfectly fine --- though I prefer limiting my results and then acting upon then.

results.select{|s| s.network == network}.each do 
  puts show
end

Copy link
Member

Choose a reason for hiding this comment

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

OK, also --- ActiveRecord provides some methods for free... Since this exists:

class Network < ActiveRecord::Base
  has_many :shows
  #..snip
end

We could do:

Network.all.each do |network|
  puts "Shows airing on #{network}"
  network.shows.each do |show|
    puts show
  end
end

Copy link
Author

Choose a reason for hiding this comment

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

I don't understand what you mean by your second comment, the above outputs all the shows. Wasn't the goal to only output the search results?

Copy link
Member

Choose a reason for hiding this comment

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

@noahpatterson --- yes, that was the requirement, and you did accomplish that.

My point -- in network.shows.each you do not have to load shows, and KNOW how to relate shows to network, it can be done for you, for free.

end
end

puts "Here are all the recipes"
Recipe.all.each { |recipe| puts recipe }

puts "What recipe would you like to learn more about?"
recipe_search = STDIN.gets.chomp.capitalize
recipe_result = Recipe.where(name: recipe_search)
recipe_result.empty? ? puts("Sorry we cannot find that recipe") : recipe_result.each { |recipe| puts recipe }