improved test spec, crop page and added rake task

This commit is contained in:
Shiho Takagi
2014-10-05 23:16:46 +11:00
parent 6e02f40e0a
commit cf07ecfa4a
6 changed files with 42 additions and 35 deletions

View File

@@ -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

View File

@@ -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|

View File

@@ -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

View File

@@ -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

View File

@@ -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

View File

@@ -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