model_mill 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/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +18 -0
- data/Gemfile.lock +129 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +23 -0
- data/Rakefile +50 -0
- data/VERSION +1 -0
- data/lib/generators/model_mill/models/models_generator.rb +47 -0
- data/lib/generators/model_mill/models/templates/base.rb +5 -0
- data/lib/generators/model_mill/models/templates/model.rb +3 -0
- data/lib/model_mill.rb +2 -0
- data/model_mill.gemspec +144 -0
- data/spec/dummy/Rakefile +7 -0
- data/spec/dummy/app/controllers/application_controller.rb +3 -0
- data/spec/dummy/app/helpers/application_helper.rb +2 -0
- data/spec/dummy/app/models/schema_migration.rb +3 -0
- data/spec/dummy/app/views/layouts/application.html.erb +14 -0
- data/spec/dummy/config/application.rb +45 -0
- data/spec/dummy/config/boot.rb +10 -0
- data/spec/dummy/config/environment.rb +5 -0
- data/spec/dummy/config/environments/development.rb +26 -0
- data/spec/dummy/config/environments/production.rb +49 -0
- data/spec/dummy/config/environments/test.rb +35 -0
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
- data/spec/dummy/config/initializers/inflections.rb +10 -0
- data/spec/dummy/config/initializers/mime_types.rb +5 -0
- data/spec/dummy/config/initializers/secret_token.rb +7 -0
- data/spec/dummy/config/initializers/session_store.rb +8 -0
- data/spec/dummy/config/locales/en.yml +5 -0
- data/spec/dummy/config/routes.rb +58 -0
- data/spec/dummy/config.ru +4 -0
- data/spec/dummy/db/migrate/20110125024703_create_some_tables.rb +21 -0
- data/spec/dummy/db/schema.rb +30 -0
- data/spec/dummy/public/404.html +26 -0
- data/spec/dummy/public/422.html +26 -0
- data/spec/dummy/public/500.html +26 -0
- data/spec/dummy/public/favicon.ico +0 -0
- data/spec/dummy/public/javascripts/application.js +2 -0
- data/spec/dummy/public/javascripts/controls.js +965 -0
- data/spec/dummy/public/javascripts/dragdrop.js +974 -0
- data/spec/dummy/public/javascripts/effects.js +1123 -0
- data/spec/dummy/public/javascripts/prototype.js +6001 -0
- data/spec/dummy/public/javascripts/rails.js +175 -0
- data/spec/dummy/public/stylesheets/.gitkeep +0 -0
- data/spec/dummy/script/rails +6 -0
- data/spec/generator_spec_helper.rb +149 -0
- data/spec/generators/model_generator_spec.rb +87 -0
- data/spec/integration/navigation_spec.rb +9 -0
- data/spec/matchers/all.rb +9 -0
- data/spec/matchers/generate_file.rb +42 -0
- data/spec/matchers/generate_migration.rb +44 -0
- data/spec/matchers/have_class_method.rb +42 -0
- data/spec/matchers/have_method.rb +46 -0
- data/spec/model_mill_spec.rb +7 -0
- data/spec/spec_helper.rb +35 -0
- metadata +285 -0
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
(function() {
|
|
2
|
+
// Technique from Juriy Zaytsev
|
|
3
|
+
// http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
|
|
4
|
+
function isEventSupported(eventName) {
|
|
5
|
+
var el = document.createElement('div');
|
|
6
|
+
eventName = 'on' + eventName;
|
|
7
|
+
var isSupported = (eventName in el);
|
|
8
|
+
if (!isSupported) {
|
|
9
|
+
el.setAttribute(eventName, 'return;');
|
|
10
|
+
isSupported = typeof el[eventName] == 'function';
|
|
11
|
+
}
|
|
12
|
+
el = null;
|
|
13
|
+
return isSupported;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
function isForm(element) {
|
|
17
|
+
return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM'
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function isInput(element) {
|
|
21
|
+
if (Object.isElement(element)) {
|
|
22
|
+
var name = element.nodeName.toUpperCase()
|
|
23
|
+
return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA'
|
|
24
|
+
}
|
|
25
|
+
else return false
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
var submitBubbles = isEventSupported('submit'),
|
|
29
|
+
changeBubbles = isEventSupported('change')
|
|
30
|
+
|
|
31
|
+
if (!submitBubbles || !changeBubbles) {
|
|
32
|
+
// augment the Event.Handler class to observe custom events when needed
|
|
33
|
+
Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap(
|
|
34
|
+
function(init, element, eventName, selector, callback) {
|
|
35
|
+
init(element, eventName, selector, callback)
|
|
36
|
+
// is the handler being attached to an element that doesn't support this event?
|
|
37
|
+
if ( (!submitBubbles && this.eventName == 'submit' && !isForm(this.element)) ||
|
|
38
|
+
(!changeBubbles && this.eventName == 'change' && !isInput(this.element)) ) {
|
|
39
|
+
// "submit" => "emulated:submit"
|
|
40
|
+
this.eventName = 'emulated:' + this.eventName
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
)
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
if (!submitBubbles) {
|
|
47
|
+
// discover forms on the page by observing focus events which always bubble
|
|
48
|
+
document.on('focusin', 'form', function(focusEvent, form) {
|
|
49
|
+
// special handler for the real "submit" event (one-time operation)
|
|
50
|
+
if (!form.retrieve('emulated:submit')) {
|
|
51
|
+
form.on('submit', function(submitEvent) {
|
|
52
|
+
var emulated = form.fire('emulated:submit', submitEvent, true)
|
|
53
|
+
// if custom event received preventDefault, cancel the real one too
|
|
54
|
+
if (emulated.returnValue === false) submitEvent.preventDefault()
|
|
55
|
+
})
|
|
56
|
+
form.store('emulated:submit', true)
|
|
57
|
+
}
|
|
58
|
+
})
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (!changeBubbles) {
|
|
62
|
+
// discover form inputs on the page
|
|
63
|
+
document.on('focusin', 'input, select, texarea', function(focusEvent, input) {
|
|
64
|
+
// special handler for real "change" events
|
|
65
|
+
if (!input.retrieve('emulated:change')) {
|
|
66
|
+
input.on('change', function(changeEvent) {
|
|
67
|
+
input.fire('emulated:change', changeEvent, true)
|
|
68
|
+
})
|
|
69
|
+
input.store('emulated:change', true)
|
|
70
|
+
}
|
|
71
|
+
})
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
function handleRemote(element) {
|
|
75
|
+
var method, url, params;
|
|
76
|
+
|
|
77
|
+
var event = element.fire("ajax:before");
|
|
78
|
+
if (event.stopped) return false;
|
|
79
|
+
|
|
80
|
+
if (element.tagName.toLowerCase() === 'form') {
|
|
81
|
+
method = element.readAttribute('method') || 'post';
|
|
82
|
+
url = element.readAttribute('action');
|
|
83
|
+
params = element.serialize();
|
|
84
|
+
} else {
|
|
85
|
+
method = element.readAttribute('data-method') || 'get';
|
|
86
|
+
url = element.readAttribute('href');
|
|
87
|
+
params = {};
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
new Ajax.Request(url, {
|
|
91
|
+
method: method,
|
|
92
|
+
parameters: params,
|
|
93
|
+
evalScripts: true,
|
|
94
|
+
|
|
95
|
+
onComplete: function(request) { element.fire("ajax:complete", request); },
|
|
96
|
+
onSuccess: function(request) { element.fire("ajax:success", request); },
|
|
97
|
+
onFailure: function(request) { element.fire("ajax:failure", request); }
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
element.fire("ajax:after");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function handleMethod(element) {
|
|
104
|
+
var method = element.readAttribute('data-method'),
|
|
105
|
+
url = element.readAttribute('href'),
|
|
106
|
+
csrf_param = $$('meta[name=csrf-param]')[0],
|
|
107
|
+
csrf_token = $$('meta[name=csrf-token]')[0];
|
|
108
|
+
|
|
109
|
+
var form = new Element('form', { method: "POST", action: url, style: "display: none;" });
|
|
110
|
+
element.parentNode.insert(form);
|
|
111
|
+
|
|
112
|
+
if (method !== 'post') {
|
|
113
|
+
var field = new Element('input', { type: 'hidden', name: '_method', value: method });
|
|
114
|
+
form.insert(field);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (csrf_param) {
|
|
118
|
+
var param = csrf_param.readAttribute('content'),
|
|
119
|
+
token = csrf_token.readAttribute('content'),
|
|
120
|
+
field = new Element('input', { type: 'hidden', name: param, value: token });
|
|
121
|
+
form.insert(field);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
form.submit();
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
document.on("click", "*[data-confirm]", function(event, element) {
|
|
129
|
+
var message = element.readAttribute('data-confirm');
|
|
130
|
+
if (!confirm(message)) event.stop();
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
document.on("click", "a[data-remote]", function(event, element) {
|
|
134
|
+
if (event.stopped) return;
|
|
135
|
+
handleRemote(element);
|
|
136
|
+
event.stop();
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
document.on("click", "a[data-method]", function(event, element) {
|
|
140
|
+
if (event.stopped) return;
|
|
141
|
+
handleMethod(element);
|
|
142
|
+
event.stop();
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
document.on("submit", function(event) {
|
|
146
|
+
var element = event.findElement(),
|
|
147
|
+
message = element.readAttribute('data-confirm');
|
|
148
|
+
if (message && !confirm(message)) {
|
|
149
|
+
event.stop();
|
|
150
|
+
return false;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
var inputs = element.select("input[type=submit][data-disable-with]");
|
|
154
|
+
inputs.each(function(input) {
|
|
155
|
+
input.disabled = true;
|
|
156
|
+
input.writeAttribute('data-original-value', input.value);
|
|
157
|
+
input.value = input.readAttribute('data-disable-with');
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
var element = event.findElement("form[data-remote]");
|
|
161
|
+
if (element) {
|
|
162
|
+
handleRemote(element);
|
|
163
|
+
event.stop();
|
|
164
|
+
}
|
|
165
|
+
});
|
|
166
|
+
|
|
167
|
+
document.on("ajax:after", "form", function(event, element) {
|
|
168
|
+
var inputs = element.select("input[type=submit][disabled=true][data-disable-with]");
|
|
169
|
+
inputs.each(function(input) {
|
|
170
|
+
input.value = input.readAttribute('data-original-value');
|
|
171
|
+
input.removeAttribute('data-original-value');
|
|
172
|
+
input.disabled = false;
|
|
173
|
+
});
|
|
174
|
+
});
|
|
175
|
+
})();
|
|
File without changes
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
# This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
|
|
3
|
+
|
|
4
|
+
APP_PATH = File.expand_path('../../config/application', __FILE__)
|
|
5
|
+
require File.expand_path('../../config/boot', __FILE__)
|
|
6
|
+
require 'rails/commands'
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
# Grabbed from here: https://github.com/kristianmandrup/canable/blob/master/spec/generator_spec_helper.rb
|
|
2
|
+
|
|
3
|
+
require 'rubygems'
|
|
4
|
+
require 'test/unit'
|
|
5
|
+
require 'rails/all'
|
|
6
|
+
require 'rails/generators'
|
|
7
|
+
require 'rails/generators/test_case'
|
|
8
|
+
|
|
9
|
+
require 'rails_spec_helper'
|
|
10
|
+
|
|
11
|
+
# Call configure to load the settings from
|
|
12
|
+
# Rails.application.config.generators to Rails::Generators
|
|
13
|
+
|
|
14
|
+
Rails::Generators.configure!
|
|
15
|
+
|
|
16
|
+
# require the generators
|
|
17
|
+
def require_generators generator_list
|
|
18
|
+
generator_list.each do |name, generators|
|
|
19
|
+
generators.each do |generator_name|
|
|
20
|
+
require File.join('generators', name.to_s, generator_name.to_s, "#{generator_name}_generator")
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
module RSpec
|
|
26
|
+
module Generators
|
|
27
|
+
class TestCase < ::Rails::Generators::TestCase
|
|
28
|
+
setup :prepare_destination
|
|
29
|
+
# setup :copy_routes
|
|
30
|
+
destination File.join(::Rails.root)
|
|
31
|
+
|
|
32
|
+
def initialize(test_method_name)
|
|
33
|
+
@method_name = test_method_name
|
|
34
|
+
@test_passed = true
|
|
35
|
+
@interrupted = false
|
|
36
|
+
#copy_routes
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
protected
|
|
40
|
+
|
|
41
|
+
def copy_routes
|
|
42
|
+
routes_file = File.join(File.dirname(__FILE__), 'fixtures', 'routes.rb')
|
|
43
|
+
# puts "routes_file: #{routes_file}"
|
|
44
|
+
routes = File.expand_path(routes_file)
|
|
45
|
+
destination = File.join(::Rails.root, "config")
|
|
46
|
+
FileUtils.mkdir_p(destination)
|
|
47
|
+
FileUtils.cp File.expand_path(routes), destination
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
module GeneratorSpec
|
|
56
|
+
class << self
|
|
57
|
+
attr_accessor :generator, :test_method_name
|
|
58
|
+
|
|
59
|
+
def clean!
|
|
60
|
+
if generator
|
|
61
|
+
generator.class.generator_class = nil
|
|
62
|
+
end
|
|
63
|
+
@generator = nil
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
def get_generator test_method_name=nil
|
|
67
|
+
@generator ||= RSpec::Generators::TestCase.new(test_method_name + '_spec')
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def run_generator *args, &block
|
|
71
|
+
generator.run_generator *args
|
|
72
|
+
if block
|
|
73
|
+
block.arity < 1 ? generator.instance_eval(&block) : block.call(generator, self)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
def check(&block)
|
|
78
|
+
if block
|
|
79
|
+
block.arity < 1 ? self.instance_eval(&block) : block.call(self)
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
|
|
83
|
+
def with(generator, &block)
|
|
84
|
+
if block
|
|
85
|
+
block.arity < 1 ? generator.instance_eval(&block) : block.call(generator) #, self, generator.class)
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
|
|
89
|
+
def with_generator &block
|
|
90
|
+
with(get_generator, &block)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def setup_generator test_method_name=nil, &block
|
|
94
|
+
clean! if test_method_name
|
|
95
|
+
generator = get_generator(test_method_name)
|
|
96
|
+
if block
|
|
97
|
+
block.arity < 1 ? generator.class.instance_eval(&block) : block.call(generator.class)
|
|
98
|
+
end
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def check_methods methods
|
|
103
|
+
methods.each do |method_name|
|
|
104
|
+
content.should_match /def #{method_name}_by?(user)/
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
alias_method :methods, :check_methods
|
|
108
|
+
|
|
109
|
+
def check_matchings matchings
|
|
110
|
+
matchings.each do |matching|
|
|
111
|
+
content.should_match /#{Regexp.escape(matching)}/
|
|
112
|
+
end
|
|
113
|
+
end
|
|
114
|
+
alias_method :matchings, :check_matchings
|
|
115
|
+
|
|
116
|
+
def check_file file
|
|
117
|
+
generator.should generate_file file
|
|
118
|
+
end
|
|
119
|
+
alias_method :file, :check_file
|
|
120
|
+
|
|
121
|
+
def check_class_methods methods
|
|
122
|
+
methods.each do |method_name|
|
|
123
|
+
content.should_match /def self.#{method_name}_by?(user)/
|
|
124
|
+
end
|
|
125
|
+
end
|
|
126
|
+
alias_method :class_methods, :check_class_methods
|
|
127
|
+
|
|
128
|
+
def check_view(folder, file_name, strings)
|
|
129
|
+
generator.should generate_file("app/views/#{folder}/#{filename}") do |file_content|
|
|
130
|
+
strings.each do |str|
|
|
131
|
+
content.should_match /#{Regexp.escape(str)}/
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
alias_method :view, :check_view
|
|
136
|
+
|
|
137
|
+
def check_model(name, clazz, options = {})
|
|
138
|
+
generator.should generate_file("app/models/#{name.underscore}.rb") do |file_content|
|
|
139
|
+
file_content.should have_class user.camelize do |content|
|
|
140
|
+
check_matchings options[:matchings]
|
|
141
|
+
check_methods(options[:methods])
|
|
142
|
+
check_class_methods(options[:class_methods])
|
|
143
|
+
end
|
|
144
|
+
end
|
|
145
|
+
end
|
|
146
|
+
alias_method :model, :check_model
|
|
147
|
+
end # class self
|
|
148
|
+
end
|
|
149
|
+
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
require 'rake'
|
|
2
|
+
require 'ap'
|
|
3
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
|
4
|
+
require 'generators/model_mill/models/models_generator'
|
|
5
|
+
|
|
6
|
+
RSpec::Matchers.define :generate_file do |expected|
|
|
7
|
+
match do |actual|
|
|
8
|
+
expected_path = File.expand_path(expected, Rails.application.root)
|
|
9
|
+
File.exists?(expected_path)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
failure_message_for_should do |actual|
|
|
13
|
+
"expected a file at #{expected}"
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
failure_message_for_should_not do |actual|
|
|
17
|
+
"expected there to be no file at #{expected}"
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
description do
|
|
21
|
+
"generate the file at #{expected}"
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def file_contents path
|
|
26
|
+
File.read(File.expand_path(path, ::Rails.root))
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
describe 'models_generator' do
|
|
30
|
+
context 'with namespace option' do
|
|
31
|
+
before :each do
|
|
32
|
+
GeneratorSpec.setup_generator 'model_generator' do
|
|
33
|
+
tests ModelMill::Generators::ModelsGenerator
|
|
34
|
+
end
|
|
35
|
+
@namespace = 'legacy'
|
|
36
|
+
@tables = %w(users widgets)
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
after :each do
|
|
40
|
+
FileUtils.rm_r File.expand_path("app/models/#{@namespace}", ::Rails.root)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
it "generates a base model for the namespace" do
|
|
44
|
+
GeneratorSpec.with_generator do |g|
|
|
45
|
+
g.run_generator @namespace
|
|
46
|
+
g.should generate_file("app/models/#{@namespace}")
|
|
47
|
+
g.should generate_file("app/models/#{@namespace}/base.rb")
|
|
48
|
+
file_contents("app/models/#{@namespace}/base.rb").should match(/class #{@namespace.camelize}::Base < ActiveRecord::Base/)
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
it "generates a model for each table" do
|
|
53
|
+
GeneratorSpec.with_generator do |g|
|
|
54
|
+
g.run_generator @namespace
|
|
55
|
+
@tables.each do |tn|
|
|
56
|
+
g.should generate_file("app/models/#{@namespace}/#{tn.singularize.underscore}.rb")
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
context 'without namespace option' do
|
|
63
|
+
before :each do
|
|
64
|
+
GeneratorSpec.setup_generator 'model_generator' do
|
|
65
|
+
tests ModelMill::Generators::ModelsGenerator
|
|
66
|
+
end
|
|
67
|
+
@tables = %w(users widgets)
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
after :each do
|
|
71
|
+
@tables.each do |tn|
|
|
72
|
+
FileUtils.rm_r File.expand_path("app/models/#{tn.singularize.underscore}.rb", ::Rails.root)
|
|
73
|
+
end
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
it "generates a model for each table" do
|
|
77
|
+
GeneratorSpec.with_generator do |g|
|
|
78
|
+
g.run_generator
|
|
79
|
+
@tables.each do |tn|
|
|
80
|
+
g.should generate_file("app/models/#{tn.singularize.underscore}.rb")
|
|
81
|
+
file_contents("app/models/#{tn.singularize.underscore}.rb").should match(/class #{tn.singularize.camelize} < ActiveRecord::Base/)
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# Asserts a given file exists. You need to supply an absolute path or a path relative
|
|
2
|
+
# to the configured destination:
|
|
3
|
+
#
|
|
4
|
+
# generator.should have_generated_file "app/controller/products_controller.rb"
|
|
5
|
+
#
|
|
6
|
+
|
|
7
|
+
module RSpec::Rails
|
|
8
|
+
module GeneratorMatchers
|
|
9
|
+
class GenerateFile
|
|
10
|
+
|
|
11
|
+
def initialize(relative)
|
|
12
|
+
@relative = relative
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# actual is the generator
|
|
16
|
+
def matches?(generator, &block)
|
|
17
|
+
@expected = File.expand_path(@relative, generator.class.destination_root)
|
|
18
|
+
ex = File.exists?(@expected)
|
|
19
|
+
if block && ex
|
|
20
|
+
read = File.read(@expected)
|
|
21
|
+
yield read
|
|
22
|
+
else
|
|
23
|
+
ex
|
|
24
|
+
end
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def failure_message
|
|
28
|
+
"Expected file #{@relative} to have been generated, but it was not"
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def negative_failure_message
|
|
32
|
+
"Did not expected file #{@relative} to have been generated, but it was"
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def generate_file(relative)
|
|
37
|
+
GenerateFile.new(relative)
|
|
38
|
+
end
|
|
39
|
+
alias_method :generate_directory, :generate_file
|
|
40
|
+
|
|
41
|
+
end
|
|
42
|
+
end
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
# This method manipulates the given path and tries to find any migration which
|
|
2
|
+
# matches the migration name. For example, the call above is converted to:
|
|
3
|
+
#
|
|
4
|
+
# generator.should generate_migration "db/migrate/003_create_products.rb"
|
|
5
|
+
#
|
|
6
|
+
# Consequently it accepts the same arguments as the matcher have_file.
|
|
7
|
+
|
|
8
|
+
module RSpec::Rails
|
|
9
|
+
module GeneratorMatchers
|
|
10
|
+
class GenerateMigration
|
|
11
|
+
|
|
12
|
+
def initialize(relative)
|
|
13
|
+
@relative = relative
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# actual is the generator
|
|
17
|
+
def matches?(generator)
|
|
18
|
+
migration_file_name(relative, generator)
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
def failure_message
|
|
22
|
+
"Expected migration #{relative} to have been generated, but it was not"
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def negative_failure_message
|
|
26
|
+
"Did not expect migration #{relative} to have been generated, but it was"
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
protected
|
|
30
|
+
|
|
31
|
+
def migration_file_name(relative, generator) #:nodoc:
|
|
32
|
+
absolute = File.expand_path(relative, generator.class.destination_root)
|
|
33
|
+
dirname, file_name = File.dirname(absolute), File.basename(absolute).sub(/\.rb$/, '')
|
|
34
|
+
Dir.glob("#{dirname}/[0-9]*_*.rb").grep(/\d+_#{file_name}.rb$/).first
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def generate_migration(relative)
|
|
40
|
+
GenerateMigration.new(relative)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
end
|
|
44
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
# This method manipulates the given path and tries to find any migration which
|
|
2
|
+
# matches the migration name. For example, the call above is converted to:
|
|
3
|
+
#
|
|
4
|
+
# generator.should generate_migration "db/migrate/003_create_products.rb"
|
|
5
|
+
#
|
|
6
|
+
# Consequently it accepts the same arguments as the matcher have_file.
|
|
7
|
+
|
|
8
|
+
module RSpec::Rails
|
|
9
|
+
module GeneratorMatchers
|
|
10
|
+
class HaveClassMethod
|
|
11
|
+
|
|
12
|
+
def initialize(content)
|
|
13
|
+
begin
|
|
14
|
+
@content = File.open(content).read
|
|
15
|
+
rescue
|
|
16
|
+
@content = content
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# actual is the generator
|
|
21
|
+
def matches?(method)
|
|
22
|
+
@method = method
|
|
23
|
+
@content =~ /def\s+self.#{method}\s*(\(.+\))?(.*?)\n/m
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def failure_message
|
|
27
|
+
"Expected there to be the class method #{method}, but there wasn't"
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def negative_failure_message
|
|
31
|
+
"Did not expect there to be the class method #{method}, but there was"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def have_class_method(method)
|
|
37
|
+
HaveClassMethod.new(method)
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
# This method tries to see if a specific method is contained in the generated file.
|
|
2
|
+
# It can operate (should) on either a file name or the raw content
|
|
3
|
+
#
|
|
4
|
+
# generated_file_name.should have_method "hello" # 'my/path/say_hello.rb'.should have_method "hello"
|
|
5
|
+
#
|
|
6
|
+
# say_hello_file_content.should have_method "hello"
|
|
7
|
+
#
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
module RSpec::Rails
|
|
11
|
+
module GeneratorMatchers
|
|
12
|
+
class HaveMethod
|
|
13
|
+
|
|
14
|
+
def initialize(content)
|
|
15
|
+
begin
|
|
16
|
+
@content = File.open(content).read
|
|
17
|
+
rescue
|
|
18
|
+
@content = content
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
# actual is the generator
|
|
23
|
+
def matches?(method)
|
|
24
|
+
@method = method
|
|
25
|
+
@content =~ /def\s+#{method}\s*(\(.+\))?(.*?)\n/m
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def failure_message
|
|
29
|
+
"Expected there to be the method #{method}, but there wasn't"
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def negative_failure_message
|
|
33
|
+
"Did not expect there to be the method #{method}, but there was"
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def have_method(method)
|
|
39
|
+
HaveMethod.new(method)
|
|
40
|
+
end
|
|
41
|
+
alias_method :have_instance_method, :have_method
|
|
42
|
+
|
|
43
|
+
end
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
|
data/spec/spec_helper.rb
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
# Configure Rails Envinronment
|
|
2
|
+
ENV["RAILS_ENV"] = "test"
|
|
3
|
+
|
|
4
|
+
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
|
5
|
+
require "rails/test_help"
|
|
6
|
+
require "rspec/rails"
|
|
7
|
+
|
|
8
|
+
ActionMailer::Base.delivery_method = :test
|
|
9
|
+
ActionMailer::Base.perform_deliveries = true
|
|
10
|
+
ActionMailer::Base.default_url_options[:host] = "test.com"
|
|
11
|
+
|
|
12
|
+
Rails.backtrace_cleaner.remove_silencers!
|
|
13
|
+
|
|
14
|
+
# Configure capybara for integration testing
|
|
15
|
+
require "capybara/rails"
|
|
16
|
+
Capybara.default_driver = :rack_test
|
|
17
|
+
Capybara.default_selector = :css
|
|
18
|
+
|
|
19
|
+
# Run any available migration
|
|
20
|
+
ActiveRecord::Migrator.migrate File.expand_path("../dummy/db/migrate/", __FILE__)
|
|
21
|
+
|
|
22
|
+
# Load support files
|
|
23
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
|
|
24
|
+
|
|
25
|
+
require 'generator_spec_helper'
|
|
26
|
+
|
|
27
|
+
RSpec.configure do |config|
|
|
28
|
+
# Remove this line if you don't want RSpec's should and should_not
|
|
29
|
+
# methods or matchers
|
|
30
|
+
require 'rspec/expectations'
|
|
31
|
+
config.include RSpec::Matchers
|
|
32
|
+
|
|
33
|
+
# == Mock Framework
|
|
34
|
+
config.mock_with :rspec
|
|
35
|
+
end
|