From 91ea5146ac29059845ef9ae4d9a0bde9ecb08bc8 Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 15 Jul 2013 21:16:47 +1000 Subject: [PATCH 1/9] rails g scaffold seed... --- app/assets/javascripts/seeds.js.coffee | 3 + app/controllers/seeds_controller.rb | 88 +++++++++++++++++++++++ app/helpers/seeds_helper.rb | 2 + app/models/member.rb | 2 + app/models/seed.rb | 5 ++ app/views/seeds/_form.html.haml | 23 ++++++ app/views/seeds/edit.html.haml | 3 + app/views/seeds/index.html.haml | 24 +++++++ app/views/seeds/new.html.haml | 3 + app/views/seeds/show.html.haml | 27 +++++++ config/routes.rb | 9 +-- db/migrate/20130715110134_create_seeds.rb | 13 ++++ db/schema.rb | 12 +++- spec/controllers/seeds_controller_spec.rb | 5 ++ spec/factories/seeds.rb | 11 +++ spec/helpers/seeds_helper_spec.rb | 15 ++++ spec/models/seed_spec.rb | 13 ++++ spec/requests/seeds_spec.rb | 11 +++ spec/routing/seeds_routing_spec.rb | 35 +++++++++ spec/views/seeds/edit.html.haml_spec.rb | 21 ++++++ spec/views/seeds/index.html.haml_spec.rb | 18 +++++ spec/views/seeds/new.html.haml_spec.rb | 21 ++++++ spec/views/seeds/show.html.haml_spec.rb | 16 +++++ 23 files changed, 375 insertions(+), 5 deletions(-) create mode 100644 app/assets/javascripts/seeds.js.coffee create mode 100644 app/controllers/seeds_controller.rb create mode 100644 app/helpers/seeds_helper.rb create mode 100644 app/models/seed.rb create mode 100644 app/views/seeds/_form.html.haml create mode 100644 app/views/seeds/edit.html.haml create mode 100644 app/views/seeds/index.html.haml create mode 100644 app/views/seeds/new.html.haml create mode 100644 app/views/seeds/show.html.haml create mode 100644 db/migrate/20130715110134_create_seeds.rb create mode 100644 spec/controllers/seeds_controller_spec.rb create mode 100644 spec/factories/seeds.rb create mode 100644 spec/helpers/seeds_helper_spec.rb create mode 100644 spec/models/seed_spec.rb create mode 100644 spec/requests/seeds_spec.rb create mode 100644 spec/routing/seeds_routing_spec.rb create mode 100644 spec/views/seeds/edit.html.haml_spec.rb create mode 100644 spec/views/seeds/index.html.haml_spec.rb create mode 100644 spec/views/seeds/new.html.haml_spec.rb create mode 100644 spec/views/seeds/show.html.haml_spec.rb diff --git a/app/assets/javascripts/seeds.js.coffee b/app/assets/javascripts/seeds.js.coffee new file mode 100644 index 000000000..761567942 --- /dev/null +++ b/app/assets/javascripts/seeds.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/seeds_controller.rb b/app/controllers/seeds_controller.rb new file mode 100644 index 000000000..8d13addb6 --- /dev/null +++ b/app/controllers/seeds_controller.rb @@ -0,0 +1,88 @@ +class SeedsController < ApplicationController + load_and_authorize_resource + # GET /seeds + # GET /seeds.json + def index + @seeds = Seed.all + + respond_to do |format| + format.html # index.html.erb + format.json { render json: @seeds } + end + end + + # GET /seeds/1 + # GET /seeds/1.json + def show + @seed = Seed.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.json { render json: @seed } + end + end + + # GET /seeds/new + # GET /seeds/new.json + def new + @seed = Seed.new + + # using find_by_id here because it returns nil, unlike find + @crop = Crop.find_by_id(params[:crop_id]) || Crop.new + + respond_to do |format| + format.html # new.html.erb + format.json { render json: @seed } + end + end + + # GET /seeds/1/edit + def edit + @seed = Seed.find(params[:id]) + end + + # POST /seeds + # POST /seeds.json + def create + params[:seed][:owner_id] = current_member.id + @seed = Seed.new(params[:seed]) + + respond_to do |format| + if @seed.save + format.html { redirect_to @seed, notice: 'Seed was successfully created.' } + format.json { render json: @seed, status: :created, location: @seed } + else + format.html { render action: "new" } + format.json { render json: @seed.errors, status: :unprocessable_entity } + end + end + end + + # PUT /seeds/1 + # PUT /seeds/1.json + def update + @seed = Seed.find(params[:id]) + + respond_to do |format| + if @seed.update_attributes(params[:seed]) + format.html { redirect_to @seed, notice: 'Seed was successfully updated.' } + format.json { head :no_content } + else + format.html { render action: "edit" } + format.json { render json: @seed.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /seeds/1 + # DELETE /seeds/1.json + def destroy + @seed = Seed.find(params[:id]) + @seed.destroy + + respond_to do |format| + format.html { redirect_to seeds_url } + format.json { head :no_content } + end + end +end diff --git a/app/helpers/seeds_helper.rb b/app/helpers/seeds_helper.rb new file mode 100644 index 000000000..46c697f02 --- /dev/null +++ b/app/helpers/seeds_helper.rb @@ -0,0 +1,2 @@ +module SeedsHelper +end diff --git a/app/models/member.rb b/app/models/member.rb index 506ce2551..0725e42f4 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -9,6 +9,8 @@ class Member < ActiveRecord::Base has_many :gardens, :foreign_key => 'owner_id' has_many :plantings, :through => :gardens + has_many :seeds + has_and_belongs_to_many :roles has_many :notifications, :foreign_key => 'recipient_id' diff --git a/app/models/seed.rb b/app/models/seed.rb new file mode 100644 index 000000000..c271678b4 --- /dev/null +++ b/app/models/seed.rb @@ -0,0 +1,5 @@ +class Seed < ActiveRecord::Base + attr_accessible :owner_id, :crop_id, :description, :quantity, :use_by + belongs_to :crop + belongs_to :owner, :class_name => 'Member', :foreign_key => 'owner_id' +end diff --git a/app/views/seeds/_form.html.haml b/app/views/seeds/_form.html.haml new file mode 100644 index 000000000..e17719fd7 --- /dev/null +++ b/app/views/seeds/_form.html.haml @@ -0,0 +1,23 @@ += form_for(@seed, :html => {:class => "form-horizontal"}) do |f| + - if @seed.errors.any? + #error_explanation + %h2= "#{pluralize(@seed.errors.count, "error")} prohibited this seed from being saved:" + %ul + - @seed.errors.full_messages.each do |msg| + %li= msg + + .control-group + = f.label 'Crop:', :class => 'control-label' + .controls= collection_select(:seed, :crop_id, Crop.all, :id, :system_name, :selected => @seed.crop_id || @crop.id) + .control-group + = f.label 'Description:', :class => 'control-label' + .controls= f.text_area :description, :rows => 6 + .control-group + = f.label 'Quantity:', :class => 'control-label' + .controls + = f.number_field :quantity, :class => 'input-small' + .control-group + = f.label 'Use by:', :class => 'control-label' + .controls= f.text_field :use_by, :value => @seed.use_by ? @seed.use_by.to_s(:ymd) : '', :class => 'add-datepicker' + .form-actions + = f.submit 'Save', :class => 'btn btn-primary' diff --git a/app/views/seeds/edit.html.haml b/app/views/seeds/edit.html.haml new file mode 100644 index 000000000..13445fb6c --- /dev/null +++ b/app/views/seeds/edit.html.haml @@ -0,0 +1,3 @@ +- content_for :title, "Editing seeds" + += render 'form' diff --git a/app/views/seeds/index.html.haml b/app/views/seeds/index.html.haml new file mode 100644 index 000000000..3a215aab9 --- /dev/null +++ b/app/views/seeds/index.html.haml @@ -0,0 +1,24 @@ +- content_for :title, "Listing seeds" + +%p= link_to 'Add seeds', new_seed_path, :class => 'btn btn-primary' + +%table.table.table-striped + %tr + %th Owner + %th Crop + %th Description + %th Quantity + %th Use by + %th + + - @seeds.each do |seed| + %tr + %td= link_to seed.owner.login_name, seed.owner + %td= link_to seed.crop.system_name, seed.crop + %td= seed.description + %td= seed.quantity + %td= seed.use_by + %td= link_to 'Details', seed, :class => 'btn btn-mini' + +%br + diff --git a/app/views/seeds/new.html.haml b/app/views/seeds/new.html.haml new file mode 100644 index 000000000..834c958d8 --- /dev/null +++ b/app/views/seeds/new.html.haml @@ -0,0 +1,3 @@ +- content_for :title, "Add seeds" + += render 'form' diff --git a/app/views/seeds/show.html.haml b/app/views/seeds/show.html.haml new file mode 100644 index 000000000..f9e5530f8 --- /dev/null +++ b/app/views/seeds/show.html.haml @@ -0,0 +1,27 @@ +- content_for :title, "#{@seed.owner}'s #{@seed.crop} seeds" + +%p#notice= notice + +.row + .span6 + %p + %b Quantity: + = @seed.quantity.blank? ? "not specified" : @seed.quantity + %p + %b Use by: + = @seed.use_by.to_s + + %p + %b Description: + :markdown + #{ @seed.description != "" ? @seed.description : "No description given." } + + - if can? :edit, @seed or can? :destroy, @seed + %p + - if can? :edit, @seed + =link_to 'Edit', edit_seed_path(@seed), :class => 'btn btn-mini' + - if can? :destroy, @seed + =link_to 'Delete', @seed, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-mini' + + .span6 + = render :partial => "crops/index_card", :locals => { :crop => @seed.crop} diff --git a/config/routes.rb b/config/routes.rb index c14a07e76..ed14e0042 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,9 +1,11 @@ Growstuff::Application.routes.draw do - resources :photos - - devise_for :members, :controllers => { :registrations => "registrations" } + resources :members do + resources :seeds + end + + resources :photos resources :authentications resources :plantings @@ -11,7 +13,6 @@ Growstuff::Application.routes.draw do resources :posts resources :scientific_names resources :crops - resources :members resources :comments resources :roles resources :forums diff --git a/db/migrate/20130715110134_create_seeds.rb b/db/migrate/20130715110134_create_seeds.rb new file mode 100644 index 000000000..1d0d49225 --- /dev/null +++ b/db/migrate/20130715110134_create_seeds.rb @@ -0,0 +1,13 @@ +class CreateSeeds < ActiveRecord::Migration + def change + create_table :seeds do |t| + t.integer :owner_id, :null => false + t.integer :crop_id, :null => false + t.text :description + t.integer :quantity + t.date :use_by + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index bf9e10438..a805a5b48 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130705104238) do +ActiveRecord::Schema.define(:version => 20130715110134) do create_table "account_types", :force => true do |t| t.string "name", :null => false @@ -234,4 +234,14 @@ ActiveRecord::Schema.define(:version => 20130705104238) do t.datetime "updated_at", :null => false end + create_table "seeds", :force => true do |t| + t.integer "owner_id", :null => false + t.integer "crop_id", :null => false + t.text "description" + t.integer "quantity" + t.date "use_by" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false + end + end diff --git a/spec/controllers/seeds_controller_spec.rb b/spec/controllers/seeds_controller_spec.rb new file mode 100644 index 000000000..b5285946e --- /dev/null +++ b/spec/controllers/seeds_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe SeedsController do + pending "add some tests if/when we do anything interesting" +end diff --git a/spec/factories/seeds.rb b/spec/factories/seeds.rb new file mode 100644 index 000000000..aeb2abe0d --- /dev/null +++ b/spec/factories/seeds.rb @@ -0,0 +1,11 @@ +# Read about factories at https://github.com/thoughtbot/factory_girl + +FactoryGirl.define do + factory :seed do + owner + crop + description "MyText" + quantity 1 + use_by "2013-07-15" + end +end diff --git a/spec/helpers/seeds_helper_spec.rb b/spec/helpers/seeds_helper_spec.rb new file mode 100644 index 000000000..ad9a2022a --- /dev/null +++ b/spec/helpers/seeds_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the SeedsHelper. For example: +# +# describe SeedsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# helper.concat_strings("this","that").should == "this that" +# end +# end +# end +describe SeedsHelper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/seed_spec.rb b/spec/models/seed_spec.rb new file mode 100644 index 000000000..46a4c5851 --- /dev/null +++ b/spec/models/seed_spec.rb @@ -0,0 +1,13 @@ +require 'spec_helper' + +describe Seed do + + before(:each) do + @seed = FactoryGirl.build(:seed) + end + + it 'should save a basic seed' do + @seed.save.should be_true + end + +end diff --git a/spec/requests/seeds_spec.rb b/spec/requests/seeds_spec.rb new file mode 100644 index 000000000..ac3ee1076 --- /dev/null +++ b/spec/requests/seeds_spec.rb @@ -0,0 +1,11 @@ +require 'spec_helper' + +describe "Seeds" do + describe "GET /seeds" do + it "works! (now write some real specs)" do + # Run the generator again with the --webrat flag if you want to use webrat methods/matchers + get seeds_path + response.status.should be(200) + end + end +end diff --git a/spec/routing/seeds_routing_spec.rb b/spec/routing/seeds_routing_spec.rb new file mode 100644 index 000000000..d4746d299 --- /dev/null +++ b/spec/routing/seeds_routing_spec.rb @@ -0,0 +1,35 @@ +require "spec_helper" + +describe SeedsController do + describe "routing" do + + it "routes to #index" do + get("/seeds").should route_to("seeds#index") + end + + it "routes to #new" do + get("/seeds/new").should route_to("seeds#new") + end + + it "routes to #show" do + get("/seeds/1").should route_to("seeds#show", :id => "1") + end + + it "routes to #edit" do + get("/seeds/1/edit").should route_to("seeds#edit", :id => "1") + end + + it "routes to #create" do + post("/seeds").should route_to("seeds#create") + end + + it "routes to #update" do + put("/seeds/1").should route_to("seeds#update", :id => "1") + end + + it "routes to #destroy" do + delete("/seeds/1").should route_to("seeds#destroy", :id => "1") + end + + end +end diff --git a/spec/views/seeds/edit.html.haml_spec.rb b/spec/views/seeds/edit.html.haml_spec.rb new file mode 100644 index 000000000..3d0ce0ab5 --- /dev/null +++ b/spec/views/seeds/edit.html.haml_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe "seeds/edit" do + before(:each) do + @member = FactoryGirl.create(:member) + sign_in @member + controller.stub(:current_user) { @member } + @seed = FactoryGirl.create(:seed, :owner => @member) + end + + it "renders the edit seed form" do + render + + # Run the generator again with the --webrat flag if you want to use webrat matchers + assert_select "form", :action => seeds_path(@seed), :method => "post" do + assert_select "select#seed_crop_id", :name => "seed[crop_id]" + assert_select "textarea#seed_description", :name => "seed[description]" + assert_select "input#seed_quantity", :name => "seed[quantity]" + end + end +end diff --git a/spec/views/seeds/index.html.haml_spec.rb b/spec/views/seeds/index.html.haml_spec.rb new file mode 100644 index 000000000..8979d0291 --- /dev/null +++ b/spec/views/seeds/index.html.haml_spec.rb @@ -0,0 +1,18 @@ +require 'spec_helper' + +describe "seeds/index" do + before(:each) do + @member = FactoryGirl.create(:member) + sign_in @member + controller.stub(:current_user) { @member } + @seed1 = FactoryGirl.create(:seed, :owner => @member) + assign(:seeds, [@seed1, @seed1]) + end + + it "renders a list of seeds" do + render + assert_select "tr>td", :text => @seed1.crop.system_name, :count => 2 + assert_select "tr>td", :text => @seed1.owner.login_name, :count => 2 + assert_select "tr>td", :text => @seed1.quantity.to_s, :count => 2 + end +end diff --git a/spec/views/seeds/new.html.haml_spec.rb b/spec/views/seeds/new.html.haml_spec.rb new file mode 100644 index 000000000..c291a9956 --- /dev/null +++ b/spec/views/seeds/new.html.haml_spec.rb @@ -0,0 +1,21 @@ +require 'spec_helper' + +describe "seeds/new" do + before(:each) do + @member = FactoryGirl.create(:member) + sign_in @member + controller.stub(:current_user) { @member } + @seed1 = FactoryGirl.create(:seed, :owner => @member) + assign(:seed, @seed1) + end + + it "renders new seed form" do + render + + assert_select "form", :action => seeds_path, :method => "post" do + assert_select "select#seed_crop_id", :name => "seed[crop_id]" + assert_select "textarea#seed_description", :name => "seed[description]" + assert_select "input#seed_quantity", :name => "seed[quantity]" + end + end +end diff --git a/spec/views/seeds/show.html.haml_spec.rb b/spec/views/seeds/show.html.haml_spec.rb new file mode 100644 index 000000000..c31d14b79 --- /dev/null +++ b/spec/views/seeds/show.html.haml_spec.rb @@ -0,0 +1,16 @@ +require 'spec_helper' + +describe "seeds/show" do + before(:each) do + @member = FactoryGirl.create(:member) + sign_in @member + controller.stub(:current_user) { @member } + @seed1 = FactoryGirl.create(:seed, :owner => @member) + assign(:seed, @seed1) + end + + it "renders attributes in

