Skip to content
Browse files

Normalizing the dependencies by creating requirements

  • Loading branch information...
1 parent 4230017 commit 832307d81081b191183649a5f8463c42cba4a0d6 @qrush qrush committed Jun 10, 2009
View
10 app/models/dependency.rb
@@ -1,5 +1,11 @@
class Dependency < ActiveRecord::Base
- belongs_to :version
- validates_presence_of :name, :requirement
+ belongs_to :rubygem
+ validates_presence_of :name
+ attr_accessor :rubygem_name
+ before_validation :link_rubygem
+ protected
+ def link_rubygem
+ self.rubygem = Rubygem.find_or_initialize_by_name(self.rubygem_name)
+ end
end
View
4 app/models/requirement.rb
@@ -0,0 +1,4 @@
+class Requirement < ActiveRecord::Base
+ belongs_to :version
+ belongs_to :dependency
+end
View
4 app/models/rubygem.rb
@@ -64,8 +64,8 @@ def build
self.spec.dependencies.each do |dependency|
version.dependencies.build(
- :name => dependency.name,
- :requirement => dependency.requirements_list.to_s)
+ :rubygem_name => dependency.name,
+ :name => dependency.requirements_list.to_s)
end
self.build_linkset(:home => self.spec.homepage)
View
3 app/models/version.rb
@@ -1,7 +1,8 @@
class Version < ActiveRecord::Base
include Pacecar
belongs_to :rubygem
- has_many :dependencies, :dependent => :destroy
+ has_many :requirements, :dependent => :destroy
+ has_many :dependencies, :through => :requirements
def to_s
number
View
17 db/migrate/20090610115456_add_requirements.rb
@@ -0,0 +1,17 @@
+class AddRequirements < ActiveRecord::Migration
+ def self.up
+ create_table :requirements do |t|
+ t.integer "version_id"
+ t.integer "dependency_id"
+ end
+
+ remove_column :dependencies, :name
+ add_column :dependencies, :rubygem_id, :integer
+ end
+
+ def self.down
+ drop_table :requirements
+ add_column :dependencies, :name, :string
+ remove_column :dependencies, :rubygem_id
+ end
+end
View
9 db/migrate/20090610121103_rename_requirement_to_name.rb
@@ -0,0 +1,9 @@
+class RenameRequirementToName < ActiveRecord::Migration
+ def self.up
+ rename_column :dependencies, :requirement, :name
+ end
+
+ def self.down
+ rename_column :dependencies, :name, :requirement
+ end
+end
View
9 db/migrate/20090610121428_remove_version_from_dependencies.rb
@@ -0,0 +1,9 @@
+class RemoveVersionFromDependencies < ActiveRecord::Migration
+ def self.up
+ remove_column :dependencies, :version_id
+ end
+
+ def self.down
+ add_column :dependencies, :version_id, :integer
+ end
+end
View
11 db/schema.rb
@@ -9,14 +9,13 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20090608111256) do
+ActiveRecord::Schema.define(:version => 20090610121428) do
create_table "dependencies", :force => true do |t|
t.string "name"
- t.string "requirement"
t.datetime "created_at"
t.datetime "updated_at"
- t.integer "version_id"
+ t.integer "rubygem_id"
end
create_table "linksets", :force => true do |t|
@@ -43,9 +42,13 @@
add_index "ownerships", ["rubygem_id"], :name => "index_ownerships_on_rubygem_id"
add_index "ownerships", ["user_id"], :name => "index_ownerships_on_user_id"
+ create_table "requirements", :force => true do |t|
+ t.integer "version_id"
+ t.integer "dependency_id"
+ end
+
create_table "rubygems", :force => true do |t|
t.string "name"
- t.string "token"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "downloads", :default => 0
View
3 test/factories/dependency.rb
@@ -1,4 +1,3 @@
Factory.define :dependency do |dependency|
- dependency.name { 'string' }
- dependency.requirement { 'string' }
+ dependency.name { '0.0.0' }
end
View
4 test/factories/requirement.rb
@@ -0,0 +1,4 @@
+Factory.define :requirement do |requirement|
+ requirement.association(:version)
+ requirement.association(:dependency)
+end
View
1 test/factories/rubygem.rb
@@ -2,7 +2,6 @@
Factory.define :rubygem do |rubygem|
rubygem.name { Factory.next(:name) }
- rubygem.token { 'asdf' }
rubygem.spec { gem_spec }
rubygem.path { gem_file.path }
end
View
37 test/unit/dependency_test.rb
@@ -1,10 +1,39 @@
require File.dirname(__FILE__) + '/../test_helper'
class DependencyTest < ActiveSupport::TestCase
- should_belong_to :version
- should_validate_presence_of :name, :requirement
+ should_belong_to :rubygem
+ should_validate_presence_of :name
- should "be valid with factory" do
- assert_valid Factory.build(:dependency)
+ context "with a dependency" do
+ setup do
+ @dependency = Factory.build(:dependency)
+ end
+
+ should "have a rubygem name so it can be linked" do
+ assert @dependency.respond_to?(:rubygem_name)
+ end
+
+ context "linking new rubygem" do
+ setup do
+ @name = "something"
+ @dependency.rubygem_name = @name
+ @dependency.save
+ end
+ should_change "Rubygem.count"
+ should "link rubygem" do
+ assert_equal @name, @dependency.rubygem.name
+ end
+ end
+
+ context "linking existing rubygem" do
+ setup do
+ @rubygem = Factory(:rubygem)
+ @dependency.rubygem_name = @rubygem.name
+ @dependency.save
+ end
+ should "link rubygem" do
+ assert_equal @rubygem, @dependency.rubygem
+ end
+ end
end
end
View
17 test/unit/requirement_test.rb
@@ -0,0 +1,17 @@
+require File.dirname(__FILE__) + '/../test_helper'
+
+class RequirementTest < ActiveSupport::TestCase
+ should_belong_to :version
+ should_belong_to :dependency
+
+ context "with requirement" do
+ setup do
+ @requirement = Factory.build(:requirement)
+ end
+
+ should "be valid with factory" do
+ assert_valid @requirement
+ end
+ end
+
+end
View
11 test/unit/rubygem_test.rb
@@ -65,15 +65,16 @@ class RubygemTest < ActiveSupport::TestCase
spec.add_dependency("liquid", ">= 1.9.0")
spec.add_dependency("open4", "= 0.9.6")
@rubygem.spec = spec
- @rubygem.build
+ @rubygem.save
+ assert_equal 2, @rubygem.current_version.requirements.size
assert_equal 2, @rubygem.current_dependencies.size
- assert_equal "liquid", @rubygem.current_dependencies.first.name
- assert_equal ">= 1.9.0", @rubygem.current_dependencies.first.requirement
+ assert_equal "liquid", @rubygem.current_dependencies.first.rubygem.name
+ assert_equal ">= 1.9.0", @rubygem.current_dependencies.first.name
- assert_equal "open4", @rubygem.current_dependencies.last.name
- assert_equal "= 0.9.6", @rubygem.current_dependencies.last.requirement
+ assert_equal "open4", @rubygem.current_dependencies.last.rubygem.name
+ assert_equal "= 0.9.6", @rubygem.current_dependencies.last.name
end
should "include platform when saving version" do
View
3 test/unit/version_test.rb
@@ -2,7 +2,8 @@
class VersionTest < ActiveSupport::TestCase
should_belong_to :rubygem
- should_have_many :dependencies, :dependent => :destroy
+ should_have_many :requirements, :dependent => :destroy
+ should_have_many :dependencies, :through => :requirements
context "with a version" do
setup do

0 comments on commit 832307d

Please sign in to comment.
Something went wrong with that request. Please try again.