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/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/app/models/planting.rb b/app/models/planting.rb index 9971e19d1..8fa0dad01 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -7,12 +7,17 @@ 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(' ', '-') + "#{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 485b54d13..d3c7c7d6c 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -1,17 +1,52 @@ =content_for :title, "#{@planting.crop} in #{@planting.location}" -%p#notice= notice +.row + .span6 + %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, @planting.garden + - if @planting.owner.location + = "(#{@planting.owner.location})" + %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, @planting.crop + - if can? :create, Planting + = link_to 'Plant this', new_planting_path, :class => 'btn' -%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." } + %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 'Edit', edit_planting_path(@planting) 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| 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 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) 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