mail_envi 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/.document +5 -0
  2. data/Gemfile +16 -0
  3. data/Gemfile.lock +91 -0
  4. data/LICENSE.txt +20 -0
  5. data/README.md +102 -0
  6. data/Rakefile +55 -0
  7. data/VERSION +1 -0
  8. data/lib/mail_envi/config.rb +23 -0
  9. data/lib/mail_envi.rb +38 -0
  10. data/mail_envi.gemspec +108 -0
  11. data/test/dummy/.gitignore +4 -0
  12. data/test/dummy/Rakefile +7 -0
  13. data/test/dummy/app/controllers/application_controller.rb +3 -0
  14. data/test/dummy/app/helpers/application_helper.rb +2 -0
  15. data/test/dummy/app/mailers/user_mailer.rb +12 -0
  16. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  17. data/test/dummy/config/application.rb +48 -0
  18. data/test/dummy/config/boot.rb +9 -0
  19. data/test/dummy/config/database.yml +22 -0
  20. data/test/dummy/config/environment.rb +5 -0
  21. data/test/dummy/config/environments/development.rb +26 -0
  22. data/test/dummy/config/environments/production.rb +49 -0
  23. data/test/dummy/config/environments/test.rb +35 -0
  24. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  25. data/test/dummy/config/initializers/inflections.rb +10 -0
  26. data/test/dummy/config/initializers/mail_envi.rb +3 -0
  27. data/test/dummy/config/initializers/mime_types.rb +5 -0
  28. data/test/dummy/config/initializers/secret_token.rb +7 -0
  29. data/test/dummy/config/initializers/session_store.rb +8 -0
  30. data/test/dummy/config/locales/en.yml +5 -0
  31. data/test/dummy/config/routes.rb +58 -0
  32. data/test/dummy/config.ru +4 -0
  33. data/test/dummy/db/seeds.rb +7 -0
  34. data/test/dummy/public/404.html +26 -0
  35. data/test/dummy/public/422.html +26 -0
  36. data/test/dummy/public/500.html +26 -0
  37. data/test/dummy/public/favicon.ico +0 -0
  38. data/test/dummy/public/images/rails.png +0 -0
  39. data/test/dummy/public/index.html +239 -0
  40. data/test/dummy/public/javascripts/application.js +2 -0
  41. data/test/dummy/public/javascripts/controls.js +965 -0
  42. data/test/dummy/public/javascripts/dragdrop.js +974 -0
  43. data/test/dummy/public/javascripts/effects.js +1123 -0
  44. data/test/dummy/public/javascripts/prototype.js +6001 -0
  45. data/test/dummy/public/javascripts/rails.js +191 -0
  46. data/test/dummy/public/robots.txt +5 -0
  47. data/test/dummy/public/stylesheets/.gitkeep +0 -0
  48. data/test/dummy/script/rails +6 -0
  49. data/test/mail_envi/test_config.rb +55 -0
  50. data/test/mailer/test_user_mailer.rb +18 -0
  51. data/test/test_helper.rb +26 -0
  52. data/test/test_mail_envi.rb +45 -0
  53. metadata +184 -0
@@ -0,0 +1,191 @@
1
+ (function() {
2
+ // Technique from Juriy Zaytsev
3
+ // http://thinkweb2.com/projects/prototype/detecting-event-support-without-browser-sniffing/
4
+ function isEventSupported(eventName) {
5
+ var el = document.createElement('div');
6
+ eventName = 'on' + eventName;
7
+ var isSupported = (eventName in el);
8
+ if (!isSupported) {
9
+ el.setAttribute(eventName, 'return;');
10
+ isSupported = typeof el[eventName] == 'function';
11
+ }
12
+ el = null;
13
+ return isSupported;
14
+ }
15
+
16
+ function isForm(element) {
17
+ return Object.isElement(element) && element.nodeName.toUpperCase() == 'FORM'
18
+ }
19
+
20
+ function isInput(element) {
21
+ if (Object.isElement(element)) {
22
+ var name = element.nodeName.toUpperCase()
23
+ return name == 'INPUT' || name == 'SELECT' || name == 'TEXTAREA'
24
+ }
25
+ else return false
26
+ }
27
+
28
+ var submitBubbles = isEventSupported('submit'),
29
+ changeBubbles = isEventSupported('change')
30
+
31
+ if (!submitBubbles || !changeBubbles) {
32
+ // augment the Event.Handler class to observe custom events when needed
33
+ Event.Handler.prototype.initialize = Event.Handler.prototype.initialize.wrap(
34
+ function(init, element, eventName, selector, callback) {
35
+ init(element, eventName, selector, callback)
36
+ // is the handler being attached to an element that doesn't support this event?
37
+ if ( (!submitBubbles && this.eventName == 'submit' && !isForm(this.element)) ||
38
+ (!changeBubbles && this.eventName == 'change' && !isInput(this.element)) ) {
39
+ // "submit" => "emulated:submit"
40
+ this.eventName = 'emulated:' + this.eventName
41
+ }
42
+ }
43
+ )
44
+ }
45
+
46
+ if (!submitBubbles) {
47
+ // discover forms on the page by observing focus events which always bubble
48
+ document.on('focusin', 'form', function(focusEvent, form) {
49
+ // special handler for the real "submit" event (one-time operation)
50
+ if (!form.retrieve('emulated:submit')) {
51
+ form.on('submit', function(submitEvent) {
52
+ var emulated = form.fire('emulated:submit', submitEvent, true)
53
+ // if custom event received preventDefault, cancel the real one too
54
+ if (emulated.returnValue === false) submitEvent.preventDefault()
55
+ })
56
+ form.store('emulated:submit', true)
57
+ }
58
+ })
59
+ }
60
+
61
+ if (!changeBubbles) {
62
+ // discover form inputs on the page
63
+ document.on('focusin', 'input, select, texarea', function(focusEvent, input) {
64
+ // special handler for real "change" events
65
+ if (!input.retrieve('emulated:change')) {
66
+ input.on('change', function(changeEvent) {
67
+ input.fire('emulated:change', changeEvent, true)
68
+ })
69
+ input.store('emulated:change', true)
70
+ }
71
+ })
72
+ }
73
+
74
+ function handleRemote(element) {
75
+ var method, url, params;
76
+
77
+ var event = element.fire("ajax:before");
78
+ if (event.stopped) return false;
79
+
80
+ if (element.tagName.toLowerCase() === 'form') {
81
+ method = element.readAttribute('method') || 'post';
82
+ url = element.readAttribute('action');
83
+ params = element.serialize();
84
+ } else {
85
+ method = element.readAttribute('data-method') || 'get';
86
+ url = element.readAttribute('href');
87
+ params = {};
88
+ }
89
+
90
+ new Ajax.Request(url, {
91
+ method: method,
92
+ parameters: params,
93
+ evalScripts: true,
94
+
95
+ onComplete: function(request) { element.fire("ajax:complete", request); },
96
+ onSuccess: function(request) { element.fire("ajax:success", request); },
97
+ onFailure: function(request) { element.fire("ajax:failure", request); }
98
+ });
99
+
100
+ element.fire("ajax:after");
101
+ }
102
+
103
+ function handleMethod(element) {
104
+ var method = element.readAttribute('data-method'),
105
+ url = element.readAttribute('href'),
106
+ csrf_param = $$('meta[name=csrf-param]')[0],
107
+ csrf_token = $$('meta[name=csrf-token]')[0];
108
+
109
+ var form = new Element('form', { method: "POST", action: url, style: "display: none;" });
110
+ element.parentNode.insert(form);
111
+
112
+ if (method !== 'post') {
113
+ var field = new Element('input', { type: 'hidden', name: '_method', value: method });
114
+ form.insert(field);
115
+ }
116
+
117
+ if (csrf_param) {
118
+ var param = csrf_param.readAttribute('content'),
119
+ token = csrf_token.readAttribute('content'),
120
+ field = new Element('input', { type: 'hidden', name: param, value: token });
121
+ form.insert(field);
122
+ }
123
+
124
+ form.submit();
125
+ }
126
+
127
+
128
+ document.on("click", "*[data-confirm]", function(event, element) {
129
+ var message = element.readAttribute('data-confirm');
130
+ if (!confirm(message)) event.stop();
131
+ });
132
+
133
+ document.on("click", "a[data-remote]", function(event, element) {
134
+ if (event.stopped) return;
135
+ handleRemote(element);
136
+ event.stop();
137
+ });
138
+
139
+ document.on("click", "a[data-method]", function(event, element) {
140
+ if (event.stopped) return;
141
+ handleMethod(element);
142
+ event.stop();
143
+ });
144
+
145
+ document.on("submit", function(event) {
146
+ var element = event.findElement(),
147
+ message = element.readAttribute('data-confirm');
148
+ if (message && !confirm(message)) {
149
+ event.stop();
150
+ return false;
151
+ }
152
+
153
+ var inputs = element.select("input[type=submit][data-disable-with]");
154
+ inputs.each(function(input) {
155
+ input.disabled = true;
156
+ input.writeAttribute('data-original-value', input.value);
157
+ input.value = input.readAttribute('data-disable-with');
158
+ });
159
+
160
+ var element = event.findElement("form[data-remote]");
161
+ if (element) {
162
+ handleRemote(element);
163
+ event.stop();
164
+ }
165
+ });
166
+
167
+ document.on("ajax:after", "form", function(event, element) {
168
+ var inputs = element.select("input[type=submit][disabled=true][data-disable-with]");
169
+ inputs.each(function(input) {
170
+ input.value = input.readAttribute('data-original-value');
171
+ input.removeAttribute('data-original-value');
172
+ input.disabled = false;
173
+ });
174
+ });
175
+
176
+ Ajax.Responders.register({
177
+ onCreate: function(request) {
178
+ var csrf_meta_tag = $$('meta[name=csrf-token]')[0];
179
+
180
+ if (csrf_meta_tag) {
181
+ var header = 'X-CSRF-Token',
182
+ token = csrf_meta_tag.readAttribute('content');
183
+
184
+ if (!request.options.requestHeaders) {
185
+ request.options.requestHeaders = {};
186
+ }
187
+ request.options.requestHeaders[header] = token;
188
+ }
189
+ }
190
+ });
191
+ })();
@@ -0,0 +1,5 @@
1
+ # See http://www.robotstxt.org/wc/norobots.html for documentation on how to use the robots.txt file
2
+ #
3
+ # To ban all spiders from the entire site uncomment the next two lines:
4
+ # User-Agent: *
5
+ # Disallow: /
File without changes
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
+
4
+ APP_PATH = File.expand_path('../../config/application', __FILE__)
5
+ require File.expand_path('../../config/boot', __FILE__)
6
+ require 'rails/commands'
@@ -0,0 +1,55 @@
1
+ require 'test_helper'
2
+
3
+ $:.unshift File.expand_path('../../../lib', __FILE__)
4
+ require 'mail_envi'
5
+
6
+ class CustomInterceptor
7
+ def self.delivering_email(msg); end
8
+ end
9
+
10
+ class TestConfig < ActiveSupport::TestCase
11
+ include ActiveSupport::Testing::Isolation
12
+
13
+ def mail_envi
14
+ MailEnvi::Rails::Railtie.new
15
+ end
16
+
17
+ should "have defaults" do
18
+ config = MailEnvi::Config.new
19
+ assert_equal ['development', 'test'], config.environments
20
+ assert_equal MailEnvi::DefaultInterceptor, config.interceptor
21
+ assert_equal 'root@localhost', config.default_to
22
+ end
23
+
24
+ should "allow the interceptor to be assigned to a custom class" do
25
+ mock(MailEnvi).ronment {'development'}
26
+
27
+ MailEnvi.config do |config|
28
+ config.interceptor = CustomInterceptor
29
+ end
30
+
31
+ mock(::Mail).register_interceptor(CustomInterceptor)
32
+ mail_envi.run_initializers
33
+ end
34
+
35
+ should "allow additional environments to be included" do
36
+ MailEnvi.config do |config|
37
+ config.include_environments [:staging, 'beta']
38
+ end
39
+
40
+ assert_equal ['development', 'test', 'staging', 'beta'],
41
+ MailEnvi.config.environments
42
+ end
43
+
44
+ should "allow the defaults in the default interceptor to be overwritten" do
45
+ MailEnvi.config do |config|
46
+ config.default_to = "another@company.com"
47
+ end
48
+
49
+ mock(msg = Object.new)
50
+ stub(msg).subject {"Hello world!"}
51
+ mock(msg).to=("another@company.com")
52
+ mock(msg).subject=(anything)
53
+ MailEnvi::DefaultInterceptor.delivering_email(msg)
54
+ end
55
+ end
@@ -0,0 +1,18 @@
1
+ require 'test_helper'
2
+
3
+ require File.expand_path("../../dummy/config/environment.rb", __FILE__)
4
+
5
+ class TestUserMailer < ActionMailer::TestCase
6
+ context "when configured with MailEnvi" do
7
+ should "be intercepted" do
8
+ mock(user = Object.new)
9
+ stub(user).email {'user@company.com'}
10
+
11
+ email = UserMailer.registration_complete(user).deliver
12
+ assert (not ActionMailer::Base.deliveries.empty?)
13
+ assert_equal ['configured@company.com'], email.to
14
+ assert_match /\(#{MailEnvi.ronment} Interception\)/i, email.subject
15
+ # assert_match /hello world/i, email.body
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,26 @@
1
+ ENV["RAILS_ENV"] = "test"
2
+
3
+ # load the gem lib/ for the require 'mail_envy'
4
+ # $:.unshift File.expand_path('../../lib', __FILE__)
5
+
6
+ # load the dummy app
7
+ # require File.expand_path("../dummy/config/environment.rb", __FILE__)
8
+
9
+
10
+ begin
11
+ require 'turn'
12
+ rescue LoadError
13
+ # /
14
+ end
15
+
16
+ require 'test/unit'
17
+ require 'shoulda'
18
+ require 'rr'
19
+
20
+ # Load support files
21
+ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
22
+
23
+ class Test::Unit::TestCase
24
+ include RR::Adapters::TestUnit
25
+ end
26
+
@@ -0,0 +1,45 @@
1
+ require 'test_helper'
2
+
3
+ $:.unshift File.expand_path('../../lib', __FILE__)
4
+ require 'mail_envi'
5
+
6
+ class TestMailEnvi < ActiveSupport::TestCase
7
+ include ActiveSupport::Testing::Isolation
8
+
9
+ def mail_envi
10
+ MailEnvi::Rails::Railtie.new
11
+ end
12
+
13
+ context "in a production environment" do
14
+ setup { stub(MailEnvi).ronment {'production'} }
15
+
16
+ should "not register the Mail interceptor" do
17
+ dont_allow(::Mail).register_interceptor(anything)
18
+ mail_envi.run_initializers
19
+ end
20
+ end
21
+
22
+ context "in a development, test or included environments" do
23
+ should "register the default Mail interceptor" do
24
+ MailEnvi.config do |config|
25
+ config.include_environments [:staging, :beta]
26
+ end
27
+
28
+ %w(development test staging beta).each do |v|
29
+ stub(MailEnvi).ronment {v}
30
+ mock(::Mail).register_interceptor(MailEnvi::DefaultInterceptor)
31
+ mail_envi.run_initializers
32
+ end
33
+ end
34
+ end
35
+
36
+ context "self.ronment" do
37
+ should "return the Rails.env" do
38
+ mock(::Rails).env {'foo'}
39
+ assert_equal 'foo', MailEnvi.ronment
40
+
41
+ mock(::Rails).env {'bar'}
42
+ assert_equal 'bar', MailEnvi.ronment
43
+ end
44
+ end
45
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mail_envi
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Yung Hwa Kwon
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-09-13 00:00:00 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rails
17
+ requirement: &id001 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ~>
21
+ - !ruby/object:Gem::Version
22
+ version: 3.0.0
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: *id001
26
+ - !ruby/object:Gem::Dependency
27
+ name: shoulda
28
+ requirement: &id002 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: "0"
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *id002
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ requirement: &id003 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ~>
43
+ - !ruby/object:Gem::Version
44
+ version: 1.0.0
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *id003
48
+ - !ruby/object:Gem::Dependency
49
+ name: jeweler
50
+ requirement: &id004 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 1.6.2
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *id004
59
+ - !ruby/object:Gem::Dependency
60
+ name: rcov
61
+ requirement: &id005 !ruby/object:Gem::Requirement
62
+ none: false
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ version: "0"
67
+ type: :development
68
+ prerelease: false
69
+ version_requirements: *id005
70
+ - !ruby/object:Gem::Dependency
71
+ name: turn
72
+ requirement: &id006 !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: "0"
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: *id006
81
+ - !ruby/object:Gem::Dependency
82
+ name: rr
83
+ requirement: &id007 !ruby/object:Gem::Requirement
84
+ none: false
85
+ requirements:
86
+ - - ">="
87
+ - !ruby/object:Gem::Version
88
+ version: "0"
89
+ type: :development
90
+ prerelease: false
91
+ version_requirements: *id007
92
+ description: ""
93
+ email: yung.kwon@damncarousel.com
94
+ executables: []
95
+
96
+ extensions: []
97
+
98
+ extra_rdoc_files:
99
+ - README.md
100
+ files:
101
+ - .document
102
+ - Gemfile
103
+ - Gemfile.lock
104
+ - LICENSE.txt
105
+ - README.md
106
+ - Rakefile
107
+ - VERSION
108
+ - lib/mail_envi.rb
109
+ - lib/mail_envi/config.rb
110
+ - mail_envi.gemspec
111
+ - test/dummy/.gitignore
112
+ - test/dummy/Rakefile
113
+ - test/dummy/app/controllers/application_controller.rb
114
+ - test/dummy/app/helpers/application_helper.rb
115
+ - test/dummy/app/mailers/user_mailer.rb
116
+ - test/dummy/app/views/layouts/application.html.erb
117
+ - test/dummy/config.ru
118
+ - test/dummy/config/application.rb
119
+ - test/dummy/config/boot.rb
120
+ - test/dummy/config/database.yml
121
+ - test/dummy/config/environment.rb
122
+ - test/dummy/config/environments/development.rb
123
+ - test/dummy/config/environments/production.rb
124
+ - test/dummy/config/environments/test.rb
125
+ - test/dummy/config/initializers/backtrace_silencers.rb
126
+ - test/dummy/config/initializers/inflections.rb
127
+ - test/dummy/config/initializers/mail_envi.rb
128
+ - test/dummy/config/initializers/mime_types.rb
129
+ - test/dummy/config/initializers/secret_token.rb
130
+ - test/dummy/config/initializers/session_store.rb
131
+ - test/dummy/config/locales/en.yml
132
+ - test/dummy/config/routes.rb
133
+ - test/dummy/db/seeds.rb
134
+ - test/dummy/public/404.html
135
+ - test/dummy/public/422.html
136
+ - test/dummy/public/500.html
137
+ - test/dummy/public/favicon.ico
138
+ - test/dummy/public/images/rails.png
139
+ - test/dummy/public/index.html
140
+ - test/dummy/public/javascripts/application.js
141
+ - test/dummy/public/javascripts/controls.js
142
+ - test/dummy/public/javascripts/dragdrop.js
143
+ - test/dummy/public/javascripts/effects.js
144
+ - test/dummy/public/javascripts/prototype.js
145
+ - test/dummy/public/javascripts/rails.js
146
+ - test/dummy/public/robots.txt
147
+ - test/dummy/public/stylesheets/.gitkeep
148
+ - test/dummy/script/rails
149
+ - test/mail_envi/test_config.rb
150
+ - test/mailer/test_user_mailer.rb
151
+ - test/test_helper.rb
152
+ - test/test_mail_envi.rb
153
+ homepage: http://github.com/nowk/mail_envi
154
+ licenses:
155
+ - MIT
156
+ post_install_message:
157
+ rdoc_options: []
158
+
159
+ require_paths:
160
+ - lib
161
+ required_ruby_version: !ruby/object:Gem::Requirement
162
+ none: false
163
+ requirements:
164
+ - - ">="
165
+ - !ruby/object:Gem::Version
166
+ hash: -3370593094154357546
167
+ segments:
168
+ - 0
169
+ version: "0"
170
+ required_rubygems_version: !ruby/object:Gem::Requirement
171
+ none: false
172
+ requirements:
173
+ - - ">="
174
+ - !ruby/object:Gem::Version
175
+ version: "0"
176
+ requirements: []
177
+
178
+ rubyforge_project:
179
+ rubygems_version: 1.8.5
180
+ signing_key:
181
+ specification_version: 3
182
+ summary: Mail interceptor for different environments
183
+ test_files: []
184
+