Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Merged
merged 5 commits into from

2 participants

@timkurvers

To ensure future ORMs can have seed data, this pull-request ensures that all operations go through the abstract model. AbstractModel#where has been added which defaults to ActiveRecord/MongoID find-behaviour.

Suggestions more than welcome :)

@mshibuya
Collaborator

I agree, this kind of abstraction is surely needed for DataMapper support :+1:
But assigning AbstractModel's instances to constants(such as AgonisticUser) might be a bit confusing.
Using local variables instead will be simpler, in my personal view.

And we need a test for AbstractModel#where method!

@timkurvers

Good point about the constants. Any proposals? A local such as abstract_user_model for example?

Had completely forgotten about the test. Might have to familiarize myself with the test suite, but eventually there shall be one!

@mshibuya
Collaborator

Yeah, abstract_user_model or user_model should be just fine, I guess.

Please update this pull request if you have done with writing tests. Then I'll merge this in.

@timkurvers

Have added the specs and tweaked the variable names :)

@mshibuya mshibuya merged commit c2c4f91 into from
@mshibuya
Collaborator

Merged.
Thanks for your work, and good luck with implementing DataMapper support!

@timkurvers

Thank you for the work on abstraction, sheer awesomeness :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
4 lib/rails_admin/abstract_model.rb
@@ -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
27 spec/dummy_app/db/seeds.rb
@@ -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"
View
4 spec/unit/adapters/active_record_spec.rb
@@ -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
View
4 spec/unit/adapters/mongoid_spec.rb
@@ -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
Something went wrong with that request. Please try again.