Skip to content

Instantly share code, notes, and snippets.

@trivett
Created December 22, 2015 02:23
Show Gist options
  • Save trivett/1b4d236be446b8d507f4 to your computer and use it in GitHub Desktop.
Save trivett/1b4d236be446b8d507f4 to your computer and use it in GitHub Desktop.
second midterm for BEWD

#Happitails on Rails

Your client, the Happitails shelter, is unsatisfied with the 80s command line program we made in November. They got some seriously pretentious hipster volunteer that demands only the best when it comes to web apps. They are all like, if this doesn't make Product Hunt you're fired. They have a grand vision that ALL shelters and ALL animal lovers will connect via this app, so it will have to support multiple shelters. However, they don't seem to give a hoot about the front end design.

##Your Assignment:

Work in groups of no fewer than two or more than four, but make sure that you have your own code on your laptop.

DO NOT PUT THIS IN THE CLASS FOLDER.

Please create a separate repo on Github for it, and work on it outside of the class folder. This will be practice for the final project.

Utilize Stack Overflow, Rails Guides, Ruby Docs, your partner, etc to get over roadblocks before you ask Vincent or Eddie for help.

###Step 1

Plan out your models and database according to this spec. Talk it through before you write the code.

####Shelter

  • name
  • has clients
  • has animals

####Client

  • name
  • age
  • photo (url)
  • has animals

####Animal

  • name
  • species
  • photo (url)

Feel free if you wish to put extra attributes such as address, phone number, etc.

###Step 2:

You can easily lose like an hour seeding your data looking at cute puppies on the internet, but I did that for you.

Copy in these seeds: https://gist.github.com/trivett/54dceb7bf100c2f0d82f

Add yourself as a client and if you have a pet, add yourself and your pet!

###Step 3:

Set up your routes. This could be tricky!! Try nesting them one way or another and executing rake routes

This is one part where the design of the app can go multiple ways and most programmers will disagree on the design of your endpoints. Even for a relatively simple project like this.

You will need to at least:

  • Show all shelters
  • Show all animals in a given shelter
  • show all clients in a shelter
  • show all animals belonging to a client
  • A show page for all animals, regardless of owner
  • A show page for clients which includes his/her animals are listed

You can do this with resourceful routes.

Some controller actions will have to be reusable for different contexts, and you won't need all of them necessarily.

You might have to revisit this later if it doesn't work out.

###Step 4

Build out your controllers and views for the endpoints listed above, plus any other ones you think make sense.

###Step 5

Time to create new records. These forms will create new things, not change the status of existing ones.

Add a view and controller action to:

  • Add new Clients to a shelter
  • Add new Animals to a shelter
  • Create a new shelter

###Step 6

This will require a bit of thought and research. This isn't a wax-on wax-off sort of exercise. It's a problem to solve with code. With a bit of googling and smarts, you can do this. You have never done something quite like this before, but I think that you are ready for this challenge. Finding a unique problem and solving it is both the fun part of programming and the most difficult part.

On the animal show page, place an if else statement in ERB that shows a link to adopt the animal if he or she is in a shelter, or donate the animal to the shelter if the animal is owned by a client.

The form should be just one field: a dropdown of potential recipients. So when adopting an animal, it should be a dropdown of all of the clients in that shelter. When putting it up for adoption, it should show a dropdown of all shelters.

For information about making drop down selectors, look up collection_select

Another action should fire after you click the submit button. This action should perform the database-side changes to the animals associations, out of the shelter's animals list and into the client's animals list, or vice versa.

You might find section 4.3 here: http://guides.rubyonrails.org/association_basics.html quite helpful when it comes to getting the animal in and out of associations with the other two models.

Finally, redirect to the animal show page with a message indicating that it worked (or didn't)

###Extra credit

Happy pets need to play! Make a form that lets you give a new toy to an animal. Yep, a new model, controller, couple of routes and a migration so animals know that they have toys.

So animals can have many toys. List the animal's toys on the animal show page.

When we say that an animal owns toys, we are also saying that the animal's caretaker owns those toys too. On the client show page, list all of the toys owned by all the animals in the client's home. Yep, that's a has many through association.

###More extra credit

Snazz up the shelter index page to show an image of a random animal in the shelter.

####Moar extra credit

Let's say that shelters would like to show potential clients the lovely animals they had found homes for in the past. There is more than one way to show the relation between the shelter and the animals that are owed by clients. Give it a go if you have time!

###Super extra credit that makes you an invincible level 99 wolf mage

Let's say you go into the rails console and create new animals but don't add them to a shelter or client.

Create a custom route to show all the poor orphan animals. Include a link to bring them to a page where you can choose the shelter that will adopt the little critter.

You do have a view for that already...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment