From 6454ad09ee3b990c26b399012e14beda3078f8d5 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Mon, 1 Oct 2012 22:22:14 +0100 Subject: [PATCH 1/8] Prevent generators from overriding our CSS. --- config/application.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/config/application.rb b/config/application.rb index 16f4ae061..832dff792 100644 --- a/config/application.rb +++ b/config/application.rb @@ -61,5 +61,10 @@ module Growstuff # Don't try to connect to the database when precompiling assets config.assets.initialize_on_precompile = false + + config.generators do |g| + g.template_engine :haml + g.stylesheets false + end end end From 1d9c1b14fc155d37e0c944dbf6aa48326bf450d1 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Mon, 1 Oct 2012 22:32:43 +0100 Subject: [PATCH 2/8] Generate scaffold for Crops; refactor tests Add link to Crops page from index Refactored index/application tests to better reflect where things are. --- Gemfile | 1 + Gemfile.lock | 6 + app/assets/javascripts/crops.js.coffee | 3 + app/controllers/crops_controller.rb | 83 +++++++++++ app/helpers/crops_helper.rb | 2 + app/models/crop.rb | 3 + app/views/crops/_form.html.haml | 16 +++ app/views/crops/edit.html.haml | 7 + app/views/crops/index.html.haml | 21 +++ app/views/crops/new.html.haml | 5 + app/views/crops/show.html.haml | 12 ++ app/views/home/index.html.haml | 2 + config/routes.rb | 2 + db/migrate/20121001212604_create_crops.rb | 10 ++ db/schema.rb | 9 +- spec/controllers/crops_controller_spec.rb | 164 ++++++++++++++++++++++ spec/helpers/crops_helper_spec.rb | 15 ++ spec/models/crop_spec.rb | 5 + spec/routing/crops_routing_spec.rb | 35 +++++ spec/views/crops/edit.html.haml_spec.rb | 20 +++ spec/views/crops/index.html.haml_spec.rb | 23 +++ spec/views/crops/new.html.haml_spec.rb | 20 +++ spec/views/crops/show.html.haml_spec.rb | 17 +++ spec/views/home/index_spec.rb | 55 ++------ spec/views/layouts/application_spec.rb | 40 +++++- 25 files changed, 526 insertions(+), 50 deletions(-) create mode 100644 app/assets/javascripts/crops.js.coffee create mode 100644 app/controllers/crops_controller.rb create mode 100644 app/helpers/crops_helper.rb create mode 100644 app/models/crop.rb create mode 100644 app/views/crops/_form.html.haml create mode 100644 app/views/crops/edit.html.haml create mode 100644 app/views/crops/index.html.haml create mode 100644 app/views/crops/new.html.haml create mode 100644 app/views/crops/show.html.haml create mode 100644 db/migrate/20121001212604_create_crops.rb create mode 100644 spec/controllers/crops_controller_spec.rb create mode 100644 spec/helpers/crops_helper_spec.rb create mode 100644 spec/models/crop_spec.rb create mode 100644 spec/routing/crops_routing_spec.rb create mode 100644 spec/views/crops/edit.html.haml_spec.rb create mode 100644 spec/views/crops/index.html.haml_spec.rb create mode 100644 spec/views/crops/new.html.haml_spec.rb create mode 100644 spec/views/crops/show.html.haml_spec.rb diff --git a/Gemfile b/Gemfile index 5adb0dd09..ab9fb82bf 100644 --- a/Gemfile +++ b/Gemfile @@ -59,6 +59,7 @@ gem 'cape' gem 'diff-lcs' group :development, :test do + gem 'haml-rails' gem 'rspec-rails' gem 'webrat' gem 'watchr' diff --git a/Gemfile.lock b/Gemfile.lock index 49801d91e..6a9acb701 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -65,6 +65,11 @@ GEM fastthread (1.0.7) fssm (0.2.9) haml (3.1.7) + haml-rails (0.3.5) + actionpack (>= 3.1, < 4.1) + activesupport (>= 3.1, < 4.1) + haml (~> 3.1) + railties (>= 3.1, < 4.1) highline (1.6.14) hike (1.2.1) i18n (0.6.1) @@ -184,6 +189,7 @@ DEPENDENCIES devise diff-lcs haml + haml-rails jquery-rails passenger rails (= 3.2.8) diff --git a/app/assets/javascripts/crops.js.coffee b/app/assets/javascripts/crops.js.coffee new file mode 100644 index 000000000..761567942 --- /dev/null +++ b/app/assets/javascripts/crops.js.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/ diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb new file mode 100644 index 000000000..8144b76c4 --- /dev/null +++ b/app/controllers/crops_controller.rb @@ -0,0 +1,83 @@ +class CropsController < ApplicationController + # GET /crops + # GET /crops.json + def index + @crops = Crop.all + + respond_to do |format| + format.html # index.html.erb + format.json { render json: @crops } + end + end + + # GET /crops/1 + # GET /crops/1.json + def show + @crop = Crop.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.json { render json: @crop } + end + end + + # GET /crops/new + # GET /crops/new.json + def new + @crop = Crop.new + + respond_to do |format| + format.html # new.html.erb + format.json { render json: @crop } + end + end + + # GET /crops/1/edit + def edit + @crop = Crop.find(params[:id]) + end + + # POST /crops + # POST /crops.json + def create + @crop = Crop.new(params[:crop]) + + respond_to do |format| + if @crop.save + format.html { redirect_to @crop, notice: 'Crop was successfully created.' } + format.json { render json: @crop, status: :created, location: @crop } + else + format.html { render action: "new" } + format.json { render json: @crop.errors, status: :unprocessable_entity } + end + end + end + + # PUT /crops/1 + # PUT /crops/1.json + def update + @crop = Crop.find(params[:id]) + + respond_to do |format| + if @crop.update_attributes(params[:crop]) + format.html { redirect_to @crop, notice: 'Crop was successfully updated.' } + format.json { head :no_content } + else + format.html { render action: "edit" } + format.json { render json: @crop.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /crops/1 + # DELETE /crops/1.json + def destroy + @crop = Crop.find(params[:id]) + @crop.destroy + + respond_to do |format| + format.html { redirect_to crops_url } + format.json { head :no_content } + end + end +end diff --git a/app/helpers/crops_helper.rb b/app/helpers/crops_helper.rb new file mode 100644 index 000000000..da6fb9a78 --- /dev/null +++ b/app/helpers/crops_helper.rb @@ -0,0 +1,2 @@ +module CropsHelper +end diff --git a/app/models/crop.rb b/app/models/crop.rb new file mode 100644 index 000000000..a96cda1fb --- /dev/null +++ b/app/models/crop.rb @@ -0,0 +1,3 @@ +class Crop < ActiveRecord::Base + attr_accessible :en_wikipedia_url, :system_name +end diff --git a/app/views/crops/_form.html.haml b/app/views/crops/_form.html.haml new file mode 100644 index 000000000..aa82d9dc5 --- /dev/null +++ b/app/views/crops/_form.html.haml @@ -0,0 +1,16 @@ += form_for @crop do |f| + - if @crop.errors.any? + #error_explanation + %h2= "#{pluralize(@crop.errors.count, "error")} prohibited this crop from being saved:" + %ul + - @crop.errors.full_messages.each do |msg| + %li= msg + + .field + = f.label :system_name + = f.text_field :system_name + .field + = f.label :en_wikipedia_url + = f.text_field :en_wikipedia_url + .actions + = f.submit 'Save' diff --git a/app/views/crops/edit.html.haml b/app/views/crops/edit.html.haml new file mode 100644 index 000000000..684956b8d --- /dev/null +++ b/app/views/crops/edit.html.haml @@ -0,0 +1,7 @@ +%h1 Editing crop + += render 'form' + += link_to 'Show', @crop +\| += link_to 'Back', crops_path diff --git a/app/views/crops/index.html.haml b/app/views/crops/index.html.haml new file mode 100644 index 000000000..46f6e3dd3 --- /dev/null +++ b/app/views/crops/index.html.haml @@ -0,0 +1,21 @@ +%h1 Listing crops + +%table + %tr + %th System name + %th En wikipedia url + %th + %th + %th + + - @crops.each do |crop| + %tr + %td= crop.system_name + %td= crop.en_wikipedia_url + %td= link_to 'Show', crop + %td= link_to 'Edit', edit_crop_path(crop) + %td= link_to 'Destroy', crop, method: :delete, data: { confirm: 'Are you sure?' } + +%br + += link_to 'New Crop', new_crop_path diff --git a/app/views/crops/new.html.haml b/app/views/crops/new.html.haml new file mode 100644 index 000000000..3d6bccc1c --- /dev/null +++ b/app/views/crops/new.html.haml @@ -0,0 +1,5 @@ +%h1 New crop + += render 'form' + += link_to 'Back', crops_path diff --git a/app/views/crops/show.html.haml b/app/views/crops/show.html.haml new file mode 100644 index 000000000..3a3d347b7 --- /dev/null +++ b/app/views/crops/show.html.haml @@ -0,0 +1,12 @@ +%p#notice= notice + +%p + %b System name: + = @crop.system_name +%p + %b En wikipedia url: + = @crop.en_wikipedia_url + += link_to 'Edit', edit_crop_path(@crop) +\| += link_to 'Back', crops_path diff --git a/app/views/home/index.html.haml b/app/views/home/index.html.haml index 2ca38d592..de7a46f52 100644 --- a/app/views/home/index.html.haml +++ b/app/views/home/index.html.haml @@ -1,3 +1,5 @@ %p Growstuff is a community of food gardeners working together to build an open source platform to track, share, and discuss edible gardens and sustainable lifestyles. You can join us right now and be part of growing our website, from seed to harvest. We welcome you regardless of your experience, and invite you to be part of our development process. +%p + = link_to "Crops", crops_path diff --git a/config/routes.rb b/config/routes.rb index c0307d943..da8709c5c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Growstuff::Application.routes.draw do + resources :crops + devise_for :users get "home/index" diff --git a/db/migrate/20121001212604_create_crops.rb b/db/migrate/20121001212604_create_crops.rb new file mode 100644 index 000000000..7364cd199 --- /dev/null +++ b/db/migrate/20121001212604_create_crops.rb @@ -0,0 +1,10 @@ +class CreateCrops < ActiveRecord::Migration + def change + create_table :crops do |t| + t.string :system_name + t.string :en_wikipedia_url + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 9baaa71af..274ee70f6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,14 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20120903112806) do +ActiveRecord::Schema.define(:version => 20121001212604) do + + create_table "crops", :force => true do |t| + t.string "system_name" + t.string "en_wikipedia_url" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end create_table "users", :force => true do |t| t.string "email", :default => "", :null => false diff --git a/spec/controllers/crops_controller_spec.rb b/spec/controllers/crops_controller_spec.rb new file mode 100644 index 000000000..7ea1db910 --- /dev/null +++ b/spec/controllers/crops_controller_spec.rb @@ -0,0 +1,164 @@ +require 'spec_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to specify the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. +# +# Compared to earlier versions of this generator, there is very limited use of +# stubs and message expectations in this spec. Stubs are only used when there +# is no simpler way to get a handle on the object needed for the example. +# Message expectations are only used when there is no simpler way to specify +# that an instance is receiving a specific message. + +describe CropsController do + + # This should return the minimal set of attributes required to create a valid + # Crop. As you add validations to Crop, be sure to + # update the return value of this method accordingly. + def valid_attributes + {} + end + + # This should return the minimal set of values that should be in the session + # in order to pass any filters (e.g. authentication) defined in + # CropsController. Be sure to keep this updated too. + def valid_session + {} + end + + describe "GET index" do + it "assigns all crops as @crops" do + crop = Crop.create! valid_attributes + get :index, {}, valid_session + assigns(:crops).should eq([crop]) + end + end + + describe "GET show" do + it "assigns the requested crop as @crop" do + crop = Crop.create! valid_attributes + get :show, {:id => crop.to_param}, valid_session + assigns(:crop).should eq(crop) + end + end + + describe "GET new" do + it "assigns a new crop as @crop" do + get :new, {}, valid_session + assigns(:crop).should be_a_new(Crop) + end + end + + describe "GET edit" do + it "assigns the requested crop as @crop" do + crop = Crop.create! valid_attributes + get :edit, {:id => crop.to_param}, valid_session + assigns(:crop).should eq(crop) + end + end + + describe "POST create" do + describe "with valid params" do + it "creates a new Crop" do + expect { + post :create, {:crop => valid_attributes}, valid_session + }.to change(Crop, :count).by(1) + end + + it "assigns a newly created crop as @crop" do + post :create, {:crop => valid_attributes}, valid_session + assigns(:crop).should be_a(Crop) + assigns(:crop).should be_persisted + end + + it "redirects to the created crop" do + post :create, {:crop => valid_attributes}, valid_session + response.should redirect_to(Crop.last) + end + end + + describe "with invalid params" do + it "assigns a newly created but unsaved crop as @crop" do + # Trigger the behavior that occurs when invalid params are submitted + Crop.any_instance.stub(:save).and_return(false) + post :create, {:crop => {}}, valid_session + assigns(:crop).should be_a_new(Crop) + end + + it "re-renders the 'new' template" do + # Trigger the behavior that occurs when invalid params are submitted + Crop.any_instance.stub(:save).and_return(false) + post :create, {:crop => {}}, valid_session + response.should render_template("new") + end + end + end + + describe "PUT update" do + describe "with valid params" do + it "updates the requested crop" do + crop = Crop.create! valid_attributes + # Assuming there are no other crops in the database, this + # specifies that the Crop created on the previous line + # receives the :update_attributes message with whatever params are + # submitted in the request. + Crop.any_instance.should_receive(:update_attributes).with({'these' => 'params'}) + put :update, {:id => crop.to_param, :crop => {'these' => 'params'}}, valid_session + end + + it "assigns the requested crop as @crop" do + crop = Crop.create! valid_attributes + put :update, {:id => crop.to_param, :crop => valid_attributes}, valid_session + assigns(:crop).should eq(crop) + end + + it "redirects to the crop" do + crop = Crop.create! valid_attributes + put :update, {:id => crop.to_param, :crop => valid_attributes}, valid_session + response.should redirect_to(crop) + end + end + + describe "with invalid params" do + it "assigns the crop as @crop" do + crop = Crop.create! valid_attributes + # Trigger the behavior that occurs when invalid params are submitted + Crop.any_instance.stub(:save).and_return(false) + put :update, {:id => crop.to_param, :crop => {}}, valid_session + assigns(:crop).should eq(crop) + end + + it "re-renders the 'edit' template" do + crop = Crop.create! valid_attributes + # Trigger the behavior that occurs when invalid params are submitted + Crop.any_instance.stub(:save).and_return(false) + put :update, {:id => crop.to_param, :crop => {}}, valid_session + response.should render_template("edit") + end + end + end + + describe "DELETE destroy" do + it "destroys the requested crop" do + crop = Crop.create! valid_attributes + expect { + delete :destroy, {:id => crop.to_param}, valid_session + }.to change(Crop, :count).by(-1) + end + + it "redirects to the crops list" do + crop = Crop.create! valid_attributes + delete :destroy, {:id => crop.to_param}, valid_session + response.should redirect_to(crops_url) + end + end + +end diff --git a/spec/helpers/crops_helper_spec.rb b/spec/helpers/crops_helper_spec.rb new file mode 100644 index 000000000..23f2f2970 --- /dev/null +++ b/spec/helpers/crops_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the CropsHelper. For example: +# +# describe CropsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# helper.concat_strings("this","that").should == "this that" +# end +# end +# end +describe CropsHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb new file mode 100644 index 000000000..662efde07 --- /dev/null +++ b/spec/models/crop_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Crop do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/routing/crops_routing_spec.rb b/spec/routing/crops_routing_spec.rb new file mode 100644 index 000000000..450ad30f3 --- /dev/null +++ b/spec/routing/crops_routing_spec.rb @@ -0,0 +1,35 @@ +require "spec_helper" + +describe CropsController do + describe "routing" do + + it "routes to #index" do + get("/crops").should route_to("crops#index") + end + + it "routes to #new" do + get("/crops/new").should route_to("crops#new") + end + + it "routes to #show" do + get("/crops/1").should route_to("crops#show", :id => "1") + end + + it "routes to #edit" do + get("/crops/1/edit").should route_to("crops#edit", :id => "1") + end + + it "routes to #create" do + post("/crops").should route_to("crops#create") + end + + it "routes to #update" do + put("/crops/1").should route_to("crops#update", :id => "1") + end + + it "routes to #destroy" do + delete("/crops/1").should route_to("crops#destroy", :id => "1") + end + + end +end diff --git a/spec/views/crops/edit.html.haml_spec.rb b/spec/views/crops/edit.html.haml_spec.rb new file mode 100644 index 000000000..9f8ac8d41 --- /dev/null +++ b/spec/views/crops/edit.html.haml_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe "crops/edit" do + before(:each) do + @crop = assign(:crop, stub_model(Crop, + :system_name => "MyString", + :en_wikipedia_url => "MyString" + )) + end + + it "renders the edit crop form" do + render + + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "form", :action => crops_path(@crop), :method => "post" do + assert_select "input#crop_system_name", :name => "crop[system_name]" + assert_select "input#crop_en_wikipedia_url", :name => "crop[en_wikipedia_url]" + end + end +end diff --git a/spec/views/crops/index.html.haml_spec.rb b/spec/views/crops/index.html.haml_spec.rb new file mode 100644 index 000000000..c82686ed7 --- /dev/null +++ b/spec/views/crops/index.html.haml_spec.rb @@ -0,0 +1,23 @@ +require 'spec_helper' + +describe "crops/index" do + before(:each) do + assign(:crops, [ + stub_model(Crop, + :system_name => "System Name", + :en_wikipedia_url => "En Wikipedia Url" + ), + stub_model(Crop, + :system_name => "System Name", + :en_wikipedia_url => "En Wikipedia Url" + ) + ]) + end + + it "renders a list of crops" do + render + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "tr>td", :text => "System Name".to_s, :count => 2 + assert_select "tr>td", :text => "En Wikipedia Url".to_s, :count => 2 + end +end diff --git a/spec/views/crops/new.html.haml_spec.rb b/spec/views/crops/new.html.haml_spec.rb new file mode 100644 index 000000000..ac1f3f021 --- /dev/null +++ b/spec/views/crops/new.html.haml_spec.rb @@ -0,0 +1,20 @@ +require 'spec_helper' + +describe "crops/new" do + before(:each) do + assign(:crop, stub_model(Crop, + :system_name => "MyString", + :en_wikipedia_url => "MyString" + ).as_new_record) + end + + it "renders new crop form" do + render + + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "form", :action => crops_path, :method => "post" do + assert_select "input#crop_system_name", :name => "crop[system_name]" + assert_select "input#crop_en_wikipedia_url", :name => "crop[en_wikipedia_url]" + end + end +end diff --git a/spec/views/crops/show.html.haml_spec.rb b/spec/views/crops/show.html.haml_spec.rb new file mode 100644 index 000000000..26c3938cc --- /dev/null +++ b/spec/views/crops/show.html.haml_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' + +describe "crops/show" do + before(:each) do + @crop = assign(:crop, stub_model(Crop, + :system_name => "System Name", + :en_wikipedia_url => "En Wikipedia Url" + )) + end + + it "renders attributes in

" do + render + # Run the generator again with the --webrat flag if you want to use webrat matchers + rendered.should match(/System Name/) + rendered.should match(/En Wikipedia Url/) + end +end diff --git a/spec/views/home/index_spec.rb b/spec/views/home/index_spec.rb index 2172f6d9c..68ba69177 100644 --- a/spec/views/home/index_spec.rb +++ b/spec/views/home/index_spec.rb @@ -1,54 +1,17 @@ require 'spec_helper' describe 'home/index.html.haml', :type => "view" do - context "when not logged in" do - - before(:each) do - view.stub(:user_signed_in).and_return(false) - view.stub(:current_user).and_return(nil) - render - end - - it 'shows the homepage' do - rendered.should contain 'Growstuff' - end - - it 'should have signup/login links' do - rendered.should contain 'Sign up' - rendered.should contain 'Log in' - end - - it 'should have description' do - render - rendered.should contain 'Growstuff is a community of food gardeners' - rendered.should contain 'We welcome you regardless of your experience, and invite you to be part of our development process.' - end - + before(:each) do + render end - context "logged in" do - - before(:each) do - @user = User.create(:email => "growstuff@example.com", :password => "irrelevant") - @user.confirm! - sign_in @user - render - end - - it 'should show username' do - rendered.should contain 'You are signed in as' - rendered.should contain 'growstuff@example.com' - end - - it 'should show logout link' do - rendered.should contain 'Log out' - end - - it 'should have description' do - render - rendered.should contain 'Growstuff is a community of food gardeners' - rendered.should contain 'We welcome you regardless of your experience, and invite you to be part of our development process.' - end + it 'links to the crops page' do + rendered.should contain 'Crops' + end + it 'should have description' do + render + rendered.should contain 'Growstuff is a community of food gardeners' + rendered.should contain 'We welcome you regardless of your experience, and invite you to be part of our development process.' end end diff --git a/spec/views/layouts/application_spec.rb b/spec/views/layouts/application_spec.rb index 8580de35e..613bca755 100644 --- a/spec/views/layouts/application_spec.rb +++ b/spec/views/layouts/application_spec.rb @@ -1,8 +1,42 @@ require 'spec_helper' describe 'layouts/application.html.haml', :type => "view" do - it 'should have links in footer' do - render - rendered.should contain 'About' + context "when not logged in" do + + before(:each) do + view.stub(:user_signed_in).and_return(false) + view.stub(:current_user).and_return(nil) + render end + + it 'shows the title' do + rendered.should contain 'Growstuff' + end + + it 'should have signup/login links' do + rendered.should contain 'Sign up' + rendered.should contain 'Log in' + end + + end + + context "logged in" do + + before(:each) do + @user = User.create(:email => "growstuff@example.com", :password => "irrelevant") + @user.confirm! + sign_in @user + render + end + + it 'should show username' do + rendered.should contain 'You are signed in as' + rendered.should contain 'growstuff@example.com' + end + + it 'should show logout link' do + rendered.should contain 'Log out' + end + + end end From b85354f09987b771c1d316d9da84837a2ad36c6e Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Mon, 1 Oct 2012 22:18:38 +0100 Subject: [PATCH 3/8] Prettify views for crops. --- app/views/crops/edit.html.haml | 2 +- app/views/crops/index.html.haml | 21 +++++---------------- app/views/crops/new.html.haml | 2 +- app/views/crops/show.html.haml | 15 +++++++-------- spec/views/crops/index.html.haml_spec.rb | 12 ++++++------ 5 files changed, 20 insertions(+), 32 deletions(-) diff --git a/app/views/crops/edit.html.haml b/app/views/crops/edit.html.haml index 684956b8d..6dfdbe012 100644 --- a/app/views/crops/edit.html.haml +++ b/app/views/crops/edit.html.haml @@ -1,4 +1,4 @@ -%h1 Editing crop +%h2 Editing crop = render 'form' diff --git a/app/views/crops/index.html.haml b/app/views/crops/index.html.haml index 46f6e3dd3..a95e26579 100644 --- a/app/views/crops/index.html.haml +++ b/app/views/crops/index.html.haml @@ -1,20 +1,9 @@ -%h1 Listing crops +%h2 Crops -%table - %tr - %th System name - %th En wikipedia url - %th - %th - %th - - - @crops.each do |crop| - %tr - %td= crop.system_name - %td= crop.en_wikipedia_url - %td= link_to 'Show', crop - %td= link_to 'Edit', edit_crop_path(crop) - %td= link_to 'Destroy', crop, method: :delete, data: { confirm: 'Are you sure?' } +%ul +- @crops.each do |crop| + %li + = link_to crop.system_name, crop %br diff --git a/app/views/crops/new.html.haml b/app/views/crops/new.html.haml index 3d6bccc1c..696432431 100644 --- a/app/views/crops/new.html.haml +++ b/app/views/crops/new.html.haml @@ -1,4 +1,4 @@ -%h1 New crop +%h2 New crop = render 'form' diff --git a/app/views/crops/show.html.haml b/app/views/crops/show.html.haml index 3a3d347b7..8478336b7 100644 --- a/app/views/crops/show.html.haml +++ b/app/views/crops/show.html.haml @@ -1,12 +1,11 @@ +%h2= @crop.system_name %p#notice= notice %p - %b System name: - = @crop.system_name -%p - %b En wikipedia url: - = @crop.en_wikipedia_url + %b More information: + = link_to @crop.en_wikipedia_url, @crop.en_wikipedia_url -= link_to 'Edit', edit_crop_path(@crop) -\| -= link_to 'Back', crops_path +%ul.link-list + %li= link_to 'Edit', edit_crop_path(@crop) + %li= link_to 'Destroy', @crop, method: :delete, data: { confirm: 'Are you sure?' } + %li= link_to 'Back', crops_path diff --git a/spec/views/crops/index.html.haml_spec.rb b/spec/views/crops/index.html.haml_spec.rb index c82686ed7..4ce2c354d 100644 --- a/spec/views/crops/index.html.haml_spec.rb +++ b/spec/views/crops/index.html.haml_spec.rb @@ -4,12 +4,12 @@ describe "crops/index" do before(:each) do assign(:crops, [ stub_model(Crop, - :system_name => "System Name", - :en_wikipedia_url => "En Wikipedia Url" + :system_name => "Maize", + :en_wikipedia_url => "http://en.wikipedia.org/wiki/Maize" ), stub_model(Crop, - :system_name => "System Name", - :en_wikipedia_url => "En Wikipedia Url" + :system_name => "Tomato", + :en_wikipedia_url => "http://en.wikipedia.org/wiki/Tomato" ) ]) end @@ -17,7 +17,7 @@ describe "crops/index" do it "renders a list of crops" do render # Run the generator again with the --webrat flag if you want to use webrat matchers - assert_select "tr>td", :text => "System Name".to_s, :count => 2 - assert_select "tr>td", :text => "En Wikipedia Url".to_s, :count => 2 + assert_select "a", :text => "Maize" + assert_select "a", :text => "Tomato" end end From 622ddf620a6ab6ca7ba69995f8fda6a1cf65c492 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Mon, 1 Oct 2012 23:09:33 +0100 Subject: [PATCH 4/8] Refactor h2s into content_for :title Now the alert and notice are shown below the subtitle, and the browser title bar tells you something useful. --- app/views/crops/_form.html.haml | 2 +- app/views/crops/edit.html.haml | 2 +- app/views/crops/index.html.haml | 2 +- app/views/crops/new.html.haml | 2 +- app/views/crops/show.html.haml | 3 +-- app/views/devise/confirmations/new.html.haml | 2 +- app/views/devise/passwords/edit.html.haml | 2 +- app/views/devise/passwords/new.html.haml | 2 +- app/views/devise/registrations/edit.html.haml | 4 +--- app/views/devise/registrations/new.html.haml | 2 +- app/views/devise/sessions/new.html.haml | 2 +- app/views/devise/unlocks/new.html.haml | 2 +- app/views/layouts/_meta.html.haml | 2 +- app/views/layouts/application.html.haml | 6 ++++-- spec/views/crops/show.html.haml_spec.rb | 1 - spec/views/devise/confirmations/new_spec.rb | 4 ++-- spec/views/devise/unlocks/new_spec.rb | 1 - 17 files changed, 19 insertions(+), 22 deletions(-) diff --git a/app/views/crops/_form.html.haml b/app/views/crops/_form.html.haml index aa82d9dc5..12c917702 100644 --- a/app/views/crops/_form.html.haml +++ b/app/views/crops/_form.html.haml @@ -1,7 +1,7 @@ = form_for @crop do |f| - if @crop.errors.any? #error_explanation - %h2= "#{pluralize(@crop.errors.count, "error")} prohibited this crop from being saved:" + %h3= "#{pluralize(@crop.errors.count, "error")} prohibited this crop from being saved:" %ul - @crop.errors.full_messages.each do |msg| %li= msg diff --git a/app/views/crops/edit.html.haml b/app/views/crops/edit.html.haml index 6dfdbe012..9621cc235 100644 --- a/app/views/crops/edit.html.haml +++ b/app/views/crops/edit.html.haml @@ -1,4 +1,4 @@ -%h2 Editing crop +- content_for :title, "Editing crop" = render 'form' diff --git a/app/views/crops/index.html.haml b/app/views/crops/index.html.haml index a95e26579..cac3728a0 100644 --- a/app/views/crops/index.html.haml +++ b/app/views/crops/index.html.haml @@ -1,4 +1,4 @@ -%h2 Crops +- content_for :title, "Crops" %ul - @crops.each do |crop| diff --git a/app/views/crops/new.html.haml b/app/views/crops/new.html.haml index 696432431..36b28e200 100644 --- a/app/views/crops/new.html.haml +++ b/app/views/crops/new.html.haml @@ -1,4 +1,4 @@ -%h2 New crop +- content_for :title, "New crop" = render 'form' diff --git a/app/views/crops/show.html.haml b/app/views/crops/show.html.haml index 8478336b7..7665dbd23 100644 --- a/app/views/crops/show.html.haml +++ b/app/views/crops/show.html.haml @@ -1,5 +1,4 @@ -%h2= @crop.system_name -%p#notice= notice +- content_for :title, @crop.system_name %p %b More information: diff --git a/app/views/devise/confirmations/new.html.haml b/app/views/devise/confirmations/new.html.haml index c55b85cf7..a92a8f3f8 100644 --- a/app/views/devise/confirmations/new.html.haml +++ b/app/views/devise/confirmations/new.html.haml @@ -1,4 +1,4 @@ -%h2 Resend confirmation instructions +- content_for :title, "Resend confirmation instructions" = form_for(resource, :as => resource_name, :url => confirmation_path(resource_name), :html => { :method => :post }) do |f| = devise_error_messages! diff --git a/app/views/devise/passwords/edit.html.haml b/app/views/devise/passwords/edit.html.haml index b5c3081de..1b901f01e 100644 --- a/app/views/devise/passwords/edit.html.haml +++ b/app/views/devise/passwords/edit.html.haml @@ -1,4 +1,4 @@ -%h2 Change your password +- content_for :title, "Change your password" - form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :put }) do |f| = devise_error_messages! diff --git a/app/views/devise/passwords/new.html.haml b/app/views/devise/passwords/new.html.haml index c851c111e..2027975d0 100644 --- a/app/views/devise/passwords/new.html.haml +++ b/app/views/devise/passwords/new.html.haml @@ -1,4 +1,4 @@ -%h2 Forgot your password? +- content_for :title, "Forgot your password?" = form_for(resource, :as => resource_name, :url => password_path(resource_name), :html => { :method => :post }) do |f| = devise_error_messages! diff --git a/app/views/devise/registrations/edit.html.haml b/app/views/devise/registrations/edit.html.haml index b419549b5..8c1ee88de 100644 --- a/app/views/devise/registrations/edit.html.haml +++ b/app/views/devise/registrations/edit.html.haml @@ -1,6 +1,4 @@ -%h2 - Edit - = resource_name.to_s.humanize +- content_for :title, "Edit " + resource_name.to_s.humanize = form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| = devise_error_messages! diff --git a/app/views/devise/registrations/new.html.haml b/app/views/devise/registrations/new.html.haml index b422445ba..78638721a 100644 --- a/app/views/devise/registrations/new.html.haml +++ b/app/views/devise/registrations/new.html.haml @@ -1,4 +1,4 @@ -%h2 Sign up +- content_for :title, "Sign up" = form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| = devise_error_messages! diff --git a/app/views/devise/sessions/new.html.haml b/app/views/devise/sessions/new.html.haml index cf78c7eb2..bfc1d162c 100644 --- a/app/views/devise/sessions/new.html.haml +++ b/app/views/devise/sessions/new.html.haml @@ -1,4 +1,4 @@ -%h2 Sign in +- content_for :title, "Sign in" = form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %div diff --git a/app/views/devise/unlocks/new.html.haml b/app/views/devise/unlocks/new.html.haml index 6ac177126..f545cd4e7 100644 --- a/app/views/devise/unlocks/new.html.haml +++ b/app/views/devise/unlocks/new.html.haml @@ -1,4 +1,4 @@ -%h2 Resend unlock instructions +- content_for :title, "Resend unlock instructions" = form_for(resource, :as => resource_name, :url => unlock_path(resource_name), :html => { :method => :post }) do |f| = devise_error_messages! diff --git a/app/views/layouts/_meta.html.haml b/app/views/layouts/_meta.html.haml index 89ca1c3d8..f2222116a 100644 --- a/app/views/layouts/_meta.html.haml +++ b/app/views/layouts/_meta.html.haml @@ -3,7 +3,7 @@ %title - = content_for?(:title) ? yield(:title) : "Growstuff" + = content_for?(:title) ? yield(:title) + " - Growstuff" : "Growstuff" = stylesheet_link_tag "application", :media => "all" = javascript_include_tag "application" = csrf_meta_tags diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 01c9e37ad..56d6f5c94 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -4,11 +4,13 @@ = render :partial => "layouts/header" .row .ten.columns + - if content_for?(:title) + %h2=yield(:title) - if notice - %p.notice + %div.alert-box.success = notice - if alert - %p.alert + %div.alert-box.alert = alert = yield = render :partial => "layouts/footer" diff --git a/spec/views/crops/show.html.haml_spec.rb b/spec/views/crops/show.html.haml_spec.rb index 26c3938cc..3cadc9c0e 100644 --- a/spec/views/crops/show.html.haml_spec.rb +++ b/spec/views/crops/show.html.haml_spec.rb @@ -11,7 +11,6 @@ describe "crops/show" do it "renders attributes in