" do + render + rendered.should contain @seed1.crop.system_name + end +end From b27e1d4db3b793d53f0b5813e9f2d2d491779075 Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 15 Jul 2013 21:44:59 +1000 Subject: [PATCH 2/9] let's not use nested resources (yet) --- config/routes.rb | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/config/routes.rb b/config/routes.rb index ed14e0042..23e7af55c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,11 +1,10 @@ Growstuff::Application.routes.draw do devise_for :members, :controllers => { :registrations => "registrations" } - resources :members do - resources :seeds - end + resources :members resources :photos + resources :seeds resources :authentications resources :plantings From cc711a0a48192f5618aed1427ae6c086a894c6bf Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 15 Jul 2013 22:50:44 +1000 Subject: [PATCH 3/9] prettified seed-related views --- app/models/ability.rb | 4 +++ app/views/crops/_index_card.html.haml | 21 +++++++++++++ app/views/crops/show.html.haml | 23 +++++++++----- app/views/plantings/show.html.haml | 21 +------------ app/views/shared/_signin_signup.html.haml | 6 ++++ config/locales/en.yml | 3 +- spec/views/crops/show.html.haml_spec.rb | 33 ++++++++++++++------- spec/views/plantings/show.html.haml_spec.rb | 7 +++-- 8 files changed, 77 insertions(+), 41 deletions(-) create mode 100644 app/views/crops/_index_card.html.haml create mode 100644 app/views/shared/_signin_signup.html.haml diff --git a/app/models/ability.rb b/app/models/ability.rb index 21a1fbf0e..ece3e809f 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -67,6 +67,10 @@ class Ability can :update, Photo, :owner_id => member.id can :destroy, Photo, :owner_id => member.id + can :create, Seed + can :update, Seed, :owner_id => member.id + can :destroy, Seed, :owner_id => member.id + # orders/shop/etc can :create, Order can :read, Order, :member_id => member.id diff --git a/app/views/crops/_index_card.html.haml b/app/views/crops/_index_card.html.haml new file mode 100644 index 000000000..c63f6ea05 --- /dev/null +++ b/app/views/crops/_index_card.html.haml @@ -0,0 +1,21 @@ +.well + .row-fluid + .span4 + = link_to image_tag((crop.default_photo ? crop.default_photo.thumbnail_url : 'placeholder_150.png'), :alt => '', :class => 'img-rounded'), crop + .span8 + %h3{:style => 'padding-top: 0px; margin-top: 0px'} + = link_to crop, crop + + %p + %b Scientific name: + = crop.default_scientific_name + %p + %b + Planted + = pluralize(crop.plantings_count, "time") + by #{Growstuff::Application.config.site_name} members + + - if can? :create, Planting + = link_to 'Plant this', new_planting_path(:params => { :crop_id => crop.id }), :class => 'btn btn-primary' + - if can? :create, Seed + = link_to 'Add seeds to stash', new_seed_path(:params => { :crop_id => crop.id }), :class => 'btn btn-primary' diff --git a/app/views/crops/show.html.haml b/app/views/crops/show.html.haml index f9a08a076..84f2bcc6d 100644 --- a/app/views/crops/show.html.haml +++ b/app/views/crops/show.html.haml @@ -15,20 +15,29 @@ = @crop.system_name != @crop.varieties.map{ |c| link_to c, c }.join(", ") - - if @crop.plantings_count > 0 - %p + %p + - if @crop.plantings_count > 0 Planted = pluralize(@crop.plantings_count, "time") by #{Growstuff::Application.config.site_name} members - %p= link_to "Plant this", new_planting_path(:crop_id => @crop.id), :class => 'btn btn-primary' + - else + Nobody is growing this yet. You could be the first! + %p + - if can? :create, Planting + = link_to "Plant this", new_planting_path(:crop_id => @crop.id), :class => 'btn btn-primary' + - else + = render :partial => 'shared/signin_signup', :locals => { :to => 'plant this crop' } + + - if can? :create, Seed + = link_to 'Add seeds to stash', new_seed_path(:params => { :crop_id => @crop.id }), :class => 'btn btn-primary' + - else + = render :partial => 'shared/signin_signup', :locals => { :to => 'add seeds for this crop to your stash' } + + - if @crop.plantings_count > 0 - @crop.plantings.each do |p| = render :partial => "plantings/thumbnail", :locals => { :planting => p, :title => 'owner' } - - else - %p Nobody is growing this yet. You could be the first! - %p= link_to "Plant this", new_planting_path(:crop_id => @crop.id), :class => 'btn btn-primary' - .span3 - if can? :edit, @crop or can? :destroy, @crop %h4 Crop wrangling diff --git a/app/views/plantings/show.html.haml b/app/views/plantings/show.html.haml index 203325a09..a2d0c8870 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -34,26 +34,7 @@ =link_to 'Delete', @planting, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-mini' .span6 - .well - .row-fluid - .span4 - = link_to image_tag((@planting.crop.default_photo ? @planting.crop.default_photo.thumbnail_url : 'placeholder_150.png'), :alt => '', :class => 'img-rounded'), @planting.crop - .span8 - %h3 - = link_to @planting.crop, @planting.crop - - if can? :edit, @planting - = link_to 'Plant another', new_planting_path, :class => 'btn btn-primary' - - elsif can? :create, Planting - = link_to 'Plant this', new_planting_path, :class => 'btn btn-primary' - - %p - %b Scientific name: - = @planting.crop_default_scientific_name - %p - %b - Planted - = pluralize(@planting.crop_plantings_count, "time") - by #{Growstuff::Application.config.site_name} members + = render :partial => "crops/index_card", :locals => { :crop => @planting.crop} %h2 Notes diff --git a/app/views/shared/_signin_signup.html.haml b/app/views/shared/_signin_signup.html.haml new file mode 100644 index 000000000..4b87fe70a --- /dev/null +++ b/app/views/shared/_signin_signup.html.haml @@ -0,0 +1,6 @@ +=link_to 'Sign in', new_member_session_path +or +=link_to 'sign up', new_member_registration_path +to += succeed "." do + = to diff --git a/config/locales/en.yml b/config/locales/en.yml index 4a9658fd1..7a30c72fe 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -8,7 +8,8 @@ en: create: planting: "Please sign in or sign up to plant something." post: "Please sign in or sign up to post." + seed: "Please sign in or sign up to add seeds." notification: "Please sign in to send a message." - all: "You don't have permission to create a %{subject}." + all: "Please sign in or sign up to create a %{subject}." manage: all: "Not authorized to %{action} %{subject}." diff --git a/spec/views/crops/show.html.haml_spec.rb b/spec/views/crops/show.html.haml_spec.rb index b1bfe26da..104c63647 100644 --- a/spec/views/crops/show.html.haml_spec.rb +++ b/spec/views/crops/show.html.haml_spec.rb @@ -28,16 +28,6 @@ describe "crops/show" do rendered.should contain "Zea mays" end - it "shows a plant this button" do - render - rendered.should contain "Plant this" - end - - it "links to the right crop in the planting link" do - render - assert_select("a[href=#{new_planting_path}?crop_id=#{@crop.id}]") - end - it "links to people who are growing this crop" do render rendered.should contain /member\d+/ @@ -71,6 +61,29 @@ describe "crops/show" do end + it 'tells you to sign in/sign up' do + render + rendered.should contain 'Sign in or sign up to plant' + rendered.should contain 'Sign in or sign up to add seed' + end + + context 'logged in' do + before(:each) do + @member = FactoryGirl.create(:member) + sign_in @member + controller.stub(:current_user) { @member } + render + end + + it "shows a plant this button" do + rendered.should contain "Plant this" + end + + it "links to the right crop in the planting link" do + assert_select("a[href=#{new_planting_path}?crop_id=#{@crop.id}]") + end + end + context "logged in and crop wrangler" do before(:each) do diff --git a/spec/views/plantings/show.html.haml_spec.rb b/spec/views/plantings/show.html.haml_spec.rb index ddd8db657..9e1c74d24 100644 --- a/spec/views/plantings/show.html.haml_spec.rb +++ b/spec/views/plantings/show.html.haml_spec.rb @@ -5,7 +5,8 @@ describe "plantings/show" do @garden = FactoryGirl.create(:garden, :owner => @member) @crop = FactoryGirl.create(:tomato) @planting = assign(:planting, - FactoryGirl.create(:planting, :garden => @garden, :crop => @crop) + FactoryGirl.create(:planting, :garden => @garden, :crop => @crop, + :planted_from => 'cutting') ) end @@ -36,7 +37,7 @@ describe "plantings/show" do it "shows planted_from" do render rendered.should contain 'Planted from:' - rendered.should contain 'seed' + rendered.should contain 'cutting' end it "doesn't show planted_from if blank" do @@ -44,7 +45,7 @@ describe "plantings/show" do @p.save render rendered.should_not contain 'Planted from:' - rendered.should_not contain 'seed' + rendered.should_not contain 'cutting' end end From 491e97c997b387621437b4424793668387bcd6a8 Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 15 Jul 2013 23:29:14 +1000 Subject: [PATCH 4/9] connected seeds into other parts of the site --- app/controllers/seeds_controller.rb | 6 ++- app/models/member.rb | 2 +- app/views/comments/show.html.haml | 2 - app/views/home/index.html.haml | 5 +++ app/views/members/show.html.haml | 7 ++++ app/views/seeds/index.html.haml | 48 +++++++++++++---------- app/views/seeds/show.html.haml | 2 - spec/controllers/seeds_controller_spec.rb | 8 +++- spec/views/home/index_spec.rb | 6 +++ 9 files changed, 59 insertions(+), 27 deletions(-) diff --git a/app/controllers/seeds_controller.rb b/app/controllers/seeds_controller.rb index 8d13addb6..1920ebe11 100644 --- a/app/controllers/seeds_controller.rb +++ b/app/controllers/seeds_controller.rb @@ -4,6 +4,10 @@ class SeedsController < ApplicationController # GET /seeds.json def index @seeds = Seed.all + @owner = Member.find_by_id(params[:owner_id]) + if @owner + @seeds = @owner.seeds.all + end respond_to do |format| format.html # index.html.erb @@ -49,7 +53,7 @@ class SeedsController < ApplicationController respond_to do |format| if @seed.save - format.html { redirect_to @seed, notice: 'Seed was successfully created.' } + format.html { redirect_to @seed, notice: "Successfully added #{@seed.crop} seed to your stash." } format.json { render json: @seed, status: :created, location: @seed } else format.html { render action: "new" } diff --git a/app/models/member.rb b/app/models/member.rb index 0725e42f4..45935e69f 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -9,7 +9,7 @@ class Member < ActiveRecord::Base has_many :gardens, :foreign_key => 'owner_id' has_many :plantings, :through => :gardens - has_many :seeds + has_many :seeds, :foreign_key => 'owner_id' has_and_belongs_to_many :roles diff --git a/app/views/comments/show.html.haml b/app/views/comments/show.html.haml index dade3ac57..a43473ffc 100644 --- a/app/views/comments/show.html.haml +++ b/app/views/comments/show.html.haml @@ -1,7 +1,5 @@ = content_for :title, @comment.post.subject -%p#notice= notice - = render :partial => "posts/single", :locals => { :post => @comment.post } %h2 Showing 1 comment diff --git a/app/views/home/index.html.haml b/app/views/home/index.html.haml index fba2dfe12..ac93bdd86 100644 --- a/app/views/home/index.html.haml +++ b/app/views/home/index.html.haml @@ -26,6 +26,11 @@ %li = link_to 'Add', new_garden_path, :class => 'btn btn-mini' + %p + %b Your seed stash: + = link_to pluralize(current_member.seeds.count, "variety"), seeds_path(:owner_id => current_member.id) + = link_to 'Add', new_seed_path, :class => 'btn btn-mini' + .span4 %p Your account: diff --git a/app/views/members/show.html.haml b/app/views/members/show.html.haml index 6c7d64428..f20071dc1 100644 --- a/app/views/members/show.html.haml +++ b/app/views/members/show.html.haml @@ -88,6 +88,13 @@ %h3 Create a new garden = render 'gardens/form' + %h3 Seeds + %p + - if @member.seeds.count > 0 + = link_to pluralize(@member.seeds.count, "variety"), seeds_path(:owner_id => @member.id) + - else + No seeds yet. + %h3 Posts - if @member.posts.count > 0 - @member.posts.each do |post| diff --git a/app/views/seeds/index.html.haml b/app/views/seeds/index.html.haml index 3a215aab9..abdabdb4e 100644 --- a/app/views/seeds/index.html.haml +++ b/app/views/seeds/index.html.haml @@ -1,24 +1,32 @@ -- content_for :title, "Listing seeds" +- content_for :title, @owner ? "#{@owner}'s seeds" : "Everyone's seeds" -%p= link_to 'Add seeds', new_seed_path, :class => 'btn btn-primary' +%p + - if can? :create, Seed + - if @owner + - if @owner == current_member + = link_to 'Add seeds', new_seed_path, :class => 'btn btn-primary' + - else # everyone's seeds + = link_to 'Add seeds', new_seed_path, :class => 'btn btn-primary' + - else + = render :partial => 'shared/signin_signup', :locals => { :to => 'add seeds to your stash' } -%table.table.table-striped - %tr - %th Owner - %th Crop - %th Description - %th Quantity - %th Use by - %th - - - @seeds.each do |seed| +- if @seeds.length > 0 + %table.table.table-striped %tr - %td= link_to seed.owner.login_name, seed.owner - %td= link_to seed.crop.system_name, seed.crop - %td= seed.description - %td= seed.quantity - %td= seed.use_by - %td= link_to 'Details', seed, :class => 'btn btn-mini' - -%br + %th Owner + %th Crop + %th Description + %th Quantity + %th Use by + %th + - @seeds.each do |seed| + %tr + %td= link_to seed.owner.login_name, seed.owner + %td= link_to seed.crop.system_name, seed.crop + %td= seed.description + %td= seed.quantity + %td= seed.use_by + %td= link_to 'Details', seed, :class => 'btn btn-mini' +- else + %p There are no seeds to display. diff --git a/app/views/seeds/show.html.haml b/app/views/seeds/show.html.haml index f9e5530f8..4b6dfa6e5 100644 --- a/app/views/seeds/show.html.haml +++ b/app/views/seeds/show.html.haml @@ -1,7 +1,5 @@ - content_for :title, "#{@seed.owner}'s #{@seed.crop} seeds" -%p#notice= notice - .row .span6 %p diff --git a/spec/controllers/seeds_controller_spec.rb b/spec/controllers/seeds_controller_spec.rb index b5285946e..8023deb69 100644 --- a/spec/controllers/seeds_controller_spec.rb +++ b/spec/controllers/seeds_controller_spec.rb @@ -1,5 +1,11 @@ require 'spec_helper' describe SeedsController do - pending "add some tests if/when we do anything interesting" + describe "GET index" do + it "picks up owner from params" do + owner = FactoryGirl.create(:member) + get :index, {:owner_id => owner.id} + assigns(:owner).should eq(owner) + end + end end diff --git a/spec/views/home/index_spec.rb b/spec/views/home/index_spec.rb index 213070596..f6891188c 100644 --- a/spec/views/home/index_spec.rb +++ b/spec/views/home/index_spec.rb @@ -43,6 +43,7 @@ describe 'home/index.html.haml', :type => "view" do :garden => @member.gardens.first ) assign(:plantings, [@planting]) + @seed = FactoryGirl.create(:seed, :owner => @member) @forum = FactoryGirl.create(:forum, :owner => @member) @post = FactoryGirl.create(:post, :author => @member) assign(:posts, [@post]) @@ -62,6 +63,11 @@ describe 'home/index.html.haml', :type => "view" do assert_select "a[href=#{url_for(@member.gardens.first)}]", "Garden" end + it 'shows seeds' do + rendered.should contain "Your seed stash" + rendered.should contain "1 variety" + end + it 'shows account type' do rendered.should contain "Free account" end From 973567bfdcaade82b05bb9f956816e9bba8daec9 Mon Sep 17 00:00:00 2001 From: Ryan Clark Date: Wed, 17 Jul 2013 18:10:49 -0700 Subject: [PATCH 5/9] Remane use_by to plant_before to humor Skud --- app/models/seed.rb | 2 +- app/views/seeds/_form.html.haml | 2 +- app/views/seeds/index.html.haml | 4 ++-- app/views/seeds/show.html.haml | 4 ++-- ...18005600_change_use_by_to_plant_before_on_seed.rb | 5 +++++ db/schema.rb | 12 ++++++------ spec/factories/seeds.rb | 2 +- 7 files changed, 18 insertions(+), 13 deletions(-) create mode 100644 db/migrate/20130718005600_change_use_by_to_plant_before_on_seed.rb diff --git a/app/models/seed.rb b/app/models/seed.rb index c271678b4..33c017b8f 100644 --- a/app/models/seed.rb +++ b/app/models/seed.rb @@ -1,5 +1,5 @@ class Seed < ActiveRecord::Base - attr_accessible :owner_id, :crop_id, :description, :quantity, :use_by + attr_accessible :owner_id, :crop_id, :description, :quantity, :plant_before belongs_to :crop belongs_to :owner, :class_name => 'Member', :foreign_key => 'owner_id' end diff --git a/app/views/seeds/_form.html.haml b/app/views/seeds/_form.html.haml index e17719fd7..9c86be1b5 100644 --- a/app/views/seeds/_form.html.haml +++ b/app/views/seeds/_form.html.haml @@ -18,6 +18,6 @@ = f.number_field :quantity, :class => 'input-small' .control-group = f.label 'Use by:', :class => 'control-label' - .controls= f.text_field :use_by, :value => @seed.use_by ? @seed.use_by.to_s(:ymd) : '', :class => 'add-datepicker' + .controls= f.text_field :plant_before, :value => @seed.plant_before ? @seed.plant_before.to_s(:ymd) : '', :class => 'add-datepicker' .form-actions = f.submit 'Save', :class => 'btn btn-primary' diff --git a/app/views/seeds/index.html.haml b/app/views/seeds/index.html.haml index abdabdb4e..23ab923aa 100644 --- a/app/views/seeds/index.html.haml +++ b/app/views/seeds/index.html.haml @@ -17,7 +17,7 @@ %th Crop %th Description %th Quantity - %th Use by + %th Plant before %th - @seeds.each do |seed| @@ -26,7 +26,7 @@ %td= link_to seed.crop.system_name, seed.crop %td= seed.description %td= seed.quantity - %td= seed.use_by + %td= seed.plant_before %td= link_to 'Details', seed, :class => 'btn btn-mini' - else %p There are no seeds to display. diff --git a/app/views/seeds/show.html.haml b/app/views/seeds/show.html.haml index 4b6dfa6e5..f072898e6 100644 --- a/app/views/seeds/show.html.haml +++ b/app/views/seeds/show.html.haml @@ -6,8 +6,8 @@ %b Quantity: = @seed.quantity.blank? ? "not specified" : @seed.quantity %p - %b Use by: - = @seed.use_by.to_s + %b Plant before: + = @seed.plant_before.to_s %p %b Description: diff --git a/db/migrate/20130718005600_change_use_by_to_plant_before_on_seed.rb b/db/migrate/20130718005600_change_use_by_to_plant_before_on_seed.rb new file mode 100644 index 000000000..23ffefaf9 --- /dev/null +++ b/db/migrate/20130718005600_change_use_by_to_plant_before_on_seed.rb @@ -0,0 +1,5 @@ +class ChangeUseByToPlantBeforeOnSeed < ActiveRecord::Migration + def change + rename_column :seeds, :use_by, :plant_before + end +end diff --git a/db/schema.rb b/db/schema.rb index a805a5b48..3a99425f8 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130715110134) do +ActiveRecord::Schema.define(:version => 20130718005600) do create_table "account_types", :force => true do |t| t.string "name", :null => false @@ -235,13 +235,13 @@ ActiveRecord::Schema.define(:version => 20130715110134) do end create_table "seeds", :force => true do |t| - t.integer "owner_id", :null => false - t.integer "crop_id", :null => false + t.integer "owner_id", :null => false + t.integer "crop_id", :null => false t.text "description" t.integer "quantity" - t.date "use_by" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.date "plant_before" + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end end diff --git a/spec/factories/seeds.rb b/spec/factories/seeds.rb index aeb2abe0d..467f01ef3 100644 --- a/spec/factories/seeds.rb +++ b/spec/factories/seeds.rb @@ -6,6 +6,6 @@ FactoryGirl.define do crop description "MyText" quantity 1 - use_by "2013-07-15" + plant_before "2013-07-15" end end From e9dd18d9778c72ee8609cbbd1bc7cb79f49739c1 Mon Sep 17 00:00:00 2001 From: Ryan Clark Date: Wed, 17 Jul 2013 18:25:55 -0700 Subject: [PATCH 6/9] Added tradable and tradable_to to seeds --- app/models/seed.rb | 9 ++++++++- db/migrate/20130718011247_add_trading_to_seeds.rb | 6 ++++++ db/schema.rb | 4 +++- spec/models/seed_spec.rb | 15 +++++++++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20130718011247_add_trading_to_seeds.rb diff --git a/app/models/seed.rb b/app/models/seed.rb index 33c017b8f..bc9038359 100644 --- a/app/models/seed.rb +++ b/app/models/seed.rb @@ -1,5 +1,12 @@ class Seed < ActiveRecord::Base - attr_accessible :owner_id, :crop_id, :description, :quantity, :plant_before + attr_accessible :owner_id, :crop_id, :description, :quantity, :plant_before, + :tradable, :tradable_to belongs_to :crop belongs_to :owner, :class_name => 'Member', :foreign_key => 'owner_id' + + TRADABLE_TO_VALUES = %w(locally nationally internationally) + validates :tradable_to, :inclusion => { :in => TRADABLE_TO_VALUES, + :message => "You may only trade seed locally, nationally, or internationally" }, + :allow_nil => true, + :allow_blank => true end diff --git a/db/migrate/20130718011247_add_trading_to_seeds.rb b/db/migrate/20130718011247_add_trading_to_seeds.rb new file mode 100644 index 000000000..2ed459577 --- /dev/null +++ b/db/migrate/20130718011247_add_trading_to_seeds.rb @@ -0,0 +1,6 @@ +class AddTradingToSeeds < ActiveRecord::Migration + def change + add_column :seeds, :tradable, :boolean + add_column :seeds, :tradable_to, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index 3a99425f8..a55296e9f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20130718005600) do +ActiveRecord::Schema.define(:version => 20130718011247) do create_table "account_types", :force => true do |t| t.string "name", :null => false @@ -242,6 +242,8 @@ ActiveRecord::Schema.define(:version => 20130718005600) do t.date "plant_before" t.datetime "created_at", :null => false t.datetime "updated_at", :null => false + t.boolean "tradable" + t.string "tradable_to" end end diff --git a/spec/models/seed_spec.rb b/spec/models/seed_spec.rb index 46a4c5851..2b7083054 100644 --- a/spec/models/seed_spec.rb +++ b/spec/models/seed_spec.rb @@ -10,4 +10,19 @@ describe Seed do @seed.save.should be_true end + context 'tradable' do + it 'all three valid tradable_to values should work' do + ['locally', 'nationally', 'internationally', nil, ''].each do |t| + @seed = FactoryGirl.build(:seed, :tradable_to => t) + @seed.should be_valid + end + end + + it 'should refuse invalid tradable_to values' do + @seed = FactoryGirl.build(:seed, :tradable_to => 'not valid') + @seed.should_not be_valid + @seed.errors[:tradable_to].should include("You may only trade seed locally, nationally, or internationally") + end + end + end From 21a059011428b50ba0c5c8cb6e0cc9870560a6be Mon Sep 17 00:00:00 2001 From: Ryan Clark Date: Wed, 17 Jul 2013 18:59:32 -0700 Subject: [PATCH 7/9] Add tradable attributes to seed views --- app/views/seeds/_form.html.haml | 13 ++++++++++++- app/views/seeds/index.html.haml | 4 ++++ app/views/seeds/show.html.haml | 6 ++++++ spec/factories/seeds.rb | 5 +++++ spec/views/seeds/edit.html.haml_spec.rb | 2 ++ spec/views/seeds/index.html.haml_spec.rb | 7 +++++++ spec/views/seeds/new.html.haml_spec.rb | 2 ++ spec/views/seeds/show.html.haml_spec.rb | 7 +++++++ 8 files changed, 45 insertions(+), 1 deletion(-) diff --git a/app/views/seeds/_form.html.haml b/app/views/seeds/_form.html.haml index 9c86be1b5..5eeba958b 100644 --- a/app/views/seeds/_form.html.haml +++ b/app/views/seeds/_form.html.haml @@ -17,7 +17,18 @@ .controls = f.number_field :quantity, :class => 'input-small' .control-group - = f.label 'Use by:', :class => 'control-label' + = f.label 'Plant before:', :class => 'control-label' .controls= f.text_field :plant_before, :value => @seed.plant_before ? @seed.plant_before.to_s(:ymd) : '', :class => 'add-datepicker' + .control-group + = f.label 'Willing to trade:', :class => 'control-label' + .controls= f.check_box :tradable + .control-group + = f.label 'Will trade:', :class => 'control-label' + .controls + = f.select(:tradable_to, Seed::TRADABLE_TO_VALUES, {:include_blank => true}) + %span.help_inline + Don't forget to + =succeed "." do + =link_to "set your location", edit_member_registration_path .form-actions = f.submit 'Save', :class => 'btn btn-primary' diff --git a/app/views/seeds/index.html.haml b/app/views/seeds/index.html.haml index 23ab923aa..8f7e1d5fd 100644 --- a/app/views/seeds/index.html.haml +++ b/app/views/seeds/index.html.haml @@ -18,6 +18,8 @@ %th Description %th Quantity %th Plant before + %th Willing to trade + %th Will trade %th - @seeds.each do |seed| @@ -27,6 +29,8 @@ %td= seed.description %td= seed.quantity %td= seed.plant_before + %td= seed.tradable ? "Yes" : "" + %td= seed.tradable_to %td= link_to 'Details', seed, :class => 'btn btn-mini' - else %p There are no seeds to display. diff --git a/app/views/seeds/show.html.haml b/app/views/seeds/show.html.haml index f072898e6..5abd1b939 100644 --- a/app/views/seeds/show.html.haml +++ b/app/views/seeds/show.html.haml @@ -8,6 +8,12 @@ %p %b Plant before: = @seed.plant_before.to_s + %p + %b Tradable: + = @seed.tradable ? "Yes" : "No" + %p + %b Will trade to: + = @seed.tradable_to ? @seed.tradable_to : "nowhere" %p %b Description: diff --git a/spec/factories/seeds.rb b/spec/factories/seeds.rb index 467f01ef3..ebafafe90 100644 --- a/spec/factories/seeds.rb +++ b/spec/factories/seeds.rb @@ -7,5 +7,10 @@ FactoryGirl.define do description "MyText" quantity 1 plant_before "2013-07-15" + + factory :tradable_seed do + tradable true + tradable_to "locally" + end end end diff --git a/spec/views/seeds/edit.html.haml_spec.rb b/spec/views/seeds/edit.html.haml_spec.rb index 3d0ce0ab5..d538ab57c 100644 --- a/spec/views/seeds/edit.html.haml_spec.rb +++ b/spec/views/seeds/edit.html.haml_spec.rb @@ -16,6 +16,8 @@ describe "seeds/edit" do assert_select "select#seed_crop_id", :name => "seed[crop_id]" assert_select "textarea#seed_description", :name => "seed[description]" assert_select "input#seed_quantity", :name => "seed[quantity]" + assert_select "input#seed_tradable", :name => "seed[tradable]" + assert_select "select#seed_tradable_to", :name => "seed[tradable_to]" end end end diff --git a/spec/views/seeds/index.html.haml_spec.rb b/spec/views/seeds/index.html.haml_spec.rb index 8979d0291..df921587c 100644 --- a/spec/views/seeds/index.html.haml_spec.rb +++ b/spec/views/seeds/index.html.haml_spec.rb @@ -15,4 +15,11 @@ describe "seeds/index" do assert_select "tr>td", :text => @seed1.owner.login_name, :count => 2 assert_select "tr>td", :text => @seed1.quantity.to_s, :count => 2 end + + it "shows tradable seeds" do + @seed1 = FactoryGirl.create(:tradable_seed) + assign(:seeds, [@seed1, @seed1]) + render + assert_select "tr>td", :text => @seed1.tradable_to, :count => 2 + end end diff --git a/spec/views/seeds/new.html.haml_spec.rb b/spec/views/seeds/new.html.haml_spec.rb index c291a9956..b3769d1da 100644 --- a/spec/views/seeds/new.html.haml_spec.rb +++ b/spec/views/seeds/new.html.haml_spec.rb @@ -16,6 +16,8 @@ describe "seeds/new" do assert_select "select#seed_crop_id", :name => "seed[crop_id]" assert_select "textarea#seed_description", :name => "seed[description]" assert_select "input#seed_quantity", :name => "seed[quantity]" + assert_select "input#seed_tradable", :name => "seed[tradable]" + assert_select "select#seed_tradable_to", :name => "seed[tradable_to]" end end end diff --git a/spec/views/seeds/show.html.haml_spec.rb b/spec/views/seeds/show.html.haml_spec.rb index c31d14b79..4f3b3c837 100644 --- a/spec/views/seeds/show.html.haml_spec.rb +++ b/spec/views/seeds/show.html.haml_spec.rb @@ -13,4 +13,11 @@ describe "seeds/show" do render rendered.should contain @seed1.crop.system_name end + + it "shows tradable attributes" do + assign(:seed, FactoryGirl.create(:tradable_seed) + render + rendered.should contain "Tradable: Yes" + rendered.should contain "Will trade to: locally" + end end From 269162c759139a02a86843c147da440943786467 Mon Sep 17 00:00:00 2001 From: Ryan Clark Date: Wed, 17 Jul 2013 19:37:35 -0700 Subject: [PATCH 8/9] Tidied up seed views and added location stuff --- app/views/seeds/index.html.haml | 4 +++ app/views/seeds/show.html.haml | 21 ++++++++++++- spec/helpers/seeds_helper_spec.rb | 15 --------- spec/views/seeds/index.html.haml_spec.rb | 20 +++++++++--- spec/views/seeds/show.html.haml_spec.rb | 39 +++++++++++++++++------- 5 files changed, 67 insertions(+), 32 deletions(-) delete mode 100644 spec/helpers/seeds_helper_spec.rb diff --git a/app/views/seeds/index.html.haml b/app/views/seeds/index.html.haml index 8f7e1d5fd..b760cfe68 100644 --- a/app/views/seeds/index.html.haml +++ b/app/views/seeds/index.html.haml @@ -20,6 +20,7 @@ %th Plant before %th Willing to trade %th Will trade + %th From location %th - @seeds.each do |seed| @@ -31,6 +32,9 @@ %td= seed.plant_before %td= seed.tradable ? "Yes" : "" %td= seed.tradable_to + %td + - if seed.tradable_to + = seed.owner.location.blank? ? "unspecified" : seed.owner.location %td= link_to 'Details', seed, :class => 'btn btn-mini' - else %p There are no seeds to display. diff --git a/app/views/seeds/show.html.haml b/app/views/seeds/show.html.haml index 5abd1b939..2d04932ba 100644 --- a/app/views/seeds/show.html.haml +++ b/app/views/seeds/show.html.haml @@ -2,6 +2,9 @@ .row .span6 + %p + %b Owner: + = link_to @seed.owner, @seed.owner %p %b Quantity: = @seed.quantity.blank? ? "not specified" : @seed.quantity @@ -14,12 +17,28 @@ %p %b Will trade to: = @seed.tradable_to ? @seed.tradable_to : "nowhere" - + - if @seed.tradable + -if @seed.owner.location.blank? + (from unspecified location) + - if current_member == @seed.owner + = link_to "Set Location", edit_registration_path(current_member), :class => 'btn btn-mini' + - else + (from + = succeed ")" do + =@seed.owner.location + %p %b Description: :markdown #{ @seed.description != "" ? @seed.description : "No description given." } + - if current_member + - if @seed.tradable && current_member != @seed.owner + %p + = link_to "Request seeds", new_notification_path(:recipient_id => @seed.owner.id, :subject => "Interested in your #{@seed.crop} seeds"), :class => 'btn btn-primary' + - else + = render :partial => 'shared/signin_signup', :locals => { :to => 'request seeds' } + - if can? :edit, @seed or can? :destroy, @seed %p - if can? :edit, @seed diff --git a/spec/helpers/seeds_helper_spec.rb b/spec/helpers/seeds_helper_spec.rb deleted file mode 100644 index ad9a2022a..000000000 --- a/spec/helpers/seeds_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'spec_helper' - -# Specs in this file have access to a helper object that includes -# the SeedsHelper. For example: -# -# describe SeedsHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# helper.concat_strings("this","that").should == "this that" -# end -# end -# end -describe SeedsHelper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/views/seeds/index.html.haml_spec.rb b/spec/views/seeds/index.html.haml_spec.rb index df921587c..a0ed699c9 100644 --- a/spec/views/seeds/index.html.haml_spec.rb +++ b/spec/views/seeds/index.html.haml_spec.rb @@ -16,10 +16,20 @@ describe "seeds/index" do assert_select "tr>td", :text => @seed1.quantity.to_s, :count => 2 end - it "shows tradable seeds" do - @seed1 = FactoryGirl.create(:tradable_seed) - assign(:seeds, [@seed1, @seed1]) - render - assert_select "tr>td", :text => @seed1.tradable_to, :count => 2 + context "tradable" do + before(:each) do + @owner = FactoryGirl.create(:london_member) + @seed1 = FactoryGirl.create(:tradable_seed, :owner => @owner) + assign(:seeds, [@seed1, @seed1]) + render + end + + it "shows tradable seeds" do + assert_select "tr>td", :text => @seed1.tradable_to, :count => 2 + end + + it "shows location of seed owner" do + assert_select "tr>td", :text => @owner.location, :count => 2 + end end end diff --git a/spec/views/seeds/show.html.haml_spec.rb b/spec/views/seeds/show.html.haml_spec.rb index 4f3b3c837..41860be43 100644 --- a/spec/views/seeds/show.html.haml_spec.rb +++ b/spec/views/seeds/show.html.haml_spec.rb @@ -2,22 +2,39 @@ require 'spec_helper' describe "seeds/show" do before(:each) do - @member = FactoryGirl.create(:member) - sign_in @member - controller.stub(:current_user) { @member } - @seed1 = FactoryGirl.create(:seed, :owner => @member) - assign(:seed, @seed1) + controller.stub(:current_user) { nil } + @seed = FactoryGirl.create(:seed) + assign(:seed, @seed) end it "renders attributes in

