From 1081131246c17c37dbf65c5aa1898f3277087839 Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 25 Feb 2013 20:44:47 +1100 Subject: [PATCH 1/8] Prettifying the plantings page. Added a couple of tiny methods on Crop, and delegated them across to Planting, to easily display the default/first scientific name, and to show the number of times a given crop's been planted. Sadly, the tests randomly fail on my box saying "No route matches {:controller=>"plantings", :action=>"show"}" which is just nonsense. I have no idea why it's doing that, so I'm committing it anyway, in the hopes that either someone can tell me what's up, or else it's just a glitch on my dev box. --- app/models/crop.rb | 13 ++++++++ app/models/planting.rb | 5 +++ app/views/plantings/show.html.haml | 50 +++++++++++++++++++++++------- spec/models/crop_spec.rb | 14 +++++++++ 4 files changed, 71 insertions(+), 11 deletions(-) diff --git a/app/models/crop.rb b/app/models/crop.rb index 73be923aa..7190e048b 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -14,4 +14,17 @@ class Crop < ActiveRecord::Base def to_s return system_name end + + def default_scientific_name + if scientific_names.count > 0 + return scientific_names.first.scientific_name + else + return nil + end + end + + def plantings_count + return plantings.count + end + end diff --git a/app/models/planting.rb b/app/models/planting.rb index 9971e19d1..c2840dea8 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -7,6 +7,11 @@ class Planting < ActiveRecord::Base belongs_to :garden belongs_to :crop + delegate :default_scientific_name, + :plantings_count, + :to => :crop, + :prefix => true + def planting_slug "#{owner.login_name}-#{garden.name}-#{crop.system_name}".downcase.gsub(' ', '-') end diff --git a/app/views/plantings/show.html.haml b/app/views/plantings/show.html.haml index 485b54d13..fc4187c17 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -2,16 +2,44 @@ %p#notice= notice -%p - %b Planted at: - = @planting.planted_at ? @planting.planted_at : "not specified" -%p - %b Quantity: - = @planting.quantity != 0 ? @planting.quantity : "not specified" -%p - %b Description: - :markdown - #{ @planting.description != "" ? @planting.description : "No description." } +.row + .span6 + %h3= @planting.crop + %p + %b Planted at: + = @planting.planted_at ? @planting.planted_at : "not specified" + - if can? :edit, @planting + %small + = link_to 'Edit', edit_planting_path(@planting) + %p + %b Quantity: + = @planting.quantity != 0 ? @planting.quantity : "not specified" + - if can? :edit, @planting + %small + = link_to 'Edit', edit_planting_path(@planting) + .span6 + .well + %h3= link_to @planting.crop + %p + %b Scientific name: + = @planting.crop_default_scientific_name + %p + %b + Planted + = pluralize(@planting.crop_plantings_count, "time") + by Growstuff members + +%h2 Pictures +.row + - (1..6).each do + .span2 + = image_tag('http://placehold.it/150x150', :alt => '', :class => 'img-rounded') + +%h2 + Notes + +:markdown + #{ @planting.description != "" ? @planting.description : "No description given." } - if can? :edit, @planting - = link_to 'Edit', edit_planting_path(@planting), :class => 'btn' + = link_to 'Add a description', edit_planting_path(@planting) diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index 4716526ac..8e336d2ba 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -50,4 +50,18 @@ describe Crop do Crop.first.should == lowercase end end + + it 'finds a default scientific name' do + @c = FactoryGirl.create(:tomato) + @c.default_scientific_name.should eq nil + @sn = FactoryGirl.create(:solanum_lycopersicum, :crop => @c) + @c.default_scientific_name.should eq @sn.scientific_name + end + + it 'counts plantings' do + @c = FactoryGirl.create(:tomato) + @c.plantings_count.should eq 0 + FactoryGirl.create(:planting, :crop => @c) + @c.plantings_count.should eq 1 + end end From 85e37aa3c9b529131dee917aca44b6f02e2a0c34 Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 25 Feb 2013 21:01:02 +1100 Subject: [PATCH 2/8] Minor tweaks to plantings page. Tests still broken for me. WTF is this even? --- app/views/plantings/show.html.haml | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/views/plantings/show.html.haml b/app/views/plantings/show.html.haml index fc4187c17..0686c807b 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -1,13 +1,11 @@ =content_for :title, "#{@planting.crop} in #{@planting.location}" -%p#notice= notice - .row .span6 %h3= @planting.crop %p - %b Planted at: - = @planting.planted_at ? @planting.planted_at : "not specified" + %b Planted: + = @planting.planted_at ? @planting.planted_at.to_s(:date) : "not specified" - if can? :edit, @planting %small = link_to 'Edit', edit_planting_path(@planting) @@ -19,7 +17,11 @@ = link_to 'Edit', edit_planting_path(@planting) .span6 .well - %h3= link_to @planting.crop + %h3 + = link_to @planting.crop + - if can? :create, Planting + = link_to 'Plant this', new_planting_path, :class => 'btn' + %p %b Scientific name: = @planting.crop_default_scientific_name @@ -42,4 +44,4 @@ #{ @planting.description != "" ? @planting.description : "No description given." } - if can? :edit, @planting - = link_to 'Add a description', edit_planting_path(@planting) + = link_to 'Edit', edit_planting_path(@planting) From cc64139a4d897fba2e5d49a2812c8b927862de90 Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 25 Feb 2013 21:47:57 +1100 Subject: [PATCH 3/8] added 'where' to planting info (incl. location if known) --- app/views/plantings/show.html.haml | 7 ++++++- db/schema.rb | 11 +++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/app/views/plantings/show.html.haml b/app/views/plantings/show.html.haml index 0686c807b..8077af9ea 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -2,13 +2,18 @@ .row .span6 - %h3= @planting.crop %p %b Planted: = @planting.planted_at ? @planting.planted_at.to_s(:date) : "not specified" - if can? :edit, @planting %small = link_to 'Edit', edit_planting_path(@planting) + %p + %b Where: + =link_to "#{@planting.owner}'s", @planting.owner + = link_to @planting.garden.name + - if defined?(@planting.owner.location) + = "(#{@planting.owner.location})" %p %b Quantity: = @planting.quantity != 0 ? @planting.quantity : "not specified" diff --git a/db/schema.rb b/db/schema.rb index 2f252f73e..7633949d3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -45,7 +45,7 @@ ActiveRecord::Schema.define(:version => 20130222060730) do create_table "gardens", :force => true do |t| t.string "name", :null => false - t.integer "owner_id", :null => false + t.integer "owner_id" t.string "slug", :null => false t.datetime "created_at", :null => false t.datetime "updated_at", :null => false @@ -97,14 +97,13 @@ ActiveRecord::Schema.define(:version => 20130222060730) do create_table "notifications", :force => true do |t| t.integer "sender_id" - t.integer "recipient_id", :null => false + t.integer "recipient_id", :null => false t.string "subject" t.text "body" - t.boolean "read", :default => false - t.integer "notification_type" + t.boolean "read", :default => false t.integer "post_id" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false end create_table "plantings", :force => true do |t| From 97ce62389c416a7707e8bc812db3b9095a5a32f7 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Mon, 25 Feb 2013 14:34:37 +0000 Subject: [PATCH 4/8] Fix one-argument calls to link_to. Fixes broken tests. --- app/views/plantings/show.html.haml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/views/plantings/show.html.haml b/app/views/plantings/show.html.haml index 8077af9ea..7e2cdc63d 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -11,7 +11,7 @@ %p %b Where: =link_to "#{@planting.owner}'s", @planting.owner - = link_to @planting.garden.name + =link_to @planting.garden.name, @planting.garden - if defined?(@planting.owner.location) = "(#{@planting.owner.location})" %p @@ -23,7 +23,7 @@ .span6 .well %h3 - = link_to @planting.crop + = link_to @planting.crop, @planting.crop - if can? :create, Planting = link_to 'Plant this', new_planting_path, :class => 'btn' From d87a1ee4fdd15153537382ddbfa16e3e3d9672a5 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Mon, 25 Feb 2013 14:58:17 +0000 Subject: [PATCH 5/8] Don't show () on plantings/show if no location given. Can someone explain the semantics of nil and defined? to me? --- app/views/plantings/show.html.haml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/views/plantings/show.html.haml b/app/views/plantings/show.html.haml index 7e2cdc63d..1d3e5c431 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -12,7 +12,7 @@ %b Where: =link_to "#{@planting.owner}'s", @planting.owner =link_to @planting.garden.name, @planting.garden - - if defined?(@planting.owner.location) + - if @planting.owner.location = "(#{@planting.owner.location})" %p %b Quantity: From b0ffd85326fa05cb313e7bbe52e90715f77a8ca1 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Mon, 25 Feb 2013 23:07:47 +0000 Subject: [PATCH 6/8] Stringify gardens as their names. --- app/models/garden.rb | 3 +++ spec/models/garden_spec.rb | 4 ++++ 2 files changed, 7 insertions(+) diff --git a/app/models/garden.rb b/app/models/garden.rb index ecacfc17f..8edbbda0b 100644 --- a/app/models/garden.rb +++ b/app/models/garden.rb @@ -27,7 +27,10 @@ class Garden < ActiveRecord::Base end return unique_plantings[0..3] + end + def to_s + name end end diff --git a/spec/models/garden_spec.rb b/spec/models/garden_spec.rb index 4a01f43d3..2527ea7f9 100644 --- a/spec/models/garden_spec.rb +++ b/spec/models/garden_spec.rb @@ -18,6 +18,10 @@ describe Garden do @garden.owner.should be_an_instance_of Member end + it "should stringify as its name" do + @garden.to_s.should == @garden.name + end + context "featured plantings" do before :each do @tomato = FactoryGirl.create(:tomato) From 627c373c4128a8298a74bdb6ddbd6a44bafc4df7 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Tue, 26 Feb 2013 00:05:32 +0000 Subject: [PATCH 7/8] Simplified some code using Garden.to_s. --- app/models/planting.rb | 4 ++-- app/views/gardens/show.html.haml | 6 +++--- app/views/plantings/show.html.haml | 2 +- 3 files changed, 6 insertions(+), 6 deletions(-) diff --git a/app/models/planting.rb b/app/models/planting.rb index c2840dea8..8fa0dad01 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -13,11 +13,11 @@ class Planting < ActiveRecord::Base :prefix => true def planting_slug - "#{owner.login_name}-#{garden.name}-#{crop.system_name}".downcase.gsub(' ', '-') + "#{owner.login_name}-#{garden}-#{crop}".downcase.gsub(' ', '-') end def location - return "#{garden.owner.login_name}'s #{garden.name}" + return "#{garden.owner.login_name}'s #{garden}" end def owner diff --git a/app/views/gardens/show.html.haml b/app/views/gardens/show.html.haml index 6f27ae004..1bd45b7b0 100644 --- a/app/views/gardens/show.html.haml +++ b/app/views/gardens/show.html.haml @@ -1,4 +1,4 @@ -=content_for :title, "#{@garden.owner}'s #{@garden.name}" +=content_for :title, "#{@garden.owner}'s #{@garden}" .row .span3 @@ -9,9 +9,9 @@ - @garden.owner.gardens.each do |othergarden| %li - if @garden == othergarden - = @garden.name + = @garden - else - = link_to "#{othergarden.name}", garden_path(othergarden) + = link_to "#{othergarden}", garden_path(othergarden) - if can? :edit, @garden = link_to 'Edit', edit_garden_path(@garden), :class => 'btn' diff --git a/app/views/plantings/show.html.haml b/app/views/plantings/show.html.haml index 1d3e5c431..d3c7c7d6c 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -11,7 +11,7 @@ %p %b Where: =link_to "#{@planting.owner}'s", @planting.owner - =link_to @planting.garden.name, @planting.garden + =link_to @planting.garden, @planting.garden - if @planting.owner.location = "(#{@planting.owner.location})" %p From 18f91414457193eaa876dd445e61248f57e1f2f8 Mon Sep 17 00:00:00 2001 From: Miles Gould Date: Tue, 26 Feb 2013 00:22:31 +0000 Subject: [PATCH 8/8] Test display of location on plantings/show. As discussed at https://github.com/pozorvlak/growstuff/commit/d87a1ee4fdd15153537382ddbfa16e3e3d9672a5. The version of app/views/plantings/show.html.haml from commit 97ce623 fails the "()" test, as expected. --- spec/views/plantings/show.html.haml_spec.rb | 45 ++++++++++++++++----- 1 file changed, 34 insertions(+), 11 deletions(-) diff --git a/spec/views/plantings/show.html.haml_spec.rb b/spec/views/plantings/show.html.haml_spec.rb index 0b46bd94e..7afb70cda 100644 --- a/spec/views/plantings/show.html.haml_spec.rb +++ b/spec/views/plantings/show.html.haml_spec.rb @@ -1,26 +1,49 @@ require 'spec_helper' describe "plantings/show" do - before(:each) do - controller.stub(:current_user) { nil } - @member = FactoryGirl.create(:member) + def create_planting_for(member) @garden = FactoryGirl.create(:garden, :owner => @member) @crop = FactoryGirl.create(:tomato) @planting = assign(:planting, FactoryGirl.create(:planting, :garden => @garden, :crop => @crop) ) - render end - it "renders the quantity planted" do - rendered.should match(/3/) + context "no location set" do + before(:each) do + controller.stub(:current_user) { nil } + @member = FactoryGirl.create(:member) + create_planting_for(@member) + render + end + + it "renders the quantity planted" do + rendered.should match(/3/) + end + + it "renders the description" do + rendered.should match(/This is a/) + end + + it "renders markdown in the description" do + assert_select "em", "really" + end + + it "doesn't contain a () if no location is set" do + rendered.should_not contain "()" + end end - it "renders the description" do - rendered.should match(/This is a/) - end + context "location set" do + before(:each) do + controller.stub(:current_user) { nil } + @member = FactoryGirl.create(:geolocated_member) + create_planting_for(@member) + render + end - it "renders markdown in the description" do - assert_select "em", "really" + it "shows the member's location in parentheses" do + rendered.should contain "(#{@member.location})" + end end end