From 14d039ee018b4d877df10c17285d7a0f471fecd4 Mon Sep 17 00:00:00 2001 From: Shiho Takagi Date: Mon, 29 Sep 2014 22:37:03 +1000 Subject: [PATCH 1/2] Added crops-posts association as well as updated GUI for crop show --- app/controllers/crops_controller.rb | 1 + app/controllers/harvests_controller.rb | 18 +++--- app/controllers/plantings_controller.rb | 18 +++--- app/models/crop.rb | 3 + app/models/post.rb | 14 ++++ app/views/crops/_harvests.html.haml | 8 ++- app/views/crops/_plantings.html.haml | 21 ++++++ app/views/crops/show.html.haml | 14 ++-- app/views/harvests/index.html.haml | 2 +- app/views/plantings/index.html.haml | 2 +- config/routes.rb | 2 + .../20140928044231_add_crops_posts_table.rb | 10 +++ db/schema.rb | 10 ++- lib/haml/filters/growstuff_markdown.rb | 3 +- lib/tasks/growstuff.rake | 7 ++ spec/controllers/harvests_controller_spec.rb | 24 ++++++- spec/controllers/plantings_controller_spec.rb | 28 ++++++-- spec/models/crop_spec.rb | 18 ++++++ spec/models/post_spec.rb | 37 +++++++++++ spec/views/crops/show.html.haml_spec.rb | 64 ++++++++++++++++--- spec/views/harvests/index.html.haml_spec.rb | 13 ++++ spec/views/plantings/index.html.haml_spec.rb | 11 ++++ 22 files changed, 281 insertions(+), 47 deletions(-) create mode 100644 app/views/crops/_plantings.html.haml create mode 100644 db/migrate/20140928044231_add_crops_posts_table.rb diff --git a/app/controllers/crops_controller.rb b/app/controllers/crops_controller.rb index 1316350de..a30de159a 100644 --- a/app/controllers/crops_controller.rb +++ b/app/controllers/crops_controller.rb @@ -69,6 +69,7 @@ class CropsController < ApplicationController # GET /crops/1.json def show @crop = Crop.includes(:scientific_names, {:plantings => :photos}).find(params[:id]) + @posts = @crop.posts.paginate(:page => params[:page]) respond_to do |format| format.html # show.html.haml diff --git a/app/controllers/harvests_controller.rb b/app/controllers/harvests_controller.rb index 96e029ac3..524007d47 100644 --- a/app/controllers/harvests_controller.rb +++ b/app/controllers/harvests_controller.rb @@ -6,23 +6,21 @@ class HarvestsController < ApplicationController # GET /harvests.json def index @owner = Member.find_by_slug(params[:owner]) + @crop = Crop.find_by_slug(params[:crop]) if @owner - @harvests = @owner.harvests.includes(:owner, :crop).paginate(:page => params[:page]) + @harvests = @owner.harvests.includes(:owner, :crop) + elsif @crop + @harvests = @crop.harvests.includes(:owner, :crop) else - @harvests = Harvest.includes(:owner, :crop).paginate(:page => params[:page]) + @harvests = Harvest.includes(:owner, :crop) end respond_to do |format| - format.html # index.html.erb + format.html { @harvests = @harvests.paginate(:page => params[:page]) } format.json { render json: @harvests } format.csv do - if @owner - @filename = "Growstuff-#{@owner}-Harvests-#{Time.zone.now.to_s(:number)}.csv" - @harvests = @owner.harvests.includes(:owner, :crop) - else - @filename = "Growstuff-Harvests-#{Time.zone.now.to_s(:number)}.csv" - @harvests = Harvest.includes(:owner, :crop) - end + specifics = (@owner ? "#{@owner.name}-" : @crop ? "#{@crop.name}-" : nil) + @filename = "Growstuff-#{specifics}Harvests-#{Time.zone.now.to_s(:number)}.csv" render :csv => @harvests end end diff --git a/app/controllers/plantings_controller.rb b/app/controllers/plantings_controller.rb index 0d419d2f1..62eaf5b79 100644 --- a/app/controllers/plantings_controller.rb +++ b/app/controllers/plantings_controller.rb @@ -7,24 +7,22 @@ class PlantingsController < ApplicationController # GET /plantings.json def index @owner = Member.find_by_slug(params[:owner]) + @crop = Crop.find_by_slug(params[:crop]) if @owner - @plantings = @owner.plantings.includes(:owner, :crop, :garden).paginate(:page => params[:page]) + @plantings = @owner.plantings.includes(:owner, :crop, :garden) + elsif @crop + @plantings = @crop.plantings.includes(:owner, :crop, :garden) else - @plantings = Planting.includes(:owner, :crop, :garden).paginate(:page => params[:page]) + @plantings = Planting.includes(:owner, :crop, :garden) end respond_to do |format| - format.html # index.html.erb + format.html { @plantings = @plantings.paginate(:page => params[:page]) } format.json { render json: @plantings } format.rss { render :layout => false } #index.rss.builder format.csv do - if @owner - @filename = "Growstuff-#{@owner}-Plantings-#{Time.zone.now.to_s(:number)}.csv" - @plantings = @owner.plantings.includes(:owner, :crop, :garden) - else - @filename = "Growstuff-Plantings-#{Time.zone.now.to_s(:number)}.csv" - @plantings = Planting.includes(:owner, :crop, :garden) - end + specifics = (@owner ? "#{@owner.name}-" : @crop ? "#{@crop.name}-" : nil) + @filename = "Growstuff-#{specifics}Plantings-#{Time.zone.now.to_s(:number)}.csv" render :csv => @plantings end end diff --git a/app/models/crop.rb b/app/models/crop.rb index 3efe8be57..387936d91 100644 --- a/app/models/crop.rb +++ b/app/models/crop.rb @@ -17,6 +17,9 @@ class Crop < ActiveRecord::Base belongs_to :parent, :class_name => 'Crop' has_many :varieties, :class_name => 'Crop', :foreign_key => 'parent_id' + has_and_belongs_to_many :posts + before_destroy {|crop| crop.posts.clear} + default_scope order("lower(name) asc") scope :recent, reorder("created_at desc") diff --git a/app/models/post.rb b/app/models/post.rb index 3a853c18a..7f72a4e57 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -5,6 +5,9 @@ class Post < ActiveRecord::Base belongs_to :author, :class_name => 'Member' belongs_to :forum has_many :comments, :dependent => :destroy + has_and_belongs_to_many :crops + before_destroy {|post| post.crops.clear} + after_save :update_crops_posts_association # also has_many notifications, but kinda meaningless to get at them # from this direction, so we won't set up an association for now. @@ -39,4 +42,15 @@ class Post < ActiveRecord::Base end end + private + def update_crops_posts_association + self.crops.destroy_all + # look for crops mentioned in the post. eg. [tomato](crop) + self.body.scan(Haml::Filters::GrowstuffMarkdown::CROP_REGEX) do |m| + # find crop case-insensitively + crop = Crop.where('lower(name) = ?', $1.downcase).first + # create association + self.crops << crop if crop and not self.crops.include?(crop) + end + end end diff --git a/app/views/crops/_harvests.html.haml b/app/views/crops/_harvests.html.haml index b8cb25447..d10f57d06 100644 --- a/app/views/crops/_harvests.html.haml +++ b/app/views/crops/_harvests.html.haml @@ -4,16 +4,18 @@ Nobody has harvested this crop yet. - else %ul - - crop.harvests.each do |harvest| + - crop.harvests.take(3).each do |harvest| %li = link_to "#{harvest.owner} harvested #{display_quantity(harvest)}.", harvest_path(harvest) = render :partial => 'members/location', :locals => { :member => harvest.owner } %small = distance_of_time_in_words(harvest.created_at, Time.zone.now) ago. - + %p.col-md-offset-1 + = link_to "See all #{crop.name} harvests", harvests_by_crop_path(crop) - if current_member - = link_to "Track your #{crop.name} harvests.", new_harvest_path() + %p.col-md-offset-1 + = link_to "Track your #{crop.name} harvests.", new_harvest_path(:crop_id => crop.id) - else = render :partial => 'shared/signin_signup', :locals => { :to => "track your #{crop.name} harvests" } diff --git a/app/views/crops/_plantings.html.haml b/app/views/crops/_plantings.html.haml new file mode 100644 index 000000000..72bce5608 --- /dev/null +++ b/app/views/crops/_plantings.html.haml @@ -0,0 +1,21 @@ +%h4 Plantings +- if crop.plantings.empty? + %p + Nobody has planted this crop yet. +- else + %ul + - crop.plantings.take(3).each do |planting| + %li + = link_to "#{planting.owner} planted #{planting.quantity} #{planting.planted_from}.", planting_path(planting) + = render :partial => 'members/location', :locals => { :member => planting.owner } + %small + = distance_of_time_in_words(planting.created_at, Time.zone.now) + ago. + %p.col-md-offset-1 + = link_to "See all #{crop.name} plantings", plantings_by_crop_path(crop) +- if current_member + %p.col-md-offset-1 + = link_to "Track your #{crop.name} plantings.", new_planting_path(:crop_id => crop.id) +- else + = render :partial => 'shared/signin_signup', :locals => { :to => "track your #{crop.name} plantings" } + diff --git a/app/views/crops/show.html.haml b/app/views/crops/show.html.haml index b79320698..d28084280 100644 --- a/app/views/crops/show.html.haml +++ b/app/views/crops/show.html.haml @@ -33,12 +33,17 @@ %div#cropmap - - if @crop.plantings.size > 0 + %div.pagination + = page_entries_info @posts, :model => "posts" + = will_paginate @posts - %h2 All plantings + - unless @posts.empty? + - @posts.each do |post| + = render :partial => "posts/single", :locals => { :post => post, :subject => true } - - @crop.plantings.each do |p| - = render :partial => "plantings/thumbnail", :locals => { :planting => p, :title => 'owner' } + %div.pagination + = page_entries_info @posts, :model => "posts" + = will_paginate @posts .col-md-3 - if can? :edit, @crop or can? :destroy, @crop @@ -76,5 +81,6 @@ %ul %li= link_to 'Wikipedia (English)', @crop.en_wikipedia_url + = render :partial => 'plantings', :locals => { :crop => @crop } = render :partial => 'harvests', :locals => { :crop => @crop } = render :partial => 'find_seeds', :locals => { :crop => @crop } diff --git a/app/views/harvests/index.html.haml b/app/views/harvests/index.html.haml index 90bbb4638..f80ce0dd5 100644 --- a/app/views/harvests/index.html.haml +++ b/app/views/harvests/index.html.haml @@ -1,4 +1,4 @@ -- content_for :title, @owner ? "#{@owner}'s harvests" : "Everyone's harvests" +- content_for :title, @owner ? "#{@owner}'s harvests" : @crop ? "Everyone's #{@crop.name} harvests" : "Everyone's harvests" %p #{ENV['GROWSTUFF_SITE_NAME']} helps you track what you're diff --git a/app/views/plantings/index.html.haml b/app/views/plantings/index.html.haml index 091974ef8..eb1e017dc 100644 --- a/app/views/plantings/index.html.haml +++ b/app/views/plantings/index.html.haml @@ -1,4 +1,4 @@ -- content_for :title, @owner ? "#{@owner}'s plantings" : "Everyone's plantings" +- content_for :title, @owner ? "#{@owner}'s plantings" : @crop ? "Everyone's #{@crop.name} plantings" : "Everyone's plantings" %p - if can? :create, Planting diff --git a/config/routes.rb b/config/routes.rb index 360ed2477..0d645a1be 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -12,6 +12,7 @@ Growstuff::Application.routes.draw do resources :plantings match '/plantings/owner/:owner' => 'plantings#index', :as => 'plantings_by_owner' + match '/plantings/crop/:crop' => 'plantings#index', :as => 'plantings_by_crop' resources :gardens match '/gardens/owner/:owner' => 'gardens#index', :as => 'gardens_by_owner' @@ -21,6 +22,7 @@ Growstuff::Application.routes.draw do resources :harvests match '/harvests/owner/:owner' => 'harvests#index', :as => 'harvests_by_owner' + match '/harvests/crop/:crop' => 'harvests#index', :as => 'harvests_by_crop' resources :posts match '/posts/author/:author' => 'posts#index', :as => 'posts_by_author' diff --git a/db/migrate/20140928044231_add_crops_posts_table.rb b/db/migrate/20140928044231_add_crops_posts_table.rb new file mode 100644 index 000000000..a8c06927b --- /dev/null +++ b/db/migrate/20140928044231_add_crops_posts_table.rb @@ -0,0 +1,10 @@ +class AddCropsPostsTable < ActiveRecord::Migration + def change + create_table :crops_posts, :id => false do |t| + t.integer :crop_id + t.integer :post_id + end + add_index :crops_posts, [:crop_id, :post_id] + add_index :crops_posts, :crop_id + end +end \ No newline at end of file diff --git a/db/schema.rb b/db/schema.rb index 29271f5e6..e103777e6 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 => 20140829230600) do +ActiveRecord::Schema.define(:version => 20140928053257) do create_table "account_types", :force => true do |t| t.string "name", :null => false @@ -64,6 +64,14 @@ ActiveRecord::Schema.define(:version => 20140829230600) do add_index "crops", ["name"], :name => "index_crops_on_name" add_index "crops", ["slug"], :name => "index_crops_on_slug", :unique => true + create_table "crops_posts", :id => false, :force => true do |t| + t.integer "crop_id" + t.integer "post_id" + end + + add_index "crops_posts", ["crop_id", "post_id"], :name => "index_crops_posts_on_crop_id_and_post_id" + add_index "crops_posts", ["crop_id"], :name => "index_crops_posts_on_crop_id" + create_table "forums", :force => true do |t| t.string "name", :null => false t.text "description", :null => false diff --git a/lib/haml/filters/growstuff_markdown.rb b/lib/haml/filters/growstuff_markdown.rb index 7069590e7..dba081e44 100644 --- a/lib/haml/filters/growstuff_markdown.rb +++ b/lib/haml/filters/growstuff_markdown.rb @@ -2,12 +2,13 @@ require 'bluecloth' module Haml::Filters module GrowstuffMarkdown + CROP_REGEX = /\[([^\[\]]+?)\]\(crop\)/ include Haml::Filters::Base def render(text) # turn [tomato](crop) into [tomato](http://growstuff.org/crops/tomato) - expanded = text.gsub(/\[([^\[\]]+?)\]\(crop\)/) do |m| + expanded = text.gsub(CROP_REGEX) do |m| crop_str = $1 # find crop case-insensitively crop = Crop.where('lower(name) = ?', crop_str.downcase).first diff --git a/lib/tasks/growstuff.rake b/lib/tasks/growstuff.rake index 34739217b..3c4a676bc 100644 --- a/lib/tasks/growstuff.rake +++ b/lib/tasks/growstuff.rake @@ -256,6 +256,13 @@ namespace :growstuff do end end + desc "October 2014: generate crops_posts records for existing posts" + task :generate_crops_posts_records => :environment do + Post.find_each do |p| + p.send :update_crops_posts_association + end + end + end # end oneoff section end diff --git a/spec/controllers/harvests_controller_spec.rb b/spec/controllers/harvests_controller_spec.rb index f2ecb8e32..38b0edf8c 100644 --- a/spec/controllers/harvests_controller_spec.rb +++ b/spec/controllers/harvests_controller_spec.rb @@ -12,10 +12,30 @@ describe HarvestsController do end describe "GET index" do + before do + @member1 = FactoryGirl.create(:member) + @member2 = FactoryGirl.create(:member) + @tomato = FactoryGirl.create(:tomato) + @maize = FactoryGirl.create(:maize) + @harvest1 = FactoryGirl.create(:harvest, :owner_id => @member1.id, :crop_id => @tomato.id) + @harvest2 = FactoryGirl.create(:harvest, :owner_id => @member2.id, :crop_id => @maize.id) + end + it "assigns all harvests as @harvests" do - harvest = Harvest.create! valid_attributes get :index, {} - assigns(:harvests).should eq([harvest]) + assigns(:harvests).should =~ [@harvest1, @harvest2] + end + + it "picks up owner from params and shows owner's harvests only" do + get :index, {:owner => @member1.slug} + assigns(:owner).should eq @member1 + assigns(:harvests).should eq [@harvest1] + end + + it "picks up crop from params and shows the harvests for the crop only" do + get :index, {:crop => @maize.name} + assigns(:crop).should eq @maize + assigns(:harvests).should eq [@harvest2] end end diff --git a/spec/controllers/plantings_controller_spec.rb b/spec/controllers/plantings_controller_spec.rb index 7e6a75d22..4b3a518a5 100644 --- a/spec/controllers/plantings_controller_spec.rb +++ b/spec/controllers/plantings_controller_spec.rb @@ -12,10 +12,30 @@ describe PlantingsController do end describe "GET index" do - it "picks up owner from params" do - owner = FactoryGirl.create(:member) - get :index, {:owner => owner.slug} - assigns(:owner).should eq(owner) + before do + @member1 = FactoryGirl.create(:member) + @member2 = FactoryGirl.create(:member) + @tomato = FactoryGirl.create(:tomato) + @maize = FactoryGirl.create(:maize) + @planting1 = FactoryGirl.create(:planting, :crop => @tomato, :owner => @member1) + @planting2 = FactoryGirl.create(:planting, :crop => @maize, :owner => @member2) + end + + it "assigns all plantings as @plantings" do + get :index, {} + assigns(:plantings).should =~ [@planting1, @planting2] + end + + it "picks up owner from params and shows owner's plantings only" do + get :index, {:owner => @member1.slug} + assigns(:owner).should eq @member1 + assigns(:plantings).should eq [@planting1] + end + + it "picks up crop from params and shows the plantings for the crop only" do + get :index, {:crop => @maize.name} + assigns(:crop).should eq @maize + assigns(:plantings).should eq [@planting2] end end diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index 1741faf74..b54097070 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -343,4 +343,22 @@ describe Crop do end end + context "crop-post association" do + before { + @tomato = FactoryGirl.create(:tomato) + @maize = FactoryGirl.create(:maize) + @post = FactoryGirl.create(:post, :body => "[maize](crop)[tomato](crop)[tomato](crop)") + } + + describe "destroying a crop" do + before do + @tomato.destroy + end + + it "shouod delete the association but not the posts" do + Post.find_by_id(@post.id).should_not eq nil + Post.find_by_id(@post.id).crops.should eq [@maize] + end + end + end end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 441b9c6a9..3751c13c7 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -95,4 +95,41 @@ describe Post do end end + context "crop-post association" do + before { + @tomato = FactoryGirl.create(:tomato) + @maize = FactoryGirl.create(:maize) + @chard = FactoryGirl.create(:chard) + @post = FactoryGirl.create(:post, :body => "[maize](crop)[tomato](crop)[tomato](crop)") + } + + it "should be generated without duplicate" do + @post.crops.should =~ [@tomato, @maize] + @tomato.posts.should eq [@post] + @maize.posts.should eq [@post] + end + + it "should be updated when post was modified" do + @post.update_attributes(:body => "[chard](crop)") + + @post.crops.should eq [@chard] + @chard.posts.should eq [@post] + @tomato.posts.should eq [] + @maize.posts.should eq [] + end + + describe "destroying the post" do + before do + @crops = @post.crops + @post.destroy + end + + it "shouod delete the association but not the crops" do + Crop.find_by_id(@tomato.id).should_not eq nil + Crop.find_by_id(@maize.id).should_not eq nil + Crop.find_by_id(@tomato.id).posts.should eq [] + Crop.find_by_id(@maize.id).posts.should eq [] + end + end + end end diff --git a/spec/views/crops/show.html.haml_spec.rb b/spec/views/crops/show.html.haml_spec.rb index 6e8250b27..1ef461508 100644 --- a/spec/views/crops/show.html.haml_spec.rb +++ b/spec/views/crops/show.html.haml_spec.rb @@ -7,6 +7,16 @@ describe "crops/show" do :scientific_names => [ FactoryGirl.create(:zea_mays) ] ) assign(:crop, @crop) + @author = FactoryGirl.create(:member) + page = 1 + per_page = 2 + total_entries = 2 + @posts = WillPaginate::Collection.create(page, per_page, total_entries) do |pager| + pager.replace([ + @post1 = FactoryGirl.create(:post, :author => @author, :body => "Post it!" ), + @post2 = FactoryGirl.create(:post, :author => @author, :body => "Done!" ) + ]) + end end context 'photos' do @@ -125,11 +135,10 @@ describe "crops/show" do context "has plantings" do before(:each) do - @owner = FactoryGirl.create(:member) - @garden = FactoryGirl.create(:garden, :owner => @owner) + @owner = FactoryGirl.create(:london_member) @planting = FactoryGirl.create(:planting, - :garden => @garden, - :crop => @crop + :crop => @crop, + :owner => @owner ) @crop.reload # to pick up latest plantings_count end @@ -137,16 +146,24 @@ describe "crops/show" do it "links to people who are growing this crop" do render rendered.should contain @owner.login_name - rendered.should contain @garden.name + rendered.should contain @owner.location end + end - it "shows photos where available" do - @photo = FactoryGirl.create(:photo) - @planting.photos << @photo + context "has posts" do + it "links to posts" do render - assert_select "img", :src => @photo.thumbnail_url + @posts.each do |p| + rendered.should contain p.author.login_name + rendered.should contain p.subject + rendered.should contain p.body + end end + it "contains two gravatar icons" do + render + assert_select "img", :src => /gravatar\.com\/avatar/, :count => 2 + end end context 'varieties' do @@ -189,9 +206,36 @@ describe "crops/show" do rendered.should contain "Harvest this" end - it "links to the right crop in the planting link" do + it "links to the right crop in the new planting link" do assert_select("a[href=#{new_planting_path}?crop_id=#{@crop.id}]") end + + it "links to the right crop in the new harvest link" do + assert_select("a[href=#{new_harvest_path}?crop_id=#{@crop.id}]") + end + + it { rendered.should contain "Nobody has planted this crop yet" } + it { rendered.should contain "Nobody has harvested this crop yet" } + + context "should have a link to" do + before do + FactoryGirl.create(:planting, :crop => @crop) + FactoryGirl.create(:harvest, :crop => @crop) + @crop.reload + render + end + + it "show all plantings by the crop link" do + assert_select("a[href=#{plantings_by_crop_path @crop}]") + end + + it "show all harvests by the crop link" do + assert_select("a[href=#{harvests_by_crop_path @crop}]") + end + end + + + end context "logged in and crop wrangler" do diff --git a/spec/views/harvests/index.html.haml_spec.rb b/spec/views/harvests/index.html.haml_spec.rb index 3a147e0b6..093a383a9 100644 --- a/spec/views/harvests/index.html.haml_spec.rb +++ b/spec/views/harvests/index.html.haml_spec.rb @@ -41,4 +41,17 @@ describe "harvests/index" do assert_select "a", :href => harvests_path(:format => 'csv') assert_select "a", :href => harvests_path(:format => 'json') end + + it "displays member's name in title" do + assign(:owner, @member) + render + view.content_for(:title).should contain @member.login_name + end + + it "displays crop's name in title" do + assign(:crop, @tomato) + render + view.content_for(:title).should contain @tomato.name + end + end diff --git a/spec/views/plantings/index.html.haml_spec.rb b/spec/views/plantings/index.html.haml_spec.rb index 5ff890514..d46b4e003 100644 --- a/spec/views/plantings/index.html.haml_spec.rb +++ b/spec/views/plantings/index.html.haml_spec.rb @@ -48,4 +48,15 @@ describe "plantings/index" do assert_select "a", :href => plantings_path(:format => 'rss') end + it "displays member's name in title" do + assign(:owner, @member) + render + view.content_for(:title).should contain @member.login_name + end + + it "displays crop's name in title" do + assign(:crop, @tomato) + render + view.content_for(:title).should contain @tomato.name + end end From cf07ecfa4aa9e96c8100437653b7088b6bfb5a20 Mon Sep 17 00:00:00 2001 From: Shiho Takagi Date: Sun, 5 Oct 2014 23:16:46 +1100 Subject: [PATCH 2/2] improved test spec, crop page and added rake task --- app/views/crops/show.html.haml | 5 ++-- db/schema.rb | 1 - lib/tasks/growstuff.rake | 2 +- script/deploy-tasks.sh | 3 +++ spec/models/crop_spec.rb | 19 +++++++------- spec/models/post_spec.rb | 47 ++++++++++++++++++---------------- 6 files changed, 42 insertions(+), 35 deletions(-) diff --git a/app/views/crops/show.html.haml b/app/views/crops/show.html.haml index d28084280..670a0d438 100644 --- a/app/views/crops/show.html.haml +++ b/app/views/crops/show.html.haml @@ -33,9 +33,10 @@ %div#cropmap + %a{:name => 'posts'} %div.pagination = page_entries_info @posts, :model => "posts" - = will_paginate @posts + = will_paginate @posts, :params => {:anchor => "posts"} - unless @posts.empty? - @posts.each do |post| @@ -43,7 +44,7 @@ %div.pagination = page_entries_info @posts, :model => "posts" - = will_paginate @posts + = will_paginate @posts, :params => {:anchor => "posts"} .col-md-3 - if can? :edit, @crop or can? :destroy, @crop diff --git a/db/schema.rb b/db/schema.rb index e4425711c..9292a6fbb 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,6 @@ # # It's strongly recommended to check this file into your version control system. - ActiveRecord::Schema.define(:version => 20141002022459) do create_table "account_types", :force => true do |t| diff --git a/lib/tasks/growstuff.rake b/lib/tasks/growstuff.rake index 62046d87a..f19bf1fed 100644 --- a/lib/tasks/growstuff.rake +++ b/lib/tasks/growstuff.rake @@ -267,7 +267,7 @@ namespace :growstuff do desc "October 2014: generate crops_posts records for existing posts" task :generate_crops_posts_records => :environment do Post.find_each do |p| - p.send :update_crops_posts_association + p.save end end end # end oneoff section diff --git a/script/deploy-tasks.sh b/script/deploy-tasks.sh index 3c6dfb909..451fe0032 100755 --- a/script/deploy-tasks.sh +++ b/script/deploy-tasks.sh @@ -15,3 +15,6 @@ rake growstuff:import_crops file=db/seeds/crops-11-tomatoes.csv echo "2014-10-02 - remove unused photos" rake growstuff:oneoff:remove_unused_photos + +echo "2014-10-05 - generate crops_posts records for existing posts" +rake growstuff:oneoff:generate_crops_posts_records diff --git a/spec/models/crop_spec.rb b/spec/models/crop_spec.rb index b54097070..d9e3d4b0a 100644 --- a/spec/models/crop_spec.rb +++ b/spec/models/crop_spec.rb @@ -344,20 +344,21 @@ describe Crop do end context "crop-post association" do - before { - @tomato = FactoryGirl.create(:tomato) - @maize = FactoryGirl.create(:maize) - @post = FactoryGirl.create(:post, :body => "[maize](crop)[tomato](crop)[tomato](crop)") - } + let!(:tomato) { FactoryGirl.create(:tomato) } + let!(:maize) { FactoryGirl.create(:maize) } + let!(:post) { FactoryGirl.create(:post, :body => "[maize](crop)[tomato](crop)[tomato](crop)") } describe "destroying a crop" do before do - @tomato.destroy + tomato.destroy end - it "shouod delete the association but not the posts" do - Post.find_by_id(@post.id).should_not eq nil - Post.find_by_id(@post.id).crops.should eq [@maize] + it "should delete the association between post and the crop(tomato)" do + expect(Post.find(post).crops).to eq [maize] + end + + it "should not delete the posts" do + expect(Post.find(post)).to_not eq nil end end end diff --git a/spec/models/post_spec.rb b/spec/models/post_spec.rb index 3751c13c7..30c19c500 100644 --- a/spec/models/post_spec.rb +++ b/spec/models/post_spec.rb @@ -96,39 +96,42 @@ describe Post do end context "crop-post association" do - before { - @tomato = FactoryGirl.create(:tomato) - @maize = FactoryGirl.create(:maize) - @chard = FactoryGirl.create(:chard) - @post = FactoryGirl.create(:post, :body => "[maize](crop)[tomato](crop)[tomato](crop)") - } + let!(:tomato) { FactoryGirl.create(:tomato) } + let!(:maize) { FactoryGirl.create(:maize) } + let!(:chard) { FactoryGirl.create(:chard) } + let!(:post) { FactoryGirl.create(:post, :body => "[maize](crop)[tomato](crop)[tomato](crop)") } - it "should be generated without duplicate" do - @post.crops.should =~ [@tomato, @maize] - @tomato.posts.should eq [@post] - @maize.posts.should eq [@post] + it "should be generated" do + expect(tomato.posts).to eq [post] + expect(maize.posts).to eq [post] + end + + it "should not duplicate" do + expect(post.crops) =~ [tomato, maize] end it "should be updated when post was modified" do - @post.update_attributes(:body => "[chard](crop)") + post.update_attributes(:body => "[chard](crop)") - @post.crops.should eq [@chard] - @chard.posts.should eq [@post] - @tomato.posts.should eq [] - @maize.posts.should eq [] + expect(post.crops).to eq [chard] + expect(chard.posts).to eq [post] + expect(tomato.posts).to eq [] + expect(maize.posts).to eq [] end describe "destroying the post" do before do - @crops = @post.crops - @post.destroy + post.destroy end - it "shouod delete the association but not the crops" do - Crop.find_by_id(@tomato.id).should_not eq nil - Crop.find_by_id(@maize.id).should_not eq nil - Crop.find_by_id(@tomato.id).posts.should eq [] - Crop.find_by_id(@maize.id).posts.should eq [] + it "should delete the association" do + expect(Crop.find(tomato).posts).to eq [] + expect(Crop.find(maize).posts).to eq [] + end + + it "should not delete the crops" do + expect(Crop.find(tomato)).to_not eq nil + expect(Crop.find(maize)).to_not eq nil end end end