1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Add Mailbox and MailboxTest generators

This commit is contained in:
David Gil 2018-12-14 21:24:03 +01:00
parent d6621cc265
commit d872de3c69
7 changed files with 156 additions and 0 deletions

3
.gitignore vendored
View file

@ -1,2 +1,5 @@
.byebug_history .byebug_history
*.sqlite3-journal *.sqlite3-journal
.ruby-version
.ruby-gemset
/tmp/

View file

@ -0,0 +1,12 @@
Description:
============
Stubs out a new mailbox class in app/mailboxes and invokes your template
engine and test framework generators.
Example:
========
rails generate mailbox inbox
creates a InboxMailbox class and test:
Mailbox: app/mailboxes/inbox_mailbox.rb
Test: test/mailboxes/inbox_mailbox_test.rb

View file

@ -0,0 +1,38 @@
# frozen_string_literal: true
module Rails
module Generators
class MailboxGenerator < NamedBase
source_root File.expand_path("templates", __dir__)
argument :actions, type: :array, default: [:process], banner: "method method"
def check_class_collision
class_collisions "#{class_name}Mailbox", "#{class_name}MailboxTest"
end
def create_mailbox_file
template "mailbox.rb", File.join("app/mailboxes", class_path, "#{file_name}_mailbox.rb")
in_root do
if behavior == :invoke && !File.exist?(application_mailbox_file_name)
template "application_mailbox.rb", application_mailbox_file_name
end
end
template "mailer_test.rb", File.join('test/mailboxes', class_path, "#{file_name}_mailbox_test.rb")
end
hook_for :test_framework
private
def file_name # :doc:
@_file_name ||= super.sub(/_mailbox\z/i, "")
end
def application_mailbox_file_name
"app/mailboxes/application_mailbox.rb"
end
end
end
end

View file

@ -0,0 +1,5 @@
# frozen_string_literal: true
class ApplicationMailbox < ActionMailbox::Base
# routing /something/i => :somewhere
end

View file

@ -0,0 +1,8 @@
# frozen_string_literal: true
class <%= class_name %>Mailbox < ApplicationMailbox
<% actions.each do |action| -%>
def <%= action %>
end
<% end -%>
end

View file

@ -0,0 +1,13 @@
# frozen_string_literal: true
require "test_helper"
class <%= class_name %>MailboxTest < ActionMailbox::TestCase
# test "receive mail" do
# receive_inbound_email_from_mail \
# to: '"someone" <someone@example.com>,
# from: '"else" <else@example.com>',
# subject: "Hello world!",
# body: "Hello?"
# end
end

View file

@ -0,0 +1,77 @@
# frozen_string_literal: true
require "test_helper"
require "rails/generators/mailbox/mailbox_generator"
class MailboxGeneratorTest < Rails::Generators::TestCase
destination File.expand_path("../../tmp", File.dirname(__FILE__))
setup :prepare_destination
tests Rails::Generators::MailboxGenerator
arguments ["inbox"]
def test_mailbox_skeleton_is_created
run_generator
assert_file "app/mailboxes/inbox_mailbox.rb" do |mailbox|
assert_match(/class InboxMailbox < ApplicationMailbox/, mailbox)
assert_match(/def process/, mailbox)
assert_no_match(%r{# routing /something/i => :somewhere}, mailbox)
end
assert_file "app/mailboxes/application_mailbox.rb" do |mailbox|
assert_match(/class ApplicationMailbox < ActionMailbox::Base/, mailbox)
assert_match(%r{# routing /something/i => :somewhere}, mailbox)
assert_no_match(/def process/, mailbox)
end
end
def test_check_class_collision
Object.send :const_set, :InboxMailbox, Class.new
content = capture(:stderr) { run_generator }
assert_match(/The name 'InboxMailbox' is either already used in your application or reserved/, content)
ensure
Object.send :remove_const, :InboxMailbox
end
def test_invokes_default_test_framework
run_generator %w(inbox foo bar)
assert_file "test/mailboxes/inbox_mailbox_test.rb" do |test|
assert_match(/class InboxMailboxTest < ActionMailbox::TestCase/, test)
assert_match(/# test "receive mail" do/, test)
assert_match(/# to: '"someone" <someone@example.com>,/, test)
end
end
def test_check_test_class_collision
Object.send :const_set, :InboxMailboxTest, Class.new
content = capture(:stderr) { run_generator }
assert_match(/The name 'InboxMailboxTest' is either already used in your application or reserved/, content)
ensure
Object.send :remove_const, :InboxMailboxTest
end
def test_actions_are_turned_into_methods
run_generator %w(inbox foo bar)
assert_file "app/mailboxes/inbox_mailbox.rb" do |mailbox|
assert_instance_method :foo, mailbox
assert_instance_method :bar, mailbox
end
end
def test_mailbox_on_revoke
run_generator
run_generator ["inbox"], behavior: :revoke
assert_no_file "app/mailboxes/inbox.rb"
end
def test_mailbox_suffix_is_not_duplicated
run_generator ["inbox_mailbox"]
assert_no_file "app/mailboxes/inbox_mailbox_mailbox.rb"
assert_file "app/mailboxes/inbox_mailbox.rb"
assert_no_file "test/mailboxes/inbox_mailbox_mailbox_test.rb"
assert_file "test/mailboxes/inbox_mailbox_test.rb"
end
end