active_schema 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.autotest +1 -0
- data/.document +11 -0
- data/.gitignore +48 -0
- data/.rspec +3 -0
- data/Gemfile +21 -0
- data/Gemfile.lock +63 -0
- data/LICENSE +20 -0
- data/README.textile +80 -0
- data/Rakefile +59 -0
- data/VERSION +1 -0
- data/autotest/discover.rb +1 -0
- data/lib/active_schema.rb +30 -0
- data/lib/active_schema/active_record/base.rb +46 -0
- data/lib/active_schema/associations/by_foreign_key.rb +50 -0
- data/lib/active_schema/associations/generator.rb +25 -0
- data/lib/active_schema/configuration.rb +20 -0
- data/lib/active_schema/feeder.rb +45 -0
- data/lib/active_schema/in_advance_feeder.rb +10 -0
- data/lib/active_schema/on_the_fly_feeder.rb +26 -0
- data/lib/active_schema/schema_feeder.rb +41 -0
- data/lib/active_schema/table.rb +30 -0
- data/lib/active_schema/table_hub.rb +42 -0
- data/lib/active_schema/validations/by_column.rb +41 -0
- data/lib/active_schema/validations/by_index.rb +5 -0
- data/lib/active_schema/validations/generator.rb +45 -0
- data/nbproject/project.properties +7 -0
- data/nbproject/project.xml +15 -0
- data/spec/.rspec +1 -0
- data/spec/active_schema/active_record/base_spec.rb +118 -0
- data/spec/active_schema/associations/by_foreign_key_spec.rb +73 -0
- data/spec/active_schema/associations/generator_spec.rb +5 -0
- data/spec/active_schema/in_advance_feeder_spec.rb +25 -0
- data/spec/active_schema/on_the_fly_feeder_spec.rb +34 -0
- data/spec/active_schema/schema_feeder_spec.rb +111 -0
- data/spec/active_schema/table_hub_spec.rb +70 -0
- data/spec/active_schema/table_spec.rb +13 -0
- data/spec/active_schema/validations/by_column_spec.rb +47 -0
- data/spec/active_schema/validations/by_index_spec.rb +15 -0
- data/spec/active_schema/validations/generator_spec.rb +23 -0
- data/spec/active_schema_spec.rb +14 -0
- data/spec/spec_helper.rb +31 -0
- data/spec/support/establish_connection.rb +8 -0
- data/spec/support/model_macros.rb +31 -0
- data/spec/support/test_models.rb +12 -0
- metadata +366 -0
data/.autotest
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require 'autotest/growl'
|
data/.document
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# .document is used by rdoc and yard to know how to generate documentation
|
2
|
+
# for example, it can be used to control how rdoc gets built when you do `gem install foo`
|
3
|
+
|
4
|
+
README.rdoc
|
5
|
+
lib/**/*.rb
|
6
|
+
bin/*
|
7
|
+
|
8
|
+
# Files below this - are treated as 'extra files', and aren't parsed for ruby code
|
9
|
+
-
|
10
|
+
features/**/*.feature
|
11
|
+
LICENSE
|
data/.gitignore
ADDED
@@ -0,0 +1,48 @@
|
|
1
|
+
# rcov generated
|
2
|
+
coverage
|
3
|
+
|
4
|
+
# rdoc generated
|
5
|
+
rdoc
|
6
|
+
|
7
|
+
# yard generated
|
8
|
+
doc
|
9
|
+
.yardoc
|
10
|
+
|
11
|
+
# bundler
|
12
|
+
.bundle
|
13
|
+
|
14
|
+
# jeweler generated
|
15
|
+
pkg
|
16
|
+
|
17
|
+
# Netbeans
|
18
|
+
nbproject/private
|
19
|
+
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
# Have editor/IDE/OS specific files you need to ignore? Consider using a global gitignore:
|
24
|
+
#
|
25
|
+
# * Create a file at ~/.gitignore
|
26
|
+
# * Include files you want ignored
|
27
|
+
# * Run: git config --global core.excludesfile ~/.gitignore
|
28
|
+
#
|
29
|
+
# After doing this, these files will be ignored in all your git projects,
|
30
|
+
# saving you from having to 'pollute' every project you touch with them
|
31
|
+
#
|
32
|
+
# Not sure what to needs to be ignored for particular editors/OSes? Here's some ideas to get you started. (Remember, remove the leading # of the line)
|
33
|
+
#
|
34
|
+
# For MacOS:
|
35
|
+
#
|
36
|
+
#.DS_Store
|
37
|
+
#
|
38
|
+
# For TextMate
|
39
|
+
#*.tmproj
|
40
|
+
#tmtags
|
41
|
+
#
|
42
|
+
# For emacs:
|
43
|
+
*~
|
44
|
+
\#*
|
45
|
+
.\#*
|
46
|
+
#
|
47
|
+
# For vim:
|
48
|
+
#*.swp
|
data/.rspec
ADDED
data/Gemfile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
gem "foreigner"
|
7
|
+
gem "activerecord"
|
8
|
+
# Add dependencies to develop your gem here.
|
9
|
+
# Include everything needed to run rake, tests, features, etc.
|
10
|
+
group :development do
|
11
|
+
gem "rspec", ">= 2.0.0.beta.22"
|
12
|
+
gem "bundler", "~> 1.0.0"
|
13
|
+
gem "jeweler", "~> 1.5.0.pre3"
|
14
|
+
gem "rcov", ">= 0"
|
15
|
+
gem 'rspec-rails', '>=2.0.0.beta.22'
|
16
|
+
gem 'autotest'
|
17
|
+
gem "autotest-growl"
|
18
|
+
gem "autotest-fsevent"
|
19
|
+
gem "spork"
|
20
|
+
gem "mysql2"
|
21
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,63 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
activemodel (3.0.0)
|
5
|
+
activesupport (= 3.0.0)
|
6
|
+
builder (~> 2.1.2)
|
7
|
+
i18n (~> 0.4.1)
|
8
|
+
activerecord (3.0.0)
|
9
|
+
activemodel (= 3.0.0)
|
10
|
+
activesupport (= 3.0.0)
|
11
|
+
arel (~> 1.0.0)
|
12
|
+
tzinfo (~> 0.3.23)
|
13
|
+
activesupport (3.0.0)
|
14
|
+
arel (1.0.1)
|
15
|
+
activesupport (~> 3.0.0)
|
16
|
+
autotest (4.3.2)
|
17
|
+
autotest-fsevent (0.2.3)
|
18
|
+
sys-uname
|
19
|
+
autotest-growl (0.2.6)
|
20
|
+
builder (2.1.2)
|
21
|
+
diff-lcs (1.1.2)
|
22
|
+
foreigner (0.9.0)
|
23
|
+
git (1.2.5)
|
24
|
+
i18n (0.4.1)
|
25
|
+
jeweler (1.5.0.pre3)
|
26
|
+
bundler (~> 1.0.0)
|
27
|
+
git (>= 1.2.5)
|
28
|
+
rake
|
29
|
+
mysql2 (0.2.4)
|
30
|
+
rake (0.8.7)
|
31
|
+
rcov (0.9.9)
|
32
|
+
rspec (2.0.0.beta.22)
|
33
|
+
rspec-core (= 2.0.0.beta.22)
|
34
|
+
rspec-expectations (= 2.0.0.beta.22)
|
35
|
+
rspec-mocks (= 2.0.0.beta.22)
|
36
|
+
rspec-core (2.0.0.beta.22)
|
37
|
+
rspec-expectations (2.0.0.beta.22)
|
38
|
+
diff-lcs (>= 1.1.2)
|
39
|
+
rspec-mocks (2.0.0.beta.22)
|
40
|
+
rspec-core (= 2.0.0.beta.22)
|
41
|
+
rspec-expectations (= 2.0.0.beta.22)
|
42
|
+
rspec-rails (2.0.0.beta.22)
|
43
|
+
rspec (= 2.0.0.beta.22)
|
44
|
+
spork (0.9.0.rc2)
|
45
|
+
sys-uname (0.8.4)
|
46
|
+
tzinfo (0.3.23)
|
47
|
+
|
48
|
+
PLATFORMS
|
49
|
+
ruby
|
50
|
+
|
51
|
+
DEPENDENCIES
|
52
|
+
activerecord
|
53
|
+
autotest
|
54
|
+
autotest-fsevent
|
55
|
+
autotest-growl
|
56
|
+
bundler (~> 1.0.0)
|
57
|
+
foreigner
|
58
|
+
jeweler (~> 1.5.0.pre3)
|
59
|
+
mysql2
|
60
|
+
rcov
|
61
|
+
rspec (>= 2.0.0.beta.22)
|
62
|
+
rspec-rails (>= 2.0.0.beta.22)
|
63
|
+
spork
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2010 Anders Johannsen
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.textile
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
h1. ActiveSchema
|
2
|
+
|
3
|
+
ActiveSchema makes ActiveRecord a bit DRYer. It discovers associations, such as belongs_to and has_many, using foreign keys, and it adds validations to ensure constraints on data, like NOT NULL and maximum length, are honored.
|
4
|
+
|
5
|
+
h2. An example
|
6
|
+
|
7
|
+
If you have a table structure like this (arrows indicate foreign keys)
|
8
|
+
|
9
|
+
!http://farm5.static.flickr.com/4113/5047260003_da4ecd08ac_z.jpg!
|
10
|
+
|
11
|
+
ActiveSchema would link your models like below
|
12
|
+
|
13
|
+
<pre>
|
14
|
+
class Prisoner
|
15
|
+
belongs_to :facility
|
16
|
+
end
|
17
|
+
|
18
|
+
class Facility
|
19
|
+
has_many :facilities
|
20
|
+
belongs_to :warden
|
21
|
+
end
|
22
|
+
|
23
|
+
class Warde
|
24
|
+
has_one :facility
|
25
|
+
end
|
26
|
+
</pre>
|
27
|
+
|
28
|
+
|
29
|
+
h2. Usage
|
30
|
+
|
31
|
+
Put
|
32
|
+
|
33
|
+
@gem 'activeschema'@
|
34
|
+
|
35
|
+
in your Gemfile.
|
36
|
+
|
37
|
+
ActiveSchema can be enabled per model, or you can choose to make it available everywhere.
|
38
|
+
|
39
|
+
Either way, it must be activated by the @active_schema@ class method. Per model:
|
40
|
+
|
41
|
+
<pre>
|
42
|
+
class Model < ActiveRecord::Base
|
43
|
+
active_schema
|
44
|
+
end
|
45
|
+
</pre>
|
46
|
+
|
47
|
+
In ActiveRecord::Base:
|
48
|
+
|
49
|
+
<pre>
|
50
|
+
class ActiveRecord::Base
|
51
|
+
active_schema
|
52
|
+
end
|
53
|
+
</pre>
|
54
|
+
|
55
|
+
|
56
|
+
h2. Foreign key support by 'foreigner'
|
57
|
+
|
58
|
+
Foreign key information is extracted by the "Foreigner":http://github.com/matthuhiggins/foreigner library.
|
59
|
+
It has out-of-the-box support for MySQL, Postgresql, and SQL2003.
|
60
|
+
|
61
|
+
h2. Rails 3 and Ruby 1.9.2
|
62
|
+
|
63
|
+
ActiveSchema has only been tested on Rails 3 and Ruby 1.9.2. It may work elsewhere, but there really is no guarantee.
|
64
|
+
|
65
|
+
h2. Preloading the associations
|
66
|
+
|
67
|
+
The speed at which MySQL supplies foreign key information can, at times, be leisurely, to say the least.
|
68
|
+
|
69
|
+
To circumvent this, ActiveSchema supports foreign key extraction without hitting the database.
|
70
|
+
Instead, it reads the information from the dumped "schema.rb" file, which of course must be current.
|
71
|
+
|
72
|
+
Adjusted configuration:
|
73
|
+
|
74
|
+
<pre>
|
75
|
+
schema_feeder = ActiveSchema::SchemaFeeder.new
|
76
|
+
schema_feeder.read("path/to/schema.rb")
|
77
|
+
ActiveSchema.configure do |c|
|
78
|
+
c.feeder = schema_feeder
|
79
|
+
end
|
80
|
+
</pre>
|
data/Rakefile
ADDED
@@ -0,0 +1,59 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'bundler'
|
3
|
+
begin
|
4
|
+
Bundler.setup(:default, :development)
|
5
|
+
rescue Bundler::BundlerError => e
|
6
|
+
$stderr.puts e.message
|
7
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
8
|
+
exit e.status_code
|
9
|
+
end
|
10
|
+
require 'rake'
|
11
|
+
|
12
|
+
require 'jeweler'
|
13
|
+
Jeweler::Tasks.new do |gem|
|
14
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
15
|
+
gem.name = "active_schema"
|
16
|
+
gem.summary = %Q{Association discovery by foreign keys}
|
17
|
+
gem.description = %Q{If you've gone through the trouble of linking your schema with proper foreign keys,
|
18
|
+
defining associations in ActiveRecord feels like double work.
|
19
|
+
|
20
|
+
ActiveSchema discovers the associations and validations that can be derived from the database schema.
|
21
|
+
}
|
22
|
+
gem.email = "anders@johannsen.com"
|
23
|
+
gem.homepage = "http://github.com/andersjo/active_schema"
|
24
|
+
gem.authors = ["Anders Johannsen"]
|
25
|
+
# Include your dependencies below. Runtime dependencies are required when using your gem,
|
26
|
+
# and development dependencies are only needed for development (ie running rake tasks, tests, etc)
|
27
|
+
# spec.add_runtime_dependency 'jabber4r', '> 0.1'
|
28
|
+
# spec.add_development_dependency 'rspec', '> 1.2.3'
|
29
|
+
# spec.add_runtime_dependency 'jabber4r', '> 0.1'
|
30
|
+
gem.add_runtime_dependency 'foreigner'
|
31
|
+
gem.add_development_dependency "rspec", ">= 2.0.0.beta.19"
|
32
|
+
gem.add_development_dependency "bundler", "~> 1.0.0"
|
33
|
+
gem.add_development_dependency "jeweler", "~> 1.5.0.pre3"
|
34
|
+
gem.add_development_dependency "rcov", ">= 0"
|
35
|
+
end
|
36
|
+
Jeweler::RubygemsDotOrgTasks.new
|
37
|
+
|
38
|
+
require 'rspec/core'
|
39
|
+
require 'rspec/core/rake_task'
|
40
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
41
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
42
|
+
end
|
43
|
+
|
44
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
45
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
46
|
+
spec.rcov = true
|
47
|
+
end
|
48
|
+
|
49
|
+
task :default => :spec
|
50
|
+
|
51
|
+
require 'rake/rdoctask'
|
52
|
+
Rake::RDocTask.new do |rdoc|
|
53
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
54
|
+
|
55
|
+
rdoc.rdoc_dir = 'rdoc'
|
56
|
+
rdoc.title = "active_schema #{version}"
|
57
|
+
rdoc.rdoc_files.include('README*')
|
58
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
59
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1 @@
|
|
1
|
+
Autotest.add_discovery { "rspec2" }
|
@@ -0,0 +1,30 @@
|
|
1
|
+
require 'active_support/core_ext/string/inflections'
|
2
|
+
|
3
|
+
require 'active_schema/configuration'
|
4
|
+
require 'active_schema/on_the_fly_feeder'
|
5
|
+
require 'active_schema/in_advance_feeder'
|
6
|
+
require 'active_schema/schema_feeder'
|
7
|
+
|
8
|
+
require 'active_schema/table'
|
9
|
+
require 'active_schema/table_hub'
|
10
|
+
|
11
|
+
|
12
|
+
require 'active_schema/validations/generator'
|
13
|
+
require 'active_schema/associations/generator'
|
14
|
+
require 'active_schema/associations/by_foreign_key'
|
15
|
+
|
16
|
+
|
17
|
+
module ActiveSchema
|
18
|
+
def self.configuration
|
19
|
+
@configuration ||= Configuration.new
|
20
|
+
end
|
21
|
+
|
22
|
+
def self.configure
|
23
|
+
yield configuration
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
require 'active_record'
|
28
|
+
require 'active_record/base'
|
29
|
+
require 'active_schema/active_record/base'
|
30
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module ActiveSchema
|
2
|
+
module ActiveRecord
|
3
|
+
module ClassMethods
|
4
|
+
def self.extended(klass)
|
5
|
+
klass.class_attribute :active_schema_activated
|
6
|
+
klass.class_attribute :active_schema_configuration
|
7
|
+
klass.active_schema_configuration = ActiveSchema.configuration
|
8
|
+
end
|
9
|
+
|
10
|
+
def active_schema
|
11
|
+
if !active_schema_activated
|
12
|
+
self.active_schema_activated = true
|
13
|
+
active_schema_load_model
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def active_schema_load_model
|
18
|
+
unless active_schema_configuration.skip_model.call(self)
|
19
|
+
active_schema_configuration.feeder.model_loaded(self)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
module InstanceMethods
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
ActiveRecord::Base
|
31
|
+
module ActiveRecord #:nodoc:
|
32
|
+
class ModelLoadedObserver
|
33
|
+
def update(event, klass)
|
34
|
+
case event
|
35
|
+
when :observed_class_inherited
|
36
|
+
klass.active_schema_load_model if klass.active_schema_activated
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Base
|
42
|
+
add_observer ModelLoadedObserver.new
|
43
|
+
extend ActiveSchema::ActiveRecord::ClassMethods
|
44
|
+
include ActiveSchema::ActiveRecord::InstanceMethods
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
module ActiveSchema::Associations
|
2
|
+
module Naming
|
3
|
+
def name_for(model)
|
4
|
+
model.name.demodulize.underscore.downcase
|
5
|
+
end
|
6
|
+
|
7
|
+
def plural_name_for(model)
|
8
|
+
name_for(model).pluralize
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
class ByForeignKey
|
13
|
+
include Naming
|
14
|
+
def initialize(from_model, to_model, key_column, key_column_unique)
|
15
|
+
@from_model = from_model
|
16
|
+
@to_model = to_model
|
17
|
+
@key_column = key_column
|
18
|
+
@key_column_unique = key_column_unique
|
19
|
+
end
|
20
|
+
|
21
|
+
def association(receiver, method_name, name, opts = {})
|
22
|
+
receiver.send(method_name, name, opts)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
class ByForwardForeignKey < ByForeignKey
|
27
|
+
def generate
|
28
|
+
association @from_model, :belongs_to,
|
29
|
+
name_for(@to_model),
|
30
|
+
{ :class_name => @to_model.name, :foreign_key => @key_column }
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class ByReverseForeignKey < ByForeignKey
|
35
|
+
def generate
|
36
|
+
if @key_column_unique
|
37
|
+
association @to_model, :has_one,
|
38
|
+
name_for(@from_model),
|
39
|
+
{ :class_name => @from_model.name, :foreign_key => @key_column }
|
40
|
+
else
|
41
|
+
association @to_model, :has_many,
|
42
|
+
plural_name_for(@from_model),
|
43
|
+
{ :class_name => @from_model.name, :foreign_key => @key_column }
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
|
50
|
+
end
|