Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proper associations, testing both ways
- Loading branch information
Showing
15 changed files
with
169 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,26 @@ | ||
# == Schema Information | ||
# Schema version: 20100618185553 | ||
# Schema version: 20100618200803 | ||
# | ||
# Table name: farms | ||
# | ||
# id :integer not null, primary key | ||
# name :string(255) | ||
# organic :boolean | ||
# latitude :float | ||
# longitude :float | ||
# altitude :float | ||
# created_at :datetime | ||
# updated_at :datetime | ||
# id :integer not null, primary key | ||
# name :string(255) | ||
# organic :boolean | ||
# latitude :float | ||
# longitude :float | ||
# altitude :float | ||
# created_at :datetime | ||
# updated_at :datetime | ||
# organic_certifier_id :integer | ||
# farmer_id :integer | ||
# | ||
|
||
class Farm < ActiveRecord::Base | ||
attr_accessible :name, :organic, :latitude, :longitude, :altitude | ||
|
||
validates_presence_of :name, :latitude, :longitude, :altitude | ||
validates_inclusion_of :organic, :in => [true, false] | ||
|
||
belongs_to :organic_certifier | ||
belongs_to :farmer | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,20 @@ | ||
# == Schema Information | ||
# Schema version: 20100618185553 | ||
# Schema version: 20100618200803 | ||
# | ||
# Table name: farmers | ||
# | ||
# id :integer not null, primary key | ||
# name :string(255) | ||
# created_at :datetime | ||
# updated_at :datetime | ||
# id :integer not null, primary key | ||
# name :string(255) | ||
# created_at :datetime | ||
# updated_at :datetime | ||
# washing_station_id :integer | ||
# | ||
|
||
class Farmer < ActiveRecord::Base | ||
attr_accessible :name | ||
|
||
validates_presence_of :name | ||
|
||
has_many :farms | ||
belongs_to :washing_station | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
db/migrate/20100618200656_add_washing_station_id_to_farmer.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class AddWashingStationIdToFarmer < ActiveRecord::Migration | ||
def self.up | ||
add_column :farmers, :washing_station_id, :integer | ||
add_index :farmers, :washing_station_id | ||
end | ||
|
||
def self.down | ||
remove_column :farmers, :washing_station_id | ||
end | ||
end |
10 changes: 10 additions & 0 deletions
10
db/migrate/20100618200737_add_organic_certifier_id_to_farm.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class AddOrganicCertifierIdToFarm < ActiveRecord::Migration | ||
def self.up | ||
add_column :farms, :organic_certifier_id, :integer | ||
add_index :farms, :organic_certifier_id | ||
end | ||
|
||
def self.down | ||
remove_column :farms, :organic_certifier_id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class AddFarmerIdToFarm < ActiveRecord::Migration | ||
def self.up | ||
add_column :farms, :farmer_id, :integer | ||
add_index :farms, :farmer_id | ||
end | ||
|
||
def self.down | ||
remove_column :farms, :farmer_id | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,14 @@ | ||
Factory.define :farmer do |farmer| | ||
farmer.name "A Farmer" | ||
end | ||
|
||
Factory.define :washing_station do |washing_station| | ||
washing_station.name "Washing Station 1" | ||
washing_station.code "abcabc123" | ||
end | ||
|
||
Factory.define :organic_certifier do |organic_certifier| | ||
organic_certifier.name "Organic Certifier 1" | ||
organic_certifier.code "123abced" | ||
end | ||
|
||
Factory.define :farmer do |farmer| | ||
farmer.name "A Farmer" | ||
farmer.association :washing_station | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,33 +1,47 @@ | ||
require 'spec_helper' | ||
|
||
describe Farmer do | ||
@washing_station = Factory(:washing_station) | ||
|
||
before(:each) do | ||
@washing_station = Factory(:washing_station) | ||
@valid_attributes = { | ||
:name => "Farmer Name" | ||
} | ||
end | ||
|
||
it "should create a new instance given valid attributes" do | ||
Farmer.create!(@valid_attributes) | ||
@washing_station.farmers.create!(@valid_attributes) | ||
end | ||
|
||
it "should require a name" do | ||
no_name_farmer = Farmer.new(@valid_attributes.merge(:name => "")) | ||
no_name_farmer.should_not be_valid | ||
end | ||
|
||
# describe "washing Station associations" do | ||
# before(:each) do | ||
# @farmer = @washing_station.farmer.create(@valid_attributes) | ||
# end | ||
# | ||
# it "should have a washing station attribute" do | ||
# @farmer.should respond_to(:washing_station) | ||
# end | ||
# | ||
# it "should have the right associated washing station" do | ||
# @farmer.washing_station.should == @washing_station | ||
# end | ||
# end | ||
describe "washing station associations" do | ||
|
||
before(:each) do | ||
@farmer = @washing_station.farmers.create(@valid_attributes) | ||
end | ||
|
||
it "should have a washing station attribute" do | ||
@farmer.should respond_to(:washing_station) | ||
end | ||
|
||
it "should have the right associated washing station" do | ||
@farmer.washing_station.should == @washing_station | ||
end | ||
end | ||
|
||
describe "farm associations" do | ||
|
||
before(:each) do | ||
@farmer = Farmer.create(@valid_attributes) | ||
end | ||
|
||
it "should have a farm attribute" do | ||
@farmer.should respond_to(:farms) | ||
end | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters