Skip to content

Instantly share code, notes, and snippets.

@jendiamond
Last active January 7, 2016 01:25
Show Gist options
  • Select an option

  • Save jendiamond/a67f4c68ae31c6f4c6a9 to your computer and use it in GitHub Desktop.

Select an option

Save jendiamond/a67f4c68ae31c6f4c6a9 to your computer and use it in GitHub Desktop.

Reach for a Peach

https://waffle.io/LARailslearners/reach-for-a-peach
https://github.com/LARailsLearners/reach-for-a-peach

Reach for a Peach is a web app designed to help you reach your goals and keep track of them. You create your goals (represented by a peach) and set a time limit for them.


Create a github repository reach-for-a-peach


Create a heroku repository reach-for-a-peach


Add postgres

$ rails new reach-for-a-peach --database=postgresql

$ cd into reach-for-a-peach

$ rake db:migrate

$ rake db:setup


git

$ git init
$ git add
$ git commit
$ git add remote GitHub
$ git add remote Heroku accounts
$ git push to GitHub
(Don't push to Heroku yet.)


Add RSpec

Add rspec-rails to both the :development and :test groups in the Gemfile:

group :development, :test do
  gem 'rspec-rails', '~> 3.0'
end

$ bundle


Create the Peach model

$ rails g model peach name deadline:integer

$ rake db:migrate

run tests $bundle exec rspec

$ git add, commit and push to github


Generate the Peaches controller and add index action to it

$ rails g controller peaches index

run tests $bundle exec rspec


Create test to see that the Peach model is working

RSpec.describe Peach, type: :model do
  it "creates a peach" do
    new_peach = Peach.create!(name: "Save the Rainforest", deadline: 5)

    expect(new_peach.name).to eq("Save the Rainforest")
    expect(new_peach.deadline).to eq(5)
  end
end

In the peaches_controller add the new and create actions

  def new
  	@peach = Peach.new
  end

  def create
  	@peach = Peach.new(peach_params)
  	if @peach.save
  		redirect_to peaches
  	else
  		render 'new'
  	end
  end

  private

  def peach_params
  	params.require(:peach).permit(:name, :deadline)
  end
end

Seed the database with some peaches



Make a controller test for the index http://everydayrails.com/2012/04/07/testing-series-rspec-controllers.html

The description of the example is written in explicit, active language. The example only expects one thing: After the post request is processed, a redirect should be returned to the browser. A factory generates test data to pass to the controller method; note the use of Factory Girl’s attributes_for option, which generates a hash of values as opposed to a Ruby object. However, there are also a couple of new things to look at:

The basic syntax of a controller spec—it’s REST method (post), controller method (:create), and, optionally, parameters being passed to the method. The aforementioned attributes_for call to Factory Girl—not rocket science, but worth mentioning again because I had a habit early on of forgetting to use it versus default factories.

Add Factory Girl and Faker Gems


$ rails g model bite

$ rails g controller peach edit

$ rails g controller peach destroy


make the peachs#index page be root

$ git add, commit and push to github


move the destroy link to the resolutions#show page, delete the edit and destroy links

$ git add, commit and push to github


on theresolutions#index page, link the resolution to the show page and delete the show link

$ git add, commit and push to github


create a navbar partial with the h1 tag Ressies which will link to the home page

$ git add, commit and push to github


change the Gemfile for postgres

$ bundle

$ git add, commit and push to github and heroku
$ heroku run rake db:migrate


$ rails g scaffold resolve date:date choice:boolean resolution:references
$ rake db:migrate
$ git add, commit and push to github


on theresolutions#index page, link the resolution to the resolve#show page $ git add, commit and push to github


move the resolution edit and destroy links to the resolve#show page

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