" do render - rendered.should contain @seed1.crop.system_name + rendered.should contain @seed.crop.system_name end - it "shows tradable attributes" do - assign(:seed, FactoryGirl.create(:tradable_seed) - render - rendered.should contain "Tradable: Yes" - rendered.should contain "Will trade to: locally" + context "tradable" do + before(:each) do + @owner = FactoryGirl.create(:london_member) + assign(:seed, FactoryGirl.create(:tradable_seed, + :owner => @owner)) + # note current_member is not the owner of this seed + @member = FactoryGirl.create(:member) + sign_in @member + controller.stub(:current_user) { @member } + render + end + + it "shows tradable attributes" do + rendered.should contain "Tradable: Yes" + rendered.should contain "Will trade to: locally" + end + + it "shows location of seed owner" do + rendered.should contain @owner.location + end + + it "shows button to send message" do + rendered.should contain "Request seeds" + end end end From 610b100f7fb9580ba1ec226da3bdf75e20d7881c Mon Sep 17 00:00:00 2001 From: Skud Date: Thu, 18 Jul 2013 23:54:51 +1000 Subject: [PATCH 9/9] seed page: cleaned up views/tests for location --- app/views/seeds/_form.html.haml | 12 +++++++--- app/views/seeds/show.html.haml | 8 +++---- spec/views/seeds/new.html.haml_spec.rb | 28 +++++++++++++++++++++++- spec/views/seeds/show.html.haml_spec.rb | 29 ++++++++++++++++++++++--- 4 files changed, 66 insertions(+), 11 deletions(-) diff --git a/app/views/seeds/_form.html.haml b/app/views/seeds/_form.html.haml index 5eeba958b..001a40668 100644 --- a/app/views/seeds/_form.html.haml +++ b/app/views/seeds/_form.html.haml @@ -27,8 +27,14 @@ .controls = f.select(:tradable_to, Seed::TRADABLE_TO_VALUES, {:include_blank => true}) %span.help_inline - Don't forget to - =succeed "." do - =link_to "set your location", edit_member_registration_path + - if current_member.location.blank? + Don't forget to + =succeed "." do + =link_to "set your location", edit_member_registration_path + - else + from + =succeed "." do + = current_member.location + =link_to "Change your location.", edit_member_registration_path .form-actions = f.submit 'Save', :class => 'btn btn-primary' diff --git a/app/views/seeds/show.html.haml b/app/views/seeds/show.html.haml index 2d04932ba..18a7da6d9 100644 --- a/app/views/seeds/show.html.haml +++ b/app/views/seeds/show.html.haml @@ -17,16 +17,16 @@ %p %b Will trade to: = @seed.tradable_to ? @seed.tradable_to : "nowhere" - - if @seed.tradable - -if @seed.owner.location.blank? + - if @seed.tradable + - if @seed.owner.location.blank? (from unspecified location) - if current_member == @seed.owner = link_to "Set Location", edit_registration_path(current_member), :class => 'btn btn-mini' - else - (from + (from = succeed ")" do =@seed.owner.location - + %p %b Description: :markdown diff --git a/spec/views/seeds/new.html.haml_spec.rb b/spec/views/seeds/new.html.haml_spec.rb index b3769d1da..876119bbf 100644 --- a/spec/views/seeds/new.html.haml_spec.rb +++ b/spec/views/seeds/new.html.haml_spec.rb @@ -11,7 +11,6 @@ describe "seeds/new" do it "renders new seed form" do render - assert_select "form", :action => seeds_path, :method => "post" do assert_select "select#seed_crop_id", :name => "seed[crop_id]" assert_select "textarea#seed_description", :name => "seed[description]" @@ -20,4 +19,31 @@ describe "seeds/new" do assert_select "select#seed_tradable_to", :name => "seed[tradable_to]" end end + + it 'reminds you to set your location' do + render + rendered.should contain "Don't forget to set your location." + assert_select "a", :text => "set your location" + end + + context 'member has location' do + before(:each) do + @member = FactoryGirl.create(:london_member) + sign_in @member + controller.stub(:current_user) { @member } + @seed1 = FactoryGirl.create(:seed, :owner => @member) + assign(:seed, @seed1) + end + + it 'shows the location' do + render + rendered.should contain "from #{@member.location}." + end + + it 'links to change location' do + render + assert_select "a", :text => "Change your location." + end + end + end diff --git a/spec/views/seeds/show.html.haml_spec.rb b/spec/views/seeds/show.html.haml_spec.rb index 41860be43..de59664ce 100644 --- a/spec/views/seeds/show.html.haml_spec.rb +++ b/spec/views/seeds/show.html.haml_spec.rb @@ -15,25 +15,48 @@ describe "seeds/show" do context "tradable" do before(:each) do @owner = FactoryGirl.create(:london_member) - assign(:seed, FactoryGirl.create(:tradable_seed, + assign(:seed, FactoryGirl.create(:tradable_seed, :owner => @owner)) # note current_member is not the owner of this seed @member = FactoryGirl.create(:member) sign_in @member controller.stub(:current_user) { @member } - render end it "shows tradable attributes" do + render rendered.should contain "Tradable: Yes" rendered.should contain "Will trade to: locally" end - it "shows location of seed owner" do + it "shows location of seed owner" do + render rendered.should contain @owner.location end + context 'with no location' do + before(:each) do + @owner = FactoryGirl.create(:member) # no location + sign_in @owner + controller.stub(:current_user) { @owner } + assign(:seed, FactoryGirl.create(:tradable_seed, :owner => @owner)) + end + + it 'says "from unspecified location"' do + render + rendered.should contain "(from unspecified location)" + end + + it "links to profile to set location" do + render + assert_select "a[href=#{url_for(edit_member_registration_path)}]", :text => "Set Location" + + end + + end + it "shows button to send message" do + render rendered.should contain "Request seeds" end end