objectreload-foreign_key_extensions 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +2 -0
- data/README +0 -0
- data/Rakefile +55 -0
- data/VERSION +1 -0
- data/init.rb +2 -0
- data/install.rb +1 -0
- data/lib/foreign_key_extensions.rb +34 -0
- data/objectreload-foreign_key_extensions.gemspec +50 -0
- data/test/foreign_key_extensions_test.rb +45 -0
- data/test/test_helper.rb +13 -0
- data/uninstall.rb +1 -0
- metadata +66 -0
data/.gitignore
ADDED
data/README
ADDED
File without changes
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
|
4
|
+
begin
|
5
|
+
require 'jeweler'
|
6
|
+
Jeweler::Tasks.new do |gem|
|
7
|
+
gem.name = "objectreload-foreign_key_extensions"
|
8
|
+
gem.summary = "Simple way to create foreign keys in migration files."
|
9
|
+
gem.email = "gems@objectreload.com"
|
10
|
+
gem.homepage = "http://github.com/objectreload/foreign_key_extensions"
|
11
|
+
gem.authors = ["Mateusz Drozdzynski"]
|
12
|
+
# gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
|
13
|
+
end
|
14
|
+
Jeweler::GemcutterTasks.new
|
15
|
+
rescue LoadError
|
16
|
+
puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
|
17
|
+
end
|
18
|
+
|
19
|
+
require 'rake/testtask'
|
20
|
+
Rake::TestTask.new(:test) do |test|
|
21
|
+
test.libs << 'lib' << 'test'
|
22
|
+
test.pattern = 'test/**/*_test.rb'
|
23
|
+
test.verbose = true
|
24
|
+
end
|
25
|
+
|
26
|
+
begin
|
27
|
+
require 'rcov/rcovtask'
|
28
|
+
Rcov::RcovTask.new do |test|
|
29
|
+
test.libs << 'test'
|
30
|
+
test.pattern = 'test/**/*_test.rb'
|
31
|
+
test.verbose = true
|
32
|
+
end
|
33
|
+
rescue LoadError
|
34
|
+
task :rcov do
|
35
|
+
abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
task :test => :check_dependencies
|
40
|
+
|
41
|
+
task :default => :test
|
42
|
+
|
43
|
+
require 'rake/rdoctask'
|
44
|
+
Rake::RDocTask.new do |rdoc|
|
45
|
+
if File.exist?('VERSION')
|
46
|
+
version = File.read('VERSION')
|
47
|
+
else
|
48
|
+
version = ""
|
49
|
+
end
|
50
|
+
|
51
|
+
rdoc.rdoc_dir = 'rdoc'
|
52
|
+
rdoc.title = "foreign_key_extensions_gem #{version}"
|
53
|
+
rdoc.rdoc_files.include('README*')
|
54
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
55
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.1
|
data/init.rb
ADDED
data/install.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Install hook code here
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module ForeignKeyExtensions
|
2
|
+
def add_foreign_key(from_table, from_column, to_table)
|
3
|
+
constraint = constraint_name(from_table, from_column)
|
4
|
+
|
5
|
+
failsafe do
|
6
|
+
execute %{alter table #{from_table} add constraint #{constraint} foreign key (#{from_column}) references #{to_table}(id)}
|
7
|
+
end
|
8
|
+
end
|
9
|
+
|
10
|
+
def remove_foreign_key(from_table, from_column)
|
11
|
+
constraint = constraint_name(from_table, from_column)
|
12
|
+
|
13
|
+
failsafe do
|
14
|
+
execute %{alter table #{from_table} drop constraint #{constraint}}
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def constraint_name(from_table, from_column)
|
19
|
+
"#{from_table}_#{from_column}_foreign_key"
|
20
|
+
end
|
21
|
+
|
22
|
+
protected
|
23
|
+
|
24
|
+
def failsafe(&block)
|
25
|
+
begin
|
26
|
+
block.call
|
27
|
+
rescue => e
|
28
|
+
puts e.to_yaml
|
29
|
+
puts "Database may not support foreign key constraints"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
ActiveRecord::Migration.send(:extend, ForeignKeyExtensions)
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{objectreload-foreign_key_extensions}
|
8
|
+
s.version = "0.1.1"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Mateusz Drozdzynski"]
|
12
|
+
s.date = %q{2009-11-24}
|
13
|
+
s.email = %q{gems@objectreload.com}
|
14
|
+
s.extra_rdoc_files = [
|
15
|
+
"README"
|
16
|
+
]
|
17
|
+
s.files = [
|
18
|
+
".gitignore",
|
19
|
+
"README",
|
20
|
+
"Rakefile",
|
21
|
+
"VERSION",
|
22
|
+
"init.rb",
|
23
|
+
"install.rb",
|
24
|
+
"lib/foreign_key_extensions.rb",
|
25
|
+
"objectreload-foreign_key_extensions.gemspec",
|
26
|
+
"test/foreign_key_extensions_test.rb",
|
27
|
+
"test/test_helper.rb",
|
28
|
+
"uninstall.rb"
|
29
|
+
]
|
30
|
+
s.homepage = %q{http://github.com/objectreload/foreign_key_extensions}
|
31
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
32
|
+
s.require_paths = ["lib"]
|
33
|
+
s.rubygems_version = %q{1.3.5}
|
34
|
+
s.summary = %q{Simple way to create foreign keys in migration files.}
|
35
|
+
s.test_files = [
|
36
|
+
"test/foreign_key_extensions_test.rb",
|
37
|
+
"test/test_helper.rb"
|
38
|
+
]
|
39
|
+
|
40
|
+
if s.respond_to? :specification_version then
|
41
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
42
|
+
s.specification_version = 3
|
43
|
+
|
44
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
45
|
+
else
|
46
|
+
end
|
47
|
+
else
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require "test_helper"
|
2
|
+
|
3
|
+
class ForeignKeyExtensionsTest < Test::Unit::TestCase
|
4
|
+
include ForeignKeyExtensions
|
5
|
+
|
6
|
+
context "Foreign key extension lib" do
|
7
|
+
|
8
|
+
context "failsafe" do
|
9
|
+
should "return warning if block raise exception" do
|
10
|
+
expects(:puts).twice
|
11
|
+
send(:failsafe) do
|
12
|
+
raise StandardError
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
should "do not return any warning if block doesnt raise exception" do
|
17
|
+
expects(:puts).never
|
18
|
+
send(:failsafe) do
|
19
|
+
#empty block
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
context "constraint_name" do
|
25
|
+
should "return constraint name" do
|
26
|
+
constraint_name("table", "column").should == "table_column_foreign_key"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "add foreign key" do
|
31
|
+
should "execute sql command" do
|
32
|
+
expects(:execute).with("alter table articles add constraint articles_user_id_foreign_key foreign key (user_id) references users(id)")
|
33
|
+
add_foreign_key "articles", "user_id", "users"
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
context "remove foreign key" do
|
38
|
+
should "execute sql command" do
|
39
|
+
expects(:execute).with("alter table articles drop constraint articles_user_id_foreign_key")
|
40
|
+
remove_foreign_key "articles", "user_id"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'activerecord'
|
3
|
+
require File.dirname(__FILE__)+'/../lib/foreign_key_extensions'
|
4
|
+
require 'shoulda'
|
5
|
+
require 'logger'
|
6
|
+
require 'mocha'
|
7
|
+
require 'matchy'
|
8
|
+
|
9
|
+
ActiveRecord::Base.configurations = {'sqlite3' => {:adapter => 'sqlite3', :database => ':memory:'}}
|
10
|
+
ActiveRecord::Base.establish_connection('sqlite3')
|
11
|
+
|
12
|
+
ActiveRecord::Base.logger = Logger.new(STDERR)
|
13
|
+
ActiveRecord::Base.logger.level = Logger::WARN
|
data/uninstall.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
# Uninstall hook code here
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: objectreload-foreign_key_extensions
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Mateusz Drozdzynski
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-24 00:00:00 +00:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email: gems@objectreload.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- README
|
24
|
+
files:
|
25
|
+
- .gitignore
|
26
|
+
- README
|
27
|
+
- Rakefile
|
28
|
+
- VERSION
|
29
|
+
- init.rb
|
30
|
+
- install.rb
|
31
|
+
- lib/foreign_key_extensions.rb
|
32
|
+
- objectreload-foreign_key_extensions.gemspec
|
33
|
+
- test/foreign_key_extensions_test.rb
|
34
|
+
- test/test_helper.rb
|
35
|
+
- uninstall.rb
|
36
|
+
has_rdoc: true
|
37
|
+
homepage: http://github.com/objectreload/foreign_key_extensions
|
38
|
+
licenses: []
|
39
|
+
|
40
|
+
post_install_message:
|
41
|
+
rdoc_options:
|
42
|
+
- --charset=UTF-8
|
43
|
+
require_paths:
|
44
|
+
- lib
|
45
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: "0"
|
50
|
+
version:
|
51
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
52
|
+
requirements:
|
53
|
+
- - ">="
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: "0"
|
56
|
+
version:
|
57
|
+
requirements: []
|
58
|
+
|
59
|
+
rubyforge_project:
|
60
|
+
rubygems_version: 1.3.5
|
61
|
+
signing_key:
|
62
|
+
specification_version: 3
|
63
|
+
summary: Simple way to create foreign keys in migration files.
|
64
|
+
test_files:
|
65
|
+
- test/foreign_key_extensions_test.rb
|
66
|
+
- test/test_helper.rb
|