Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add mail_form generator and tests

  • Loading branch information...
commit 3e28e79adc3522257a13fe3cb05195b9165e3e89 1 parent 63e0505
@wycats wycats authored
View
1  .gitignore
@@ -1,4 +1,5 @@
.DS_Store
tmp
+tmp_bundle
pkg
*.gem
View
1  beard.gemspec
@@ -19,6 +19,7 @@ Gem::Specification.new do |s|
s.add_dependency "dm-rails", "0.10.2"
# depend on dm-migrations because dm-rails forgot to
s.add_dependency "dm-migrations", "0.10.2"
+ s.add_dependency "mail_form", "1.2.1"
s.files = Dir.glob("{lib}/**/*") + %w(LICENSE README.md ROADMAP.md CHANGELOG.md)
s.require_path = 'lib'
View
10 lib/generators/mail_form/mail_form_generator.rb
@@ -0,0 +1,10 @@
+class MailFormGenerator < Rails::Generators::NamedBase
+ source_root File.expand_path("../templates", __FILE__)
+
+ class_option :attributes, :type => :array, :default => []
+
+ def create_mail_form
+ options[:attributes].concat([:email]).uniq!
+ template "mail_form.rb.tt", "app/models/#{file_name}.rb"
+ end
+end
View
13 lib/generators/mail_form/templates/mail_form.rb.tt
@@ -0,0 +1,13 @@
+class <%= class_name %> < MailForm::Base
+ <% options[:attributes].each do |attribute| -%>
+ attribute :<%= attribute %>
+ <% end -%>
+
+ # The headers that should be sent with the
+ # email. Note that the from email is automatically
+ # set to the :email attribute
+ def headers
+ { :subject => "Subject for this email",
+ :to => "your@email" }
+ end
+end
View
43 spec/generator_spec.rb
@@ -0,0 +1,43 @@
+require "spec_helper"
+
+describe "using the packaged mail_form generator" do
+ it "creates a file in the app/models/directory" do
+ run_command "script/rails g mail_form ContactForm"
+
+ app.should have_structure {
+ directory "app" do
+ directory "models" do
+ file "contact_form.rb" do
+ contains "attribute :email"
+ contains "def headers"
+ end
+ end
+ end
+ }
+ end
+end
+
+describe "using the packaged haml generator" do
+ it "creates normal controllery stuff" do
+ run_command "script/rails g controller users index"
+
+ app.should have_structure {
+ directory "app" do
+ directory "controllers" do
+ file "users_controller.rb" do
+ contains "class UsersController < ApplicationController"
+ end
+ end
+ end
+
+ # It should pull in the default rspec stubs
+ directory "spec" do
+ directory "controllers" do
+ file "users_controller_spec.rb" do
+ contains "describe UsersController do"
+ end
+ end
+ end
+ }
+ end
+end
View
1  spec/spec_helper.rb
@@ -18,6 +18,7 @@
wipe_tmp("temp_app")
Dir.chdir(root)
FileUtils.cp_r("original", "temp_app")
+ Dir.chdir(app)
end
config.after do
View
23 spec/support/runner.rb
@@ -19,6 +19,29 @@ def app
root.join("temp_app")
end
+ def with_clean_env
+ gemfile, ENV["BUNDLE_GEMFILE"] = ENV["BUNDLE_GEMFILE"], nil
+ path, ENV["BUNDLE_PATH"] = ENV["BUNDLE_PATH"], nil
+ opt, ENV["RUBYOPT"] = ENV["RUBYOPT"], nil
+ yield
+ ensure
+ ENV["BUNDLE_GEMFILE"] = gemfile
+ ENV["BUNDLE_PATH"] = path
+ ENV["RUBYOPT"] = opt
+ end
+
+ def run_command(*args)
+ with_clean_env { open_without_dm(*args) }
+ end
+
+ def open_without_dm(*args)
+ IO.popen(*args) do |io|
+ while string = io.gets
+ puts string unless string =~ /^\[datamapper\]/
+ end
+ end
+ end
+
def generate_beard(location = "temp_app")
beard_builder = beard_root.join("lib/beard/app_builder.rb")
system "rails #{location} -b #{beard_builder}"
Please sign in to comment.
Something went wrong with that request. Please try again.