email_form_generator 1.0.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/History.txt ADDED
@@ -0,0 +1,3 @@
1
+ == 1.0.0 / 2008-05-21
2
+ * Birthday!
3
+
data/README.txt ADDED
@@ -0,0 +1,124 @@
1
+ =Email Form Generator
2
+ by Brian Hogan
3
+ http://www.napcs.com
4
+
5
+ == DESCRIPTION:
6
+
7
+ This generator creates a working contact form with validations and delivery, and provides a method for working with configuration from YML files as well as a mechanism for working with models that don't require a database but still need validations and callbacks.
8
+
9
+ == Usage:
10
+
11
+ ruby script/generate email_form contact
12
+
13
+ or
14
+ ruby script/generate email_form Contact first_name:string last_name:string email:string message:string
15
+
16
+ The command generates the following output:
17
+
18
+ Your form is ready for use in your application.
19
+ Before you start the server, fill in the right details into config/config.yml.
20
+ exists app/models/
21
+ exists app/controllers/
22
+ exists app/helpers/
23
+ create app/views/contact_forms
24
+ create app/views/contact_form_mailer
25
+ exists app/views/layouts/
26
+ exists test/functional/
27
+ exists test/unit/
28
+ create app/views/contact_forms/success.html.erb
29
+ create app/views/contact_forms/new.html.erb
30
+ create app/views/contact_form_mailer/form.html.erb
31
+ create app/controllers/contact_forms_controller.rb
32
+ create config/config.yml
33
+ create config/initializers/config.rb
34
+ create app/models/contact_form.rb
35
+ create app/models/contact_form_mailer.rb
36
+ create app/models/tableless.rb
37
+ create test/functional/contact_forms_controller_test.rb
38
+ create test/unit/contact_form_mailer_test.rb
39
+
40
+ Notice that it generates a model called ContactForm with a RESTful resource ContactForms, mapped to a singular resource route (map.resource :contact_form)
41
+
42
+ Also generates a configuration file and an initializer to make configuring email for various environments a snap and a tableless model is included so you can use validations and form helpers just like you do with regular models.
43
+
44
+
45
+
46
+ === Configuration
47
+ When the generator runs, it creates a configuration file called config.yml. You need to modify this file so that it reflects your settings for your email server.
48
+
49
+ development:
50
+ email:
51
+ delivery_method: smtp
52
+ server: smtp.yourdomain.com
53
+ port: 25
54
+ domain: yourdomain.com
55
+ authentication: none
56
+ username:
57
+ password:
58
+ contact_recipient: admin@yourdomain.com
59
+ production:
60
+ email:
61
+ delivery_method: sendmail
62
+ contact_recipient: admin@yourdomain.com
63
+ test:
64
+ fms_host: localhost:3000
65
+ disable_google_ads: false
66
+ email:
67
+ delivery_method: test
68
+ contact_recipient: admin@yourdomain.com
69
+
70
+ This file is read by config/initializers/config.rb, where it configures ActionMailer's settings. It should work fine out of the box for most environments.
71
+
72
+ == Tableless Model
73
+ The Tableless model inherits from ActiveRecord::Base and then removes the
74
+ database functionality from the model.
75
+
76
+ ===Examples of use:
77
+
78
+ class Foo < Tableless
79
+ column :bar, :string
80
+ validates_presense_of :bar
81
+ end
82
+
83
+ @foo = Foo.new
84
+ @foo.save #=> false
85
+ @foo.bar = "hello"
86
+ @foo.save #=> true
87
+
88
+ This concept was inspired by Rick Olson
89
+
90
+
91
+ == REQUIREMENTS:
92
+
93
+ * The Mocha gem is required because I use it for testing. Change the tests if you want to use something else.
94
+ * Rails with ActiveRecord
95
+
96
+ == INSTALL:
97
+
98
+ * Mac and Linux: sudo gem install email_form_generator
99
+ * Windows: gem install email_form_generator
100
+
101
+ == LICENSE:
102
+
103
+ (The MIT License)
104
+
105
+ Copyright (c) 2008 Brian Hogan with code from http://dev.rubyonrails.org/
106
+
107
+ Permission is hereby granted, free of charge, to any person obtaining
108
+ a copy of this software and associated documentation files (the
109
+ 'Software'), to deal in the Software without restriction, including
110
+ without limitation the rights to use, copy, modify, merge, publish,
111
+ distribute, sublicense, and/or sell copies of the Software, and to
112
+ permit persons to whom the Software is furnished to do so, subject to
113
+ the following conditions:
114
+
115
+ The above copyright notice and this permission notice shall be
116
+ included in all copies or substantial portions of the Software.
117
+
118
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
119
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
120
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
121
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
122
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
123
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
124
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,129 @@
1
+
2
+ require 'rails_commands'
3
+ class EmailFormGenerator < Rails::Generator::NamedBase
4
+ default_options :skip_timestamps => false, :skip_migration => false
5
+
6
+ attr_reader :controller_name,
7
+ :controller_class_path,
8
+ :controller_file_path,
9
+ :controller_class_nesting,
10
+ :controller_class_nesting_depth,
11
+ :controller_class_name,
12
+ :controller_underscore_name,
13
+ :controller_singular_name,
14
+ :controller_plural_name
15
+ alias_method :controller_file_name, :controller_underscore_name
16
+ alias_method :controller_table_name, :controller_plural_name
17
+
18
+ def initialize(runtime_args, runtime_options = {})
19
+ @original_name = ARGV[0]
20
+ if ! ARGV[0].downcase.include?("form")
21
+ ARGV[0] = ARGV[0].underscore + "_form"
22
+ end
23
+ super
24
+ # @name = @name + "_form"
25
+
26
+ @controller_name = @name.pluralize
27
+
28
+ base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name)
29
+ @controller_class_name_without_nesting, @controller_underscore_name, @controller_plural_name = inflect_names(base_name)
30
+ @controller_singular_name=base_name.singularize
31
+ if @controller_class_nesting.empty?
32
+ @controller_class_name = @controller_class_name_without_nesting
33
+ else
34
+ @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}"
35
+ end
36
+ end
37
+
38
+ def manifest
39
+ recorded_session = record do |m|
40
+ # Check for class naming collisions.
41
+ m.class_collisions(controller_class_path, "#{controller_class_name}Controller", "#{controller_class_name}Helper")
42
+ m.class_collisions(class_path, "#{class_name}")
43
+
44
+ # Controller, helper, views, test and stylesheets directories.
45
+ m.directory(File.join('app/models', class_path))
46
+ m.directory(File.join('app/controllers', controller_class_path))
47
+ m.directory(File.join('app/helpers', controller_class_path))
48
+ m.directory(File.join('app/views', controller_class_path, controller_file_name))
49
+ m.directory(File.join('app/views', controller_class_path, file_name + "_mailer"))
50
+ m.directory(File.join('app/views/layouts', controller_class_path))
51
+ m.directory(File.join('test/functional', controller_class_path))
52
+ m.directory(File.join('test/unit', class_path))
53
+
54
+ for action in scaffold_views
55
+ m.template(
56
+ "view_#{action}.html.erb",
57
+ File.join('app/views', controller_class_path, controller_file_name, "#{action}.html.erb")
58
+ )
59
+ end
60
+
61
+ # mailer template
62
+ m.template(
63
+ "view_form.html.erb",
64
+ File.join('app/views', controller_class_path, file_name + "_mailer", "form.html.erb")
65
+ )
66
+
67
+
68
+ m.template(
69
+ 'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
70
+ )
71
+
72
+ m.template('config.yml', File.join('config', 'config.yml'))
73
+ m.template('config.rb', File.join('config/initializers', 'config.rb'))
74
+
75
+ m.template(
76
+ 'model.rb', File.join('app/models', "#{file_name}.rb")
77
+ )
78
+
79
+ m.template(
80
+ 'mailer.rb', File.join('app/models', "#{file_name}_mailer.rb")
81
+ )
82
+
83
+ m.template(
84
+ 'tableless.rb', File.join('app/models', "tableless.rb"), :collision => :skip
85
+ )
86
+
87
+ m.template('functional_test.rb', File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb"))
88
+ m.template('mailer_test.rb', File.join('test/unit', "#{file_name}_mailer_test.rb"))
89
+
90
+ m.route_resource controller_singular_name
91
+
92
+ action = nil
93
+ action = $0.split("/")[1]
94
+ case action
95
+ when "generate"
96
+ puts "Your form is ready for use in your application."
97
+ puts "Before you start the server, fill in the right details into config/config.yml."
98
+
99
+ end
100
+
101
+ end
102
+
103
+ recorded_session
104
+
105
+ end
106
+
107
+ protected
108
+ # Override with your own usage banner.
109
+ def banner
110
+ "Usage: #{$0} email_form Contact [field:type, field:type]"
111
+ end
112
+
113
+ def add_options!(opt)
114
+ opt.separator ''
115
+ opt.separator 'Options:'
116
+ opt.on("--skip-timestamps",
117
+ "Don't add timestamps to the migration file for this model") { |v| options[:skip_timestamps] = v }
118
+ opt.on("--skip-migration",
119
+ "Don't generate a migration file for this model") { |v| options[:skip_migration] = v }
120
+ end
121
+
122
+ def scaffold_views
123
+ %w[success new ]
124
+ end
125
+
126
+ def model_name
127
+ class_name.demodulize
128
+ end
129
+ end
data/lib/email_form.rb ADDED
@@ -0,0 +1,6 @@
1
+ #=Email Form Generator
2
+ # Generate a simple "Contact Us" or "Feeback" type form for your app
3
+ # complete with validations and unit/functional tests.
4
+ #
5
+ # This generator creates a RESTful controller as well, though it uses a singular resource.
6
+
@@ -0,0 +1,31 @@
1
+ # borrowed from restful_authentication
2
+
3
+ Rails::Generator::Commands::Create.class_eval do
4
+ def route_resource(*resources)
5
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
6
+ sentinel = 'ActionController::Routing::Routes.draw do |map|'
7
+
8
+ logger.route "map.resource #{resource_list}"
9
+ unless options[:pretend]
10
+ gsub_file 'config/routes.rb', /(#{Regexp.escape(sentinel)})/mi do |match|
11
+ "#{match}\n map.resource #{resource_list}\n"
12
+ end
13
+ end
14
+ end
15
+ end
16
+
17
+ Rails::Generator::Commands::Destroy.class_eval do
18
+ def route_resource(*resources)
19
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
20
+ look_for = "\n map.resource #{resource_list}\n"
21
+ logger.route "map.resource #{resource_list}"
22
+ gsub_file 'config/routes.rb', /(#{look_for})/mi, ''
23
+ end
24
+ end
25
+
26
+ Rails::Generator::Commands::List.class_eval do
27
+ def route_resource(*resources)
28
+ resource_list = resources.map { |r| r.to_sym.inspect }.join(', ')
29
+ logger.route "map.resource #{resource_list}"
30
+ end
31
+ end
@@ -0,0 +1,25 @@
1
+ c = YAML::load(File.open("#{RAILS_ROOT}/config/config.yml"))
2
+
3
+ if c[RAILS_ENV]['email']['delivery_method'] == "sendmail"
4
+ ActionMailer::Base.delivery_method = :sendmail
5
+
6
+ elsif c[RAILS_ENV]['email']['delivery_method'] == "test"
7
+ ActionMailer::Base.delivery_method = :test
8
+ else
9
+ ActionMailer::Base.delivery_method = :smtp
10
+
11
+
12
+ ActionMailer::Base.smtp_settings[:tls] = true
13
+
14
+ ActionMailer::Base.smtp_settings = {
15
+ :address => c[RAILS_ENV]['email']['server'],
16
+ :port => c[RAILS_ENV]['email']['port'],
17
+ :domain => c[RAILS_ENV]['email']['domain'],
18
+ :authentication => c[RAILS_ENV]['email']['authentication'],
19
+ :user_name => c[RAILS_ENV]['email']['username'],
20
+ :password => c[RAILS_ENV]['email']['password']
21
+ }
22
+
23
+ end
24
+
25
+ CONTACT_RECIPIENT = c[RAILS_ENV]['email']['contact_recipient']
@@ -0,0 +1,20 @@
1
+ development:
2
+ email:
3
+ delivery_method: smtp
4
+ server: smtp.yourdomain.com
5
+ port: 25
6
+ domain: yourdomain.com
7
+ authentication: none
8
+ username:
9
+ password:
10
+ contact_recipient: admin@yourdomain.com
11
+ production:
12
+ email:
13
+ delivery_method: sendmail
14
+ contact_recipient: admin@yourdomain.com
15
+ test:
16
+ fms_host: localhost:3000
17
+ disable_google_ads: false
18
+ email:
19
+ delivery_method: test
20
+ contact_recipient: admin@yourdomain.com
@@ -0,0 +1,34 @@
1
+ class <%= controller_class_name %>Controller < ApplicationController
2
+
3
+
4
+ # GET /<%= table_name %>/new
5
+ # GET /<%= table_name %>/new.xml
6
+ def new
7
+ @<%= file_name %> = <%= class_name %>.new
8
+
9
+ respond_to do |format|
10
+ format.html # new.html.erb
11
+ format.xml { render :xml => @<%= file_name %> }
12
+ end
13
+ end
14
+
15
+
16
+ # POST /<%= table_name %>
17
+ # POST /<%= table_name %>.xml
18
+ def create
19
+ @<%= file_name %> = <%= class_name %>.new(params[:<%= file_name %>])
20
+
21
+ respond_to do |format|
22
+ if @<%= file_name %>.deliver
23
+ flash[:notice] = 'The message was sent successfully.'
24
+ format.html { render :action=>"success" }
25
+ format.xml { render :xml => @<%= file_name %>, :status => :created, :location => @<%= file_name %> }
26
+ else
27
+ format.html { render :action => "new" }
28
+ format.xml { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
29
+ end
30
+ end
31
+ end
32
+
33
+
34
+ end
@@ -0,0 +1,47 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+ require '<%= controller_file_name %>_controller'
3
+
4
+ # requires mocha gem - gem install mocha
5
+ require 'mocha'
6
+
7
+ # Re-raise errors caught by the controller.
8
+ class <%= controller_class_name %>Controller; def rescue_action(e) raise e end; end
9
+
10
+ class <%= controller_class_name %>ControllerTest < Test::Unit::TestCase
11
+
12
+
13
+
14
+ def setup
15
+ @controller = <%= controller_class_name %>Controller.new
16
+ @request = ActionController::TestRequest.new
17
+ @response = ActionController::TestResponse.new
18
+
19
+ ActionMailer::Base.delivery_method = :test
20
+ ActionMailer::Base.perform_deliveries = false
21
+ ActionMailer::Base.deliveries = []
22
+
23
+ end
24
+
25
+ def test_should_display_form
26
+ get :new
27
+ assert_not_nil assigns(:<%= file_name %>)
28
+ assert_response :success
29
+ end
30
+
31
+
32
+ def test_should_show_success_when_created
33
+ post :create, {:<%= file_name %> => {} }
34
+ assert_response :success
35
+ assert_template "success"
36
+ end
37
+
38
+ def test_should_fail_to_send_and_show_form_again
39
+ <%=class_name %>.any_instance.expects(:deliver).returns(false)
40
+
41
+ post :create
42
+ assert_response :success
43
+ assert_template "new"
44
+ end
45
+
46
+
47
+ end
@@ -0,0 +1,18 @@
1
+ class <%= class_name %>Mailer < ActionMailer::Base
2
+ def form(<%= file_name %>)
3
+ setup_email(<%= file_name %>)
4
+ @subject += 'Message from the site'
5
+ @recipients = CONTACT_RECIPIENT
6
+
7
+ end
8
+
9
+
10
+
11
+ protected
12
+ def setup_email(<%= file_name %>)
13
+ @from = "ADMINEMAIL"
14
+ @subject = "[YOURSITE] "
15
+ @sent_on = Time.now
16
+ @body[:<%= file_name %>] = <%= file_name %>
17
+ end
18
+ end
@@ -0,0 +1,28 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+ require '<%= file_name %>_mailer'
3
+
4
+ class <%= class_name %>MailerTest < Test::Unit::TestCase
5
+
6
+
7
+ def setup
8
+ ActionMailer::Base.delivery_method = :test
9
+ ActionMailer::Base.perform_deliveries = true
10
+ ActionMailer::Base.deliveries = []
11
+
12
+
13
+ end
14
+
15
+ def test_deliver_form
16
+ @f = <%=class_name %>.new
17
+
18
+ <% attributes.each do |attribute| %>
19
+ @f.<%=attribute.name %> = "foo"
20
+ <% end %>
21
+
22
+ <%=class_name%>Mailer::deliver_form(@f)
23
+ assert_equal 1, ActionMailer::Base.deliveries.length
24
+
25
+ end
26
+
27
+
28
+ end
@@ -0,0 +1,21 @@
1
+ class <%= class_name %> < Tableless
2
+ <% for attribute in attributes -%>
3
+ <%= "column :#{attribute.name}, :#{attribute.type}" %>
4
+ <% end -%>
5
+
6
+
7
+ def deliver
8
+ if valid?
9
+ begin
10
+ <%= class_name %>Mailer::deliver_form(self)
11
+ rescue
12
+ @errors.add_to_base "Your message could not be sent due to configuration issues with the server."
13
+ return false
14
+ end
15
+ else
16
+ return false
17
+ end
18
+ end
19
+
20
+
21
+ end
@@ -0,0 +1,30 @@
1
+ #=Tableless
2
+ #
3
+ # The Tableless model inherits from ActiveRecord::Base and then removes the
4
+ # database functionality from the model.
5
+ #
6
+ #==Examples of use:
7
+ #
8
+ # class Foo < Tableless
9
+ # column :bar, :string
10
+ # validates_presense_of :bar
11
+ # end
12
+ #
13
+ # @foo = Foo.new
14
+ # @foo.save #=> false
15
+ # @foo.bar = "hello"
16
+ # @foo.save #=> true
17
+ #
18
+ # This concept was inspired by Rick Olson
19
+ class Tableless < ActiveRecord::Base
20
+ def self.columns() @columns ||= []; end
21
+ def self.column(name, sql_type = nil, default = nil, null = true)
22
+ columns << ActiveRecord::ConnectionAdapters::Column.new(name.to_s, default,
23
+ sql_type.to_s, null)
24
+ end
25
+
26
+ # override the save method to prevent exceptions.
27
+ def save(validate = true)
28
+ validate ? valid? : true
29
+ end
30
+ end
@@ -0,0 +1,26 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class <%= class_name %>Test < Test::Unit::TestCase
4
+
5
+ def test_should_deliver_message
6
+
7
+ @f = <%=class_name %>.new
8
+
9
+ <% attributes.each do |attribute| %>
10
+ @f.<%=attribute.name %> = "foo"
11
+ <% end %>
12
+
13
+ assert @f.deliver
14
+ end
15
+
16
+ # modify this test in such a way as to cause the
17
+ # validations to fail, if you have any.
18
+ def test_should_not_deliver_message
19
+
20
+ @f = <%=class_name %>.new
21
+ assert ! @f.deliver
22
+
23
+ end
24
+
25
+
26
+ end
@@ -0,0 +1,3 @@
1
+ <% for attribute in attributes -%>
2
+ <%%= @<%= singular_name %>.<%=attribute.name%> %>
3
+ <% end %>
@@ -0,0 +1,17 @@
1
+
2
+ <%% form_for(@<%= singular_name %>, :url=><%= singular_name %>_url ) do |f| %>
3
+ <%%= f.error_messages :header_message=>"Please try again", :message=> "We couldn't process your email form for the following reasons:" %>
4
+
5
+ <% for attribute in attributes -%>
6
+ <p>
7
+ <%%= f.label :<%= attribute.name %> %><br />
8
+ <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
9
+ </p>
10
+ <% end -%>
11
+ <p>
12
+ <%%= f.submit "Send" %>
13
+ </p>
14
+ <%% end %>
15
+
16
+
17
+
@@ -0,0 +1,3 @@
1
+ <h1>Your message has been sent to the site administrator. </h1>
2
+
3
+
File without changes
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: email_form_generator
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Brian Hogan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2008-05-21 00:00:00 -05:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Generates a feedback form with model, controller, and views to make it dead simple for your users to send messages to you.
17
+ email: info@napcs.com
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files:
23
+ - README.txt
24
+ - History.txt
25
+ files:
26
+ - README.txt
27
+ - History.txt
28
+ - email_form_generator.rb
29
+ - lib/rails_commands.rb
30
+ - lib/email_form.rb
31
+ - templates/config.rb
32
+ - templates/config.yml
33
+ - templates/controller.rb
34
+ - templates/functional_test.rb
35
+ - templates/mailer_test.rb
36
+ - templates/mailer.rb
37
+ - templates/model.rb
38
+ - templates/tableless.rb
39
+ - templates/unit_test.rb
40
+ - templates/view_form.html.erb
41
+ - templates/view_new.html.erb
42
+ - templates/view_success.html.erb
43
+ has_rdoc: true
44
+ homepage: This generator creates a working contact form with validations and delivery, and provides a method for working with configuration from YML files as well as a mechanism for working with models that don't require a database but still need validations and callbacks.
45
+ post_install_message:
46
+ rdoc_options:
47
+ - --main
48
+ - README.txt
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: "0"
62
+ version:
63
+ requirements: []
64
+
65
+ rubyforge_project: emailform
66
+ rubygems_version: 1.0.1
67
+ signing_key:
68
+ specification_version: 2
69
+ summary: Email form generation with model, controller, tests, and mailer.
70
+ test_files:
71
+ - test/test_scaffold_form.rb