From cf07ecfa4aa9e96c8100437653b7088b6bfb5a20 Mon Sep 17 00:00:00 2001 From: Shiho Takagi Date: Sun, 5 Oct 2014 23:16:46 +1100 Subject: [PATCH] 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