diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index f9d6bf4c5..75ce5297e 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -28,9 +28,16 @@ class PhotosController < ApplicationController @photo = Photo.new @planting_id = params[:planting_id] + page = params[:page] || 1 + @flickr_auth = current_member.auth('flickr') if @flickr_auth - @photos = current_member.flickr_photos + photos = current_member.flickr_photos(page) + total = photos.instance_of?(FlickRaw::ResponseList) ? photos.total : 0 + + @photos = WillPaginate::Collection.create(page, 30, total) do |pager| + pager.replace photos.to_a + end end respond_to do |format| diff --git a/app/models/member.rb b/app/models/member.rb index f29010ad1..506ce2551 100644 --- a/app/models/member.rb +++ b/app/models/member.rb @@ -148,8 +148,12 @@ class Member < ActiveRecord::Base return @flickr end - def flickr_photos - return flickr.people.getPhotos(:user_id => 'me', :per_page => 30) + def flickr_photos(page_num=1) + return flickr.people.getPhotos( + :user_id => 'me', + :page => page_num, + :per_page => 30 + ) end protected diff --git a/app/models/planting.rb b/app/models/planting.rb index 8bf6dda2c..8d1eb70b9 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -3,7 +3,7 @@ class Planting < ActiveRecord::Base friendly_id :planting_slug, use: :slugged attr_accessible :crop_id, :description, :garden_id, :planted_at, - :quantity, :sunniness + :quantity, :sunniness, :planted_from belongs_to :garden belongs_to :crop @@ -29,6 +29,22 @@ class Planting < ActiveRecord::Base :allow_nil => true, :allow_blank => true + PLANTED_FROM_VALUES = [ + 'seed', + 'seedling', + 'cutting', + 'root division', + 'runner', + 'bare root plant', + 'advanced plant', + 'graft', + 'layering' + ] + validates :planted_from, :inclusion => { :in => PLANTED_FROM_VALUES, + :message => "%{value} is not a valid planting method" }, + :allow_nil => true, + :allow_blank => true + def planting_slug "#{owner.login_name}-#{garden}-#{crop}".downcase.gsub(' ', '-') end diff --git a/app/views/photos/new.html.haml b/app/views/photos/new.html.haml index ca363dc26..811bba956 100644 --- a/app/views/photos/new.html.haml +++ b/app/views/photos/new.html.haml @@ -5,8 +5,12 @@ Connected to Flickr as = succeed "." do = link_to @flickr_auth.name, "http://flickr.com/photos/#{@flickr_auth.uid}" + Please select a photo from your recent uploads. - %p Select a photo from your recent uploads: + + %div.pagination + = page_entries_info @photos, :model => "photos" + = will_paginate @photos - c = 0 %ul.thumbnails diff --git a/app/views/plantings/_form.html.haml b/app/views/plantings/_form.html.haml index b21add0c9..29709d153 100644 --- a/app/views/plantings/_form.html.haml +++ b/app/views/plantings/_form.html.haml @@ -20,6 +20,10 @@ = f.label 'How many?', :class => 'control-label' .controls = f.number_field :quantity, :class => 'input-small' + .control-group + = f.label 'Planted from:', :class => 'control-label' + .controls + = f.select(:planted_from, Planting::PLANTED_FROM_VALUES, {:include_blank => true}) .control-group = f.label 'Sun or shade?', :class => 'control-label' .controls diff --git a/app/views/plantings/show.html.haml b/app/views/plantings/show.html.haml index 9d33ecf17..203325a09 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -13,7 +13,12 @@ = "(#{@planting.owner.location})" %p %b Quantity: - = @planting.quantity != 0 ? @planting.quantity : "not specified" + = @planting.quantity.blank? ? "not specified" : @planting.quantity + + - if ! @planting.planted_from.blank? + %p + %b Planted from: + = @planting.planted_from - if ! @planting.sunniness.blank? %p diff --git a/db/migrate/20130705104238_add_planted_from_to_planting.rb b/db/migrate/20130705104238_add_planted_from_to_planting.rb new file mode 100644 index 000000000..7eb362a85 --- /dev/null +++ b/db/migrate/20130705104238_add_planted_from_to_planting.rb @@ -0,0 +1,5 @@ +class AddPlantedFromToPlanting < ActiveRecord::Migration + def change + add_column :plantings, :planted_from, :string + end +end diff --git a/db/schema.rb b/db/schema.rb index aab2827b1..bf9e10438 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 => 20130606233733) do +ActiveRecord::Schema.define(:version => 20130705104238) do create_table "account_types", :force => true do |t| t.string "name", :null => false @@ -179,15 +179,16 @@ ActiveRecord::Schema.define(:version => 20130606233733) do end create_table "plantings", :force => true do |t| - t.integer "garden_id", :null => false - t.integer "crop_id", :null => false + t.integer "garden_id", :null => false + t.integer "crop_id", :null => false t.date "planted_at" t.integer "quantity" t.text "description" - t.datetime "created_at", :null => false - t.datetime "updated_at", :null => false + t.datetime "created_at", :null => false + t.datetime "updated_at", :null => false t.string "slug" t.string "sunniness" + t.string "planted_from" end add_index "plantings", ["slug"], :name => "index_plantings_on_slug", :unique => true diff --git a/spec/factories/planting.rb b/spec/factories/planting.rb index 06499b4a4..963b7a2b0 100644 --- a/spec/factories/planting.rb +++ b/spec/factories/planting.rb @@ -6,5 +6,6 @@ FactoryGirl.define do quantity 33 description "This is a *really* good plant." sunniness 'sun' + planted_from 'seed' end end diff --git a/spec/models/planting_spec.rb b/spec/models/planting_spec.rb index cfadcf9cd..ca2d4e02d 100644 --- a/spec/models/planting_spec.rb +++ b/spec/models/planting_spec.rb @@ -49,21 +49,44 @@ describe Planting do end end - it 'should have a sunniness value' do - @planting.sunniness.should eq 'sun' - end + context 'sunniness' do + it 'should have a sunniness value' do + @planting.sunniness.should eq 'sun' + end - it 'all three valid sunniness values should work' do - ['sun', 'shade', 'semi-shade', nil, ''].each do |s| - @planting = FactoryGirl.build(:planting, :sunniness => s) - @planting.should be_valid + it 'all three valid sunniness values should work' do + ['sun', 'shade', 'semi-shade', nil, ''].each do |s| + @planting = FactoryGirl.build(:planting, :sunniness => s) + @planting.should be_valid + end + end + + it 'should refuse invalid sunniness values' do + @planting = FactoryGirl.build(:planting, :sunniness => 'not valid') + @planting.should_not be_valid + @planting.errors[:sunniness].should include("not valid is not a valid sunniness value") end end - it 'should refuse invalid sunniness values' do - @planting = FactoryGirl.build(:planting, :sunniness => 'not valid') - @planting.should_not be_valid - @planting.errors[:sunniness].should include("not valid is not a valid sunniness value") + context 'planted from' do + it 'should have a planted_from value' do + @planting.planted_from.should eq 'seed' + end + + it 'all valid planted_from values should work' do + ['seed', 'seedling', 'cutting', 'root division', + 'runner', 'bare root plant', 'advanced plant', + 'graft', 'layering', nil, ''].each do |p| + @planting = FactoryGirl.build(:planting, :planted_from => p) + @planting.should be_valid + end + end + + it 'should refuse invalid planted_from values' do + @planting = FactoryGirl.build(:planting, :planted_from => 'not valid') + @planting.should_not be_valid + @planting.errors[:planted_from].should include("not valid is not a valid planting method") + end end # we decided that all the tests for the planting/photo association would diff --git a/spec/views/photos/new.html.haml_spec.rb b/spec/views/photos/new.html.haml_spec.rb index 1ea5a34d9..9eb15eab7 100644 --- a/spec/views/photos/new.html.haml_spec.rb +++ b/spec/views/photos/new.html.haml_spec.rb @@ -4,7 +4,13 @@ describe "photos/new" do before(:each) do @member = FactoryGirl.create(:member) controller.stub(:current_user) { @member } - assign(:photos, []) + page = 1 + per_page = 2 + total_entries = 2 + photos = WillPaginate::Collection.create(page, per_page, total_entries) do |pager| + pager.replace([]) + end + assign(:photos, photos) assign(:flickr_auth, FactoryGirl.create(:flickr_authentication, :member => @member)) end diff --git a/spec/views/plantings/edit.html.haml_spec.rb b/spec/views/plantings/edit.html.haml_spec.rb index 51f993549..3965c0e03 100644 --- a/spec/views/plantings/edit.html.haml_spec.rb +++ b/spec/views/plantings/edit.html.haml_spec.rb @@ -33,6 +33,8 @@ describe "plantings/edit" do assert_select "form", :action => plantings_path(@planting), :method => "post" do assert_select "input#planting_quantity", :name => "planting[quantity]" assert_select "textarea#planting_description", :name => "planting[description]" + assert_select "select#planting_sunniness", :name => "planting[sunniness]" + assert_select "select#planting_planted_from", :name => "planting[planted_from]" end end diff --git a/spec/views/plantings/new.html.haml_spec.rb b/spec/views/plantings/new.html.haml_spec.rb index a07273297..03aa8fe7f 100644 --- a/spec/views/plantings/new.html.haml_spec.rb +++ b/spec/views/plantings/new.html.haml_spec.rb @@ -34,6 +34,7 @@ describe "plantings/new" do assert_select "input#planting_quantity", :name => "planting[quantity]" assert_select "textarea#planting_description", :name => "planting[description]" assert_select "select#planting_sunniness", :name => "planting[sunniness]" + assert_select "select#planting_planted_from", :name => "planting[planted_from]" end end diff --git a/spec/views/plantings/show.html.haml_spec.rb b/spec/views/plantings/show.html.haml_spec.rb index 71c6c94d1..ddd8db657 100644 --- a/spec/views/plantings/show.html.haml_spec.rb +++ b/spec/views/plantings/show.html.haml_spec.rb @@ -9,30 +9,46 @@ describe "plantings/show" do ) end - it "shows the sunniness" do - controller.stub(:current_user) { nil } - @member = FactoryGirl.create(:member) - create_planting_for(@member) - render - rendered.should contain 'Sun or shade?' - rendered.should contain 'sun' - end - - it "doesn't show sunniness if blank" do - controller.stub(:current_user) { nil } - @member = FactoryGirl.create(:member) - @p = create_planting_for(@member) - @p.sunniness = '' - @p.save - render - rendered.should_not contain 'Sun or shade?' - rendered.should_not contain 'sun' - end - - it "shows photos" do + before (:each) do @member = FactoryGirl.create(:member) controller.stub(:current_user) { @member } @p = create_planting_for(@member) + end + + context 'sunniness' do + + it "shows the sunniness" do + render + rendered.should contain 'Sun or shade?' + rendered.should contain 'sun' + end + + it "doesn't show sunniness if blank" do + @p.sunniness = '' + @p.save + render + rendered.should_not contain 'Sun or shade?' + rendered.should_not contain 'sun' + end + end + + context 'planted from' do + it "shows planted_from" do + render + rendered.should contain 'Planted from:' + rendered.should contain 'seed' + end + + it "doesn't show planted_from if blank" do + @p.planted_from = '' + @p.save + render + rendered.should_not contain 'Planted from:' + rendered.should_not contain 'seed' + end + end + + it "shows photos" do @photo = FactoryGirl.create(:photo, :owner => @member) @p.photos << @photo render @@ -40,18 +56,12 @@ describe "plantings/show" do end it "shows a link to add photos" do - @member = FactoryGirl.create(:member) - controller.stub(:current_user) { @member } - @p = create_planting_for(@member) render rendered.should contain "Add photo" end context "no location set" do before(:each) do - controller.stub(:current_user) { nil } - @member = FactoryGirl.create(:member) - create_planting_for(@member) render end @@ -74,9 +84,8 @@ describe "plantings/show" do context "location set" do before(:each) do - controller.stub(:current_user) { nil } - @member = FactoryGirl.create(:london_member) - create_planting_for(@member) + @member.location = 'Greenwich, UK' + @member.save render end