merb-mailer 0.9.7 → 0.9.8

Sign up to get free protection for your applications and to get access to all the features.
data/Generators ADDED
@@ -0,0 +1,4 @@
1
+ scope 'merb-gen' do
2
+ dir = File.join(File.dirname(__FILE__), 'lib', 'generators/')
3
+ Merb.add_generators dir + 'mailer_generator'
4
+ end
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2008 YOUR NAME
1
+ Copyright (c) 2008 Yehuda Katz
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/Rakefile CHANGED
@@ -17,7 +17,7 @@ GEM_EMAIL = "ykatz@engineyard.com"
17
17
 
18
18
  GEM_NAME = "merb-mailer"
19
19
  PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
20
- GEM_VERSION = (Merb::MORE_VERSION rescue "0.9.7") + PKG_BUILD
20
+ GEM_VERSION = (Merb::MORE_VERSION rescue "0.9.8") + PKG_BUILD
21
21
 
22
22
  RELEASE_NAME = "REL #{GEM_VERSION}"
23
23
 
@@ -35,10 +35,10 @@ spec = Gem::Specification.new do |s|
35
35
  s.author = GEM_AUTHOR
36
36
  s.email = GEM_EMAIL
37
37
  s.homepage = PROJECT_URL
38
- s.add_dependency('merb-core', '>= 0.9.7')
38
+ s.add_dependency('merb-core', '>= 0.9.8')
39
39
  s.add_dependency('mailfactory', '>= 1.2.3')
40
40
  s.require_path = 'lib'
41
- s.files = %w(LICENSE README.textile Rakefile TODO) + Dir.glob("{lib,spec}/**/*")
41
+ s.files = %w(LICENSE README.textile Rakefile TODO Generators) + Dir.glob("{lib,spec}/**/*")
42
42
  end
43
43
 
44
44
  Rake::GemPackageTask.new(spec) do |pkg|
@@ -46,21 +46,33 @@ Rake::GemPackageTask.new(spec) do |pkg|
46
46
  end
47
47
 
48
48
  desc "Install the gem"
49
- task :install => [:package] do
50
- sh install_command(GEM_NAME, GEM_VERSION)
49
+ task :install do
50
+ Merb::RakeHelper.install(GEM_NAME, :version => GEM_VERSION)
51
51
  end
52
52
 
53
- namespace :jruby do
53
+ desc "Uninstall the gem"
54
+ task :uninstall do
55
+ Merb::RakeHelper.uninstall(GEM_NAME, :version => GEM_VERSION)
56
+ end
54
57
 
55
- desc "Run :package and install the resulting .gem with jruby"
56
- task :install => :package do
57
- sh jinstall_command(GEM_NAME, GEM_VERSION)
58
+ desc "Create a gemspec file"
59
+ task :gemspec do
60
+ File.open("#{GEM_NAME}.gemspec", "w") do |file|
61
+ file.puts spec.to_ruby
58
62
  end
59
-
60
63
  end
61
64
 
62
- desc "Run all specs"
63
- Spec::Rake::SpecTask.new("specs") do |t|
64
- t.spec_opts = ["--format", "specdoc", "--colour"]
65
- t.spec_files = Dir["spec/**/*_spec.rb"].sort
65
+ desc "Run all examples (or a specific spec with TASK=xxxx)"
66
+ Spec::Rake::SpecTask.new('spec') do |t|
67
+ t.spec_opts = ["-cfs"]
68
+ t.spec_files = begin
69
+ if ENV["TASK"]
70
+ ENV["TASK"].split(',').map { |task| "spec/**/#{task}_spec.rb" }
71
+ else
72
+ FileList['spec/**/*_spec.rb']
73
+ end
74
+ end
66
75
  end
76
+
77
+ desc 'Default: run spec examples'
78
+ task :default => 'spec'
@@ -6,21 +6,21 @@ module Merb
6
6
  # * Create a MailController subclass with actions and templates.
7
7
  # * Call the MailController from another Controller via the send_mail method.
8
8
  #
9
- # First, create a file in app/mailers that subclasses Merb::MailController.
9
+ # First, create a file in app/mailers that subclasses Merb::MailController.
10
10
  # The actions in this controller will do nothing but render mail.
11
11
  #
12
12
  # # app/mailers/article_mailer.rb
13
13
  # class ArticleMailer < Merb::MailController
14
- #
14
+ #
15
15
  # def notify
16
16
  # @user = params[:user]
17
17
  # render_mail
18
18
  # end
19
- #
19
+ #
20
20
  # end
21
21
  #
22
- # You also can access the params hash for values passed with the
23
- # Controller.send_mail method. See also the documentation for
22
+ # You also can access the params hash for values passed with the
23
+ # Controller.send_mail method. See also the documentation for
24
24
  # render_mail to see all the ways it can be called.
25
25
  #
26
26
  # Create a template in a subdirectory of app/mailers/views that corresponds
@@ -28,10 +28,10 @@ module Merb
28
28
  #
29
29
  # # app/mailers/views/article_mailer/notify.text.erb
30
30
  # Hey, <%= @user.name %>,
31
- #
31
+ #
32
32
  # We're running a sale on dog bones!
33
33
  #
34
- # Finally, call the Controller.send_mail method from a standard
34
+ # Finally, call the Controller.send_mail method from a standard
35
35
  # Merb controller.
36
36
  #
37
37
  # class Articles < Application
@@ -47,13 +47,13 @@ module Merb
47
47
  # render
48
48
  # end
49
49
  #
50
- # end
50
+ # end
51
51
  #
52
52
  # Note: If you don't pass a fourth argument to Controller.send_mail,
53
53
  # the controller's params will be sent to the MailController subclass
54
- # as params. However, you can explicitly send a hash of objects that
55
- # will populate the params hash instead. In either case, you must
56
- # set instance variables in the MailController's actions if you
54
+ # as params. However, you can explicitly send a hash of objects that
55
+ # will populate the params hash instead. In either case, you must
56
+ # set instance variables in the MailController's actions if you
57
57
  # want to use them in the MailController's views.
58
58
  #
59
59
  # The MailController class is very powerful. You can:
@@ -69,7 +69,7 @@ module Merb
69
69
  self._mailer_klass = Merb::Mailer
70
70
 
71
71
  attr_accessor :params, :mailer, :mail
72
- attr_reader :session, :base_controller
72
+ attr_reader :base_controller
73
73
 
74
74
  cattr_accessor :_subclasses
75
75
  self._subclasses = Set.new
@@ -91,15 +91,15 @@ module Merb
91
91
  def _template_location(action, type = nil, controller = controller_name)
92
92
  "#{controller}/#{action}.#{type}"
93
93
  end
94
-
95
- # The location to look for a template and mime-type. This is overridden
96
- # from AbstractController, which defines a version of this that does not
94
+
95
+ # The location to look for a template and mime-type. This is overridden
96
+ # from AbstractController, which defines a version of this that does not
97
97
  # involve mime-types.
98
98
  #
99
99
  # ==== Parameters
100
- # template<String>::
100
+ # template<String>::
101
101
  # The absolute path to a template - without mime and template extension.
102
- # The mime-type extension is optional - it will be appended from the
102
+ # The mime-type extension is optional - it will be appended from the
103
103
  # current content type if it hasn't been added already.
104
104
  # type<~to_s>::
105
105
  # The mime-type of the template that will be rendered. Defaults to nil.
@@ -115,15 +115,18 @@ module Merb
115
115
  def initialize(params = {}, controller = nil)
116
116
  @params = params
117
117
  @base_controller = controller
118
- @session = (controller && controller.session) || {}
119
118
  super
120
119
  end
121
120
 
121
+ def session
122
+ self.base_controller.request.session rescue {}
123
+ end
124
+
122
125
  # Sets the template root to the default mailer view directory.
123
126
  #
124
127
  # ==== Parameters
125
128
  # klass<Class>::
126
- # The Merb::MailController inheriting from the base class.
129
+ # The Merb::MailController inheriting from the base class.
127
130
  def self.inherited(klass)
128
131
  super
129
132
  klass._template_root = Merb.dir_for(:mailer) / "views" unless self._template_root
@@ -169,7 +172,7 @@ module Merb
169
172
  # "foo", this is identical to render_mail :foo.
170
173
  #
171
174
  # render_mail :foo
172
- #
175
+ #
173
176
  # checks for foo.html.ext and foo.text.ext and applies them as appropriate.
174
177
  #
175
178
  # render_mail :action => {:html => :foo, :text => :bar}
@@ -210,13 +213,13 @@ module Merb
210
213
  @_missing_templates = false # used to make sure that at least one template was found
211
214
  # If the options are not a hash, normalize to an action hash
212
215
  options = {:action => {:html => options, :text => options}} if !options.is_a?(Hash)
213
-
216
+
214
217
  # Take care of the options
215
218
  opts_hash = {}
216
219
  opts = options.dup
217
220
  actions = opts.delete(:action) if opts[:action].is_a?(Hash)
218
221
  templates = opts.delete(:template) if opts[:template].is_a?(Hash)
219
-
222
+
220
223
  # Prepare the options hash for each format
221
224
  # We need to delete anything relating to the other format here
222
225
  # before we try to render the template.
@@ -225,7 +228,7 @@ module Merb
225
228
  opts_hash[fmt] ||= actions[fmt] if actions && actions[fmt]
226
229
  opts_hash[:template] = templates[fmt] if templates && templates[fmt]
227
230
  end
228
-
231
+
229
232
  # Send the result to the mailer
230
233
  { :html => "rawhtml=", :text => "text="}.each do |fmt,meth|
231
234
  begin
@@ -249,14 +252,23 @@ module Merb
249
252
 
250
253
  # Mimic the behavior of absolute_url in AbstractController
251
254
  # but use @base_controller.request
252
- def absolute_url(name, rparams={})
253
- req = @base_controller.request
254
- uri = req.protocol + req.host + url(name, rparams)
255
+ def url(name, *args)
256
+ return base_controller.url(name, *args) if base_controller
257
+ super
258
+ end
259
+
260
+ alias_method :relative_url, :url
261
+
262
+ # Mimic the behavior of absolute_url in AbstractController
263
+ # but use @base_controller.request
264
+ def absolute_url(name, *args)
265
+ return base_controller.absolute_url(name, *args) if base_controller
266
+ super
255
267
  end
256
268
 
257
269
  # Attaches a file or multiple files to an email. You call this from a
258
270
  # method in your MailController (including a before filter).
259
- #
271
+ #
260
272
  # ==== Parameters
261
273
  # file_or_files<File, Array[File]>:: File(s) to attach.
262
274
  # filename<String>::
@@ -268,17 +280,17 @@ module Merb
268
280
  # ==== Examples
269
281
  # attach File.open("foo")
270
282
  # attach [File.open("foo"), File.open("bar")]
271
- #
283
+ #
272
284
  # If you are passing an array of files, you should use an array of the
273
285
  # allowed parameters:
274
- #
286
+ #
275
287
  # attach [[File.open("foo"), "bar", "text/html"], [File.open("baz"),
276
288
  # "bat", "text/css"]
277
- #
289
+ #
278
290
  # which would attach two files ("foo" and "baz" in the filesystem) as
279
291
  # "bar" and "bat" respectively. It would also set the mime-type as
280
292
  # "text/html" and "text/css" respectively.
281
- def attach( file_or_files, filename = file_or_files.is_a?(File) ? File.basename(file_or_files.path) : nil,
293
+ def attach( file_or_files, filename = file_or_files.is_a?(File) ? File.basename(file_or_files.path) : nil,
282
294
  type = nil, headers = nil)
283
295
  @mailer.attach(file_or_files, filename, type, headers)
284
296
  end
@@ -302,7 +314,7 @@ module Merb
302
314
  @mailer = self.class._mailer_klass.new(mail_params)
303
315
  @mail = @mailer.mail
304
316
  @method = method
305
-
317
+
306
318
  # dispatch and render use params[:action], so set it
307
319
  self.action_name = method
308
320
 
@@ -86,11 +86,11 @@ module Merb
86
86
  type = nil, headers = nil)
87
87
  if file_or_files.is_a?(Array)
88
88
  file_or_files.each do |v|
89
- if v.length < 2
90
- v << v.first.is_a?(File) ? File.basename(v.first.path) : nil
91
- end
92
- @mail.add_attachment_as *v
93
- end
89
+ if v.length < 2
90
+ v << v.first.is_a?(File) ? File.basename(v.first.path) : nil
91
+ end
92
+ @mail.add_attachment_as *v
93
+ end
94
94
  else
95
95
  raise ArgumentError, "You did not pass in a file. Instead, you sent a #{file_or_files.class}" if !file_or_files.is_a?(File)
96
96
  @mail.add_attachment_as(file_or_files, filename, type, headers)
@@ -100,7 +100,7 @@ module Merb
100
100
  # ==== Parameters
101
101
  # o<Hash{~to_s => Object}>:: Configuration commands to send to MailFactory.
102
102
  def initialize(o={})
103
- self.config = {:sendmail_path => '/usr/sbin/sendmail'} if config.nil?
103
+ self.config = { :sendmail_path => '/usr/sbin/sendmail' } if config.nil?
104
104
  o[:rawhtml] = o.delete(:html)
105
105
  m = MailFactory.new()
106
106
  o.each { |k,v| m.send "#{k}=", v }
@@ -1,7 +1,8 @@
1
1
  require File.dirname(__FILE__) + '/spec_helper'
2
2
 
3
3
  Spec::Runner.configure do |config|
4
- config.include Merb::Test::RequestHelper
4
+ config.include Merb::Test::RequestHelper
5
+ config.include Merb::Test::ControllerHelper
5
6
  end
6
7
 
7
8
  class Merb::Mailer
@@ -48,19 +49,32 @@ class TestMailController < Merb::MailController
48
49
  def ninth
49
50
  render_mail
50
51
  end
51
-
52
+
52
53
  def tenth
53
54
  render_mail
54
55
  end
55
56
 
57
+ def generates_relative_url
58
+ render_mail
59
+ end
60
+
61
+ def generates_absolute_url
62
+ render_mail
63
+ end
64
+ end
65
+
66
+
67
+ Merb::Router.prepare do
68
+ match("/subprojects/:subproject").
69
+ to(:controller => "test_controller", :action => "whatever").
70
+ name(:merb_subproject)
56
71
  end
57
72
 
73
+
58
74
  class TestController < Merb::Controller
59
-
60
75
  def one
61
76
  send_mail TestMailController, :ninth, {:from => "foo@bar.com", :to => "foo@bar.com"}, {:x => "ONE_CONTROLLER"}
62
77
  end
63
-
64
78
  end
65
79
 
66
80
  describe "A Merb Mail controller" do
@@ -138,37 +152,20 @@ describe "A Merb Mail controller" do
138
152
  Merb.logger.should_receive(:error).once
139
153
  deliver :tenth
140
154
  end
141
-
142
- end
143
155
 
144
- # describe "Merb::MailController with url generation" do
145
- #
146
- # it_should_behave_like "class with general url generation"
147
- # it_should_behave_like "non routeable controller with url mixin"
148
- #
149
- # def new_url_controller(route, params = {:action => 'show', :controller => 'Test'})
150
- # request = OpenStruct.new
151
- # request.route = route
152
- # request.params = params
153
- # response = OpenStruct.new
154
- # response.read = ""
155
- #
156
- # @controller = Merb::Controller.build(request, response)
157
- # TestMailController.new(params, @controller)
158
- # end
159
- #
160
- # it "should raise an error if no controller is specified and the base controller is not set" do
161
- # c = new_url_controller(@default_route, {})
162
- # lambda do
163
- # the_url = c.url(:action => "bar")
164
- # end.should raise_error
165
- # end
166
- #
167
- # it "should use the base controller when it is set to generate a url when no :controller option is specified" do
168
- # c = new_url_controller(@defualt_route, :controller => "foo")
169
- # lambda do
170
- # the_url = c.url(:action => "bar")
171
- # the_url.should == "/foo/bar"
172
- # end.should_not raise_error
173
- # end
174
- # end
156
+ it "delegates relative url generation to base controller" do
157
+ controller = TestController.new(fake_request)
158
+ TestMailController.new({ :subproject => "core" }, controller).
159
+ dispatch_and_deliver :generates_relative_url, :from => "foo@bar.com", :to => "foo@bar.com"
160
+
161
+ Merb::Mailer.deliveries.last.text.should == "TEXT\n/subprojects/core\nENDTEXT"
162
+ end
163
+
164
+ it "delegates absolute url generation to base controller" do
165
+ controller = TestController.new(fake_request)
166
+ TestMailController.new({ :subproject => "extlib" }, controller).
167
+ dispatch_and_deliver :generates_absolute_url, :from => "foo@bar.com", :to => "foo@bar.com"
168
+
169
+ Merb::Mailer.deliveries.last.text.should == "TEXT\nhttp://merbivore.com/subprojects/extlib\nENDTEXT"
170
+ end
171
+ end
data/spec/mailer_spec.rb CHANGED
@@ -26,6 +26,12 @@ def setup_test_mailer klass = TestMailer
26
26
  end
27
27
 
28
28
  describe "a merb mailer" do
29
+
30
+ before(:each) do
31
+ TestMailer.deliveries.clear
32
+ TestSMTPMailer.deliveries.clear
33
+ TestSendmailMailer.deliveries.clear
34
+ end
29
35
 
30
36
  it "should be able to send test emails" do
31
37
  setup_test_mailer
@@ -34,7 +40,8 @@ describe "a merb mailer" do
34
40
  delivery = TestMailer.deliveries.last
35
41
  delivery.to.should include("test@test.com")
36
42
  delivery.from.should include("foo@bar.com")
37
- delivery.subject.should include("=?utf-8?Q?Test_Subject=?=")
43
+ delivery.subject.first.should =~ /utf-8/
44
+ delivery.subject.first.should =~ /Test_Subject/
38
45
  delivery.body.should include("Test")
39
46
  end
40
47
 
@@ -0,0 +1 @@
1
+ <%= absolute_url(:merb_subproject, :subproject => params[:subproject], :host => "merbivore.com") %>
@@ -0,0 +1 @@
1
+ <%= url(:merb_subproject, params[:subproject]) %>
data/spec/spec_helper.rb CHANGED
@@ -1,7 +1,11 @@
1
- $TESTING=true
2
1
  $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
3
2
  require "rubygems"
4
3
  require "merb-core"
5
4
  require "merb-mailer"
6
5
 
7
- Merb.start :environment => 'test'
6
+ Merb::Config.use do |c|
7
+ c[:session_store] = :memory
8
+ end
9
+
10
+
11
+ Merb.start :environment => 'test'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: merb-mailer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.7
4
+ version: 0.9.8
5
5
  platform: ruby
6
6
  authors:
7
7
  - Yehuda Katz
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2008-09-10 00:00:00 +03:00
12
+ date: 2008-10-06 00:00:00 +03:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -20,7 +20,7 @@ dependencies:
20
20
  requirements:
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 0.9.7
23
+ version: 0.9.8
24
24
  version:
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: mailfactory
@@ -47,6 +47,7 @@ files:
47
47
  - README.textile
48
48
  - Rakefile
49
49
  - TODO
50
+ - Generators
50
51
  - lib/generators
51
52
  - lib/generators/mailer_generator.rb
52
53
  - lib/generators/templates
@@ -78,6 +79,8 @@ files:
78
79
  - spec/mailers/views/test_mail_controller/eighth.text.erb
79
80
  - spec/mailers/views/test_mail_controller/first.html.erb
80
81
  - spec/mailers/views/test_mail_controller/first.text.erb
82
+ - spec/mailers/views/test_mail_controller/generates_absolute_url.text.erb
83
+ - spec/mailers/views/test_mail_controller/generates_relative_url.text.erb
81
84
  - spec/mailers/views/test_mail_controller/ninth.html.erb
82
85
  - spec/mailers/views/test_mail_controller/ninth.text.erb
83
86
  - spec/mailers/views/test_mail_controller/second.text.erb