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 --- 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 ++++++++ 5 files changed, 76 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 (limited to 'lib/rails') 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 -- 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 (limited to 'lib/rails') 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