send_to_friends_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.
Binary file
@@ -0,0 +1,57 @@
1
+ SendToFriendsGenerator
2
+ by Jeroen Houben <jeroen.houben@lostboys.nl>
3
+
4
+
5
+ == DESCRIPTION:
6
+
7
+ This will create two models (sender and recipient), a controller and a bunch of templates
8
+ Together they implement basic send to friend functionality
9
+
10
+ == FEATURES/PROBLEMS:
11
+
12
+ * a simple FriendMailer object
13
+ * email templates for HTML and plain text alternative
14
+ * configurable amount of friends
15
+ * support for including a user submitted message in the email
16
+ * support for HTML as well as XML (nice for Flash integration)
17
+ * server-side validation
18
+ * error messages in HTML or XML
19
+
20
+ * future feature: search and replace script to replace all relative image tags with absolute ones (http://)
21
+
22
+ == SYNOPSIS:
23
+
24
+ ./script/generate send_to_friends [options]
25
+
26
+ == REQUIREMENTS:
27
+
28
+ rails plugin: http://agilewebdevelopment.com/plugins/activerecord_base_without_table
29
+
30
+ == INSTALL:
31
+
32
+ sudo gem install send_to_friends_generator
33
+
34
+ == LICENSE:
35
+
36
+ (The MIT License)
37
+
38
+ Copyright (c) 2007 FIX
39
+
40
+ Permission is hereby granted, free of charge, to any person obtaining
41
+ a copy of this software and associated documentation files (the
42
+ 'Software'), to deal in the Software without restriction, including
43
+ without limitation the rights to use, copy, modify, merge, publish,
44
+ distribute, sublicense, and/or sell copies of the Software, and to
45
+ permit persons to whom the Software is furnished to do so, subject to
46
+ the following conditions:
47
+
48
+ The above copyright notice and this permission notice shall be
49
+ included in all copies or substantial portions of the Software.
50
+
51
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
52
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
53
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
54
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
55
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
56
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
57
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/USAGE ADDED
@@ -0,0 +1,11 @@
1
+ Description:
2
+ This will create two models (sender and recipient), a controller and a bunch of templates
3
+ Together they implement basic send to friend functionality
4
+
5
+ You can optionally choose to include a plaintext alternative. The mailer will automatically pick this up.
6
+
7
+ Example:
8
+ ./script/generate send_to_friends
9
+
10
+ ./script/generate send_to_friends --include-plaintext
11
+
@@ -0,0 +1,40 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Created by jeroen.houben on 2007-09-30.
4
+ # Copyright (c) 2007. All rights reserved.
5
+
6
+ require 'optparse'
7
+
8
+ USAGE = "Usage: #{$0} inputfile [options]\n\nexample: #{$0} html/sendfriend.html > app/views/friendmailer/message.text.html.rhtml"
9
+ options = {}
10
+
11
+ OptionParser.new do |opts|
12
+ opts.banner = USAGE
13
+
14
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
15
+ options[:verbose] = v
16
+ end
17
+
18
+ end.parse!
19
+
20
+ unless src_file = ARGV[0]
21
+ puts("\n" + USAGE + "\n\n")
22
+ exit(0)
23
+ end
24
+
25
+
26
+ begin
27
+ output = File.read(src_file)
28
+ rescue Exception => e
29
+ puts "error: #{e}"
30
+ exit(0)
31
+ end
32
+
33
+
34
+ output.gsub!("]", " %>")
35
+ output.gsub!("[", "<%= ")
36
+ output.gsub!('<img src="../static', '<img src="<%= HOME_URL %>/static')
37
+ output.gsub!('<img src="images', '<img src="<%= HOME_URL %>/images')
38
+ output.gsub!('<img src="../images', '<img src="<%= HOME_URL %>/images')
39
+
40
+ puts output
File without changes
@@ -0,0 +1,65 @@
1
+ class SendToFriendsGenerator < Rails::Generator::Base
2
+
3
+ def manifest
4
+ record do |m|
5
+ # create directories that might not exist
6
+ m.directory File.join('app', 'views', 'send_to_friends')
7
+ m.directory File.join('app', 'views', 'friend_mailer')
8
+ # controller
9
+ m.file 'send_to_friends_controller.rb', File.join('app', 'controllers', 'send_to_friends_controller.rb')
10
+ # stf views
11
+ %w(_errors.rhtml errors.rxml create.rhtml index.rhtml).each do |f|
12
+ m.file f, File.join('app', 'views', 'send_to_friends', f)
13
+ end
14
+ # models
15
+ %w(friend_mailer.rb recipient.rb sender.rb).each do |f|
16
+ m.file f, File.join('app', 'models', f)
17
+ end
18
+ # email file
19
+ if options[:include_plaintext]
20
+ m.file 'message.text.html.rhtml', File.join('app', 'views', 'friend_mailer', 'message.text.html.rhtml')
21
+ m.file 'message.text.plain.rhtml', File.join('app', 'views', 'friend_mailer', 'message.text.plain.rhtml')
22
+ else
23
+ m.file 'message.text.html.rhtml', File.join('app', 'views', 'friend_mailer', 'message.rhtml')
24
+ end
25
+
26
+ add_route
27
+ m.readme "INSTRUCTIONS"
28
+ m.readme "INSTRUCTIONS_FOR_MISSING_PLUGIN" unless File.exists? RAILS_ROOT + '/plugins/active_record_base_without_table'
29
+ end
30
+
31
+ end
32
+
33
+ def add_route
34
+ # now add a route
35
+ route_definition = "map.resources :send_to_friends"
36
+ routes_file = "#{RAILS_ROOT}/config/routes.rb"
37
+ begin
38
+ lines = IO.readlines(routes_file)
39
+ # add the route on line 2
40
+ File.open(routes_file, 'w') do |f|
41
+ lines.insert(1, " #{route_definition}\n")
42
+ f.write lines.join
43
+ end
44
+ rescue => e
45
+ puts "Could not add route to your config/routes.rb : #{e} \n\n"
46
+ puts "Please add '#{route_definition}' to your routes file"
47
+ end
48
+
49
+ end
50
+
51
+ protected
52
+ # Override with your own usage banner.
53
+ def banner
54
+ "Usage: #{$0} send_to_friends [options]"
55
+ end
56
+
57
+ def add_options!(opt)
58
+ opt.separator ''
59
+ opt.separator 'Options:'
60
+ opt.on("--include-plaintext",
61
+ "Generate a multipart email message including a plain text alternative") { |v| options[:include_plaintext] = v }
62
+ end
63
+
64
+
65
+ end
@@ -0,0 +1,11 @@
1
+
2
+ All done. Point you browser to http://localhost:3000/send_to_friends to see if it works
3
+ NOTE: you will need to restart your server!
4
+
5
+ Also, make sure your environment config has:
6
+
7
+ - a valid smtp config
8
+ - a value for HOME_URL (the generated views use this value)
9
+
10
+ If you would like the result of the send_to_friends to be XML, POST to /send_to_friends.xml
11
+
@@ -0,0 +1,4 @@
1
+
2
+ ** WARNING: please install required plugin **
3
+ ruby scrip/plugin install http://svn.viney.net.nz/things/rails/plugins/active_record_base_without_table
4
+
@@ -0,0 +1,24 @@
1
+ <h2>The form was is valid</h2>
2
+
3
+ <% if !@sender.valid? %>
4
+ <h3>Sender Invalid</h3>
5
+ <ul class="errors">
6
+ <% @sender.errors.each do |field, msg| %>
7
+ <li><%= msg %></li>
8
+ <% end %>
9
+ </ul>
10
+ <% end %>
11
+
12
+ <% if @all_friends_empty %>
13
+ <ul class="errors">
14
+ <li>Supply at least on friend</li>
15
+ </ul>
16
+ <% elsif @invalid_recipients %>
17
+ <ul class="errors">
18
+ <% for r in @invalid_recipients %>
19
+ <% r.errors.each do |field, msg| %>
20
+ <li><%= msg %></li>
21
+ <% end %>
22
+ <% end %>
23
+ </ul>
24
+ <% end %>
@@ -0,0 +1 @@
1
+ Bedankt voor het sturen
@@ -0,0 +1,22 @@
1
+ xml.response(:success => 0) do
2
+ xml.errors do
3
+
4
+ if !@sender.valid?
5
+ @sender.errors.each do |field, msg|
6
+ xml.error(msg, :field => "sender[#{field}]")
7
+ end
8
+ end
9
+
10
+ if @all_friends_empty
11
+ xml.error("Supply at least on friend")
12
+
13
+ elsif @invalid_recipients
14
+ @invalid_recipients.each_with_index do |r, index|
15
+ r.errors.each do |field, msg|
16
+ xml.error(msg, :field => "recipient_#{index}[#{field}]")
17
+ end
18
+ end
19
+ end
20
+
21
+ end
22
+ end
@@ -0,0 +1,11 @@
1
+ class FriendMailer < ActionMailer::Base
2
+
3
+ def message(sender, recipient)
4
+ subject 'Dit is het onderwerp van de stf mail'
5
+ recipients "#{recipient.name} <#{recipient.email}>"
6
+ from "#{sender.name} <#{sender.email}>"
7
+ body :sender => sender, :recipient => recipient
8
+ content_type "text/html"
9
+ end
10
+
11
+ end
@@ -0,0 +1,41 @@
1
+ <% if !@form_valid %>
2
+ <%= render :partial => "errors" %>
3
+ <% end %>
4
+
5
+ <% form_for(nil, :url => send_to_friends_path) do |f| %>
6
+ <fieldset>
7
+ <h3>Your friends</h3>
8
+ <% @recipients.each_with_index do |recipient, i| %>
9
+ <% f.object_name = "recipient_#{i}" %>
10
+ <% f.object = recipient %>
11
+ <div>
12
+ <label for="name_recipient">Name<% if i==0 %><span class="required">*</span><% end %></label>
13
+ <%= f.text_field :name, :class => "text" %>
14
+ </div>
15
+ <div>
16
+ <label for="email_recipient">Email<% if i==0 %><span class="required">*</span><% end %></label>
17
+ <%= f.text_field :email, :class => "text" %>
18
+ </div>
19
+ <% end %>
20
+
21
+ <% f.object_name = "sender" %>
22
+ <% f.object = @sender %>
23
+ <div>
24
+ <label for="name_sender">Your name<span class="required">*</span></label>
25
+ <%= f.text_field :name, :class => "text" %>
26
+ </div>
27
+ <div>
28
+ <label for="email_sender">Your email<span class="required">*</span></label>
29
+ <%= f.text_field :email, :class => "text" %>
30
+ </div>
31
+ <div>
32
+ <%= f.text_area :message %>
33
+ </div>
34
+ <input type="submit" class="submit-button" value="Send-a-Friend" />
35
+
36
+ <div>
37
+ <span class="required">*</span> required fields
38
+ </div>
39
+
40
+ </fieldset>
41
+ <% end %>
@@ -0,0 +1,19 @@
1
+ <h1>Dear <%= @recipient.name %>,</h1>
2
+
3
+ <p>
4
+ have a look at this site, it's cool!! <a href="<%= HOME_URL %>"><%= HOME_URL %></a>
5
+ </p>
6
+
7
+ <p>
8
+ <% if !@sender.message.blank? %>
9
+ <%= h @sender.message %>
10
+ <% else %>
11
+ no custom message was sent
12
+ <% end %>
13
+ </p>
14
+
15
+ <p>
16
+ Regards,
17
+ <br><br><br>
18
+ <%= @sender.name %>
19
+ </p>
@@ -0,0 +1,15 @@
1
+ ** plain text alternative **
2
+
3
+ Dear <%= @recipient.name %>,
4
+
5
+ have a look at this site: <%= HOME_URL %>
6
+
7
+ <% if !@sender.message.blank? %>
8
+ <%= @sender.message %>
9
+ <% else %>
10
+ no custom message was sent
11
+ <% end %>
12
+
13
+ Regards,
14
+
15
+ <%= @sender.name %>
@@ -0,0 +1,15 @@
1
+ class Recipient < ActiveRecord::BaseWithoutTable
2
+
3
+ column :name, :string
4
+ column :email, :string
5
+
6
+ validates_presence_of :name, :message => 'empty_recipient_name'
7
+
8
+ validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/,
9
+ :message => "invalid_recipient_email_ address"
10
+
11
+ def empty?
12
+ self.name.blank? && self.email.blank?
13
+ end
14
+
15
+ end
@@ -0,0 +1,60 @@
1
+ class SendToFriendsController < ApplicationController
2
+
3
+ FRIEND_COUNT = 3
4
+
5
+ # GET /send_to_friends
6
+ # shows the stf friend form
7
+ def index
8
+ @form_valid = true # don't show errors on unsubmitted form
9
+ # init senderr
10
+ @sender = Sender.new
11
+ @sender.name = "Your name"
12
+ @sender.email = "sender@example.com"
13
+ # init recipients
14
+ @recipients = Array.new(FRIEND_COUNT) { |i| Recipient.new }
15
+ end
16
+
17
+ # POST /send_to_friends
18
+ # POST /send_to_friends.xml
19
+ def create
20
+ @sender = Sender.new params[:sender]
21
+ @recipients = Array.new(FRIEND_COUNT) { |i| Recipient.new params["recipient_#{i}"] }
22
+
23
+ @valid_recipients = []
24
+ @invalid_recipients = []
25
+ @empty_recipients = []
26
+
27
+ @recipients.each do |r|
28
+ if r.empty?
29
+ @empty_recipients << r
30
+ elsif r.valid?
31
+ @valid_recipients << r
32
+ else
33
+ @invalid_recipients << r
34
+ end
35
+ end
36
+
37
+ # the form is only valid if the sender if valid
38
+ # and at least one valid friend was received
39
+ # and no invalid friends were received
40
+ if @sender.valid? && @valid_recipients.size >= 1 && @invalid_recipients.empty?
41
+ @form_valid = true
42
+ # send out emails one-by-one
43
+ @valid_recipients.each {|r| FriendMailer.deliver_message(@sender, r) }
44
+ else
45
+ @form_valid = false
46
+ @all_friends_empty = @empty_recipients.size == FRIEND_COUNT
47
+ end
48
+
49
+ respond_to do |format|
50
+ if @form_valid
51
+ format.xml { render :xml => '<response success="0" />' }
52
+ format.html # create.rhtml
53
+ else
54
+ format.xml { render :action => 'errors' }
55
+ format.html { render :action => 'index' }
56
+ end
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,13 @@
1
+ class Sender < ActiveRecord::BaseWithoutTable
2
+
3
+ column :name, :string
4
+ column :email, :string
5
+ column :url, :string
6
+ column :message, :text
7
+
8
+ validates_format_of :email, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/,
9
+ :message => "invalid_sender_address"
10
+
11
+ validates_presence_of :name, :message => 'invalid_sender_name'
12
+
13
+ end
@@ -0,0 +1,12 @@
1
+ require "test/unit"
2
+ class SendToFriendsGeneratorTest < Test::Unit::TestCase
3
+
4
+ def setup
5
+ end
6
+
7
+ def test_send_to_friends_generator
8
+ assert true
9
+ end
10
+ end
11
+
12
+
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.9.4
3
+ specification_version: 1
4
+ name: send_to_friends_generator
5
+ version: !ruby/object:Gem::Version
6
+ version: 1.0.0
7
+ date: 2007-10-01 00:00:00 +02:00
8
+ summary: Rails generator for boiler plate send-to-friend code
9
+ require_paths:
10
+ - lib
11
+ email: jeroen.houben@lostboys.nl
12
+ homepage: " by Jeroen Houben <jeroen.houben@lostboys.nl>"
13
+ rubyforge_project: lostboysrb
14
+ description: "== FEATURES/PROBLEMS: * a simple FriendMailer object * email templates for HTML and plain text alternative * configurable amount of friends * support for including a user submitted message in the email * support for HTML as well as XML (nice for Flash integration) * server-side validation * error messages in HTML or XML * future feature: search and replace script to replace all relative image tags with absolute ones (http://) == SYNOPSIS: ./script/generate send_to_friends [options]"
15
+ autorequire:
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: true
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ - |
29
+ -----BEGIN CERTIFICATE-----
30
+ MIIDNDCCAhygAwIBAgIBADANBgkqhkiG9w0BAQUFADBAMQ8wDQYDVQQDDAZqZXJv
31
+ ZW4xGTAXBgoJkiaJk/IsZAEZFglzdXBlcmNvb2wxEjAQBgoJkiaJk/IsZAEZFgJu
32
+ bDAeFw0wNzA5MjkxNzQyNTNaFw0wODA5MjgxNzQyNTNaMEAxDzANBgNVBAMMBmpl
33
+ cm9lbjEZMBcGCgmSJomT8ixkARkWCXN1cGVyY29vbDESMBAGCgmSJomT8ixkARkW
34
+ Am5sMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApAoWdXiqyx8ykSf6
35
+ vo9cLxji1BzPlXKtm4KazoseSI6l3Tfn4Y1Uj5qVH7wU215wccuAVjQRvORfC/St
36
+ f7epz77zKLLGbTsgI75UYs04NmYqKb0qs9Z1VQezuextLKxuBucwIftJ1Gc+BoQV
37
+ /aZyfXIstvVXtEb1goVx92+UZhoLbYl1Y2E7dwFkAIQFisLxbYitkgkf5Qs0S5fJ
38
+ Ps4JeZbxyCZSpBvIHf9mqBpuz8EgxoA8wHgqYbRH+6r7QpZIktL9PGpCetk8O5Lp
39
+ nQ37TtDe90PN6YzD8nTa2E27ENEPoWrcrdgk34ZoAyvczgSgCCVksAHupVdeauuY
40
+ 0gbOowIDAQABozkwNzAJBgNVHRMEAjAAMAsGA1UdDwQEAwIEsDAdBgNVHQ4EFgQU
41
+ 2bc9z2clRZmUJFhabTaD49ztV64wDQYJKoZIhvcNAQEFBQADggEBABhLP2zJCOj4
42
+ BSDxOM7DKtFxbQxVvJ+cp1Bh+qxKWnIzf+zIsXo3dS/leqwu90ttemPiA1jx4dlR
43
+ z2sEUMc0UT8nmE8+BYrC40CZEGWeLQJf/3PnHmDBBWi8sXtCk52YA6Y+e+Guiict
44
+ Nb4xLZy5Cj7AWhRnkb7t50hveBlggUMvGqVCI8y5Bw9CG5JZJLyO4kKee+E1cEr+
45
+ ViFjbjwsynRO3D9HQK9bybqh0T47KAKAU8FJN6SAxZbWC6HIJ6QqQsoc9J3CfPiM
46
+ 8HsOzocrGEmlI+ACaz+n3gEKCnGdwqxxxU0zUJAFNGBfKl71IKjQioyPrLc/y+nF
47
+ jNXDjwyAUrA=
48
+ -----END CERTIFICATE-----
49
+
50
+ post_install_message:
51
+ authors:
52
+ - Jeroen Houben
53
+ files:
54
+ - README.txt
55
+ - send_to_friends_generator.rb
56
+ - USAGE
57
+ - lib/dummy.txt
58
+ - bin/replace_vars
59
+ - templates/INSTRUCTIONS
60
+ - templates/INSTRUCTIONS_FOR_MISSING_PLUGIN
61
+ - templates/_errors.rhtml
62
+ - templates/create.rhtml
63
+ - templates/errors.rxml
64
+ - templates/friend_mailer.rb
65
+ - templates/index.rhtml
66
+ - templates/message.text.html.rhtml
67
+ - templates/message.text.plain.rhtml
68
+ - templates/recipient.rb
69
+ - templates/send_to_friends_controller.rb
70
+ - templates/sender.rb
71
+ - test/test_send_to_friends_generator.rb
72
+ test_files:
73
+ - test/test_send_to_friends_generator.rb
74
+ rdoc_options:
75
+ - --main
76
+ - README.txt
77
+ extra_rdoc_files:
78
+ - README.txt
79
+ - lib/dummy.txt
80
+ executables:
81
+ - replace_vars
82
+ extensions: []
83
+
84
+ requirements: []
85
+
86
+ dependencies:
87
+ - !ruby/object:Gem::Dependency
88
+ name: hoe
89
+ version_requirement:
90
+ version_requirements: !ruby/object:Gem::Version::Requirement
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ version: 1.3.0
95
+ version:
Binary file