machinist_callbacks 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +50 -0
- data/Rakefile +37 -0
- data/VERSION +1 -0
- data/lib/machinist_callbacks.rb +49 -0
- data/machinist_callbacks.gemspec +87 -0
- data/spec/app_root/app/controllers/application_controller.rb +2 -0
- data/spec/app_root/app/models/director.rb +7 -0
- data/spec/app_root/app/models/movie.rb +9 -0
- data/spec/app_root/app/models/producer.rb +5 -0
- data/spec/app_root/config/boot.rb +114 -0
- data/spec/app_root/config/database.yml +21 -0
- data/spec/app_root/config/environment.rb +14 -0
- data/spec/app_root/config/environments/in_memory.rb +0 -0
- data/spec/app_root/config/environments/mysql.rb +0 -0
- data/spec/app_root/config/environments/postgresql.rb +0 -0
- data/spec/app_root/config/environments/sqlite.rb +0 -0
- data/spec/app_root/config/environments/sqlite3.rb +0 -0
- data/spec/app_root/config/routes.rb +4 -0
- data/spec/app_root/db/migrate/001_create_movies.rb +17 -0
- data/spec/app_root/db/migrate/002_create_directors.rb +13 -0
- data/spec/app_root/db/migrate/003_create_producers.rb +13 -0
- data/spec/app_root/lib/console_with_fixtures.rb +4 -0
- data/spec/app_root/log/.gitignore +1 -0
- data/spec/app_root/script/console +7 -0
- data/spec/callbacks_spec.rb +113 -0
- data/spec/spec_helper.rb +24 -0
- data/spec/support/blueprints.rb +3 -0
- data/spec/support/rcov.opts +2 -0
- data/spec/support/spec.opts +4 -0
- metadata +110 -0
data/MIT-LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2009 Henning Koch
|
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.rdoc
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
= machinist_callbacks
|
2
|
+
|
3
|
+
Some record construction rules cannot be expressed with a {machinist}[http://github.com/notahat/machinist] blueprint, for instance:
|
4
|
+
|
5
|
+
* Creating a record with a has_many association already filled in.
|
6
|
+
* Creating a record with interdependencies between associated object, such as having to belong to the same container.
|
7
|
+
* Needing to process a {delayed_job}[http://github.com/tobi/delayed_job] queue after record construction.
|
8
|
+
|
9
|
+
This gem provides <tt>before_make</tt> and <tt>after_make</tt> callbacks for machinist blueprints,
|
10
|
+
enabling more freedom in your construction rules.
|
11
|
+
|
12
|
+
== Example
|
13
|
+
|
14
|
+
Movie.blueprint do
|
15
|
+
title
|
16
|
+
year 2001
|
17
|
+
before_make do
|
18
|
+
self.producer = Producer.make(:name => director.name)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
Director.blueprint do
|
23
|
+
name
|
24
|
+
after_make do
|
25
|
+
movies << Movie.make
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
== Does it work with make_unsaved?
|
30
|
+
|
31
|
+
Yes. Machinist is built in a way that every <tt>make</tt> in your callback implicitely becomes a <tt>make_unsaved</tt>
|
32
|
+
when an object graph is created with <tt>make_unsaved</tt>. Just do not <tt>save</tt> records manually in your callbacks
|
33
|
+
because that would be crazy.
|
34
|
+
|
35
|
+
== Installation
|
36
|
+
|
37
|
+
Install the gem:
|
38
|
+
sudo gem install machinist_callbacks
|
39
|
+
|
40
|
+
Require the gem after requiring machinist in your <tt>environment.rb</tt>:
|
41
|
+
config.gem 'machinist'
|
42
|
+
config.gem 'machinist_callbacks'
|
43
|
+
|
44
|
+
=== Credits
|
45
|
+
|
46
|
+
Henning Koch
|
47
|
+
|
48
|
+
{gem-session.com}[http://gem-session.com/]
|
49
|
+
|
50
|
+
{www.makandra.de}[http://www.makandra.de/]
|
data/Rakefile
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'rake'
|
2
|
+
# require 'rake/testtask'
|
3
|
+
require 'rake/rdoctask'
|
4
|
+
require 'spec/rake/spectask'
|
5
|
+
|
6
|
+
desc 'Default: Run all specs.'
|
7
|
+
task :default => :spec
|
8
|
+
|
9
|
+
desc "Run all specs"
|
10
|
+
Spec::Rake::SpecTask.new() do |t|
|
11
|
+
t.spec_opts = ['--options', "\"spec/support/spec.opts\""]
|
12
|
+
t.spec_files = FileList['spec/**/*_spec.rb']
|
13
|
+
end
|
14
|
+
|
15
|
+
desc 'Generate documentation for the machinist_callbacks plugin.'
|
16
|
+
Rake::RDocTask.new(:rdoc) do |rdoc|
|
17
|
+
rdoc.rdoc_dir = 'rdoc'
|
18
|
+
rdoc.title = 'machinist_callbacks'
|
19
|
+
rdoc.options << '--line-numbers' << '--inline-source'
|
20
|
+
rdoc.rdoc_files.include('README')
|
21
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
22
|
+
end
|
23
|
+
|
24
|
+
begin
|
25
|
+
require 'jeweler'
|
26
|
+
Jeweler::Tasks.new do |gemspec|
|
27
|
+
gemspec.name = "machinist_callbacks"
|
28
|
+
gemspec.summary = "Callback hooks for machinist blueprints"
|
29
|
+
gemspec.email = "github@makandra.de"
|
30
|
+
gemspec.homepage = "http://github.com/makandra/machinist_callbacks"
|
31
|
+
gemspec.description = "Callback hooks for machinist blueprints"
|
32
|
+
gemspec.authors = ["Henning Koch"]
|
33
|
+
end
|
34
|
+
rescue LoadError
|
35
|
+
puts "Jeweler not available. Install it with: sudo gem install jeweler"
|
36
|
+
end
|
37
|
+
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.1.0
|
@@ -0,0 +1,49 @@
|
|
1
|
+
require 'machinist'
|
2
|
+
require 'machinist/active_record'
|
3
|
+
|
4
|
+
|
5
|
+
module Machinist
|
6
|
+
class Lathe
|
7
|
+
|
8
|
+
attr_reader :before_make_callback, :after_make_callback
|
9
|
+
|
10
|
+
def before_make(&callback)
|
11
|
+
object.instance_variable_set(:@before_make_callback, callback)
|
12
|
+
end
|
13
|
+
|
14
|
+
def after_make(&callback)
|
15
|
+
object.instance_variable_set(:@after_make_callback, callback)
|
16
|
+
end
|
17
|
+
|
18
|
+
class << self
|
19
|
+
|
20
|
+
def run_with_before_make(*args)
|
21
|
+
lathe = run_without_before_make(*args)
|
22
|
+
if callback = lathe.object.instance_variable_get(:@before_make_callback)
|
23
|
+
lathe.object.instance_eval(&callback)
|
24
|
+
end
|
25
|
+
lathe
|
26
|
+
end
|
27
|
+
|
28
|
+
alias_method_chain :run, :before_make
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
|
36
|
+
makers = []
|
37
|
+
makers << ActiveRecord::Associations::HasManyAssociation
|
38
|
+
makers << ActiveRecord::Base.send(ActiveRecord::Base.respond_to?(:metaclass) ? :metaclass : :singleton_class)
|
39
|
+
|
40
|
+
makers.each do |maker|
|
41
|
+
maker.send :define_method, :make_with_after_make do |*args|
|
42
|
+
object = make_without_after_make(*args)
|
43
|
+
if callback = object.instance_variable_get(:@after_make_callback)
|
44
|
+
object.instance_eval(&callback)
|
45
|
+
end
|
46
|
+
object
|
47
|
+
end
|
48
|
+
maker.alias_method_chain :make, :after_make
|
49
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{machinist_callbacks}
|
8
|
+
s.version = "0.1.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Henning Koch"]
|
12
|
+
s.date = %q{2010-06-20}
|
13
|
+
s.description = %q{Callback hooks for machinist blueprints}
|
14
|
+
s.email = %q{github@makandra.de}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"README.rdoc"
|
17
|
+
]
|
18
|
+
s.files = [
|
19
|
+
".gitignore",
|
20
|
+
"MIT-LICENSE",
|
21
|
+
"README.rdoc",
|
22
|
+
"Rakefile",
|
23
|
+
"VERSION",
|
24
|
+
"lib/machinist_callbacks.rb",
|
25
|
+
"machinist_callbacks.gemspec",
|
26
|
+
"spec/app_root/app/controllers/application_controller.rb",
|
27
|
+
"spec/app_root/app/models/director.rb",
|
28
|
+
"spec/app_root/app/models/movie.rb",
|
29
|
+
"spec/app_root/app/models/producer.rb",
|
30
|
+
"spec/app_root/config/boot.rb",
|
31
|
+
"spec/app_root/config/database.yml",
|
32
|
+
"spec/app_root/config/environment.rb",
|
33
|
+
"spec/app_root/config/environments/in_memory.rb",
|
34
|
+
"spec/app_root/config/environments/mysql.rb",
|
35
|
+
"spec/app_root/config/environments/postgresql.rb",
|
36
|
+
"spec/app_root/config/environments/sqlite.rb",
|
37
|
+
"spec/app_root/config/environments/sqlite3.rb",
|
38
|
+
"spec/app_root/config/routes.rb",
|
39
|
+
"spec/app_root/db/migrate/001_create_movies.rb",
|
40
|
+
"spec/app_root/db/migrate/002_create_directors.rb",
|
41
|
+
"spec/app_root/db/migrate/003_create_producers.rb",
|
42
|
+
"spec/app_root/lib/console_with_fixtures.rb",
|
43
|
+
"spec/app_root/log/.gitignore",
|
44
|
+
"spec/app_root/script/console",
|
45
|
+
"spec/callbacks_spec.rb",
|
46
|
+
"spec/spec_helper.rb",
|
47
|
+
"spec/support/blueprints.rb",
|
48
|
+
"spec/support/rcov.opts",
|
49
|
+
"spec/support/spec.opts"
|
50
|
+
]
|
51
|
+
s.homepage = %q{http://github.com/makandra/machinist_callbacks}
|
52
|
+
s.rdoc_options = ["--charset=UTF-8"]
|
53
|
+
s.require_paths = ["lib"]
|
54
|
+
s.rubygems_version = %q{1.3.6}
|
55
|
+
s.summary = %q{Callback hooks for machinist blueprints}
|
56
|
+
s.test_files = [
|
57
|
+
"spec/support/blueprints.rb",
|
58
|
+
"spec/app_root/db/migrate/001_create_movies.rb",
|
59
|
+
"spec/app_root/db/migrate/002_create_directors.rb",
|
60
|
+
"spec/app_root/db/migrate/003_create_producers.rb",
|
61
|
+
"spec/app_root/config/boot.rb",
|
62
|
+
"spec/app_root/config/environment.rb",
|
63
|
+
"spec/app_root/config/routes.rb",
|
64
|
+
"spec/app_root/config/environments/in_memory.rb",
|
65
|
+
"spec/app_root/config/environments/mysql.rb",
|
66
|
+
"spec/app_root/config/environments/postgresql.rb",
|
67
|
+
"spec/app_root/config/environments/sqlite.rb",
|
68
|
+
"spec/app_root/config/environments/sqlite3.rb",
|
69
|
+
"spec/app_root/lib/console_with_fixtures.rb",
|
70
|
+
"spec/app_root/app/controllers/application_controller.rb",
|
71
|
+
"spec/app_root/app/models/movie.rb",
|
72
|
+
"spec/app_root/app/models/director.rb",
|
73
|
+
"spec/app_root/app/models/producer.rb",
|
74
|
+
"spec/callbacks_spec.rb",
|
75
|
+
"spec/spec_helper.rb"
|
76
|
+
]
|
77
|
+
|
78
|
+
if s.respond_to? :specification_version then
|
79
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
80
|
+
s.specification_version = 3
|
81
|
+
|
82
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
83
|
+
else
|
84
|
+
end
|
85
|
+
else
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,114 @@
|
|
1
|
+
# Allow customization of the rails framework path
|
2
|
+
RAILS_FRAMEWORK_ROOT = (ENV['RAILS_FRAMEWORK_ROOT'] || "#{File.dirname(__FILE__)}/../../../../../../vendor/rails") unless defined?(RAILS_FRAMEWORK_ROOT)
|
3
|
+
|
4
|
+
# Don't change this file!
|
5
|
+
# Configure your app in config/environment.rb and config/environments/*.rb
|
6
|
+
|
7
|
+
RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)
|
8
|
+
|
9
|
+
module Rails
|
10
|
+
class << self
|
11
|
+
def boot!
|
12
|
+
unless booted?
|
13
|
+
preinitialize
|
14
|
+
pick_boot.run
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
def booted?
|
19
|
+
defined? Rails::Initializer
|
20
|
+
end
|
21
|
+
|
22
|
+
def pick_boot
|
23
|
+
(vendor_rails? ? VendorBoot : GemBoot).new
|
24
|
+
end
|
25
|
+
|
26
|
+
def vendor_rails?
|
27
|
+
File.exist?(RAILS_FRAMEWORK_ROOT)
|
28
|
+
end
|
29
|
+
|
30
|
+
def preinitialize
|
31
|
+
load(preinitializer_path) if File.exist?(preinitializer_path)
|
32
|
+
end
|
33
|
+
|
34
|
+
def preinitializer_path
|
35
|
+
"#{RAILS_ROOT}/config/preinitializer.rb"
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
class Boot
|
40
|
+
def run
|
41
|
+
load_initializer
|
42
|
+
Rails::Initializer.run(:set_load_path)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
class VendorBoot < Boot
|
47
|
+
def load_initializer
|
48
|
+
require "#{RAILS_FRAMEWORK_ROOT}/railties/lib/initializer"
|
49
|
+
Rails::Initializer.run(:install_gem_spec_stubs)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
class GemBoot < Boot
|
54
|
+
def load_initializer
|
55
|
+
self.class.load_rubygems
|
56
|
+
load_rails_gem
|
57
|
+
require 'initializer'
|
58
|
+
end
|
59
|
+
|
60
|
+
def load_rails_gem
|
61
|
+
if version = self.class.gem_version
|
62
|
+
gem 'rails', version
|
63
|
+
else
|
64
|
+
gem 'rails'
|
65
|
+
end
|
66
|
+
rescue Gem::LoadError => load_error
|
67
|
+
$stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
|
68
|
+
exit 1
|
69
|
+
end
|
70
|
+
|
71
|
+
class << self
|
72
|
+
def rubygems_version
|
73
|
+
Gem::RubyGemsVersion rescue nil
|
74
|
+
end
|
75
|
+
|
76
|
+
def gem_version
|
77
|
+
if defined? RAILS_GEM_VERSION
|
78
|
+
RAILS_GEM_VERSION
|
79
|
+
elsif ENV.include?('RAILS_GEM_VERSION')
|
80
|
+
ENV['RAILS_GEM_VERSION']
|
81
|
+
else
|
82
|
+
parse_gem_version(read_environment_rb)
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
def load_rubygems
|
87
|
+
require 'rubygems'
|
88
|
+
min_version = '1.1.1'
|
89
|
+
unless rubygems_version >= min_version
|
90
|
+
$stderr.puts %Q(Rails requires RubyGems >= #{min_version} (you have #{rubygems_version}). Please `gem update --system` and try again.)
|
91
|
+
exit 1
|
92
|
+
end
|
93
|
+
|
94
|
+
rescue LoadError
|
95
|
+
$stderr.puts %Q(Rails requires RubyGems >= #{min_version}. Please install RubyGems and try again: http://rubygems.rubyforge.org)
|
96
|
+
exit 1
|
97
|
+
end
|
98
|
+
|
99
|
+
def parse_gem_version(text)
|
100
|
+
$1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
|
101
|
+
end
|
102
|
+
|
103
|
+
private
|
104
|
+
def read_environment_rb
|
105
|
+
environment_rb = "#{RAILS_ROOT}/config/environment.rb"
|
106
|
+
environment_rb = "#{HELPER_RAILS_ROOT}/config/environment.rb" unless File.exists?(environment_rb)
|
107
|
+
File.read(environment_rb)
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
# All that for this:
|
114
|
+
Rails.boot!
|
@@ -0,0 +1,21 @@
|
|
1
|
+
in_memory:
|
2
|
+
adapter: sqlite3
|
3
|
+
database: ":memory:"
|
4
|
+
verbosity: quiet
|
5
|
+
sqlite:
|
6
|
+
adapter: sqlite
|
7
|
+
dbfile: plugin_test.sqlite.db
|
8
|
+
sqlite3:
|
9
|
+
adapter: sqlite3
|
10
|
+
dbfile: plugin_test.sqlite3.db
|
11
|
+
postgresql:
|
12
|
+
adapter: postgresql
|
13
|
+
username: postgres
|
14
|
+
password: postgres
|
15
|
+
database: plugin_test
|
16
|
+
mysql:
|
17
|
+
adapter: mysql
|
18
|
+
host: localhost
|
19
|
+
username: root
|
20
|
+
password:
|
21
|
+
database: plugin_test
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require File.join(File.dirname(__FILE__), 'boot')
|
2
|
+
|
3
|
+
Rails::Initializer.run do |config|
|
4
|
+
config.cache_classes = false
|
5
|
+
config.whiny_nils = true
|
6
|
+
config.action_controller.session = { :key => "_myapp_session", :secret => "gwirofjweroijger8924rt2zfwehfuiwehb1378rifowenfoqwphf23" }
|
7
|
+
config.plugin_locators.unshift(
|
8
|
+
Class.new(Rails::Plugin::Locator) do
|
9
|
+
def plugins
|
10
|
+
[Rails::Plugin.new(File.expand_path('.'))]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
) unless defined?(PluginTestHelper::PluginLocator)
|
14
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class CreateMovies < ActiveRecord::Migration
|
2
|
+
|
3
|
+
def self.up
|
4
|
+
create_table :movies do |t|
|
5
|
+
t.string :title
|
6
|
+
t.integer :year
|
7
|
+
t.integer :prequel_id
|
8
|
+
t.integer :director_id
|
9
|
+
t.integer :producer_id
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.down
|
14
|
+
drop_table :movies
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
@@ -0,0 +1,4 @@
|
|
1
|
+
# Loads fixtures into the database when running the test app via the console
|
2
|
+
(ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(Rails.root, '../fixtures/*.{yml,csv}'))).each do |fixture_file|
|
3
|
+
Fixtures.create_fixtures(File.join(Rails.root, '../fixtures'), File.basename(fixture_file, '.*'))
|
4
|
+
end
|
@@ -0,0 +1 @@
|
|
1
|
+
*.log
|
@@ -0,0 +1,113 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/spec_helper'
|
2
|
+
require 'machinist/active_record'
|
3
|
+
require 'ostruct'
|
4
|
+
require 'sham'
|
5
|
+
require 'faker'
|
6
|
+
|
7
|
+
describe 'machinist blueprints' do
|
8
|
+
|
9
|
+
before :all do
|
10
|
+
Sham.name { Faker::Name.name }
|
11
|
+
Sham.title { Faker::Lorem.sentence }
|
12
|
+
end
|
13
|
+
|
14
|
+
describe 'without callbacks' do
|
15
|
+
|
16
|
+
it "should not obstruct standard machinist behavior" do
|
17
|
+
|
18
|
+
Movie.blueprint do
|
19
|
+
title
|
20
|
+
year 2001
|
21
|
+
director
|
22
|
+
end
|
23
|
+
|
24
|
+
Director.blueprint do
|
25
|
+
name
|
26
|
+
end
|
27
|
+
|
28
|
+
movie = Movie.make
|
29
|
+
movie.title.should be_a(String)
|
30
|
+
movie.year.should == 2001
|
31
|
+
movie.director.should be_a(Director)
|
32
|
+
movie.director.name.should be_a(String)
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
describe "before_make" do
|
39
|
+
|
40
|
+
before :each do
|
41
|
+
|
42
|
+
Movie.blueprint do
|
43
|
+
title
|
44
|
+
year 2001
|
45
|
+
before_make do
|
46
|
+
if director
|
47
|
+
self.producer = Producer.make(:name => director.name)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
Director.blueprint do
|
53
|
+
name
|
54
|
+
end
|
55
|
+
|
56
|
+
Producer.blueprint do
|
57
|
+
name
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should allow to configure a freshly made record" do
|
63
|
+
movie = Movie.make(:director => Director.make(:name => "director name"))
|
64
|
+
movie.producer.name.should == 'director name'
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should work with make_unsaved" do
|
68
|
+
movie = Movie.make_unsaved(:director => Director.make_unsaved(:name => "director name"))
|
69
|
+
movie.should be_new_record
|
70
|
+
movie.producer.name.should == 'director name'
|
71
|
+
movie.producer.should be_new_record
|
72
|
+
end
|
73
|
+
|
74
|
+
end
|
75
|
+
|
76
|
+
describe "after_make" do
|
77
|
+
|
78
|
+
before :each do
|
79
|
+
|
80
|
+
Movie.blueprint do
|
81
|
+
title
|
82
|
+
year 2001
|
83
|
+
end
|
84
|
+
|
85
|
+
Director.blueprint do
|
86
|
+
name
|
87
|
+
after_make do
|
88
|
+
movies << Movie.make(:director => self)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
end
|
93
|
+
|
94
|
+
it "should run the callback after saving" do
|
95
|
+
director = Director.make
|
96
|
+
director.should_not be_new_record
|
97
|
+
director.movies.size.should == 1
|
98
|
+
director.movies[0].director.should == director
|
99
|
+
director.movies[0].should_not be_new_record
|
100
|
+
end
|
101
|
+
|
102
|
+
it "should work with make_unsaved" do
|
103
|
+
director = Director.make_unsaved
|
104
|
+
director.should be_new_record
|
105
|
+
director.movies.size.should == 1
|
106
|
+
director.movies[0].director.should == director
|
107
|
+
director.movies[0].should be_new_record
|
108
|
+
end
|
109
|
+
|
110
|
+
end
|
111
|
+
|
112
|
+
end
|
113
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
$: << File.join(File.dirname(__FILE__), "/../lib" )
|
2
|
+
|
3
|
+
# Set the default environment to sqlite3's in_memory database
|
4
|
+
ENV['RAILS_ENV'] ||= 'in_memory'
|
5
|
+
|
6
|
+
|
7
|
+
# Load the Rails environment and testing framework
|
8
|
+
require "#{File.dirname(__FILE__)}/app_root/config/environment"
|
9
|
+
require 'rubygems'
|
10
|
+
require 'machinist'
|
11
|
+
require 'spec/rails'
|
12
|
+
require "#{File.dirname(__FILE__)}/../lib/machinist_callbacks"
|
13
|
+
|
14
|
+
# Undo changes to RAILS_ENV
|
15
|
+
silence_warnings {RAILS_ENV = ENV['RAILS_ENV']}
|
16
|
+
|
17
|
+
# Run the migrations
|
18
|
+
ActiveRecord::Migrator.migrate("#{Rails.root}/db/migrate")
|
19
|
+
|
20
|
+
Spec::Runner.configure do |config|
|
21
|
+
config.use_transactional_fixtures = true
|
22
|
+
config.use_instantiated_fixtures = false
|
23
|
+
end
|
24
|
+
|
metadata
ADDED
@@ -0,0 +1,110 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: machinist_callbacks
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 1
|
8
|
+
- 0
|
9
|
+
version: 0.1.0
|
10
|
+
platform: ruby
|
11
|
+
authors:
|
12
|
+
- Henning Koch
|
13
|
+
autorequire:
|
14
|
+
bindir: bin
|
15
|
+
cert_chain: []
|
16
|
+
|
17
|
+
date: 2010-06-20 00:00:00 +02:00
|
18
|
+
default_executable:
|
19
|
+
dependencies: []
|
20
|
+
|
21
|
+
description: Callback hooks for machinist blueprints
|
22
|
+
email: github@makandra.de
|
23
|
+
executables: []
|
24
|
+
|
25
|
+
extensions: []
|
26
|
+
|
27
|
+
extra_rdoc_files:
|
28
|
+
- README.rdoc
|
29
|
+
files:
|
30
|
+
- .gitignore
|
31
|
+
- MIT-LICENSE
|
32
|
+
- README.rdoc
|
33
|
+
- Rakefile
|
34
|
+
- VERSION
|
35
|
+
- lib/machinist_callbacks.rb
|
36
|
+
- machinist_callbacks.gemspec
|
37
|
+
- spec/app_root/app/controllers/application_controller.rb
|
38
|
+
- spec/app_root/app/models/director.rb
|
39
|
+
- spec/app_root/app/models/movie.rb
|
40
|
+
- spec/app_root/app/models/producer.rb
|
41
|
+
- spec/app_root/config/boot.rb
|
42
|
+
- spec/app_root/config/database.yml
|
43
|
+
- spec/app_root/config/environment.rb
|
44
|
+
- spec/app_root/config/environments/in_memory.rb
|
45
|
+
- spec/app_root/config/environments/mysql.rb
|
46
|
+
- spec/app_root/config/environments/postgresql.rb
|
47
|
+
- spec/app_root/config/environments/sqlite.rb
|
48
|
+
- spec/app_root/config/environments/sqlite3.rb
|
49
|
+
- spec/app_root/config/routes.rb
|
50
|
+
- spec/app_root/db/migrate/001_create_movies.rb
|
51
|
+
- spec/app_root/db/migrate/002_create_directors.rb
|
52
|
+
- spec/app_root/db/migrate/003_create_producers.rb
|
53
|
+
- spec/app_root/lib/console_with_fixtures.rb
|
54
|
+
- spec/app_root/log/.gitignore
|
55
|
+
- spec/app_root/script/console
|
56
|
+
- spec/callbacks_spec.rb
|
57
|
+
- spec/spec_helper.rb
|
58
|
+
- spec/support/blueprints.rb
|
59
|
+
- spec/support/rcov.opts
|
60
|
+
- spec/support/spec.opts
|
61
|
+
has_rdoc: true
|
62
|
+
homepage: http://github.com/makandra/machinist_callbacks
|
63
|
+
licenses: []
|
64
|
+
|
65
|
+
post_install_message:
|
66
|
+
rdoc_options:
|
67
|
+
- --charset=UTF-8
|
68
|
+
require_paths:
|
69
|
+
- lib
|
70
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
segments:
|
75
|
+
- 0
|
76
|
+
version: "0"
|
77
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - ">="
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
segments:
|
82
|
+
- 0
|
83
|
+
version: "0"
|
84
|
+
requirements: []
|
85
|
+
|
86
|
+
rubyforge_project:
|
87
|
+
rubygems_version: 1.3.6
|
88
|
+
signing_key:
|
89
|
+
specification_version: 3
|
90
|
+
summary: Callback hooks for machinist blueprints
|
91
|
+
test_files:
|
92
|
+
- spec/support/blueprints.rb
|
93
|
+
- spec/app_root/db/migrate/001_create_movies.rb
|
94
|
+
- spec/app_root/db/migrate/002_create_directors.rb
|
95
|
+
- spec/app_root/db/migrate/003_create_producers.rb
|
96
|
+
- spec/app_root/config/boot.rb
|
97
|
+
- spec/app_root/config/environment.rb
|
98
|
+
- spec/app_root/config/routes.rb
|
99
|
+
- spec/app_root/config/environments/in_memory.rb
|
100
|
+
- spec/app_root/config/environments/mysql.rb
|
101
|
+
- spec/app_root/config/environments/postgresql.rb
|
102
|
+
- spec/app_root/config/environments/sqlite.rb
|
103
|
+
- spec/app_root/config/environments/sqlite3.rb
|
104
|
+
- spec/app_root/lib/console_with_fixtures.rb
|
105
|
+
- spec/app_root/app/controllers/application_controller.rb
|
106
|
+
- spec/app_root/app/models/movie.rb
|
107
|
+
- spec/app_root/app/models/director.rb
|
108
|
+
- spec/app_root/app/models/producer.rb
|
109
|
+
- spec/callbacks_spec.rb
|
110
|
+
- spec/spec_helper.rb
|