has_web_fallback 0.1.3 → 0.2.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/README CHANGED
@@ -37,7 +37,7 @@ end
37
37
 
38
38
  ... and in the template
39
39
 
40
- <%= link_to("View this email online", @web_fallback_url, :path_only=>false) %>
40
+ <%= link_to("View this email online", APPLICATION_URL+@web_fallback_url) %>
41
41
 
42
42
 
43
43
  Copyright (c) 2009 Gavin Montague, released under the MIT license
@@ -3,26 +3,34 @@ require 'ftools'
3
3
  require 'uuidtools'
4
4
 
5
5
  module Leftbrained
6
-
6
+
7
7
  module HasWebFallback #:nodoc:
8
+
9
+ @has_web_fallback_cache_path = nil
10
+
11
+ def self.has_web_fallback_cache_path=(path)
12
+ @has_web_fallback_cache_path = path
13
+ end
14
+
15
+ def self.has_web_fallback_cache_path
16
+ @has_web_fallback_cache_path || raise(NoCachePathSetException)
17
+ end
8
18
 
9
- WEB_FALLBACK_PATH = File.join(File.expand_path(RAILS_ROOT), "public")
10
19
 
11
20
  def self.included(base)
12
21
  base.extend(ClassMethods)
13
22
  end
14
23
  # mattr_accessor :write_web_fallbacks
15
-
24
+
16
25
  module ClassMethods
17
-
18
26
  def has_web_fallback?(method_name=nil)
19
27
  false
20
28
  end
21
-
29
+
22
30
  def has_web_fallback(name, options={})
23
31
  extend Leftbrained::HasWebFallback::IncludedClassMethods
24
32
  write_inheritable_attribute(:methods_with_web_fallback, {}) if methods_with_web_fallback.nil?
25
- methods_with_web_fallback[name] = { :keep_for=>7.days }.merge(options)
33
+ methods_with_web_fallback[name] = { :keep_for=>7.days, :cache_path=>nil }.merge(options)
26
34
 
27
35
  class_eval <<-EOV
28
36
  include Leftbrained::HasWebFallback::InstanceMethods
@@ -75,15 +83,23 @@ module Leftbrained
75
83
  end
76
84
 
77
85
  def cache_period_for_method_with_web_fallback(method_name)
78
- methods_with_web_fallback[method_name.to_sym][:keep_for]
86
+ methods_with_web_fallback[method_name.to_sym][:keep_for]
79
87
  end
80
88
 
81
89
  def url_for_web_fallback(method_name)
90
+ if methods_with_web_fallback[method_name.to_sym]
91
+ methods_with_web_fallback[method_name.to_sym][:cache_path] || default_url_for_web_fallback(method_name)
92
+ else
93
+ default_url_for_web_fallback(method_name)
94
+ end
95
+ end
96
+
97
+ def default_url_for_web_fallback(method_name)
82
98
  "/system/web_fallback/#{self.to_s.underscore.gsub("_mailer", '')}/#{method_name}"
83
99
  end
84
-
100
+
85
101
  def path_for_web_fallback(method_name)
86
- File.join(WEB_FALLBACK_PATH, url_for_web_fallback(method_name))
102
+ File.join(Leftbrained::HasWebFallback.has_web_fallback_cache_path, url_for_web_fallback(method_name))
87
103
  end
88
104
 
89
105
  end # ClassMethods
@@ -138,9 +154,15 @@ module Leftbrained
138
154
  end
139
155
 
140
156
  end # InstanceMethods
141
-
157
+
158
+ class NoCachePathSetException < RuntimeError
159
+ def message
160
+ "No path was set on Leftbrained::HasWebFallback.has_web_fallback_cache_path - you must set this before creating fallbacks"
161
+ end
162
+ end
163
+
142
164
  end
143
165
 
144
166
  end
145
167
 
146
- ActionMailer::Base.class_eval { include Leftbrained::HasWebFallback }
168
+ ActionMailer::Base.class_eval { include Leftbrained::HasWebFallback }
metadata CHANGED
@@ -1,7 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_web_fallback
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
5
11
  platform: ruby
6
12
  authors:
7
13
  - Gavin Montague
@@ -9,39 +15,51 @@ autorequire:
9
15
  bindir: bin
10
16
  cert_chain: []
11
17
 
12
- date: 2009-10-27 00:00:00 +00:00
18
+ date: 2011-06-18 00:00:00 +01:00
13
19
  default_executable:
14
20
  dependencies:
15
21
  - !ruby/object:Gem::Dependency
16
22
  name: thoughtbot-shoulda
17
- type: :development
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
20
26
  requirements:
21
27
  - - ">="
22
28
  - !ruby/object:Gem::Version
29
+ hash: 3
30
+ segments:
31
+ - 0
23
32
  version: "0"
24
- version:
33
+ type: :development
34
+ version_requirements: *id001
25
35
  - !ruby/object:Gem::Dependency
26
36
  name: actionmailer
27
- type: :runtime
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
37
+ prerelease: false
38
+ requirement: &id002 !ruby/object:Gem::Requirement
39
+ none: false
30
40
  requirements:
31
41
  - - ">="
32
42
  - !ruby/object:Gem::Version
43
+ hash: 3
44
+ segments:
45
+ - 0
33
46
  version: "0"
34
- version:
47
+ type: :runtime
48
+ version_requirements: *id002
35
49
  - !ruby/object:Gem::Dependency
36
50
  name: uuidtools
37
- type: :runtime
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
51
+ prerelease: false
52
+ requirement: &id003 !ruby/object:Gem::Requirement
53
+ none: false
40
54
  requirements:
41
55
  - - ">="
42
56
  - !ruby/object:Gem::Version
57
+ hash: 3
58
+ segments:
59
+ - 0
43
60
  version: "0"
44
- version:
61
+ type: :runtime
62
+ version_requirements: *id003
45
63
  description: Adds the commonly required behaviour of caching web accessible versions of HTML emails to ActionMailer
46
64
  email: gavin@leftbrained.co.uk
47
65
  executables: []
@@ -60,29 +78,34 @@ homepage: http://github.com/Govan/has_web_fallback
60
78
  licenses: []
61
79
 
62
80
  post_install_message:
63
- rdoc_options:
64
- - --charset=UTF-8
81
+ rdoc_options: []
82
+
65
83
  require_paths:
66
84
  - lib
67
85
  required_ruby_version: !ruby/object:Gem::Requirement
86
+ none: false
68
87
  requirements:
69
88
  - - ">="
70
89
  - !ruby/object:Gem::Version
90
+ hash: 3
91
+ segments:
92
+ - 0
71
93
  version: "0"
72
- version:
73
94
  required_rubygems_version: !ruby/object:Gem::Requirement
95
+ none: false
74
96
  requirements:
75
97
  - - ">="
76
98
  - !ruby/object:Gem::Version
99
+ hash: 3
100
+ segments:
101
+ - 0
77
102
  version: "0"
78
- version:
79
103
  requirements: []
80
104
 
81
105
  rubyforge_project:
82
- rubygems_version: 1.3.5
106
+ rubygems_version: 1.3.7
83
107
  signing_key:
84
108
  specification_version: 3
85
109
  summary: Adds the commonly required behaviour of caching web accessible versions of HTML emails to ActionMailer
86
- test_files:
87
- - test/has_web_fallback_test.rb
88
- - test/test_helper.rb
110
+ test_files: []
111
+
@@ -1,132 +0,0 @@
1
- require 'test_helper'
2
-
3
-
4
- require "#{File.dirname(__FILE__)}/test_helper"
5
-
6
- class HasWebFallbackTest < ActionMailer::TestCase
7
-
8
- class ExampleMailer < ActionMailer::Base
9
- has_web_fallback :welcome
10
- has_web_fallback :goodbye
11
-
12
- def welcome
13
- recipients "no-one@example.com"
14
- body[:name] = "Joe"
15
- subject "welcome"
16
- end
17
-
18
- def goodbye
19
- body[:name] = "Joe"
20
- subject "goodbye"
21
- end
22
-
23
- def recover_password
24
- end
25
- end
26
- #
27
-
28
- context "When included, ExampleMailer" do
29
-
30
- should "have a class attribute for tracking methods with fallbacks" do
31
- assert ExampleMailer.inheritable_attributes.include? :methods_with_web_fallback
32
- end
33
-
34
- should "be able to identify if a method has a web fallback" do
35
- assert ExampleMailer.has_web_fallback?(:welcome)
36
- assert ExampleMailer.has_web_fallback?(:goodbye)
37
- end
38
-
39
- should "be able to identify a method without a web fallback" do
40
- assert !ExampleMailer.has_web_fallback?(:recover_password)
41
- end
42
-
43
- should "be able to generate UUIDs" do # I know this is a lousy test, but how the hell would one test a UUID?
44
- uuid_1 = ExampleMailer.web_fallback_uuid_for(:welcome)
45
- uuid_2 = ExampleMailer.web_fallback_uuid_for(:welcome)
46
- assert uuid_1 != uuid_2, "UUID clash - #{uuid_1} vs #{uuid_2}"
47
- end
48
-
49
- should "not report that ActionMailer::Base uses has_web_fallback" do
50
- assert !ActionMailer::Base.has_web_fallback?
51
- end
52
-
53
- should "respond to has_web_fallback" do
54
- assert ExampleMailer.has_web_fallback?
55
- end
56
-
57
- should_eventually "be able to list cached files for a given function that should be deleted" do
58
- # I have quite literally no idea how to test this without
59
- # mocking the living shit out File, or writing out to the file system.
60
- end
61
-
62
- end
63
-
64
- context "A method with no fallback" do
65
- setup do
66
- @recover_password_mail = ExampleMailer.send(:new, 'recover_password')
67
- end
68
-
69
- should "know it doesn't have a fallback" do
70
- assert !@recover_password_mail.has_web_fallback?
71
- end
72
-
73
- should "not write out an html version" do
74
- @recover_password_mail.deliver!
75
- assert !File.exists?(@recover_password_mail.web_fallback_path)
76
- end
77
- end
78
-
79
- context "A method with fallback" do
80
- setup do
81
- @welcome_mail = ExampleMailer.send(:new, 'welcome')
82
- end
83
-
84
- should "know if it has a web fallback " do
85
- assert @welcome_mail.has_web_fallback?
86
- end
87
-
88
- context "and an html part" do
89
- should "have a url to the email " do
90
- assert_match /^\/system\/web_fallback\/has_web_fallback_test\/example\/welcome\/[a-z0-9\-]+\.html$/, @welcome_mail.web_fallback_url
91
- end
92
-
93
- should "have an absolute_path to the file's location" do
94
- assert @welcome_mail.web_fallback_path
95
- end
96
-
97
- should "know it has an html part" do
98
- assert @welcome_mail.has_html_part?
99
- end
100
-
101
- should "be able to include a link in any of the body parts." do
102
- @welcome_mail.parts.each do |part|
103
- assert_match @welcome_mail.web_fallback_url, part.body
104
- end
105
- end
106
-
107
- should "write out an html version" do
108
- @welcome_mail.deliver!
109
- assert File.exists?(@welcome_mail.web_fallback_path)
110
- end
111
- end
112
-
113
- context "but no html part" do
114
- setup do
115
- @goodbye_mail = ExampleMailer.send(:new, 'goodbye')
116
- end
117
-
118
- should "know it doesn't have an html part" do
119
- assert !@goodbye_mail.has_html_part?
120
- end
121
-
122
- should "not write out an html version" do
123
- @goodbye_mail.deliver!
124
- assert !File.exists?(@goodbye_mail.web_fallback_path)
125
- end
126
-
127
- end
128
-
129
- end
130
-
131
- end
132
-
data/test/test_helper.rb DELETED
@@ -1,6 +0,0 @@
1
- require 'stringio'
2
- require 'test/unit'
3
- require File.dirname(__FILE__) + '/../lib/has_web_fallback'
4
-
5
- # Reset out template path for AM to work with tests, apologies to any one else running tests....
6
- ActionMailer::Base.template_root = File.join(File.dirname(__FILE__), 'app', 'views')