Ensure seeds are applied in an ORM-agnostic manner #1069

Merged
merged 5 commits into from Apr 15, 2012
@@ -76,6 +76,10 @@ def pretty_name
model.model_name.human
end
+ def where(conditions)
+ model.where(conditions)
+ end
+
private
def get_filtering_duration(operator, value)
View
@@ -1,18 +1,29 @@
require 'mlb'
-User.create(:email => 'username@example.com', :password => 'password', :password_confirmation => 'password')
+user_model = RailsAdmin::AbstractModel.new(User)
+league_model = RailsAdmin::AbstractModel.new(League)
+division_model = RailsAdmin::AbstractModel.new(Division)
+team_model = RailsAdmin::AbstractModel.new(Team)
+player_model = RailsAdmin::AbstractModel.new(Player)
+
+user_model.new(:email => 'username@example.com', :password => 'password', :password_confirmation => 'password').save
MLB::Team.all.each do |mlb_team|
- unless league = League.where(:name => mlb_team.league).first
- league = League.create(:name => mlb_team.league)
+ unless league = league_model.where(:name => mlb_team.league).first
+ league = league_model.new(:name => mlb_team.league)
+ league.save
end
- unless division = Division.where(:name => mlb_team.division).first
- division = Division.create(:name => mlb_team.division, :league => league)
+ unless division = division_model.where(:name => mlb_team.division).first
+ division = division_model.new(:name => mlb_team.division, :league => league)
+ division.save
end
- unless team = Team.where(:name => mlb_team.name).first
- team = Team.create(:name => mlb_team.name, :logo_url => mlb_team.logo_url, :manager => mlb_team.manager, :ballpark => mlb_team.ballpark, :mascot => mlb_team.mascot, :founded => mlb_team.founded, :wins => mlb_team.wins, :losses => mlb_team.losses, :win_percentage => ("%.3f" % (mlb_team.wins.to_f / (mlb_team.wins + mlb_team.losses))).to_f, :division => division)
+ unless team = team_model.where(:name => mlb_team.name).first
+ team = team_model.new(:name => mlb_team.name, :logo_url => mlb_team.logo_url, :manager => mlb_team.manager, :ballpark => mlb_team.ballpark, :mascot => mlb_team.mascot, :founded => mlb_team.founded, :wins => mlb_team.wins, :losses => mlb_team.losses, :win_percentage => ("%.3f" % (mlb_team.wins.to_f / (mlb_team.wins + mlb_team.losses))).to_f, :division => division)
+ team.save
end
mlb_team.players.reject{|player| player.number.nil?}.each do |player|
- Player.create(:name => player.name, :number => player.number, :position => player.position, :team => team)
+ player_model.new(:name => player.name, :number => player.number, :position => player.position, :team => team).save
end
end
+
+puts "Seeded #{league_model.count} leagues, #{division_model.count} divisions, #{team_model.count} teams and #{player_model.count} players"
@@ -192,6 +192,10 @@ class ARComment < ActiveRecord::Base
Player.all.should == @players[2..2]
end
+ it "#where returns filtered results" do
+ @abstract_model.where(:name => @players.first.name).should == [@players.first]
+ end
+
describe "#all" do
it "works without options" do
@abstract_model.all.should =~ @players
@@ -413,6 +413,10 @@ class MongoEmbedded
Player.all.should == @players[2..2]
end
+ it "#where returns filtered results" do
+ @abstract_model.where(:name => @players.first.name).to_a.should == [@players.first]
+ end
+
describe "#all" do
it "works without options" do
@abstract_model.all.to_a.should =~ @players