openrain-migration_model 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +22 -0
- data/README.markdown +68 -0
- data/Rakefile +55 -0
- data/lib/migration_model.rb +13 -0
- metadata +58 -0
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2009 OpenRain, LLC. All rights reserved.
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person
|
4
|
+
obtaining a copy of this software and associated documentation
|
5
|
+
files (the "Software"), to deal in the Software without
|
6
|
+
restriction, including without limitation the rights to use,
|
7
|
+
copy, modify, merge, publish, distribute, sublicense, and/or sell
|
8
|
+
copies of the Software, and to permit persons to whom the
|
9
|
+
Software is furnished to do so, subject to the following
|
10
|
+
conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be
|
13
|
+
included in all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
17
|
+
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
18
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
19
|
+
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
20
|
+
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
21
|
+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
data/README.markdown
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
Migration Model
|
2
|
+
===============
|
3
|
+
|
4
|
+
Ever wanted to use models in your migration? Everybody told you not to, but you did anyway.
|
5
|
+
|
6
|
+
Later on, your migrations started exploding because of changes you made to your model. Major FAIL.
|
7
|
+
|
8
|
+
Now, your can safely use your models in migrations without getting yelled at! YAY!
|
9
|
+
|
10
|
+
|
11
|
+
FAIL Example
|
12
|
+
------------
|
13
|
+
|
14
|
+
def self.up
|
15
|
+
User.create :with => 'some stuff' # FAIL! Changing the User model may easily break this
|
16
|
+
end
|
17
|
+
|
18
|
+
|
19
|
+
YAY OK Example
|
20
|
+
--------------
|
21
|
+
|
22
|
+
def self.up
|
23
|
+
mm(User).create :with => 'some stuff' # YAY! So long as the columns exist, you're good!
|
24
|
+
end
|
25
|
+
|
26
|
+
|
27
|
+
How to install?
|
28
|
+
---------------
|
29
|
+
|
30
|
+
$ ./script/plugin install git://github.com/remi/migration_model.git
|
31
|
+
|
32
|
+
|
33
|
+
Usage
|
34
|
+
-----
|
35
|
+
|
36
|
+
safe_user_model = migration_model(User)
|
37
|
+
safe_user_model.delete_all
|
38
|
+
safe_user_model.create :some => 'stuff'
|
39
|
+
|
40
|
+
safe_user_model = mm(User) # mm is a shortcut to migration_model
|
41
|
+
bob = safe_user_model.create :name => 'bob'
|
42
|
+
mm(User).find_all_by_name 'bob'
|
43
|
+
|
44
|
+
You can use your 'migration model' just like you use the real one, EXCEPT:
|
45
|
+
|
46
|
+
* the migration model has NO associations
|
47
|
+
* the migration model has NO validations
|
48
|
+
* the migration model has NONE of your model's custom logic
|
49
|
+
|
50
|
+
When you use models in your migrations, you typically just want to use the
|
51
|
+
convenience of ActiveRecord to move around data.
|
52
|
+
|
53
|
+
*IF* for some reason you need to add custom logic to your migration models:
|
54
|
+
|
55
|
+
safe_user = mm(User) do
|
56
|
+
|
57
|
+
# anything that works in a normal model will work in here
|
58
|
+
|
59
|
+
has_many :comments
|
60
|
+
|
61
|
+
named_scope :active, :conditions => ['active = ?', true]
|
62
|
+
|
63
|
+
def something_custom
|
64
|
+
'w00t'
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
safe_user.comments
|
data/Rakefile
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
require 'rake'
|
2
|
+
require 'rubygems'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'spec/rake/spectask'
|
5
|
+
|
6
|
+
begin
|
7
|
+
require 'jeweler'
|
8
|
+
Jeweler::Tasks.new do |s|
|
9
|
+
s.name = "migration_model"
|
10
|
+
s.summary = "Rails plugin for safely using models in your Rails migrations"
|
11
|
+
s.email = "remi@remitaylor.com"
|
12
|
+
s.homepage = "http://github.com/openrain/migration_model"
|
13
|
+
s.description = "Rails plugin for safely using models in your Rails migrations"
|
14
|
+
s.authors = %w( remi )
|
15
|
+
s.files = FileList["[A-Z]*", "{lib,spec,examples,rails_generators}/**/*"]
|
16
|
+
end
|
17
|
+
rescue LoadError
|
18
|
+
puts "Jeweler not available. Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
|
19
|
+
end
|
20
|
+
|
21
|
+
Spec::Rake::SpecTask.new do |t|
|
22
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
23
|
+
end
|
24
|
+
|
25
|
+
desc "Run all examples with RCov"
|
26
|
+
Spec::Rake::SpecTask.new('rcov') do |t|
|
27
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
28
|
+
t.rcov = true
|
29
|
+
end
|
30
|
+
|
31
|
+
Rake::RDocTask.new do |rdoc|
|
32
|
+
rdoc.rdoc_dir = 'rdoc'
|
33
|
+
rdoc.title = 'migration_model'
|
34
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
35
|
+
rdoc.rdoc_files.include('README*')
|
36
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
37
|
+
end
|
38
|
+
|
39
|
+
desc 'Confirm that gemspec is $SAFE'
|
40
|
+
task :safe do
|
41
|
+
require 'yaml'
|
42
|
+
require 'rubygems/specification'
|
43
|
+
data = File.read('migration_model.gemspec')
|
44
|
+
spec = nil
|
45
|
+
if data !~ %r{!ruby/object:Gem::Specification}
|
46
|
+
Thread.new { spec = eval("$SAFE = 3\n#{data}") }.join
|
47
|
+
else
|
48
|
+
spec = YAML.load(data)
|
49
|
+
end
|
50
|
+
spec.validate
|
51
|
+
puts spec
|
52
|
+
puts "OK"
|
53
|
+
end
|
54
|
+
|
55
|
+
task :default => :spec
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module MigrationModel
|
2
|
+
|
3
|
+
# returns a raw model class with no associations or validations
|
4
|
+
# # or anything like that, so it's safe to use in migrations
|
5
|
+
def migration_model model_class, &block
|
6
|
+
safe_model = Class.new(model_class.superclass){ set_table_name model_class.table_name }
|
7
|
+
safe_model.instance_eval(&block) if block
|
8
|
+
safe_model
|
9
|
+
end
|
10
|
+
|
11
|
+
alias mm migration_model
|
12
|
+
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,58 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: openrain-migration_model
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- remi
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-02-06 00:00:00 -08:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Rails plugin for safely using models in your Rails migrations
|
17
|
+
email: remi@remitaylor.com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- Rakefile
|
26
|
+
- VERSION.yml
|
27
|
+
- README.markdown
|
28
|
+
- LICENSE
|
29
|
+
- lib/migration_model.rb
|
30
|
+
has_rdoc: true
|
31
|
+
homepage: http://github.com/openrain/migration_model
|
32
|
+
post_install_message:
|
33
|
+
rdoc_options:
|
34
|
+
- --inline-source
|
35
|
+
- --charset=UTF-8
|
36
|
+
require_paths:
|
37
|
+
- lib
|
38
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: "0"
|
43
|
+
version:
|
44
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
45
|
+
requirements:
|
46
|
+
- - ">="
|
47
|
+
- !ruby/object:Gem::Version
|
48
|
+
version: "0"
|
49
|
+
version:
|
50
|
+
requirements: []
|
51
|
+
|
52
|
+
rubyforge_project:
|
53
|
+
rubygems_version: 1.2.0
|
54
|
+
signing_key:
|
55
|
+
specification_version: 2
|
56
|
+
summary: Rails plugin for safely using models in your Rails migrations
|
57
|
+
test_files: []
|
58
|
+
|