From 772eec77a7f69885a673c95bcabad6a4d8844787 Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 2 Sep 2013 11:13:02 +1000 Subject: [PATCH 1/6] added routes and controller stuff for crop hierarchy --- app/controllers/crops_controller.rb | 10 +++++++++- app/views/crops/hierarchy.html.haml | 6 ++++++ config/routes.rb | 1 + spec/controllers/crops_controller_spec.rb | 8 ++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) create mode 100644 app/views/crops/hierarchy.html.haml diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 4238c1031..29f15543f 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -1,5 +1,6 @@ class CropsController < ApplicationController load_and_authorize_resource + skip_authorize_resource :only => :hierarchy cache_sweeper :crop_sweeper @@ -15,7 +16,7 @@ class CropsController < ApplicationController end end - # GET /wrangle + # GET /crops/wrangle def wrangle @crops = Crop.recent.paginate(:page => params[:page]) @@ -24,6 +25,13 @@ class CropsController < ApplicationController end end + # GET /crops/hierarchy + def hierarchy + respond_to do |format| + format.html + end + end + # GET /crops/1 # GET /crops/1.json def show diff --git a/app/views/crops/hierarchy.html.haml b/app/views/crops/hierarchy.html.haml new file mode 100644 index 000000000..27abb46cd --- /dev/null +++ b/app/views/crops/hierarchy.html.haml @@ -0,0 +1,6 @@ +- content_for :title, "Crop Hierarchy" + +%p + This page shows the hierarchical tree of all crops in our + = succeed "." do + = link_to "crops database", crops_path diff --git a/config/routes.rb b/config/routes.rb index fb1b5689c..4ad09136e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -22,6 +22,7 @@ Growstuff::Application.routes.draw do resources :scientific_names match 'crops/wrangle' => 'crops#wrangle', :as => 'wrangle_crops' + match 'crops/hierarchy' => 'crops#hierarchy', :as => 'crops_hierarchy' resources :crops resources :comments diff --git a/spec/controllers/crops_controller_spec.rb b/spec/controllers/crops_controller_spec.rb index 27901bc06..9d2e7124c 100644 --- a/spec/controllers/crops_controller_spec.rb +++ b/spec/controllers/crops_controller_spec.rb @@ -19,6 +19,14 @@ describe CropsController do end end + describe "GET crop hierarchy " do + it 'fetches the crop hierarchy page' do + get :hierarchy + response.should be_success + response.should render_template("crops/hierarchy") + end + end + describe "GET RSS feed" do it "returns an RSS feed" do get :index, :format => "rss" From 4dbfecd3159c5565f11226145f67e7577f4029ca Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 2 Sep 2013 11:52:01 +1000 Subject: [PATCH 2/6] Added toplevel scope for crops without a parent --- app/models/crop.rb | 1 + spec/models/crop_spec.rb | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/app/models/crop.rb b/app/models/crop.rb index 9717eb1da..8bae75bc4 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -14,6 +14,7 @@ class Crop < ActiveRecord::Base default_scope order("lower(system_name) asc") scope :recent, reorder("created_at desc") + scope :toplevel, where(:parent_id => nil) scope :randomized, reorder('random()') # ok on sqlite and psql, but not on mysql validates :en_wikipedia_url, diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index f7f606736..e872d2ecd 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -81,6 +81,12 @@ describe Crop do @roma.parent.should eq @tomato @tomato.varieties.should eq [@roma] end + + it 'toplevel scope works' do + @tomato = FactoryGirl.create(:tomato) + @roma = FactoryGirl.create(:roma, :parent_id => @tomato.id) + Crop.toplevel.should eq [ @tomato ] + end end context 'photos' do From 995f9b2d60e7d45efcb7acef1d7eaa9a93dca7e9 Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 2 Sep 2013 12:01:58 +1000 Subject: [PATCH 3/6] Display crop hierarchy (recursively!) --- app/controllers/crops_controller.rb | 1 + app/views/crops/_hierarchy.html.haml | 7 +++++++ app/views/crops/hierarchy.html.haml | 2 ++ spec/views/crops/hierarchy.html.haml_spec.rb | 15 +++++++++++++++ 4 files changed, 25 insertions(+) create mode 100644 app/views/crops/_hierarchy.html.haml create mode 100644 spec/views/crops/hierarchy.html.haml_spec.rb diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 29f15543f..29725d959 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -27,6 +27,7 @@ class CropsController < ApplicationController # GET /crops/hierarchy def hierarchy + @crops = Crop.toplevel respond_to do |format| format.html end diff --git a/app/views/crops/_hierarchy.html.haml b/app/views/crops/_hierarchy.html.haml new file mode 100644 index 000000000..54ec68d7e --- /dev/null +++ b/app/views/crops/_hierarchy.html.haml @@ -0,0 +1,7 @@ +%ul + - display_crops.each do |c| + %li + = link_to c, c + - if c.varieties.present? + - c.varieties.each do |v| + = render :partial => 'hierarchy', :locals => { :display_crops => [ v ] } diff --git a/app/views/crops/hierarchy.html.haml b/app/views/crops/hierarchy.html.haml index 27abb46cd..59995ab51 100644 --- a/app/views/crops/hierarchy.html.haml +++ b/app/views/crops/hierarchy.html.haml @@ -4,3 +4,5 @@ This page shows the hierarchical tree of all crops in our = succeed "." do = link_to "crops database", crops_path + += render :partial => "hierarchy", :locals => { :display_crops => @crops } diff --git a/spec/views/crops/hierarchy.html.haml_spec.rb b/spec/views/crops/hierarchy.html.haml_spec.rb new file mode 100644 index 000000000..5af869938 --- /dev/null +++ b/spec/views/crops/hierarchy.html.haml_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe "crops/hierarchy" do + before(:each) do + controller.stub(:current_user) { nil } + @tomato = FactoryGirl.create(:tomato) + @roma = FactoryGirl.create(:crop, :system_name => 'Roma tomato', :parent => @tomato) + assign(:crops, [@tomato, @roma]) + render + end + + it "shows crop hierarchy" do + assert_select "ul>li>ul>li", :text => @roma.system_name + end +end From cf9c1d7aa307370aeda5ca5a97d67e6b6e335dd2 Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 2 Sep 2013 12:02:34 +1000 Subject: [PATCH 4/6] Make all list items use disc style (filled-in circle) --- app/assets/stylesheets/bootstrap_and_overrides.css.less | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/assets/stylesheets/bootstrap_and_overrides.css.less b/app/assets/stylesheets/bootstrap_and_overrides.css.less index 6bc825068..f6d386f95 100644 --- a/app/assets/stylesheets/bootstrap_and_overrides.css.less +++ b/app/assets/stylesheets/bootstrap_and_overrides.css.less @@ -156,3 +156,7 @@ p.stats { padding-left: 0; } } + +li { + list-style-type: disc; +} From 9e190d7345c7ba1170b8af56abd9998f5806affd Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 2 Sep 2013 12:03:28 +1000 Subject: [PATCH 5/6] Show new hierarchical varieties on crop page --- app/views/crops/_varieties.html.haml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/views/crops/_varieties.html.haml b/app/views/crops/_varieties.html.haml index e268ac7b4..c2eb2163c 100644 --- a/app/views/crops/_varieties.html.haml +++ b/app/views/crops/_varieties.html.haml @@ -6,7 +6,5 @@ = link_to crop.parent, crop.parent - if crop.varieties.count > 0 %p - Varieties of - = succeed ":" do - = crop.system_name - != crop.varieties.map{ |c| link_to c, c }.join(", ") + Varieties: + = render :partial => 'hierarchy', :locals => { :display_crops => [ crop ] } From be6d6fd6bd07af4367e6ae4ced7444b094038cbc Mon Sep 17 00:00:00 2001 From: Skud Date: Mon, 2 Sep 2013 12:07:58 +1000 Subject: [PATCH 6/6] Added a crop hierarchy link to the crop wrangler page --- app/views/crops/wrangle.html.haml | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/crops/wrangle.html.haml b/app/views/crops/wrangle.html.haml index a52107f2d..e1988412c 100644 --- a/app/views/crops/wrangle.html.haml +++ b/app/views/crops/wrangle.html.haml @@ -5,6 +5,7 @@ %li= link_to "Requests for new crops", 'http://growstuff.org/posts/skud-20130319-requests-for-new-crops' %li= link_to "Crop wrangler guidelines", "http://wiki.growstuff.org/index.php/Crop_wrangling" %li= link_to "crop-wranglers mailing list", "http://lists.growstuff.org/listinfo/crop-wranglers" + %li= link_to "Full crop hierarchy", crops_hierarchy_path %li= link_to "Add Crop", new_crop_path %h2 Recently added crops