" do render # Run the generator again with the --webrat flag if you want to use webrat matchers - rendered.should match(/System Name/) rendered.should match(/En Wikipedia Url/) end end diff --git a/spec/views/devise/confirmations/new_spec.rb b/spec/views/devise/confirmations/new_spec.rb index 51cb0a018..d75568a3e 100644 --- a/spec/views/devise/confirmations/new_spec.rb +++ b/spec/views/devise/confirmations/new_spec.rb @@ -8,7 +8,7 @@ describe 'devise/confirmations/new.html.haml', :type => "view" do render end - it 'should have a Resend button' do - rendered.should contain "Resend confirmation instructions" + it 'should contain an Email field' do + rendered.should contain "Email" end end diff --git a/spec/views/devise/unlocks/new_spec.rb b/spec/views/devise/unlocks/new_spec.rb index 9ec3657d2..6b3640d17 100644 --- a/spec/views/devise/unlocks/new_spec.rb +++ b/spec/views/devise/unlocks/new_spec.rb @@ -12,7 +12,6 @@ describe 'devise/unlocks/new.html.haml', :type => "view" do end it 'should have some fields' do - rendered.should contain 'Resend unlock instructions' rendered.should contain 'Email' end end From 7452d7ae39a03c98e2099b6745a0597c443469fd Mon Sep 17 00:00:00 2001 From: Skud Date: Tue, 2 Oct 2012 21:44:01 +0100 Subject: [PATCH 5/8] Switched to postgres instead of sqlite3 --- Gemfile | 3 +-- Gemfile.lock | 4 ++-- config/database.yml | 26 ++++++++++++-------------- 3 files changed, 15 insertions(+), 18 deletions(-) diff --git a/Gemfile b/Gemfile index ab9fb82bf..26d69e670 100644 --- a/Gemfile +++ b/Gemfile @@ -9,8 +9,7 @@ gem 'haml' # Bundle edge Rails instead: # gem 'rails', :git => 'git://github.com/rails/rails.git' -gem 'sqlite3' - +gem 'pg' # Gems used only for assets and not required # in production environments by default. diff --git a/Gemfile.lock b/Gemfile.lock index 6a9acb701..4226e20e9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -100,6 +100,7 @@ GEM fastthread (>= 1.0.1) rack rake (>= 0.8.1) + pg (0.14.1) polyglot (0.3.3) rack (1.4.1) rack-cache (1.2) @@ -152,7 +153,6 @@ GEM hike (~> 1.2) rack (~> 1.0) tilt (~> 1.1, != 1.3.0) - sqlite3 (1.3.6) therubyracer (0.10.2) libv8 (~> 3.3.10) thor (0.16.0) @@ -192,13 +192,13 @@ DEPENDENCIES haml-rails jquery-rails passenger + pg rails (= 3.2.8) rake rspec-rails rvm-capistrano sass-rails (~> 3.2.3) spork (~> 0.9.0.rc) - sqlite3 therubyracer uglifier (>= 1.0.3) watchr diff --git a/config/database.yml b/config/database.yml index 51a4dd459..fe86fee42 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,25 +1,23 @@ -# SQLite version 3.x -# gem install sqlite3 -# -# Ensure the SQLite 3 gem is defined in your Gemfile -# gem 'sqlite3' development: - adapter: sqlite3 - database: db/development.sqlite3 + adapter: postgresql + database: growstuff_dev pool: 5 timeout: 5000 + username: growstuff + host: localhost -# Warning: The database defined as "test" will be erased and -# re-generated from your development database when you run "rake". -# Do not set this db to the same as development or production. test: - adapter: sqlite3 - database: db/test.sqlite3 + adapter: postgresql + database: growstuff_test pool: 5 timeout: 5000 + username: growstuff + host: localhost production: - adapter: sqlite3 - database: db/production.sqlite3 + adapter: postgresql + database: growstuff_prod pool: 5 timeout: 5000 + username: growstuff + host: localhost From 79a98d622522b21f589a591646f64b627bbea725 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Wed, 3 Oct 2012 20:04:56 +0100 Subject: [PATCH 6/8] Only use Postgres in production. --- Gemfile | 5 ++++- Gemfile.lock | 2 ++ config/database.yml | 12 ++++-------- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/Gemfile b/Gemfile index 26d69e670..04d0ee428 100644 --- a/Gemfile +++ b/Gemfile @@ -9,7 +9,9 @@ gem 'haml' # Bundle edge Rails instead: # gem 'rails', :git => 'git://github.com/rails/rails.git' -gem 'pg' +group :production do + gem 'pg' +end # Gems used only for assets and not required # in production environments by default. @@ -58,6 +60,7 @@ gem 'cape' gem 'diff-lcs' group :development, :test do + gem 'sqlite3' gem 'haml-rails' gem 'rspec-rails' gem 'webrat' diff --git a/Gemfile.lock b/Gemfile.lock index 4226e20e9..1f8039111 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -153,6 +153,7 @@ GEM hike (~> 1.2) rack (~> 1.0) tilt (~> 1.1, != 1.3.0) + sqlite3 (1.3.6) therubyracer (0.10.2) libv8 (~> 3.3.10) thor (0.16.0) @@ -199,6 +200,7 @@ DEPENDENCIES rvm-capistrano sass-rails (~> 3.2.3) spork (~> 0.9.0.rc) + sqlite3 therubyracer uglifier (>= 1.0.3) watchr diff --git a/config/database.yml b/config/database.yml index fe86fee42..ef9ffe6ce 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,18 +1,14 @@ development: - adapter: postgresql - database: growstuff_dev + adapter: sqlite3 + database: db/growstuff_dev.sqlite3 pool: 5 timeout: 5000 - username: growstuff - host: localhost test: - adapter: postgresql - database: growstuff_test + adapter: sqlite3 + database: db/growstuff_test.sqlite3 pool: 5 timeout: 5000 - username: growstuff - host: localhost production: adapter: postgresql From 3419a559e1796a6738d56ca315075e5c7bd43df4 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Wed, 3 Oct 2012 20:38:39 +0100 Subject: [PATCH 7/8] Require a system name for crops. --- ...003190731_require_system_name_for_crops.rb | 15 +++++++++++ db/schema.rb | 6 +++-- spec/controllers/crops_controller_spec.rb | 2 +- spec/models/crop_spec.rb | 26 ++++++++++++++++++- spec/{model => models}/user_spec.rb | 0 5 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20121003190731_require_system_name_for_crops.rb rename spec/{model => models}/user_spec.rb (100%) diff --git a/db/migrate/20121003190731_require_system_name_for_crops.rb b/db/migrate/20121003190731_require_system_name_for_crops.rb new file mode 100644 index 000000000..47a46ec26 --- /dev/null +++ b/db/migrate/20121003190731_require_system_name_for_crops.rb @@ -0,0 +1,15 @@ +class RequireSystemNameForCrops < ActiveRecord::Migration + def up + change_table :crops do |t| + t.index :system_name + t.change :system_name, :string, :null => false + end + end + + def down + change_table :crops do |t| + t.change :system_name, :string, :null => true + t.remove_index :system_name + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 274ee70f6..ec04d917e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,15 +11,17 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20121001212604) do +ActiveRecord::Schema.define(:version => 20121003190731) do create_table "crops", :force => true do |t| - t.string "system_name" + t.string "system_name", :null => false t.string "en_wikipedia_url" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false end + add_index "crops", ["system_name"], :name => "index_crops_on_system_name" + create_table "users", :force => true do |t| t.string "email", :default => "", :null => false t.string "encrypted_password", :default => "", :null => false diff --git a/spec/controllers/crops_controller_spec.rb b/spec/controllers/crops_controller_spec.rb index 7ea1db910..05630c3f1 100644 --- a/spec/controllers/crops_controller_spec.rb +++ b/spec/controllers/crops_controller_spec.rb @@ -24,7 +24,7 @@ describe CropsController do # Crop. As you add validations to Crop, be sure to # update the return value of this method accordingly. def valid_attributes - {} + { :system_name => "Tomato" } end # This should return the minimal set of values that should be in the session diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index 662efde07..aa7f4a1b0 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -1,5 +1,29 @@ require 'spec_helper' describe Crop do - pending "add some examples to (or delete) #{__FILE__}" + context 'all fields present' do + + before(:each) do + @crop = Crop.new + @crop.system_name = "Tomato" + @crop.en_wikipedia_url = "http://en.wikipedia.org/wiki/Tomato" + end + + it 'should save a basic crop' do + @crop.save.should be_true + end + + it 'should be fetchable from the database' do + @crop.save + @crop2 = Crop.find_by_system_name('Tomato') + @crop2.en_wikipedia_url.should == "http://en.wikipedia.org/wiki/Tomato" + end + end + + context 'invalid data' do + it 'should not save a crop without a system name' do + @crop = Crop.new + expect { @crop.save }.to raise_error ActiveRecord::StatementInvalid + end + end end diff --git a/spec/model/user_spec.rb b/spec/models/user_spec.rb similarity index 100% rename from spec/model/user_spec.rb rename to spec/models/user_spec.rb From 70245912694677c4b05543ba3f0c59bd0b08a7b9 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Wed, 3 Oct 2012 20:59:22 +0100 Subject: [PATCH 8/8] Include password for production from secret file. We read in the file using ERB. See http://stackoverflow.com/questions/18290/how-do-you-secure-database-yml --- config/database.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/config/database.yml b/config/database.yml index ef9ffe6ce..d84e85f09 100644 --- a/config/database.yml +++ b/config/database.yml @@ -17,3 +17,4 @@ production: timeout: 5000 username: growstuff host: localhost + password: <% begin IO.read("/home/deploy/.db_password") rescue "" end %>