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; +} diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 4238c1031..29725d959 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,14 @@ class CropsController < ApplicationController end end + # GET /crops/hierarchy + def hierarchy + @crops = Crop.toplevel + respond_to do |format| + format.html + end + end + # GET /crops/1 # GET /crops/1.json def show 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/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/_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 ] } diff --git a/app/views/crops/hierarchy.html.haml b/app/views/crops/hierarchy.html.haml new file mode 100644 index 000000000..59995ab51 --- /dev/null +++ b/app/views/crops/hierarchy.html.haml @@ -0,0 +1,8 @@ +- 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 + += render :partial => "hierarchy", :locals => { :display_crops => @crops } 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 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" 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 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