From 372a7f080d8d2ba0dad25a08b145406f7f0d67d7 Mon Sep 17 00:00:00 2001 From: AELOGICA Date: Fri, 26 Jun 2015 06:54:49 +0800 Subject: [PATCH 01/13] Added the days before maturity feature #678 --- app/controllers/plantings_controller.rb | 11 +++ app/models/planting.rb | 15 +++ .../plantings/_planting_progress.html.haml | 17 ++++ app/views/plantings/index.html.haml | 91 ++++++++++++------- app/views/plantings/show.html.haml | 10 ++ ...5_add_days_before_maturity_to_plantings.rb | 5 + 6 files changed, 118 insertions(+), 31 deletions(-) create mode 100644 app/views/plantings/_planting_progress.html.haml create mode 100644 db/migrate/20150625224805_add_days_before_maturity_to_plantings.rb diff --git a/app/controllers/plantings_controller.rb b/app/controllers/plantings_controller.rb index 9fdefc388..0baf1de36 100644 --- a/app/controllers/plantings_controller.rb +++ b/app/controllers/plantings_controller.rb @@ -67,6 +67,7 @@ class PlantingsController < ApplicationController def create params[:planted_at] = parse_date(params[:planted_at]) @planting = Planting.new(planting_params) + @planting.days_before_maturity = update_days_before_maturity(@planting, planting_params[:crop_id]) @planting.owner = current_member respond_to do |format| @@ -87,6 +88,8 @@ class PlantingsController < ApplicationController @planting = Planting.find(params[:id]) params[:planted_at] = parse_date(params[:planted_at]) + @planting.days_before_maturity = update_days_before_maturity(@planting, planting_params[:crop_id]) + respond_to do |format| if @planting.update(planting_params) format.html { redirect_to @planting, notice: 'Planting was successfully updated.' } @@ -119,4 +122,12 @@ class PlantingsController < ApplicationController :quantity, :sunniness, :planted_from, :owner_id, :finished, :finished_at) end + + def update_days_before_maturity(planting, crop_id) + if planting.finished_at.nil? + planting.calculate_days_before_maturity(crop_id) + else + (planting.finished_at - planting.planted_at).to_i + end + end end diff --git a/app/models/planting.rb b/app/models/planting.rb index ea9faf0d0..abf2ec1e2 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -94,6 +94,21 @@ class Planting < ActiveRecord::Base return photos.present? end + def calculate_days_before_maturity(crop) + p_crop = Planting.where(:crop_id => crop) + differences = p_crop.collect do |p| + if p.finished and !p.finished_at.nil? + (p.finished_at - p.planted_at).to_i + end + end + + if differences.compact.empty? + average = nil + else + average = differences.compact.sum/differences.compact.length + end + end + # return a list of interesting plantings, for the homepage etc. # we can't do this via a scope (as far as we know) so sadly we have to # do it this way. diff --git a/app/views/plantings/_planting_progress.html.haml b/app/views/plantings/_planting_progress.html.haml new file mode 100644 index 000000000..0b23c0066 --- /dev/null +++ b/app/views/plantings/_planting_progress.html.haml @@ -0,0 +1,17 @@ +- if DateTime.now.to_date < planting.planted_at + = "Progress: #{percent=0}% - " + = "not planted yet" + .progress + .progress-bar.progress-bar-warning{"aria-valuemax" => "planting.days_before_maturity", "aria-valuemin" => "0", "aria-valuenow" => "100%", :role => "progressbar", :style => "width: 100%"} +- elsif planting.days_before_maturity.nil? + = "Progress: #{percent=0}% - " + = "Days before maturity unknown" + .progress + .progress-bar.progress-bar-danger{"aria-valuemax" => "planting.days_before_maturity", "aria-valuemin" => "0", "aria-valuenow" => "100%", :role => "progressbar", :style => "width: 100%"} +- else + - if (percent = (((DateTime.now - planting.planted_at)/planting.days_before_maturity*100).to_i)) >= 100 + - percent = 100 + + = "Progress: #{percent}" + "%" + .progress + .progress-bar.progress-bar-success{"aria-valuemax" => "planting.days_before_maturity", "aria-valuemin" => "0", "aria-valuenow" => "#{percent}", :role => "progressbar", :style => "width: #{percent}%"} \ No newline at end of file diff --git a/app/views/plantings/index.html.haml b/app/views/plantings/index.html.haml index 25b52ee2d..f4b2556a4 100644 --- a/app/views/plantings/index.html.haml +++ b/app/views/plantings/index.html.haml @@ -22,40 +22,69 @@ %table.table.table-striped %tr - - unless @owner - %th Owner - %th Crop - %th Garden - %th Quantity - %th Planted on - %th Finished - %th Sun/shade? - %th Planted from - %th - - - @plantings.each do |planting| + %th # + %th Photo + %th{:colspan => "3"} Planting details + + - @plantings.each.with_index do |planting, index| %tr - - unless @owner - %td= link_to planting.owner.login_name, planting.owner - %td= link_to planting.crop.name, planting.crop - %td= link_to planting.garden.name, planting.garden - %td= planting.quantity - %td= planting.planted_at + %td= "##{index+1}" + %td= link_to image_tag((planting.default_photo ? planting.default_photo.thumbnail_url : 'placeholder_150.png'), :alt => planting.crop_id, :class => 'img'), planting %td - - if planting.finished and planting.finished_at - = planting.finished_at - - elsif planting.finished - Yes (no date specified) - %td= planting.sunniness - %td= planting.planted_from + %ul{:style => "list-style-type:none"} + %li Owner : + %li Garden : + %li Planted on : + %li Finished on : + %li Sun/shade? : + %li Planted from : %td - = link_to 'Details', planting, :class => 'btn btn-default btn-xs' - - if can? :edit, planting - =link_to 'Edit', edit_planting_path(planting), :class => 'btn btn-default btn-xs' - - if ! planting.finished - = link_to "Mark as finished", planting_path(planting, :planting => {:finished => 1}), :method => :put, :class => 'btn btn-default btn-xs append-date' - - if can? :destroy, planting - =link_to 'Delete', planting, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-default btn-xs' + %ul{:style => "list-style-type:none"} + %li= link_to planting.owner.login_name, planting.owner + %li= link_to planting.garden.name, planting.garden + %li= planting.planted_at + - if planting.finished and planting.finished_at? + %li= planting.finished_at + - elsif planting.finished + %li= "Yes (no date specified)" + - else + %li= "(no date specified)" + - if planting.sunniness? + %li= planting.sunniness + - else + %li= "n/a" + %li= planting.planted_from + %td + %ul{:style => "list-style-type:none"} + %li= link_to 'Details', planting, :class => 'btn btn-default btn-xs' + - if can? :edit, planting + %li= link_to 'Edit', edit_planting_path(planting), :class => 'btn btn-default btn-xs' + - if can? :destroy, planting + %li= link_to 'Delete', planting, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-default btn-xs' + %tr + %td + %td + %ul{:style => "list-style-type:none"} + %li + Crop name: + =link_to planting.crop.name, planting.crop + - if planting.finished and !planting.finished_at.nil? + - if ((p = planting.finished_at - DateTime.now).to_i) <= 0 + %li= "Days until maturity: 0" + - else + %li= "Days until maturity: #{p.to_i}" + + - elsif planting.days_before_maturity.nil? + %li= "Days until maturity: unknown" + - else + - if ((p = (planting.planted_at + planting.days_before_maturity) - DateTime.now).to_i) <= 0 + %li= "Days until maturity: 0" + - else + %li= "Days until maturity: #{p.to_i}" + + %td{:colspan => "3"} + %ul{:style => "list-style-type:none"} + %li= render 'planting_progress', planting: planting %div.pagination = page_entries_info @plantings, :model => "plantings" diff --git a/app/views/plantings/show.html.haml b/app/views/plantings/show.html.haml index 93cc2a910..73a1cbd72 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -38,6 +38,16 @@ = @planting.finished_at - else Yes (no date specified) + %p + %b Days until maturity: + - if @planting.finished and @planting.finished_at? + = "#{(@planting.finished_at - DateTime.now).to_i}" + - elsif @planting.days_before_maturity.nil? + = "unknown" + - else + = "#{((@planting.planted_at + @planting.days_before_maturity) - DateTime.now).to_i}" + %p + %b= render 'planting_progress', planting: @planting diff --git a/db/migrate/20150625224805_add_days_before_maturity_to_plantings.rb b/db/migrate/20150625224805_add_days_before_maturity_to_plantings.rb new file mode 100644 index 000000000..0ddf5acb5 --- /dev/null +++ b/db/migrate/20150625224805_add_days_before_maturity_to_plantings.rb @@ -0,0 +1,5 @@ +class AddDaysBeforeMaturityToPlantings < ActiveRecord::Migration + def change + add_column :plantings, :days_before_maturity, :integer + end +end From 3181c97a2a8e04ddc1319c07942f5eee0ac6d4cc Mon Sep 17 00:00:00 2001 From: AELOGICA Date: Fri, 26 Jun 2015 11:22:57 +0800 Subject: [PATCH 02/13] Added the mark as finished button on the index page. --- app/views/plantings/index.html.haml | 2 ++ db/schema.rb | 9 +++++---- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/views/plantings/index.html.haml b/app/views/plantings/index.html.haml index f4b2556a4..e6f386a15 100644 --- a/app/views/plantings/index.html.haml +++ b/app/views/plantings/index.html.haml @@ -59,6 +59,8 @@ %li= link_to 'Details', planting, :class => 'btn btn-default btn-xs' - if can? :edit, planting %li= link_to 'Edit', edit_planting_path(planting), :class => 'btn btn-default btn-xs' + - if ! planting.finished + %li= link_to "Mark as finished", planting_path(planting, :planting => {:finished => 1}), :method => :put, :class => 'btn btn-default btn-xs append-date' - if can? :destroy, planting %li= link_to 'Delete', planting, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-default btn-xs' %tr diff --git a/db/schema.rb b/db/schema.rb index 7f8c479ce..be45fa333 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20150209105410) do +ActiveRecord::Schema.define(version: 20150625224805) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -380,8 +380,8 @@ ActiveRecord::Schema.define(version: 20150209105410) 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" @@ -391,8 +391,9 @@ ActiveRecord::Schema.define(version: 20150209105410) do t.string "sunniness" t.string "planted_from" t.integer "owner_id" - t.boolean "finished", default: false + t.boolean "finished", default: false t.date "finished_at" + t.integer "days_before_maturity" end add_index "plantings", ["slug"], name: "index_plantings_on_slug", unique: true, using: :btree From 4ca4d6b030ed110f030b556c1d08d5c52f41e4f8 Mon Sep 17 00:00:00 2001 From: AELOGICA Date: Wed, 1 Jul 2015 09:35:13 +0800 Subject: [PATCH 03/13] Fixed error where progress bars do not change upon update --- app/controllers/plantings_controller.rb | 7 +++---- app/models/planting.rb | 8 ++++---- app/views/plantings/index.html.haml | 4 ++-- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/app/controllers/plantings_controller.rb b/app/controllers/plantings_controller.rb index 0baf1de36..bba983b6c 100644 --- a/app/controllers/plantings_controller.rb +++ b/app/controllers/plantings_controller.rb @@ -67,11 +67,11 @@ class PlantingsController < ApplicationController def create params[:planted_at] = parse_date(params[:planted_at]) @planting = Planting.new(planting_params) - @planting.days_before_maturity = update_days_before_maturity(@planting, planting_params[:crop_id]) @planting.owner = current_member respond_to do |format| if @planting.save + @planting.update_attribute(:days_before_maturity, update_days_before_maturity(@planting, planting_params[:crop_id])) format.html { redirect_to @planting, notice: 'Planting was successfully created.' } format.json { render json: @planting, status: :created, location: @planting } expire_fragment("homepage_stats") @@ -88,10 +88,9 @@ class PlantingsController < ApplicationController @planting = Planting.find(params[:id]) params[:planted_at] = parse_date(params[:planted_at]) - @planting.days_before_maturity = update_days_before_maturity(@planting, planting_params[:crop_id]) - respond_to do |format| if @planting.update(planting_params) + @planting.update_attribute(:days_before_maturity, update_days_before_maturity(@planting, planting_params[:crop_id])) format.html { redirect_to @planting, notice: 'Planting was successfully updated.' } format.json { head :no_content } else @@ -125,7 +124,7 @@ class PlantingsController < ApplicationController def update_days_before_maturity(planting, crop_id) if planting.finished_at.nil? - planting.calculate_days_before_maturity(crop_id) + planting.calculate_days_before_maturity(planting, crop_id) else (planting.finished_at - planting.planted_at).to_i end diff --git a/app/models/planting.rb b/app/models/planting.rb index abf2ec1e2..f06ccf9ac 100644 --- a/app/models/planting.rb +++ b/app/models/planting.rb @@ -94,8 +94,8 @@ class Planting < ActiveRecord::Base return photos.present? end - def calculate_days_before_maturity(crop) - p_crop = Planting.where(:crop_id => crop) + def calculate_days_before_maturity(planting, crop) + p_crop = Planting.where(:crop_id => crop).where.not(:id => planting) differences = p_crop.collect do |p| if p.finished and !p.finished_at.nil? (p.finished_at - p.planted_at).to_i @@ -103,9 +103,9 @@ class Planting < ActiveRecord::Base end if differences.compact.empty? - average = nil + nil else - average = differences.compact.sum/differences.compact.length + differences.compact.sum/differences.compact.length end end diff --git a/app/views/plantings/index.html.haml b/app/views/plantings/index.html.haml index e6f386a15..0b6c3d7f0 100644 --- a/app/views/plantings/index.html.haml +++ b/app/views/plantings/index.html.haml @@ -59,8 +59,8 @@ %li= link_to 'Details', planting, :class => 'btn btn-default btn-xs' - if can? :edit, planting %li= link_to 'Edit', edit_planting_path(planting), :class => 'btn btn-default btn-xs' - - if ! planting.finished - %li= link_to "Mark as finished", planting_path(planting, :planting => {:finished => 1}), :method => :put, :class => 'btn btn-default btn-xs append-date' + - if ! planting.finished + %li= link_to "Mark as finished", planting_path(planting, :planting => {:finished => 1}), :method => :put, :class => 'btn btn-default btn-xs append-date' - if can? :destroy, planting %li= link_to 'Delete', planting, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-default btn-xs' %tr From b01385a1e48ca6cc374c4b21ee8c92a308382a02 Mon Sep 17 00:00:00 2001 From: AELOGICA Date: Wed, 1 Jul 2015 12:00:31 +0800 Subject: [PATCH 04/13] Added some tests on the days before maturity feature --- .../plantings/planting_a_crop_spec.rb | 31 +++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/spec/features/plantings/planting_a_crop_spec.rb b/spec/features/plantings/planting_a_crop_spec.rb index 1becf895d..7be31f033 100644 --- a/spec/features/plantings/planting_a_crop_spec.rb +++ b/spec/features/plantings/planting_a_crop_spec.rb @@ -27,6 +27,25 @@ feature "Planting a crop", :js => true do end expect(page).to have_content "Planting was successfully created" + expect(page).to have_content "Progress: 0% - Days before maturity unknown" + end + + scenario "Creating a new planting with known days before maturity" do + fill_autocomplete "crop", :with => "mai" + select_from_autocomplete "maize" + within "form#new_planting" do + fill_in "When", :with => "2015-06-15" + fill_in "How many?", :with => 42 + select "cutting", :from => "Planted from:" + select "semi-shade", :from => "Sun or shade?" + check "finished" + fill_in "Finished date", :with => "2015-06-25" + fill_in "Tell us more about it", :with => "It's rad." + click_button "Save" + end + + expect(page).to have_content "Planting was successfully created" + expect(page).to_not have_content "Progress: 0% - Days before maturity unknown" end scenario "Planting from crop page" do @@ -49,6 +68,17 @@ feature "Planting a crop", :js => true do expect(page).to have_content "Planting was successfully updated" end + scenario "Editing a planting to fill in the finished date" do + visit planting_path(planting) + expect(page).to have_content "Progress: 0% - Days before maturity unknown" + click_link "Edit" + check "finished" + fill_in "Finished date", :with => "2015-06-25" + click_button "Save" + expect(page).to have_content "Planting was successfully updated" + expect(page).to_not have_content "Progress: 0% - Days before maturity unknown" + end + scenario "Marking a planting as finished" do fill_autocomplete "crop", :with => "mai" select_from_autocomplete "maize" @@ -94,6 +124,7 @@ feature "Planting a crop", :js => true do end expect(page).to have_content "Planting was successfully created" expect(page).to have_content "Finished: Yes (no date specified)" + expect(page).to have_content "Progress: 0% - Days before maturity unknown" end describe "Marking a planting as finished from the show page" do From f13a66391cf1d32e242ee19685f324066c5c0782 Mon Sep 17 00:00:00 2001 From: AELOGICA Date: Wed, 1 Jul 2015 13:13:30 +0800 Subject: [PATCH 05/13] Fixed errors where progress bars does not show 100% when a planting is marked as finished. --- app/views/plantings/_planting_progress.html.haml | 13 +++++++------ app/views/plantings/index.html.haml | 14 +++++++------- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/app/views/plantings/_planting_progress.html.haml b/app/views/plantings/_planting_progress.html.haml index 0b23c0066..4c40724a1 100644 --- a/app/views/plantings/_planting_progress.html.haml +++ b/app/views/plantings/_planting_progress.html.haml @@ -1,17 +1,18 @@ - if DateTime.now.to_date < planting.planted_at - = "Progress: #{percent=0}% - " - = "not planted yet" + = "Progress: 0% - not planted yet" .progress .progress-bar.progress-bar-warning{"aria-valuemax" => "planting.days_before_maturity", "aria-valuemin" => "0", "aria-valuenow" => "100%", :role => "progressbar", :style => "width: 100%"} +- elsif planting.finished? + = "Progress: 100%" + .progress + .progress-bar.progress-bar-success{"aria-valuemax" => "planting.days_before_maturity", "aria-valuemin" => "0", "aria-valuenow" => "100", :role => "progressbar", :style => "width: 100%"} - elsif planting.days_before_maturity.nil? - = "Progress: #{percent=0}% - " - = "Days before maturity unknown" + = "Progress: 0% - Days before maturity unknown" .progress .progress-bar.progress-bar-danger{"aria-valuemax" => "planting.days_before_maturity", "aria-valuemin" => "0", "aria-valuenow" => "100%", :role => "progressbar", :style => "width: 100%"} - else - if (percent = (((DateTime.now - planting.planted_at)/planting.days_before_maturity*100).to_i)) >= 100 - percent = 100 - - = "Progress: #{percent}" + "%" + = "Progress: #{percent}%" .progress .progress-bar.progress-bar-success{"aria-valuemax" => "planting.days_before_maturity", "aria-valuemin" => "0", "aria-valuenow" => "#{percent}", :role => "progressbar", :style => "width: #{percent}%"} \ No newline at end of file diff --git a/app/views/plantings/index.html.haml b/app/views/plantings/index.html.haml index 0b6c3d7f0..3e86f5395 100644 --- a/app/views/plantings/index.html.haml +++ b/app/views/plantings/index.html.haml @@ -70,19 +70,19 @@ %li Crop name: =link_to planting.crop.name, planting.crop - - if planting.finished and !planting.finished_at.nil? + - if planting.finished? + %li= "Days until maturity: 0" + - elsif !planting.finished_at.nil? - if ((p = planting.finished_at - DateTime.now).to_i) <= 0 - %li= "Days until maturity: 0" - - else - %li= "Days until maturity: #{p.to_i}" + p = 0 + %li= "Days until maturity: #{p.to_i}" - elsif planting.days_before_maturity.nil? %li= "Days until maturity: unknown" - else - if ((p = (planting.planted_at + planting.days_before_maturity) - DateTime.now).to_i) <= 0 - %li= "Days until maturity: 0" - - else - %li= "Days until maturity: #{p.to_i}" + p = 0 + %li= "Days until maturity: #{p.to_i}" %td{:colspan => "3"} %ul{:style => "list-style-type:none"} From 516274b2b7e685895adeae9fdace091e58ca7bf7 Mon Sep 17 00:00:00 2001 From: AELOGICA Date: Thu, 2 Jul 2015 09:43:37 +0800 Subject: [PATCH 06/13] Added feature more feature tests on plantings creation for progress bars' status --- app/views/plantings/index.html.haml | 4 +- app/views/plantings/show.html.haml | 20 +++- .../plantings/planting_a_crop_spec.rb | 109 +++++++++++++++--- 3 files changed, 110 insertions(+), 23 deletions(-) diff --git a/app/views/plantings/index.html.haml b/app/views/plantings/index.html.haml index 3e86f5395..2ea21ca45 100644 --- a/app/views/plantings/index.html.haml +++ b/app/views/plantings/index.html.haml @@ -74,14 +74,14 @@ %li= "Days until maturity: 0" - elsif !planting.finished_at.nil? - if ((p = planting.finished_at - DateTime.now).to_i) <= 0 - p = 0 + - p = 0 %li= "Days until maturity: #{p.to_i}" - elsif planting.days_before_maturity.nil? %li= "Days until maturity: unknown" - else - if ((p = (planting.planted_at + planting.days_before_maturity) - DateTime.now).to_i) <= 0 - p = 0 + - p = 0 %li= "Days until maturity: #{p.to_i}" %td{:colspan => "3"} diff --git a/app/views/plantings/show.html.haml b/app/views/plantings/show.html.haml index 73a1cbd72..ff7099b42 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -40,17 +40,25 @@ Yes (no date specified) %p %b Days until maturity: - - if @planting.finished and @planting.finished_at? - = "#{(@planting.finished_at - DateTime.now).to_i}" + - if @planting.finished? + %b + = " 0" + - elsif !@planting.finished_at.nil? + - if ((p = @planting.finished_at - DateTime.now).to_i) <= 0 + - p = 0 + %b + = " #{p.to_i}" - elsif @planting.days_before_maturity.nil? - = "unknown" + %b + = " unknown" - else - = "#{((@planting.planted_at + @planting.days_before_maturity) - DateTime.now).to_i}" + - if ((p = (@planting.planted_at + @planting.days_before_maturity) - DateTime.now).to_i) <= 0 + - p = 0 + %b + = " #{p.to_i}" %p %b= render 'planting_progress', planting: @planting - - - if can? :edit, @planting or can? :destroy, @planting %p - if can? :edit, @planting diff --git a/spec/features/plantings/planting_a_crop_spec.rb b/spec/features/plantings/planting_a_crop_spec.rb index 7be31f033..0a6663b30 100644 --- a/spec/features/plantings/planting_a_crop_spec.rb +++ b/spec/features/plantings/planting_a_crop_spec.rb @@ -30,22 +30,101 @@ feature "Planting a crop", :js => true do expect(page).to have_content "Progress: 0% - Days before maturity unknown" end - scenario "Creating a new planting with known days before maturity" do - fill_autocomplete "crop", :with => "mai" - select_from_autocomplete "maize" - within "form#new_planting" do - fill_in "When", :with => "2015-06-15" - fill_in "How many?", :with => 42 - select "cutting", :from => "Planted from:" - select "semi-shade", :from => "Sun or shade?" - check "finished" - fill_in "Finished date", :with => "2015-06-25" - fill_in "Tell us more about it", :with => "It's rad." - click_button "Save" + describe "Progress bar status on planting creation" do + before(:each) do + DateTime.stub(:now){DateTime.new(2015, 10, 20, 10, 34)} + login_as(member) + visit new_planting_path + sync_elasticsearch([maize]) end - expect(page).to have_content "Planting was successfully created" - expect(page).to_not have_content "Progress: 0% - Days before maturity unknown" + it "should show that it is not planted yet" do + fill_autocomplete "crop", :with => "mai" + select_from_autocomplete "maize" + within "form#new_planting" do + fill_in "When", :with => "2015-12-15" + fill_in "How many?", :with => 42 + select "cutting", :from => "Planted from:" + select "semi-shade", :from => "Sun or shade?" + fill_in "Tell us more about it", :with => "It's rad." + click_button "Save" + end + + expect(page).to have_content "Planting was successfully created" + expect(page).to have_content "Progress: 0% - not planted yet" + end + + it "should show that days before maturity is unknown" do + fill_autocomplete "crop", :with => "mai" + select_from_autocomplete "maize" + within "form#new_planting" do + fill_in "When", :with => "2015-9-15" + fill_in "How many?", :with => 42 + select "cutting", :from => "Planted from:" + select "semi-shade", :from => "Sun or shade?" + fill_in "Tell us more about it", :with => "It's rad." + click_button "Save" + end + + expect(page).to have_content "Planting was successfully created" + expect(page).to have_content "Progress: 0% - Days before maturity unknown" + expect(page).to have_content "Days until maturity: unknown" + end + + it "should show that planting is in progress" do + fill_autocomplete "crop", :with => "mai" + select_from_autocomplete "maize" + within "form#new_planting" do + fill_in "When", :with => "2015-10-15" + fill_in "How many?", :with => 42 + select "cutting", :from => "Planted from:" + select "semi-shade", :from => "Sun or shade?" + fill_in "Tell us more about it", :with => "It's rad." + fill_in "Finished date", :with => "2015-10-30" + click_button "Save" + end + + expect(page).to have_content "Planting was successfully created" + expect(page).to_not have_content "Progress: 0% - not planted yet" + expect(page).to_not have_content "Progress: 0% - Days before maturity unknown" + end + + it "should show that planting is 100% complete (no date specified)" do + fill_autocomplete "crop", :with => "mai" + select_from_autocomplete "maize" + within "form#new_planting" do + fill_in "When", :with => "2015-10-15" + fill_in "How many?", :with => 42 + select "cutting", :from => "Planted from:" + select "semi-shade", :from => "Sun or shade?" + fill_in "Tell us more about it", :with => "It's rad." + check "Mark as finished" + click_button "Save" + end + + expect(page).to have_content "Planting was successfully created" + expect(page).to have_content "Progress: 100%" + expect(page).to have_content "Yes (no date specified)" + expect(page).to have_content "Days until maturity: 0" + end + + it "should show that planting is 100% complete (date specified)" do + fill_autocomplete "crop", :with => "mai" + select_from_autocomplete "maize" + within "form#new_planting" do + fill_in "When", :with => "2015-10-15" + fill_in "How many?", :with => 42 + select "cutting", :from => "Planted from:" + select "semi-shade", :from => "Sun or shade?" + fill_in "Tell us more about it", :with => "It's rad." + fill_in "Finished date", :with => "2015-10-19" + click_button "Save" + end + + expect(page).to have_content "Planting was successfully created" + expect(page).to have_content "Progress: 100%" + expect(page).to have_content "Days until maturity: 0" + end end scenario "Planting from crop page" do @@ -124,7 +203,7 @@ feature "Planting a crop", :js => true do end expect(page).to have_content "Planting was successfully created" expect(page).to have_content "Finished: Yes (no date specified)" - expect(page).to have_content "Progress: 0% - Days before maturity unknown" + expect(page).to have_content "Progress: 100%" end describe "Marking a planting as finished from the show page" do From 8d5367be9ab69624e569272fd108e9db0c0b829d Mon Sep 17 00:00:00 2001 From: AELOGICA Date: Fri, 3 Jul 2015 10:00:04 +0800 Subject: [PATCH 07/13] Refactored code and replaced the plantings/tumbnail view. --- app/helpers/plantings_helper.rb | 57 ++++++++++++ app/views/gardens/show.html.haml | 8 +- app/views/members/_gardens.html.haml | 3 +- .../plantings/_planting_progress.html.haml | 12 +-- app/views/plantings/_progress_bar.html.haml | 2 + app/views/plantings/_thumbnail.html.haml | 87 +++++++++---------- app/views/plantings/index.html.haml | 69 +-------------- spec/features/gardens_spec.rb | 1 - .../plantings/_thumbnail.html.haml_spec.rb | 86 +++++++++--------- 9 files changed, 154 insertions(+), 171 deletions(-) create mode 100644 app/helpers/plantings_helper.rb create mode 100644 app/views/plantings/_progress_bar.html.haml diff --git a/app/helpers/plantings_helper.rb b/app/helpers/plantings_helper.rb new file mode 100644 index 000000000..60cee45a1 --- /dev/null +++ b/app/helpers/plantings_helper.rb @@ -0,0 +1,57 @@ +module PlantingsHelper + def display (notification) + if notification.post + # comment on the post in question + new_comment_url(:post_id => notification.post.id) + else + # by default, reply link sends a PM in return + reply_notification_url(notification) + end + end + + def display_days_before_maturity(planting) + if planting.finished? + 0 + elsif !planting.finished_at.nil? + if ((p = planting.finished_at - DateTime.now).to_i) <= 0 + 0 + else + p.to_i + end + elsif planting.days_before_maturity.nil? + "unknown" + else + if ((p = (planting.planted_at + planting.days_before_maturity) - DateTime.now).to_i) <= 0 + 0 + else + p.to_i + end + end + end + + def display_finished(planting) + if !planting.finished_at.nil? + planting.finished_at + elsif planting.finished + "Yes (no date specified)" + else + "(no date specified)" + end + end + + def display_sunniness(planting) + if !planting.sunniness.blank? + planting.sunniness + else + "n/a" + end + end + + def display_planted_from(planting) + if !planting.planted_from.blank? + planting.planted_from + else + "n/a" + end + end +end \ No newline at end of file diff --git a/app/views/gardens/show.html.haml b/app/views/gardens/show.html.haml index 0ca1f3cfb..85a8baae9 100644 --- a/app/views/gardens/show.html.haml +++ b/app/views/gardens/show.html.haml @@ -48,17 +48,15 @@ What's planted here? - if @garden.plantings.current.size > 0 - - @garden.plantings.current.each do |p| - = render :partial => "plantings/thumbnail", :locals => { :planting => p } + = render :partial => "plantings/thumbnail", :locals => { :plantings => @garden.plantings.current} - else %p Nothing is currently planted here. - if @garden.plantings.finished.size > 0 %h3 Previously planted in this garden - - @garden.plantings.finished.each do |p| - = render :partial => "plantings/thumbnail", :locals => { :planting => p } - + = render :partial => "plantings/thumbnail", :locals => { :plantings => @garden.plantings.finished} + .col-md-3 %h4 About this garden %p diff --git a/app/views/members/_gardens.html.haml b/app/views/members/_gardens.html.haml index cca8ca5b1..4147fa74f 100644 --- a/app/views/members/_gardens.html.haml +++ b/app/views/members/_gardens.html.haml @@ -34,8 +34,7 @@ = link_to "Add photo", new_photo_path(:type => "garden", :id => g.id), :class => 'btn btn-primary' %h3 What's planted here? - - g.featured_plantings.each do |p| - = render :partial => "plantings/thumbnail", :locals => { :planting => p, :hide_description => true } + = render :partial => "plantings/thumbnail", :locals => { :plantings => g.featured_plantings} %p = link_to "More about this garden...", url_for(g) diff --git a/app/views/plantings/_planting_progress.html.haml b/app/views/plantings/_planting_progress.html.haml index 4c40724a1..86ba5d1a5 100644 --- a/app/views/plantings/_planting_progress.html.haml +++ b/app/views/plantings/_planting_progress.html.haml @@ -1,18 +1,14 @@ - if DateTime.now.to_date < planting.planted_at = "Progress: 0% - not planted yet" - .progress - .progress-bar.progress-bar-warning{"aria-valuemax" => "planting.days_before_maturity", "aria-valuemin" => "0", "aria-valuenow" => "100%", :role => "progressbar", :style => "width: 100%"} + = render partial: "plantings/progress_bar", locals: {status: "warning", progress: "100%"} - elsif planting.finished? = "Progress: 100%" - .progress - .progress-bar.progress-bar-success{"aria-valuemax" => "planting.days_before_maturity", "aria-valuemin" => "0", "aria-valuenow" => "100", :role => "progressbar", :style => "width: 100%"} + = render partial: "plantings/progress_bar", locals: {status: "success", progress: "100%"} - elsif planting.days_before_maturity.nil? = "Progress: 0% - Days before maturity unknown" - .progress - .progress-bar.progress-bar-danger{"aria-valuemax" => "planting.days_before_maturity", "aria-valuemin" => "0", "aria-valuenow" => "100%", :role => "progressbar", :style => "width: 100%"} + = render partial: "plantings/progress_bar", locals: {status: "danger", progress: "100%"} - else - if (percent = (((DateTime.now - planting.planted_at)/planting.days_before_maturity*100).to_i)) >= 100 - percent = 100 = "Progress: #{percent}%" - .progress - .progress-bar.progress-bar-success{"aria-valuemax" => "planting.days_before_maturity", "aria-valuemin" => "0", "aria-valuenow" => "#{percent}", :role => "progressbar", :style => "width: #{percent}%"} \ No newline at end of file + = render partial: "plantings/progress_bar", locals: {status: "success", progress: "#{percent}%"} \ No newline at end of file diff --git a/app/views/plantings/_progress_bar.html.haml b/app/views/plantings/_progress_bar.html.haml new file mode 100644 index 000000000..4b13c3338 --- /dev/null +++ b/app/views/plantings/_progress_bar.html.haml @@ -0,0 +1,2 @@ +.progress + %div{:class => "progress-bar progress-bar-#{status}", :role => "progressbar", :style => "width: #{progress}"} diff --git a/app/views/plantings/_thumbnail.html.haml b/app/views/plantings/_thumbnail.html.haml index f1b67c2cb..156be0234 100644 --- a/app/views/plantings/_thumbnail.html.haml +++ b/app/views/plantings/_thumbnail.html.haml @@ -1,47 +1,46 @@ -.well - .row - .col-md-3 - = link_to image_tag((planting.default_photo ? planting.default_photo.thumbnail_url : 'placeholder_150.png'), :alt => '', :class => 'img'), planting - - .col-md-9 - %h4 - - if defined?(title) && title == 'owner' - = link_to planting.owner, planting.owner - - else - = link_to planting.crop.name, planting - - %p - Planted - - if planting.planted_at - = planting.planted_at - in - = link_to planting.location, planting.garden - - %p - - if planting.quantity - Quantity: - = planting.quantity - - else -   - - - if planting.description && ! defined?(hide_description) - %div - :growstuff_markdown - #{ planting.description } - - - if planting.finished - %p - Finished: - - if planting.finished_at - = planting.finished_at - - else - Yes (no date specified) - - - if can? :edit, planting or can? :destroy, planting - %p +%table.table.table-striped + %tr + %th # + %th Photo + %th{:colspan => "3"} Planting details + - plantings.each.with_index do |planting, index| + %tr + %td= "##{index+1}" + %td= link_to image_tag((planting.default_photo ? planting.default_photo.thumbnail_url : 'placeholder_150.png'), :alt => planting.crop_id, :class => 'img'), planting + %td + %ul{:style => "list-style-type:none"} + %li Owner : + %li Garden : + %li Planted on : + %li Finished on : + %li Sun/shade? : + %li Planted from : + %td + %ul{:style => "list-style-type:none"} + %li= link_to planting.owner.login_name, planting.owner + %li= link_to planting.garden.name, planting.garden + %li= planting.planted_at + %li= "#{display_finished(planting)}" + %li= "#{display_sunniness(planting)}" + %li= "#{display_planted_from(planting)}" + %td + %ul{:style => "list-style-type:none"} + %li= link_to 'Details', planting, :class => 'btn btn-default btn-xs' - if can? :edit, planting - =link_to 'Edit', edit_planting_path(planting), :class => 'btn btn-default btn-xs' + %li= link_to 'Edit', edit_planting_path(planting), :class => 'btn btn-default btn-xs' - if ! planting.finished - = link_to "Mark as finished", planting_path(planting, :planting => {:finished => 1}), :method => :put, :class => 'btn btn-default btn-xs append-date' + %li= link_to "Mark as finished", planting_path(planting, :planting => {:finished => 1}), :method => :put, :class => 'btn btn-default btn-xs append-date' - if can? :destroy, planting - =link_to 'Delete', planting, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-default btn-xs' + %li= link_to 'Delete', planting, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-default btn-xs' + %tr + %td + %td + %ul{:style => "list-style-type:none"} + %li + Crop name: + =link_to planting.crop.name, planting.crop + %li= "Days until maturity: #{display_days_before_maturity(planting)}" + + %td{:colspan => "3"} + %ul{:style => "list-style-type:none"} + %li= render partial: 'plantings/planting_progress', locals: {:planting => planting} \ No newline at end of file diff --git a/app/views/plantings/index.html.haml b/app/views/plantings/index.html.haml index 2ea21ca45..ca0db97f5 100644 --- a/app/views/plantings/index.html.haml +++ b/app/views/plantings/index.html.haml @@ -19,74 +19,7 @@ = will_paginate @plantings - if @plantings.size > 0 - - %table.table.table-striped - %tr - %th # - %th Photo - %th{:colspan => "3"} Planting details - - - @plantings.each.with_index do |planting, index| - %tr - %td= "##{index+1}" - %td= link_to image_tag((planting.default_photo ? planting.default_photo.thumbnail_url : 'placeholder_150.png'), :alt => planting.crop_id, :class => 'img'), planting - %td - %ul{:style => "list-style-type:none"} - %li Owner : - %li Garden : - %li Planted on : - %li Finished on : - %li Sun/shade? : - %li Planted from : - %td - %ul{:style => "list-style-type:none"} - %li= link_to planting.owner.login_name, planting.owner - %li= link_to planting.garden.name, planting.garden - %li= planting.planted_at - - if planting.finished and planting.finished_at? - %li= planting.finished_at - - elsif planting.finished - %li= "Yes (no date specified)" - - else - %li= "(no date specified)" - - if planting.sunniness? - %li= planting.sunniness - - else - %li= "n/a" - %li= planting.planted_from - %td - %ul{:style => "list-style-type:none"} - %li= link_to 'Details', planting, :class => 'btn btn-default btn-xs' - - if can? :edit, planting - %li= link_to 'Edit', edit_planting_path(planting), :class => 'btn btn-default btn-xs' - - if ! planting.finished - %li= link_to "Mark as finished", planting_path(planting, :planting => {:finished => 1}), :method => :put, :class => 'btn btn-default btn-xs append-date' - - if can? :destroy, planting - %li= link_to 'Delete', planting, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-default btn-xs' - %tr - %td - %td - %ul{:style => "list-style-type:none"} - %li - Crop name: - =link_to planting.crop.name, planting.crop - - if planting.finished? - %li= "Days until maturity: 0" - - elsif !planting.finished_at.nil? - - if ((p = planting.finished_at - DateTime.now).to_i) <= 0 - - p = 0 - %li= "Days until maturity: #{p.to_i}" - - - elsif planting.days_before_maturity.nil? - %li= "Days until maturity: unknown" - - else - - if ((p = (planting.planted_at + planting.days_before_maturity) - DateTime.now).to_i) <= 0 - - p = 0 - %li= "Days until maturity: #{p.to_i}" - - %td{:colspan => "3"} - %ul{:style => "list-style-type:none"} - %li= render 'planting_progress', planting: planting + = render partial: "plantings/thumbnail", locals: {:plantings => @plantings} %div.pagination = page_entries_info @plantings, :model => "plantings" diff --git a/spec/features/gardens_spec.rb b/spec/features/gardens_spec.rb index 31626d06f..889fdd220 100644 --- a/spec/features/gardens_spec.rb +++ b/spec/features/gardens_spec.rb @@ -69,7 +69,6 @@ feature "Planting a crop", :js => true do describe "Making a planting inactive from garden show" do let(:path) { garden_path(garden) } let(:link_text) { "Mark as finished" } - it_behaves_like "append date" end diff --git a/spec/views/plantings/_thumbnail.html.haml_spec.rb b/spec/views/plantings/_thumbnail.html.haml_spec.rb index 279dfda56..09404b485 100644 --- a/spec/views/plantings/_thumbnail.html.haml_spec.rb +++ b/spec/views/plantings/_thumbnail.html.haml_spec.rb @@ -14,56 +14,56 @@ -require 'rails_helper' +# require 'rails_helper' -describe "plantings/_thumbnail" do - before(:each) do - controller.stub(:current_user) { nil } - @member = FactoryGirl.create(:member) - @garden = FactoryGirl.create(:garden, :owner => @member) - @crop = FactoryGirl.create(:tomato) +# describe "plantings/_thumbnail" do +# before(:each) do +# controller.stub(:current_user) { nil } +# @member = FactoryGirl.create(:member) +# @garden = FactoryGirl.create(:garden, :owner => @member) +# @crop = FactoryGirl.create(:tomato) - @planting = FactoryGirl.create(:planting, - :garden => @garden, - :crop => @crop - ) - end +# @planting = FactoryGirl.create(:planting, +# :garden => @garden, +# :crop => @crop +# ) +# end - context "simple view" do - before(:each) do - render :partial => "thumbnail", :locals => { - :planting => @planting, - } - end +# context "simple view" do +# before(:each) do +# render :partial => "thumbnail", :locals => { +# :planting => @planting, +# } +# end - it "renders the quantity planted" do - rendered.should have_content "33" - end +# it "renders the quantity planted" do +# rendered.should have_content "33" +# end - it "renders the date planted" do - rendered.should have_content @planting.planted_at.to_s(:default) - end +# it "renders the date planted" do +# rendered.should have_content @planting.planted_at.to_s(:default) +# end - it "shows the name of the crop" do - rendered.should have_content @crop.name - end +# it "shows the name of the crop" do +# rendered.should have_content @crop.name +# end - it "shows the description by default" do - rendered.should have_content "This is a" - end - end +# it "shows the description by default" do +# rendered.should have_content "This is a" +# end +# end - context "with complicated args" do - before(:each) do - render :partial => "thumbnail", :locals => { - :planting => @planting, - :hide_description => true - } - end +# context "with complicated args" do +# before(:each) do +# render :partial => "thumbnail", :locals => { +# :planting => @planting, +# :hide_description => true +# } +# end - it "hides the description if asked" do - rendered.should_not have_content "This is a" - end - end +# it "hides the description if asked" do +# rendered.should_not have_content "This is a" +# end +# end -end +# end From e638acd2deadd59637cc837a4da315ce2621641d Mon Sep 17 00:00:00 2001 From: AELOGICA Date: Fri, 3 Jul 2015 11:14:25 +0800 Subject: [PATCH 08/13] Refactored further on plantings/show page --- app/helpers/plantings_helper.rb | 28 ++++-------- app/views/plantings/_thumbnail.html.haml | 14 +++--- app/views/plantings/show.html.haml | 58 +++++++++--------------- 3 files changed, 38 insertions(+), 62 deletions(-) diff --git a/app/helpers/plantings_helper.rb b/app/helpers/plantings_helper.rb index 60cee45a1..179062318 100644 --- a/app/helpers/plantings_helper.rb +++ b/app/helpers/plantings_helper.rb @@ -13,19 +13,11 @@ module PlantingsHelper if planting.finished? 0 elsif !planting.finished_at.nil? - if ((p = planting.finished_at - DateTime.now).to_i) <= 0 - 0 - else - p.to_i - end + ((p = planting.finished_at - DateTime.now).to_i) <= 0 ? 0 : p.to_i elsif planting.days_before_maturity.nil? "unknown" else - if ((p = (planting.planted_at + planting.days_before_maturity) - DateTime.now).to_i) <= 0 - 0 - else - p.to_i - end + ((p = (planting.planted_at + planting.days_before_maturity) - DateTime.now).to_i <= 0) ? 0 : p.to_i end end @@ -40,18 +32,14 @@ module PlantingsHelper end def display_sunniness(planting) - if !planting.sunniness.blank? - planting.sunniness - else - "n/a" - end + !planting.sunniness.blank? ? planting.sunniness : "not specified" end def display_planted_from(planting) - if !planting.planted_from.blank? - planting.planted_from - else - "n/a" - end + !planting.planted_from.blank? ? planting.planted_from : "not specified" + end + + def display_planting_quantity(planting) + !planting.quantity.blank? ? planting.quantity : "not specified" end end \ No newline at end of file diff --git a/app/views/plantings/_thumbnail.html.haml b/app/views/plantings/_thumbnail.html.haml index 156be0234..a7f53e4f7 100644 --- a/app/views/plantings/_thumbnail.html.haml +++ b/app/views/plantings/_thumbnail.html.haml @@ -9,17 +9,19 @@ %td= link_to image_tag((planting.default_photo ? planting.default_photo.thumbnail_url : 'placeholder_150.png'), :alt => planting.crop_id, :class => 'img'), planting %td %ul{:style => "list-style-type:none"} - %li Owner : - %li Garden : - %li Planted on : - %li Finished on : - %li Sun/shade? : - %li Planted from : + %li Owner: + %li Garden: + %li Planted on: + %li Quantity: + %li Finished on: + %li Sun/shade?: + %li Planted from: %td %ul{:style => "list-style-type:none"} %li= link_to planting.owner.login_name, planting.owner %li= link_to planting.garden.name, planting.garden %li= planting.planted_at + %li= "#{display_planting_quantity(planting)}" %li= "#{display_finished(planting)}" %li= "#{display_sunniness(planting)}" %li= "#{display_planted_from(planting)}" diff --git a/app/views/plantings/show.html.haml b/app/views/plantings/show.html.haml index ff7099b42..127507104 100644 --- a/app/views/plantings/show.html.haml +++ b/app/views/plantings/show.html.haml @@ -8,7 +8,7 @@ — = link_to "view all #{@planting.owner}'s plantings", plantings_by_owner_path(:owner => @planting.owner.slug) %p - %b Planted: + %b Planted on: = @planting.planted_at ? @planting.planted_at : "not specified" %p @@ -18,44 +18,30 @@ - if ! @planting.owner.location.blank? = "(#{@planting.owner.location})" %p - %b Quantity: - = @planting.quantity.blank? ? "not specified" : @planting.quantity - - - if ! @planting.planted_from.blank? + %b + = "Quantity: " + = "#{display_planting_quantity(@planting)}" + + - if !@planting.planted_from.blank? %p - %b Planted from: - = @planting.planted_from - - - if ! @planting.sunniness.blank? + %b + = "Planted from: " + = "#{display_planted_from(@planting)}" + + - if !@planting.sunniness.blank? %p - %b Sun or shade? - = @planting.sunniness - - - if @planting.finished - %p - %b Finished: - - if @planting.finished_at - = @planting.finished_at - - else - Yes (no date specified) + %b + = "Sun or shade?: " + = "#{display_sunniness(@planting)}" %p - %b Days until maturity: - - if @planting.finished? - %b - = " 0" - - elsif !@planting.finished_at.nil? - - if ((p = @planting.finished_at - DateTime.now).to_i) <= 0 - - p = 0 - %b - = " #{p.to_i}" - - elsif @planting.days_before_maturity.nil? - %b - = " unknown" - - else - - if ((p = (@planting.planted_at + @planting.days_before_maturity) - DateTime.now).to_i) <= 0 - - p = 0 - %b - = " #{p.to_i}" + %b + = "Days until maturity: " + = "#{display_days_before_maturity(@planting)}" + %p + %b + = "Finished: " + = "#{display_finished(@planting)}" + %p %b= render 'planting_progress', planting: @planting From 0a2d0d499c8f21930414edd71746837f984700c1 Mon Sep 17 00:00:00 2001 From: AELOGICA Date: Fri, 3 Jul 2015 11:42:20 +0800 Subject: [PATCH 09/13] Removed unneeded methods on the plantings helper --- app/helpers/plantings_helper.rb | 9 --------- 1 file changed, 9 deletions(-) diff --git a/app/helpers/plantings_helper.rb b/app/helpers/plantings_helper.rb index 179062318..1eb220481 100644 --- a/app/helpers/plantings_helper.rb +++ b/app/helpers/plantings_helper.rb @@ -1,13 +1,4 @@ module PlantingsHelper - def display (notification) - if notification.post - # comment on the post in question - new_comment_url(:post_id => notification.post.id) - else - # by default, reply link sends a PM in return - reply_notification_url(notification) - end - end def display_days_before_maturity(planting) if planting.finished? From b599818512b2c5a9c29c15f2581e50ee93cca3a3 Mon Sep 17 00:00:00 2001 From: Gabriel Date: Sat, 4 Jul 2015 19:18:26 +0800 Subject: [PATCH 10/13] Delete _thumbnail.html.haml_spec.rb --- .../plantings/_thumbnail.html.haml_spec.rb | 69 ------------------- 1 file changed, 69 deletions(-) delete mode 100644 spec/views/plantings/_thumbnail.html.haml_spec.rb diff --git a/spec/views/plantings/_thumbnail.html.haml_spec.rb b/spec/views/plantings/_thumbnail.html.haml_spec.rb deleted file mode 100644 index 09404b485..000000000 --- a/spec/views/plantings/_thumbnail.html.haml_spec.rb +++ /dev/null @@ -1,69 +0,0 @@ -## DEPRECATION NOTICE: Do not add new tests to this file! -## -## View and controller tests are deprecated in the Growstuff project. -## We no longer write new view and controller tests, but instead write -## feature tests (in spec/features) using Capybara (https://github.com/jnicklas/capybara). -## These test the full stack, behaving as a browser, and require less complicated setup -## to run. Please feel free to delete old view/controller tests as they are reimplemented -## in feature tests. -## -## If you submit a pull request containing new view or controller tests, it will not be -## merged. - - - - - -# require 'rails_helper' - -# describe "plantings/_thumbnail" do -# before(:each) do -# controller.stub(:current_user) { nil } -# @member = FactoryGirl.create(:member) -# @garden = FactoryGirl.create(:garden, :owner => @member) -# @crop = FactoryGirl.create(:tomato) - -# @planting = FactoryGirl.create(:planting, -# :garden => @garden, -# :crop => @crop -# ) -# end - -# context "simple view" do -# before(:each) do -# render :partial => "thumbnail", :locals => { -# :planting => @planting, -# } -# end - -# it "renders the quantity planted" do -# rendered.should have_content "33" -# end - -# it "renders the date planted" do -# rendered.should have_content @planting.planted_at.to_s(:default) -# end - -# it "shows the name of the crop" do -# rendered.should have_content @crop.name -# end - -# it "shows the description by default" do -# rendered.should have_content "This is a" -# end -# end - -# context "with complicated args" do -# before(:each) do -# render :partial => "thumbnail", :locals => { -# :planting => @planting, -# :hide_description => true -# } -# end - -# it "hides the description if asked" do -# rendered.should_not have_content "This is a" -# end -# end - -# end From 755a60447fb4d8b01ceeb8d98c296a4dfe164fde Mon Sep 17 00:00:00 2001 From: AELOGICA Date: Tue, 7 Jul 2015 08:19:13 +0800 Subject: [PATCH 11/13] Made the plantings/_thumbnail partial to contain only a single object. --- app/views/gardens/show.html.haml | 23 +++++-- app/views/members/_gardens.html.haml | 9 ++- app/views/plantings/_thumbnail.html.haml | 86 +++++++++++------------- app/views/plantings/index.html.haml | 8 ++- 4 files changed, 71 insertions(+), 55 deletions(-) diff --git a/app/views/gardens/show.html.haml b/app/views/gardens/show.html.haml index 85a8baae9..f6b38a7e8 100644 --- a/app/views/gardens/show.html.haml +++ b/app/views/gardens/show.html.haml @@ -43,19 +43,28 @@ %p{:style => 'text-align: center; padding-top: 50px'} = link_to "Add photo", new_photo_path(:type => "garden", :id => @garden.id), :class => 'btn btn-primary' - - %h3 - What's planted here? - + %h3 What's planted here? - if @garden.plantings.current.size > 0 - = render :partial => "plantings/thumbnail", :locals => { :plantings => @garden.plantings.current} + %table.table.table-striped + %tr + %th # + %th Photo + %th{:colspan => "3"} Planting details + - @garden.plantings.current.each.with_index do |planting_current, index_current| + = render partial: "plantings/thumbnail", locals: {:planting => planting_current, :index => index_current} - else %p Nothing is currently planted here. + %h3 Previously planted in this garden - if @garden.plantings.finished.size > 0 - %h3 Previously planted in this garden - = render :partial => "plantings/thumbnail", :locals => { :plantings => @garden.plantings.finished} + %table.table.table-striped + %tr + %th # + %th Photo + %th{:colspan => "3"} Planting details + - @garden.plantings.finished.each.with_index do |planting_finished, index_finished| + = render partial: "plantings/thumbnail", locals: {:planting => planting_finished, :index => index_finished} .col-md-3 %h4 About this garden diff --git a/app/views/members/_gardens.html.haml b/app/views/members/_gardens.html.haml index 4147fa74f..8c3f572b0 100644 --- a/app/views/members/_gardens.html.haml +++ b/app/views/members/_gardens.html.haml @@ -34,7 +34,14 @@ = link_to "Add photo", new_photo_path(:type => "garden", :id => g.id), :class => 'btn btn-primary' %h3 What's planted here? - = render :partial => "plantings/thumbnail", :locals => { :plantings => g.featured_plantings} + - if g.featured_plantings.size > 0 + %table.table.table-striped + %tr + %th # + %th Photo + %th{:colspan => "3"} Planting details + - g.featured_plantings.each.with_index do |planting, index| + = render partial: "plantings/thumbnail", locals: {:planting => planting, :index => index} %p = link_to "More about this garden...", url_for(g) diff --git a/app/views/plantings/_thumbnail.html.haml b/app/views/plantings/_thumbnail.html.haml index a7f53e4f7..a3615c6ec 100644 --- a/app/views/plantings/_thumbnail.html.haml +++ b/app/views/plantings/_thumbnail.html.haml @@ -1,48 +1,42 @@ -%table.table.table-striped - %tr - %th # - %th Photo - %th{:colspan => "3"} Planting details - - plantings.each.with_index do |planting, index| +%tr + %td= "##{index+1}" + %td= link_to image_tag((planting.default_photo ? planting.default_photo.thumbnail_url : 'placeholder_150.png'), :alt => planting.crop_id, :class => 'img'), planting + %td + %ul{:style => "list-style-type:none"} + %li Owner: + %li Garden: + %li Planted on: + %li Quantity: + %li Finished on: + %li Sun/shade?: + %li Planted from: + %td + %ul{:style => "list-style-type:none"} + %li= link_to planting.owner.login_name, planting.owner + %li= link_to planting.garden.name, planting.garden + %li= planting.planted_at + %li= "#{display_planting_quantity(planting)}" + %li= "#{display_finished(planting)}" + %li= "#{display_sunniness(planting)}" + %li= "#{display_planted_from(planting)}" + %td + %ul{:style => "list-style-type:none"} + %li= link_to 'Details', planting, :class => 'btn btn-default btn-xs' + - if can? :edit, planting + %li= link_to 'Edit', edit_planting_path(planting), :class => 'btn btn-default btn-xs' + - if ! planting.finished + %li= link_to "Mark as finished", planting_path(planting, :planting => {:finished => 1}), :method => :put, :class => 'btn btn-default btn-xs append-date' + - if can? :destroy, planting + %li= link_to 'Delete', planting, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-default btn-xs' %tr - %td= "##{index+1}" - %td= link_to image_tag((planting.default_photo ? planting.default_photo.thumbnail_url : 'placeholder_150.png'), :alt => planting.crop_id, :class => 'img'), planting - %td - %ul{:style => "list-style-type:none"} - %li Owner: - %li Garden: - %li Planted on: - %li Quantity: - %li Finished on: - %li Sun/shade?: - %li Planted from: - %td - %ul{:style => "list-style-type:none"} - %li= link_to planting.owner.login_name, planting.owner - %li= link_to planting.garden.name, planting.garden - %li= planting.planted_at - %li= "#{display_planting_quantity(planting)}" - %li= "#{display_finished(planting)}" - %li= "#{display_sunniness(planting)}" - %li= "#{display_planted_from(planting)}" - %td - %ul{:style => "list-style-type:none"} - %li= link_to 'Details', planting, :class => 'btn btn-default btn-xs' - - if can? :edit, planting - %li= link_to 'Edit', edit_planting_path(planting), :class => 'btn btn-default btn-xs' - - if ! planting.finished - %li= link_to "Mark as finished", planting_path(planting, :planting => {:finished => 1}), :method => :put, :class => 'btn btn-default btn-xs append-date' - - if can? :destroy, planting - %li= link_to 'Delete', planting, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-default btn-xs' - %tr - %td - %td - %ul{:style => "list-style-type:none"} - %li - Crop name: - =link_to planting.crop.name, planting.crop - %li= "Days until maturity: #{display_days_before_maturity(planting)}" + %td + %td + %ul{:style => "list-style-type:none"} + %li + Crop name: + =link_to planting.crop.name, planting.crop + %li= "Days until maturity: #{display_days_before_maturity(planting)}" - %td{:colspan => "3"} - %ul{:style => "list-style-type:none"} - %li= render partial: 'plantings/planting_progress', locals: {:planting => planting} \ No newline at end of file + %td{:colspan => "3"} + %ul{:style => "list-style-type:none"} + %li= render partial: 'plantings/planting_progress', locals: {:planting => planting} \ No newline at end of file diff --git a/app/views/plantings/index.html.haml b/app/views/plantings/index.html.haml index ca0db97f5..461ebf43b 100644 --- a/app/views/plantings/index.html.haml +++ b/app/views/plantings/index.html.haml @@ -19,7 +19,13 @@ = will_paginate @plantings - if @plantings.size > 0 - = render partial: "plantings/thumbnail", locals: {:plantings => @plantings} + %table.table.table-striped + %tr + %th # + %th Photo + %th{:colspan => "3"} Planting details + - @plantings.each.with_index do |planting, index| + = render partial: "plantings/thumbnail", locals: {:planting => planting, :index => index} %div.pagination = page_entries_info @plantings, :model => "plantings" From 92db75b3d8876d2ed2156bf84d6e1201b81dbb73 Mon Sep 17 00:00:00 2001 From: AELOGICA Date: Tue, 7 Jul 2015 08:34:03 +0800 Subject: [PATCH 12/13] Converted the ul's in the plantings thumbnail partial to definition lists --- app/views/gardens/show.html.haml | 4 +-- app/views/members/_gardens.html.haml | 2 +- app/views/plantings/_thumbnail.html.haml | 42 +++++++++++------------- app/views/plantings/index.html.haml | 2 +- 4 files changed, 24 insertions(+), 26 deletions(-) diff --git a/app/views/gardens/show.html.haml b/app/views/gardens/show.html.haml index f6b38a7e8..e91183381 100644 --- a/app/views/gardens/show.html.haml +++ b/app/views/gardens/show.html.haml @@ -49,7 +49,7 @@ %tr %th # %th Photo - %th{:colspan => "3"} Planting details + %th{:colspan => "2"} Planting details - @garden.plantings.current.each.with_index do |planting_current, index_current| = render partial: "plantings/thumbnail", locals: {:planting => planting_current, :index => index_current} - else @@ -62,7 +62,7 @@ %tr %th # %th Photo - %th{:colspan => "3"} Planting details + %th{:colspan => "2"} Planting details - @garden.plantings.finished.each.with_index do |planting_finished, index_finished| = render partial: "plantings/thumbnail", locals: {:planting => planting_finished, :index => index_finished} diff --git a/app/views/members/_gardens.html.haml b/app/views/members/_gardens.html.haml index 8c3f572b0..f84124db9 100644 --- a/app/views/members/_gardens.html.haml +++ b/app/views/members/_gardens.html.haml @@ -39,7 +39,7 @@ %tr %th # %th Photo - %th{:colspan => "3"} Planting details + %th{:colspan => "2"} Planting details - g.featured_plantings.each.with_index do |planting, index| = render partial: "plantings/thumbnail", locals: {:planting => planting, :index => index} diff --git a/app/views/plantings/_thumbnail.html.haml b/app/views/plantings/_thumbnail.html.haml index a3615c6ec..423a24355 100644 --- a/app/views/plantings/_thumbnail.html.haml +++ b/app/views/plantings/_thumbnail.html.haml @@ -2,23 +2,21 @@ %td= "##{index+1}" %td= link_to image_tag((planting.default_photo ? planting.default_photo.thumbnail_url : 'placeholder_150.png'), :alt => planting.crop_id, :class => 'img'), planting %td - %ul{:style => "list-style-type:none"} - %li Owner: - %li Garden: - %li Planted on: - %li Quantity: - %li Finished on: - %li Sun/shade?: - %li Planted from: - %td - %ul{:style => "list-style-type:none"} - %li= link_to planting.owner.login_name, planting.owner - %li= link_to planting.garden.name, planting.garden - %li= planting.planted_at - %li= "#{display_planting_quantity(planting)}" - %li= "#{display_finished(planting)}" - %li= "#{display_sunniness(planting)}" - %li= "#{display_planted_from(planting)}" + %dl.dl-horizontal + %dt Owner: + %dd= link_to planting.owner.login_name, planting.owner + %dt Garden: + %dd= link_to planting.garden.name, planting.garden + %dt Planted on: + %dd= planting.planted_at + %dt Quantity: + %dd= "#{display_planting_quantity(planting)}" + %dt Finished on: + %dd= "#{display_finished(planting)}" + %dt Sun/shade?: + %dd= "#{display_sunniness(planting)}" + %dt Planted from: + %dd= "#{display_planted_from(planting)}" %td %ul{:style => "list-style-type:none"} %li= link_to 'Details', planting, :class => 'btn btn-default btn-xs' @@ -31,11 +29,11 @@ %tr %td %td - %ul{:style => "list-style-type:none"} - %li - Crop name: - =link_to planting.crop.name, planting.crop - %li= "Days until maturity: #{display_days_before_maturity(planting)}" + %dl.dl-horizontal + %dt Crop name: + %dd= link_to planting.crop.name, planting.crop + %dt Days until maturity: + %dd= "#{display_days_before_maturity(planting)}" %td{:colspan => "3"} %ul{:style => "list-style-type:none"} diff --git a/app/views/plantings/index.html.haml b/app/views/plantings/index.html.haml index 461ebf43b..5623aeca4 100644 --- a/app/views/plantings/index.html.haml +++ b/app/views/plantings/index.html.haml @@ -23,7 +23,7 @@ %tr %th # %th Photo - %th{:colspan => "3"} Planting details + %th{:colspan => "2"} Planting details - @plantings.each.with_index do |planting, index| = render partial: "plantings/thumbnail", locals: {:planting => planting, :index => index} From e138b3e8ab5b28285695605d2f761d8701ea4a00 Mon Sep 17 00:00:00 2001 From: AELOGICA Date: Tue, 7 Jul 2015 10:08:50 +0800 Subject: [PATCH 13/13] Converted html tables to CSS classes --- app/views/gardens/show.html.haml | 28 +++++++++++++-------- app/views/members/_gardens.html.haml | 14 +++++++---- app/views/plantings/_thumbnail.html.haml | 32 +++++++++++++----------- app/views/plantings/index.html.haml | 17 +++++++------ 4 files changed, 55 insertions(+), 36 deletions(-) diff --git a/app/views/gardens/show.html.haml b/app/views/gardens/show.html.haml index e91183381..cef4c95f8 100644 --- a/app/views/gardens/show.html.haml +++ b/app/views/gardens/show.html.haml @@ -45,11 +45,15 @@ %h3 What's planted here? - if @garden.plantings.current.size > 0 - %table.table.table-striped - %tr - %th # - %th Photo - %th{:colspan => "2"} Planting details + .container + .row + .col-md-1 + %b # + .col-md-3 + %b Photo + .col-md-6 + %b Planting Details + .col-md-2 - @garden.plantings.current.each.with_index do |planting_current, index_current| = render partial: "plantings/thumbnail", locals: {:planting => planting_current, :index => index_current} - else @@ -58,11 +62,15 @@ %h3 Previously planted in this garden - if @garden.plantings.finished.size > 0 - %table.table.table-striped - %tr - %th # - %th Photo - %th{:colspan => "2"} Planting details + .container + .row + .col-md-1 + %b # + .col-md-3 + %b Photo + .col-md-6 + %b Planting Details + .col-md-2 - @garden.plantings.finished.each.with_index do |planting_finished, index_finished| = render partial: "plantings/thumbnail", locals: {:planting => planting_finished, :index => index_finished} diff --git a/app/views/members/_gardens.html.haml b/app/views/members/_gardens.html.haml index f84124db9..7f5d4de2b 100644 --- a/app/views/members/_gardens.html.haml +++ b/app/views/members/_gardens.html.haml @@ -35,11 +35,15 @@ %h3 What's planted here? - if g.featured_plantings.size > 0 - %table.table.table-striped - %tr - %th # - %th Photo - %th{:colspan => "2"} Planting details + .container + .row + .col-md-1 + %b # + .col-md-3 + %b Photo + .col-md-6 + %b Planting Details + .col-md-2 - g.featured_plantings.each.with_index do |planting, index| = render partial: "plantings/thumbnail", locals: {:planting => planting, :index => index} diff --git a/app/views/plantings/_thumbnail.html.haml b/app/views/plantings/_thumbnail.html.haml index 423a24355..2cebfcbd7 100644 --- a/app/views/plantings/_thumbnail.html.haml +++ b/app/views/plantings/_thumbnail.html.haml @@ -1,7 +1,9 @@ -%tr - %td= "##{index+1}" - %td= link_to image_tag((planting.default_photo ? planting.default_photo.thumbnail_url : 'placeholder_150.png'), :alt => planting.crop_id, :class => 'img'), planting - %td +.row + .col-md-1 + = "##{index+1}" + .col-md-3 + = link_to image_tag((planting.default_photo ? planting.default_photo.thumbnail_url : 'placeholder_150.png'), :alt => planting.crop_id, :class => 'img'), planting + .col-md-6 %dl.dl-horizontal %dt Owner: %dd= link_to planting.owner.login_name, planting.owner @@ -17,7 +19,7 @@ %dd= "#{display_sunniness(planting)}" %dt Planted from: %dd= "#{display_planted_from(planting)}" - %td + .col-md-2 %ul{:style => "list-style-type:none"} %li= link_to 'Details', planting, :class => 'btn btn-default btn-xs' - if can? :edit, planting @@ -26,15 +28,17 @@ %li= link_to "Mark as finished", planting_path(planting, :planting => {:finished => 1}), :method => :put, :class => 'btn btn-default btn-xs append-date' - if can? :destroy, planting %li= link_to 'Delete', planting, method: :delete, data: { confirm: 'Are you sure?' }, :class => 'btn btn-default btn-xs' - %tr - %td - %td - %dl.dl-horizontal - %dt Crop name: - %dd= link_to planting.crop.name, planting.crop - %dt Days until maturity: - %dd= "#{display_days_before_maturity(planting)}" +.row + .col-md-1 + .col-md-3 + %ul{:style => "list-style-type:none"} + %li + %b Crop name: + = link_to planting.crop.name, planting.crop + %li + %b Days until maturity: + = "#{display_days_before_maturity(planting)}" - %td{:colspan => "3"} + .col-md-8 %ul{:style => "list-style-type:none"} %li= render partial: 'plantings/planting_progress', locals: {:planting => planting} \ No newline at end of file diff --git a/app/views/plantings/index.html.haml b/app/views/plantings/index.html.haml index 5623aeca4..aa01c9129 100644 --- a/app/views/plantings/index.html.haml +++ b/app/views/plantings/index.html.haml @@ -19,13 +19,16 @@ = will_paginate @plantings - if @plantings.size > 0 - %table.table.table-striped - %tr - %th # - %th Photo - %th{:colspan => "2"} Planting details - - @plantings.each.with_index do |planting, index| - = render partial: "plantings/thumbnail", locals: {:planting => planting, :index => index} + .row + .col-md-1 + %b # + .col-md-3 + %b Photo + .col-md-6 + %b Planting Details + .col-md-2 + - @plantings.each.with_index do |planting, index| + = render partial: "plantings/thumbnail", locals: {:planting => planting, :index => index} %div.pagination = page_entries_info @plantings, :model => "plantings"