Skip to content

Instantly share code, notes, and snippets.

@jimmypocock
Last active March 12, 2018 17:50
Show Gist options
  • Save jimmypocock/580fd8ade0a30c34944f26b39a9b9492 to your computer and use it in GitHub Desktop.
Save jimmypocock/580fd8ade0a30c34944f26b39a9b9492 to your computer and use it in GitHub Desktop.
The purpose of this exercise is to generate discussion of application design patterns as they apply to Ruby on Rails.
#-------------------
# Description
#-------------------
# This exercise is broken into 5 Rails-based tasks. Each task utilizes the
# type of files you would operate within an average Rails application.
# Keep in mind, if one file asks for something that requires an alteration in a file
# from a different task, please complete these tasks as if you were in a production
# application. Alter as necessary.
# We want to gain insight into how you write code. You are welcome to clone this file
# and work within it. Please follow standard Ruby on Rails conventions and Ruby coding
# practices.
# If you have questions throughout the exercise, please write them as comments so they
# can be discussed during the development team interview.
#-------------------
# Expectations
#-------------------
# - Build the simplest possible solution, as it is often the most maintainable.
# - Make sure you successfully complete the tasks we ask. Don't overcomplicate things.
# - Write clean code, follow Ruby on Rails conventions.
# - When you are finished, send us a Gist link with your solution. You can create a file
# with all the code in it, create separate files, or code directly in a clone of this file.
# We just need it publicly available to view.
#-------------------
# Technology Stack
#-------------------
# Ruby 2.4
# Rails 5.1
# Postgresql
# HAML, SCSS, Twitter Bootstrap
#-------------------
# 1. Basic Migrations
#-------------------
# We need to generate two models: City and Campground. We know the fields we need,
# and we know the necessary validations on each field, but we're not sure
# exactly how to set it up. Complete the migrations below.
# Campground
#
# :name
# This attribute is required and every :name in the database should be different
# from any other.
#
# :cost_per_night
# This attribute is required. If no value is assigned to :cost_per_night upon
# creation, then it should automatically be set to 0. It should never have more than
# 2 decimal places and the maximum amount of digits in the field should be 12
#
# :public_park
# This will determine whether or not the Campground object represents a publicly-
# owned park (for example, a national or state park). The ONLY values allowed for this attribute will be `true` or `false`.
# If no value is provided upon creation, it should automatically be set to `false`.
#
# Association with City model
# This field should represent a "link" with another model in the database: City.
# Keep in mind that on every view where there is a Campground, its associated
# City will also need to be displayed. Also, every Campground object MUST have
# an associated City object. Remember to consider best practices for performance
# and data integrity checks
# City
#
# :name
# The :name attribute can NOT be blank. However, unlike the Campground model, there may
# be two or more City objects with the same name.
# db/migrate/00000000000000_create_campgrounds.rb
class CreateCampgrounds < ActiveRecord::Migration[5.1]
def change
end
end
# db/migrate/00000000000001_create_cities.rb
class CreateCities < ActiveRecord::Migration[5.1]
def change
end
end
#-------------------
# 2. Models
#-------------------
# Now that we've created the table, we should build the corresponding Campground and City models.
# We know that it's associated with a city in some way and should have access to the :name
# attribute of a City. That can get confusing since both a Campground and City have a :name
# attribute, so we'll need to make sure we always retrieve the correct :name on the Campground
# object.
# We should probably make sure the validations we added to the attributes in the migrations
# are also on the models. The :name attribute should always be populated for both the Campground
# and the City. The Campground must also always have a value for the :cost_per_night and
# :public_park attributes, and the associated City object. Also on the Campground, each name
# must be different from any other Campground object.
# One more thing: For any City object that gets deleted, it should replicate the deletion to
# its associated objects.
# app/models/campground.rb
class Campground < ApplicationRecord
end
# app/models/city.rb
class City < ApplicationRecord
end
#-------------------
# 3. Controller
#-------------------
# We have a few constraints for which campgrounds we'd like to show:
# - We want to list our campgrounds alphabetically by name.
# - Only campgrounds whose City name is "Austin"
# - We also don't want to display more than 20 campgrounds.
# - Only show the Campgrounds whose price >= $20 but < $100.
# - Non-public parks only.
# The last thing we'll likely need to take into account is that for
# every campground we list, we will also have to display the
# name of its associated City object.
# Hint: You can update your model for this step. Follow best practices.
# app/controllers/campgrounds_controller.rb
require_dependency 'application_controller'
class CampgroundsController < ApplicationController
def index; end
end
#-------------------
# 4. Views
#-------------------
# We'd like to list all the campgrounds we retrieved in our controller. We are going
# to use Haml instead of ERB as our markup language and SCSS for custom styling (if any,
# because we expect you to use as much Twitter Bootstrap as possible).
# We'll be using the Bootstrap framework to style the page, and while we don't know exactly
# how to write the code, we know for any screen less than 768px, there should only be
# 1 campground per row. Above that and below 992px, we should only have 2 campgrounds per
# row. Beyond that but below 1200px, we should have 3 campgrounds per row. And for any other
# screen width, there should be 4 campgrounds per row.
# For each campground, we want the name to link to the campground's individual page. Maybe
# Rails has a helper for that. We'd like the associated City name to be in an H4 tag. For
# :cost_per_night and :public_park, if the campground IS a public park, display "Public Park"
# in an H5 tag. However, if it IS NOT a public park, display the :cost_per_night as
# U.S. currency in an H5 tag.
# While we ask you to link to the individual campground's page, you do not need to build that.
# The only page we're looking for is a page to list the campgrounds. If you feel it is best
# to split some content into partials, please write the project path of the new file
# above the code.
# Hint: remember you can create partials and other files you may need... we want to see how
# you would do it in real life
# app/views/campgrounds/index.html.haml
.campgrounds.container
-# TODO: Render campgrounds
# app/assets/stylesheets/campgrounds.scss
.campgrounds {
}
#-------------------
# 5. Form
#-------------------
# For the last task, we need you to make a form that will create a Campground object. You will
# need to utilize the CampgroundsController you set up in step 3. Two new methods will be
# necessary: one to display the page with the form and another to actually create the campground.
# The attributes we will be updating are :name, :city_id, :cost_per_night, and :public_park. Make
# sure to use Rails conventions when collecting this data from the client.
# The form should be built with both Rails helpers and Bootstrap classes in mind. All four attributes
# will be required in order to successfully create a Campground object.
# The first field will be for the :name attribute.
# The second field should be a dropdown that allows you to pick the associated
# City object, sorted by `name` from a list of all City objects in our database.
# The next field should be for the :cost_per_night. Make sure the validations on the front end
# match the validations on the back end, and of course, we would never have a negative
# :cost_per_night.
# And lastly, we need a field for the :public_park attribute. Should that be an input where the
# user types in `true` or `false`? There's got to be a better way to collect that information.
# (Hint: There is...)
# app/views/campgrounds/new.html.haml
.campground-form
-# TODO: Display form to create a new campground
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment