From d872de3c69a8762834d206d2f080550c39143080 Mon Sep 17 00:00:00 2001 From: David Gil Date: Fri, 14 Dec 2018 21:24:03 +0100 Subject: Add Mailbox and MailboxTest generators --- .gitignore | 3 + lib/rails/generators/mailbox/USAGE | 12 ++++ lib/rails/generators/mailbox/mailbox_generator.rb | 38 +++++++++++ .../mailbox/templates/application_mailbox.rb.tt | 5 ++ .../generators/mailbox/templates/mailbox.rb.tt | 8 +++ .../generators/mailbox/templates/mailer_test.rb.tt | 13 ++++ test/generators/mailbox_generator_test.rb | 77 ++++++++++++++++++++++ 7 files changed, 156 insertions(+) create mode 100644 lib/rails/generators/mailbox/USAGE create mode 100644 lib/rails/generators/mailbox/mailbox_generator.rb create mode 100644 lib/rails/generators/mailbox/templates/application_mailbox.rb.tt create mode 100644 lib/rails/generators/mailbox/templates/mailbox.rb.tt create mode 100644 lib/rails/generators/mailbox/templates/mailer_test.rb.tt create mode 100644 test/generators/mailbox_generator_test.rb diff --git a/.gitignore b/.gitignore index 4862d67b18..ead1a2d308 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ .byebug_history *.sqlite3-journal +.ruby-version +.ruby-gemset +/tmp/ diff --git a/lib/rails/generators/mailbox/USAGE b/lib/rails/generators/mailbox/USAGE new file mode 100644 index 0000000000..d679dd63ae --- /dev/null +++ b/lib/rails/generators/mailbox/USAGE @@ -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 diff --git a/lib/rails/generators/mailbox/mailbox_generator.rb b/lib/rails/generators/mailbox/mailbox_generator.rb new file mode 100644 index 0000000000..7fcdfd3384 --- /dev/null +++ b/lib/rails/generators/mailbox/mailbox_generator.rb @@ -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 diff --git a/lib/rails/generators/mailbox/templates/application_mailbox.rb.tt b/lib/rails/generators/mailbox/templates/application_mailbox.rb.tt new file mode 100644 index 0000000000..be51eb3639 --- /dev/null +++ b/lib/rails/generators/mailbox/templates/application_mailbox.rb.tt @@ -0,0 +1,5 @@ +# frozen_string_literal: true + +class ApplicationMailbox < ActionMailbox::Base + # routing /something/i => :somewhere +end diff --git a/lib/rails/generators/mailbox/templates/mailbox.rb.tt b/lib/rails/generators/mailbox/templates/mailbox.rb.tt new file mode 100644 index 0000000000..9788bd9bb4 --- /dev/null +++ b/lib/rails/generators/mailbox/templates/mailbox.rb.tt @@ -0,0 +1,8 @@ +# frozen_string_literal: true + +class <%= class_name %>Mailbox < ApplicationMailbox +<% actions.each do |action| -%> + def <%= action %> + end +<% end -%> +end diff --git a/lib/rails/generators/mailbox/templates/mailer_test.rb.tt b/lib/rails/generators/mailbox/templates/mailer_test.rb.tt new file mode 100644 index 0000000000..41749808e3 --- /dev/null +++ b/lib/rails/generators/mailbox/templates/mailer_test.rb.tt @@ -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" , + # from: '"else" ', + # subject: "Hello world!", + # body: "Hello?" + # end +end diff --git a/test/generators/mailbox_generator_test.rb b/test/generators/mailbox_generator_test.rb new file mode 100644 index 0000000000..9295c2d8c8 --- /dev/null +++ b/test/generators/mailbox_generator_test.rb @@ -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" ,/, 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 -- cgit v1.2.3 From 5c3f0d2e19bfe56c14c9fbea213e4421003f7b1b Mon Sep 17 00:00:00 2001 From: David Gil Date: Fri, 14 Dec 2018 21:40:04 +0100 Subject: Uses the new generator to install ApplicationMailbox in action_mailbox:install rake --- lib/tasks/install.rake | 7 +++---- lib/templates/installer.rb | 4 ---- lib/templates/mailboxes/application_mailbox.rb | 5 ----- 3 files changed, 3 insertions(+), 13 deletions(-) delete mode 100644 lib/templates/installer.rb delete mode 100644 lib/templates/mailboxes/application_mailbox.rb diff --git a/lib/tasks/install.rake b/lib/tasks/install.rake index cfc06fbb5f..1f4c071494 100644 --- a/lib/tasks/install.rake +++ b/lib/tasks/install.rake @@ -5,11 +5,10 @@ namespace :action_mailbox do Rake::Task["install:migrations"].clear_comments desc "Copy over the migration" - task install: %w[ environment run_installer copy_migrations ] + task install: %w[ environment run_generator copy_migrations ] - task :run_installer do - installer_template = File.expand_path("../templates/installer.rb", __dir__) - system "#{RbConfig.ruby} ./bin/rails app:template LOCATION=#{installer_template}" + task :run_generator do + system "#{RbConfig.ruby} ./bin/rails generate mailbox application" end task :copy_migrations do diff --git a/lib/templates/installer.rb b/lib/templates/installer.rb deleted file mode 100644 index 18a562c0e5..0000000000 --- a/lib/templates/installer.rb +++ /dev/null @@ -1,4 +0,0 @@ -say "Copying application_mailbox.rb to app/mailboxes" -copy_file "#{__dir__}/mailboxes/application_mailbox.rb", "app/mailboxes/application_mailbox.rb" - -environment "# Prepare the ingress controller used to receive mail\n# config.action_mailbox.ingress = :amazon\n\n", env: 'production' diff --git a/lib/templates/mailboxes/application_mailbox.rb b/lib/templates/mailboxes/application_mailbox.rb deleted file mode 100644 index be51eb3639..0000000000 --- a/lib/templates/mailboxes/application_mailbox.rb +++ /dev/null @@ -1,5 +0,0 @@ -# frozen_string_literal: true - -class ApplicationMailbox < ActionMailbox::Base - # routing /something/i => :somewhere -end -- cgit v1.2.3 From acae20e2abee8843ae2b33929553fe1e100dd1c4 Mon Sep 17 00:00:00 2001 From: David Gil Date: Fri, 14 Dec 2018 21:42:20 +0100 Subject: renames file names --- lib/rails/generators/mailbox/mailbox_generator.rb | 2 +- lib/rails/generators/mailbox/templates/mailbox_test.rb.tt | 13 +++++++++++++ lib/rails/generators/mailbox/templates/mailer_test.rb.tt | 13 ------------- 3 files changed, 14 insertions(+), 14 deletions(-) create mode 100644 lib/rails/generators/mailbox/templates/mailbox_test.rb.tt delete mode 100644 lib/rails/generators/mailbox/templates/mailer_test.rb.tt diff --git a/lib/rails/generators/mailbox/mailbox_generator.rb b/lib/rails/generators/mailbox/mailbox_generator.rb index 7fcdfd3384..5511545a98 100644 --- a/lib/rails/generators/mailbox/mailbox_generator.rb +++ b/lib/rails/generators/mailbox/mailbox_generator.rb @@ -20,7 +20,7 @@ module Rails end end - template "mailer_test.rb", File.join('test/mailboxes', class_path, "#{file_name}_mailbox_test.rb") + template "mailbox_test.rb", File.join('test/mailboxes', class_path, "#{file_name}_mailbox_test.rb") end hook_for :test_framework diff --git a/lib/rails/generators/mailbox/templates/mailbox_test.rb.tt b/lib/rails/generators/mailbox/templates/mailbox_test.rb.tt new file mode 100644 index 0000000000..41749808e3 --- /dev/null +++ b/lib/rails/generators/mailbox/templates/mailbox_test.rb.tt @@ -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" , + # from: '"else" ', + # subject: "Hello world!", + # body: "Hello?" + # end +end diff --git a/lib/rails/generators/mailbox/templates/mailer_test.rb.tt b/lib/rails/generators/mailbox/templates/mailer_test.rb.tt deleted file mode 100644 index 41749808e3..0000000000 --- a/lib/rails/generators/mailbox/templates/mailer_test.rb.tt +++ /dev/null @@ -1,13 +0,0 @@ -# frozen_string_literal: true - -require "test_helper" - -class <%= class_name %>MailboxTest < ActionMailbox::TestCase - # test "receive mail" do - # receive_inbound_email_from_mail \ - # to: '"someone" , - # from: '"else" ', - # subject: "Hello world!", - # body: "Hello?" - # end -end -- cgit v1.2.3