Rubocop Hanami dry-rb rules (1.3) (#86)

This commit is contained in:
Luca Guidi 2020-10-18 08:40:12 +02:00 committed by GitHub
parent 45500a19e0
commit d40b869904
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
21 changed files with 249 additions and 388 deletions

View File

@ -1,177 +0,0 @@
kind: pipeline
name: ruby-2-7
group: build
steps:
- name: install
image: ruby:2.7
volumes:
- name: bundle
path: /usr/local/bundle
commands:
- ruby -v
- gem install bundler
- bundle install --jobs=3 --retry=3
- name: unit
image: ruby:2.7
volumes:
- name: bundle
path: /usr/local/bundle
commands:
- COVERAGE=true bundle exec rake spec:unit
- name: quality
image: ruby:2.7
environment:
CODECOV_TOKEN:
from_secret: codecov
volumes:
- name: bundle
path: /usr/local/bundle
commands:
- CI=true bundle exec rake codecov:upload
volumes:
- name: bundle
temp: {}
---
kind: pipeline
name: ruby-2-6
group: build
steps:
- name: install
image: ruby:2.6
volumes:
- name: bundle
path: /usr/local/bundle
commands:
- ruby -v
- gem install bundler
- bundle install --jobs=3 --retry=3
- name: unit
image: ruby:2.6
volumes:
- name: bundle
path: /usr/local/bundle
commands:
- COVERAGE=true bundle exec rake spec:unit
- name: quality
image: ruby:2.6
environment:
CODECOV_TOKEN:
from_secret: codecov
volumes:
- name: bundle
path: /usr/local/bundle
commands:
- CI=true bundle exec rake codecov:upload
volumes:
- name: bundle
temp: {}
---
kind: pipeline
name: ruby-2-5
group: build
steps:
- name: install
image: ruby:2.5
volumes:
- name: bundle
path: /usr/local/bundle
commands:
- ruby -v
- gem install bundler
- bundle install --jobs=3 --retry=3
- name: unit
image: ruby:2.5
volumes:
- name: bundle
path: /usr/local/bundle
commands:
- COVERAGE=true bundle exec rake spec:unit
- name: quality
image: ruby:2.5
environment:
CODECOV_TOKEN:
from_secret: codecov
volumes:
- name: bundle
path: /usr/local/bundle
commands:
- CI=true bundle exec rake codecov:upload
volumes:
- name: bundle
temp: {}
---
kind: pipeline
name: jruby-9-2
group: build
steps:
- name: install
image: hanami/jruby-9.2
volumes:
- name: bundle
path: /usr/local/bundle
commands:
- ruby -v
- gem install bundler
- bundle install --jobs=3 --retry=3
- name: unit
image: hanami/jruby-9.2
volumes:
- name: bundle
path: /usr/local/bundle
commands:
- COVERAGE=true bundle exec rake spec:unit
- name: quality
image: hanami/jruby-9.2
environment:
CODECOV_TOKEN:
from_secret: codecov
volumes:
- name: bundle
path: /usr/local/bundle
commands:
- CI=true bundle exec rake codecov:upload
volumes:
- name: bundle
temp: {}
---
kind: pipeline
name: slack
group: build
clone:
disable: true
depends_on:
- jruby-9-2
steps:
- name: slack
image: plugins/slack
settings:
link_names: true
webhook:
from_secret: slack
channel: dev
when:
event:
- push

16
Gemfile
View File

@ -1,12 +1,14 @@
source 'https://rubygems.org'
# frozen_string_literal: true
source "https://rubygems.org"
gemspec
unless ENV['CI']
gem 'byebug', require: false, platforms: :mri
gem 'yard', require: false
unless ENV["CI"]
gem "byebug", require: false, platforms: :mri
gem "yard", require: false
end
gem 'hanami-utils', '~> 1.3', require: false, git: 'https://github.com/hanami/utils.git', branch: 'master'
gem 'haml'
gem "hanami-utils", "~> 1.3", require: false, git: "https://github.com/hanami/utils.git", branch: "master"
gem "haml"
gem 'hanami-devtools', require: false, git: 'https://github.com/hanami/devtools.git'
gem "hanami-devtools", require: false, git: "https://github.com/hanami/devtools.git"

View File

@ -5,7 +5,6 @@ Mail for Ruby applications.
## Status
[![Gem Version](https://badge.fury.io/rb/hanami-mailer.svg)](https://badge.fury.io/rb/hanami-mailer)
[![Build Status](https://ci.hanamirb.org/api/badges/hanami/mailer/status.svg)](https://ci.hanamirb.org/hanami/mailer)
[![CircleCI](https://circleci.com/gh/hanami/mailer/tree/master.svg?style=svg)](https://circleci.com/gh/hanami/mailer/tree/master)
[![Test Coverage](https://codecov.io/gh/hanami/mailer/branch/master/graph/badge.svg)](https://codecov.io/gh/hanami/mailer)
[![Depfu](https://badges.depfu.com/badges/739c6e10eaf20d3ba4240d00828284db/overview.svg)](https://depfu.com/github/hanami/mailer?project=Bundler)
@ -416,6 +415,6 @@ __Hanami::Mailer__ uses [Semantic Versioning 2.0.0](http://semver.org)
## Copyright
Copyright © 2015-2017 Luca Guidi Released under MIT License
Copyright © 2015-2020 Luca Guidi Released under MIT License
This project was formerly known as Lotus (`lotus-mailer`).

View File

@ -1,7 +1,8 @@
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'bundler/setup'
require 'hanami/mailer'
require "bundler/setup"
require "hanami/mailer"
# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.
@ -10,5 +11,5 @@ require 'hanami/mailer'
# require "pry"
# Pry.start
require 'irb'
require "irb"
IRB.start

View File

@ -1,29 +1,32 @@
lib = File.expand_path('../lib', __FILE__)
# frozen_string_literal: true
lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'hanami/mailer/version'
require "hanami/mailer/version"
Gem::Specification.new do |spec|
spec.name = 'hanami-mailer'
spec.name = "hanami-mailer"
spec.version = Hanami::Mailer::VERSION
spec.authors = ['Luca Guidi']
spec.email = ['me@lucaguidi.com']
spec.authors = ["Luca Guidi"]
spec.email = ["me@lucaguidi.com"]
spec.summary = 'Mail for Ruby applications.'
spec.description = 'Mail for Ruby applications and Hanami mailers'
spec.homepage = 'http://hanamirb.org'
spec.license = 'MIT'
spec.summary = "Mail for Ruby applications."
spec.description = "Mail for Ruby applications and Hanami mailers"
spec.homepage = "http://hanamirb.org"
spec.license = "MIT"
spec.files = `git ls-files -- lib/* CHANGELOG.md LICENSE.md README.md hanami-mailer.gemspec`.split($/) # rubocop:disable Style/SpecialGlobalVars
spec.bindir = 'exe'
spec.files = `git ls-files -- lib/* CHANGELOG.md LICENSE.md README.md hanami-mailer.gemspec`.split($/)
spec.bindir = "exe"
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
spec.require_paths = ['lib']
spec.required_ruby_version = '>= 2.3.0'
spec.require_paths = ["lib"]
spec.required_ruby_version = ">= 2.3.0"
spec.add_dependency 'hanami-utils', '~> 1.3'
spec.add_dependency 'tilt', '~> 2.0', '>= 2.0.1'
spec.add_dependency 'mail', '~> 2.6'
spec.add_dependency "hanami-utils", "~> 1.3"
spec.add_dependency "tilt", "~> 2.0", ">= 2.0.1"
spec.add_dependency "mail", "~> 2.6"
spec.add_development_dependency 'bundler', '>= 1.6', '< 3'
spec.add_development_dependency 'rake', '~> 13'
spec.add_development_dependency 'rspec', '~> 3.7'
spec.add_development_dependency "bundler", ">= 1.6", "< 3"
spec.add_development_dependency "rake", "~> 13"
spec.add_development_dependency "rspec", "~> 3.7"
spec.add_development_dependency "rubocop", "0.81" # rubocop 0.81+ removed support for Ruby 2.3
end

View File

@ -1 +1,3 @@
require 'hanami/mailer'
# frozen_string_literal: true
require "hanami/mailer"

View File

@ -1,8 +1,10 @@
require 'hanami/utils/class_attribute'
require 'hanami/mailer/version'
require 'hanami/mailer/configuration'
require 'hanami/mailer/dsl'
require 'mail'
# frozen_string_literal: true
require "hanami/utils/class_attribute"
require "hanami/mailer/version"
require "hanami/mailer/configuration"
require "hanami/mailer/dsl"
require "mail"
# Hanami
#
@ -34,8 +36,8 @@ module Hanami
# @since 0.1.0
# @api private
CONTENT_TYPES = {
html: 'text/html',
txt: 'text/plain'
html: "text/html",
txt: "text/plain"
}.freeze
include Utils::ClassAttribute
@ -181,10 +183,17 @@ module Hanami
# invoice = Invoice.new
# user = User.new(name: 'L', email: 'user@example.com')
#
# Billing::Invoice.deliver(invoice: invoice, user: user) # Deliver both text, HTML parts and the attachment
# Billing::Invoice.deliver(invoice: invoice, user: user, format: :txt) # Deliver only the text part and the attachment
# Billing::Invoice.deliver(invoice: invoice, user: user, format: :html) # Deliver only the text part and the attachment
# Billing::Invoice.deliver(invoice: invoice, user: user, charset: 'iso-8859') # Deliver both the parts with "iso-8859"
# # Deliver both text, HTML parts and the attachment
# Billing::Invoice.deliver(invoice: invoice, user: user)
#
# # Deliver only the text part and the attachment
# Billing::Invoice.deliver(invoice: invoice, user: user, format: :txt)
#
# # Deliver only the text part and the attachment
# Billing::Invoice.deliver(invoice: invoice, user: user, format: :html)
#
# # Deliver both the parts with "iso-8859"
# Billing::Invoice.deliver(invoice: invoice, user: user, charset: "iso-8859")
def deliver(locals = {})
new(locals).deliver
end
@ -280,8 +289,6 @@ module Hanami
private
# rubocop:disable Metrics/MethodLength
# rubocop:disable Metrics/AbcSize
def build
Mail.new.tap do |m|
m.return_path = __dsl(:return_path)
@ -299,8 +306,6 @@ module Hanami
m.delivery_method(*Hanami::Mailer.configuration.delivery_method)
end
end
# rubocop:enable Metrics/MethodLength
# rubocop:enable Metrics/AbcSize
# @api private
# @since 0.1.0

View File

@ -1,5 +1,7 @@
require 'set'
require 'hanami/utils/kernel'
# frozen_string_literal: true
require "set"
require "hanami/utils/kernel"
module Hanami
module Mailer
@ -11,7 +13,7 @@ module Hanami
#
# @since 0.1.0
# @api private
DEFAULT_ROOT = '.'.freeze
DEFAULT_ROOT = "."
# Default delivery method
#
@ -23,7 +25,7 @@ module Hanami
#
# @since 0.1.0
# @api private
DEFAULT_CHARSET = 'UTF-8'.freeze
DEFAULT_CHARSET = "UTF-8"
# @since 0.1.0
# @api private
@ -144,10 +146,10 @@ module Hanami
#
# @see Hanami::Mailer.configure
def prepare(&blk)
if block_given? # rubocop:disable Style/GuardClause
if block_given?
@modules.push(blk)
else
raise ArgumentError.new('Please provide a block')
raise ArgumentError.new("Please provide a block")
end
end
@ -191,7 +193,7 @@ module Hanami
@modules = []
end
alias unload! reset!
alias_method :unload!, :reset!
# Copy the configuration for the given mailer
#

View File

@ -1,5 +1,7 @@
require 'hanami/mailer/rendering/template_name'
require 'hanami/mailer/rendering/templates_finder'
# frozen_string_literal: true
require "hanami/mailer/rendering/template_name"
require "hanami/mailer/rendering/templates_finder"
module Hanami
module Mailer

View File

@ -1,4 +1,6 @@
require 'hanami/utils/string'
# frozen_string_literal: true
require "hanami/utils/string"
module Hanami
module Mailer
@ -10,7 +12,7 @@ module Hanami
class TemplateName
# @since 0.1.0
# @api private
NAMESPACE_SEPARATOR = '::'.freeze
NAMESPACE_SEPARATOR = "::"
# @since 0.1.0
# @api private
@ -45,7 +47,7 @@ module Hanami
# @since 0.1.0
# @api private
def replace!(token)
@name = @name.gsub(/\A#{token}#{NAMESPACE_SEPARATOR}/, '')
@name = @name.gsub(/\A#{token}#{NAMESPACE_SEPARATOR}/, "")
end
end
end

View File

@ -1,4 +1,6 @@
require 'hanami/mailer/template'
# frozen_string_literal: true
require "hanami/mailer/template"
module Hanami
module Mailer
@ -14,19 +16,19 @@ module Hanami
#
# @api private
# @since 0.1.0
FORMAT = '*'.freeze
FORMAT = "*"
# Default template engines
#
# @api private
# @since 0.1.0
ENGINES = '*'.freeze
ENGINES = "*"
# Recursive pattern
#
# @api private
# @since 0.1.0
RECURSIVE = '**'.freeze
RECURSIVE = "**"
# Initialize a finder
#
@ -72,7 +74,7 @@ module Hanami
templates = Hash[]
_find.map do |template|
name = File.basename(template)
format = (name.split('.')[-2]).to_sym
format = (name.split(".")[-2]).to_sym
templates[format] = Mailer::Template.new(template)
end
templates

View File

@ -1,4 +1,6 @@
require 'tilt'
# frozen_string_literal: true
require "tilt"
module Hanami
module Mailer

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true
module Hanami
module Mailer
# @since 0.1.0
VERSION = '1.3.2'.freeze
VERSION = "1.3.2"
end
end

View File

@ -26,11 +26,11 @@ upload_code_coverage() {
}
main() {
prepare_build &&
print_ruby_version &&
run_code_quality_checks &&
run_unit_tests &&
upload_code_coverage
prepare_build
print_ruby_version
run_code_quality_checks
run_unit_tests
upload_code_coverage
}
main

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
$LOAD_PATH.unshift "lib"
require "hanami/utils"
require "hanami/devtools/unit"

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true
class InvoiceMailer
include Hanami::Mailer
template 'invoice'
template "invoice"
end
class RenderMailer
@ -14,43 +16,43 @@ end
class CharsetMailer
include Hanami::Mailer
from 'noreply@example.com'
to 'user@example.com'
subject 'こんにちは'
from "noreply@example.com"
to "user@example.com"
subject "こんにちは"
end
class MissingFromMailer
include Hanami::Mailer
template 'missing'
template "missing"
to 'recipient@example.com'
subject 'Hello'
to "recipient@example.com"
subject "Hello"
end
class MissingToMailer
include Hanami::Mailer
template 'missing'
template "missing"
from 'sender@example.com'
subject 'Hello'
from "sender@example.com"
subject "Hello"
end
class CcOnlyMailer
include Hanami::Mailer
template 'missing'
template "missing"
cc 'recipient@example.com'
from 'sender@example.com'
subject 'Hello'
cc "recipient@example.com"
from "sender@example.com"
subject "Hello"
end
class BccOnlyMailer
include Hanami::Mailer
template 'missing'
template "missing"
bcc 'recipient@example.com'
from 'sender@example.com'
subject 'Hello'
bcc "recipient@example.com"
from "sender@example.com"
subject "Hello"
end
User = Struct.new(:name, :email)
@ -84,21 +86,21 @@ end
class WelcomeMailer
include Hanami::Mailer
return_path 'bounce@sender.com'
from 'noreply@sender.com'
to ['noreply@recipient.com', 'owner@recipient.com']
cc 'cc@recipient.com'
bcc 'bcc@recipient.com'
reply_to 'reply_to@recipient.com'
return_path "bounce@sender.com"
from "noreply@sender.com"
to ["noreply@recipient.com", "owner@recipient.com"]
cc "cc@recipient.com"
bcc "bcc@recipient.com"
reply_to "reply_to@recipient.com"
subject 'Welcome'
subject "Welcome"
def greeting
'Ahoy'
"Ahoy"
end
def prepare
mail.attachments['invoice.pdf'] = '/path/to/invoice.pdf'
mail.attachments["invoice.pdf"] = "/path/to/invoice.pdf"
end
end
@ -120,6 +122,6 @@ end
module DefaultSubject
def self.included(mailer)
mailer.subject 'default subject'
mailer.subject "default subject"
end
end

View File

@ -1,25 +1,27 @@
# frozen_string_literal: true
RSpec.describe Hanami::Mailer::Configuration do
before do
@configuration = Hanami::Mailer::Configuration.new
end
describe '#root' do
describe 'when a value is given' do
describe 'and it is a string' do
it 'sets it as a Pathname' do
@configuration.root 'spec'
expect(@configuration.root).to eq(Pathname.new('spec').realpath)
describe "#root" do
describe "when a value is given" do
describe "and it is a string" do
it "sets it as a Pathname" do
@configuration.root "spec"
expect(@configuration.root).to eq(Pathname.new("spec").realpath)
end
end
describe 'and it is a pathname' do
it 'sets it' do
@configuration.root Pathname.new('spec')
expect(@configuration.root).to eq(Pathname.new('spec').realpath)
describe "and it is a pathname" do
it "sets it" do
@configuration.root Pathname.new("spec")
expect(@configuration.root).to eq(Pathname.new("spec").realpath)
end
end
describe 'and it implements #to_pathname' do
describe "and it implements #to_pathname" do
before do
RootPath = Struct.new(:path) do
def to_pathname
@ -32,39 +34,39 @@ RSpec.describe Hanami::Mailer::Configuration do
Object.send(:remove_const, :RootPath)
end
it 'sets the converted value' do
@configuration.root RootPath.new('spec')
expect(@configuration.root).to eq(Pathname.new('spec').realpath)
it "sets the converted value" do
@configuration.root RootPath.new("spec")
expect(@configuration.root).to eq(Pathname.new("spec").realpath)
end
end
describe 'and it is an unexisting path' do
it 'raises an error' do
describe "and it is an unexisting path" do
it "raises an error" do
expect do
@configuration.root '/path/to/unknown'
@configuration.root "/path/to/unknown"
end.to raise_error(Errno::ENOENT)
end
end
end
describe 'when a value is not given' do
it 'defaults to the current path' do
expect(@configuration.root).to eq(Pathname.new('.').realpath)
describe "when a value is not given" do
it "defaults to the current path" do
expect(@configuration.root).to eq(Pathname.new(".").realpath)
end
end
end
describe '#mailers' do
it 'defaults to an empty set' do
describe "#mailers" do
it "defaults to an empty set" do
expect(@configuration.mailers).to be_empty
end
it 'allows to add mailers' do
it "allows to add mailers" do
@configuration.add_mailer(InvoiceMailer)
expect(@configuration.mailers).to include(InvoiceMailer)
end
it 'eliminates duplications' do
it "eliminates duplications" do
@configuration.add_mailer(RenderMailer)
@configuration.add_mailer(RenderMailer)
@ -72,11 +74,11 @@ RSpec.describe Hanami::Mailer::Configuration do
end
end
describe '#prepare' do
describe "#prepare" do
before do
module FooRendering
def render
'foo'
"foo"
end
end
@ -89,8 +91,8 @@ RSpec.describe Hanami::Mailer::Configuration do
Object.__send__(:remove_const, :PrepareMailer)
end
it 'raises error in case of missing block' do
expect { @configuration.prepare }.to raise_error(ArgumentError, 'Please provide a block')
it "raises error in case of missing block" do
expect { @configuration.prepare }.to raise_error(ArgumentError, "Please provide a block")
end
end
@ -118,75 +120,75 @@ RSpec.describe Hanami::Mailer::Configuration do
# end
describe '#load!' do
describe "#load!" do
before do
@configuration.root 'spec'
@configuration.root "spec"
@configuration.load!
end
it 'loads root' do
root = Pathname.new('spec').realpath
it "loads root" do
root = Pathname.new("spec").realpath
expect(@configuration.root).to eq(root)
end
end
describe '#delivery_method' do
describe 'when not previously set' do
describe "#delivery_method" do
describe "when not previously set" do
before do
@configuration.reset!
end
it 'defaults to SMTP' do
it "defaults to SMTP" do
expect(@configuration.delivery_method).to eq([:smtp, {}])
end
end
describe 'set with a symbol' do
describe "set with a symbol" do
before do
@configuration.delivery_method :exim, location: '/path/to/exim'
@configuration.delivery_method :exim, location: "/path/to/exim"
end
it 'saves the delivery method in the configuration' do
expect(@configuration.delivery_method).to eq([:exim, { location: '/path/to/exim' }])
it "saves the delivery method in the configuration" do
expect(@configuration.delivery_method).to eq([:exim, {location: "/path/to/exim"}])
end
end
describe 'set with a class' do
describe "set with a class" do
before do
@configuration.delivery_method MandrillDeliveryMethod,
username: 'mandrill-username', password: 'mandrill-api-key'
username: "mandrill-username", password: "mandrill-api-key"
end
it 'saves the delivery method in the configuration' do
expect(@configuration.delivery_method).to eq([MandrillDeliveryMethod, username: 'mandrill-username', password: 'mandrill-api-key'])
it "saves the delivery method in the configuration" do
expect(@configuration.delivery_method).to eq([MandrillDeliveryMethod, username: "mandrill-username", password: "mandrill-api-key"])
end
end
end
describe '#default_charset' do
describe 'when not previously set' do
describe "#default_charset" do
describe "when not previously set" do
before do
@configuration.reset!
end
it 'defaults to UTF-8' do
expect(@configuration.default_charset).to eq('UTF-8')
it "defaults to UTF-8" do
expect(@configuration.default_charset).to eq("UTF-8")
end
end
describe 'when set' do
describe "when set" do
before do
@configuration.default_charset 'iso-8859-1'
@configuration.default_charset "iso-8859-1"
end
it 'saves the delivery method in the configuration' do
expect(@configuration.default_charset).to eq('iso-8859-1')
it "saves the delivery method in the configuration" do
expect(@configuration.default_charset).to eq("iso-8859-1")
end
end
end
describe '#prepare' do
it 'injects code in each mailer'
describe "#prepare" do
it "injects code in each mailer"
# it 'injects code in each mailer' do
# InvoiceMailer.subject.must_equal 'default subject'
# end

View File

@ -1,11 +1,13 @@
# frozen_string_literal: true
RSpec.describe Hanami::Mailer do
describe '.deliver' do
describe ".deliver" do
before do
Hanami::Mailer.deliveries.clear
end
it 'can deliver with specified charset' do
CharsetMailer.deliver(charset: charset = 'iso-2022-jp')
it "can deliver with specified charset" do
CharsetMailer.deliver(charset: charset = "iso-2022-jp")
mail = Hanami::Mailer.deliveries.first
expect(mail.charset).to eq(charset)
@ -40,7 +42,7 @@ RSpec.describe Hanami::Mailer do
expect { mailer.deliver }.to raise_error(ArgumentError, "ouch")
end
describe 'test delivery with hardcoded values' do
describe "test delivery with hardcoded values" do
before do
WelcomeMailer.deliver
@mail = Hanami::Mailer.deliveries.first
@ -50,27 +52,27 @@ RSpec.describe Hanami::Mailer do
Hanami::Mailer.deliveries.clear
end
it 'delivers the mail' do
it "delivers the mail" do
expect(Hanami::Mailer.deliveries.length).to eq(1)
end
it 'sends the correct information' do
expect(@mail.return_path).to eq('bounce@sender.com')
expect(@mail.from).to eq(['noreply@sender.com'])
expect(@mail.to).to eq(['noreply@recipient.com', 'owner@recipient.com'])
expect(@mail.cc).to eq(['cc@recipient.com'])
expect(@mail.bcc).to eq(['bcc@recipient.com'])
expect(@mail.reply_to).to eq(['reply_to@recipient.com'])
expect(@mail.subject).to eq('Welcome')
it "sends the correct information" do
expect(@mail.return_path).to eq("bounce@sender.com")
expect(@mail.from).to eq(["noreply@sender.com"])
expect(@mail.to).to eq(["noreply@recipient.com", "owner@recipient.com"])
expect(@mail.cc).to eq(["cc@recipient.com"])
expect(@mail.bcc).to eq(["bcc@recipient.com"])
expect(@mail.reply_to).to eq(["reply_to@recipient.com"])
expect(@mail.subject).to eq("Welcome")
end
it 'has the correct templates' do
it "has the correct templates" do
expect(@mail.html_part.to_s).to include(%(template))
expect(@mail.text_part.to_s).to include(%(template))
end
it 'interprets the prepare statement' do
attachment = @mail.attachments['invoice.pdf']
it "interprets the prepare statement" do
attachment = @mail.attachments["invoice.pdf"]
expect(attachment).to be_kind_of(Mail::Part)
@ -78,16 +80,16 @@ RSpec.describe Hanami::Mailer do
expect(attachment).to_not be_inline
expect(attachment).to_not be_multipart
expect(attachment.filename).to eq('invoice.pdf')
expect(attachment.filename).to eq("invoice.pdf")
expect(attachment.content_type).to match('application/pdf')
expect(attachment.content_type).to match('filename=invoice.pdf')
expect(attachment.content_type).to match("application/pdf")
expect(attachment.content_type).to match("filename=invoice.pdf")
end
end
describe 'test delivery with methods' do
describe "test delivery with methods" do
before do
@user = User.new('Name', 'student@deigirls.com')
@user = User.new("Name", "student@deigirls.com")
MethodMailer.deliver(user: @user)
@mail = Hanami::Mailer.deliveries.first
@ -97,23 +99,23 @@ RSpec.describe Hanami::Mailer do
Hanami::Mailer.deliveries.clear
end
it 'delivers the mail' do
it "delivers the mail" do
expect(Hanami::Mailer.deliveries.length).to eq(1)
end
it 'sends the correct information' do
it "sends the correct information" do
expect(@mail.from).to eq(["hello-#{@user.name.downcase}@example.com"])
expect(@mail.to).to eq([@user.email])
expect(@mail.subject).to eq("Hello, #{@user.name}")
end
end
describe 'multipart' do
describe "multipart" do
after do
Hanami::Mailer.deliveries.clear
end
it 'delivers all the parts by default' do
it "delivers all the parts by default" do
WelcomeMailer.deliver
mail = Hanami::Mailer.deliveries.first
@ -123,7 +125,7 @@ RSpec.describe Hanami::Mailer do
expect(body).to include(%(This is a txt template))
end
it 'can deliver only the text part' do
it "can deliver only the text part" do
WelcomeMailer.deliver(format: :txt)
mail = Hanami::Mailer.deliveries.first
@ -133,7 +135,7 @@ RSpec.describe Hanami::Mailer do
expect(body).to include(%(This is a txt template))
end
it 'can deliver only the html part' do
it "can deliver only the html part" do
WelcomeMailer.deliver(format: :html)
mail = Hanami::Mailer.deliveries.first
@ -144,9 +146,9 @@ RSpec.describe Hanami::Mailer do
end
end
describe 'custom delivery' do
describe "custom delivery" do
before do
@options = options = { deliveries: [] }
@options = options = {deliveries: []}
# Hanami::Mailer.reset!
# Hanami::Mailer.configure do
@ -158,7 +160,7 @@ RSpec.describe Hanami::Mailer do
@mail = options.fetch(:deliveries).first
end
it 'delivers the mail'
it "delivers the mail"
# it 'delivers the mail' do
# @options.fetch(:deliveries).length.must_equal 1
# end

View File

@ -1,43 +1,45 @@
# frozen_string_literal: true
RSpec.describe Hanami::Mailer do
describe '.template' do
describe 'when no value is set' do
it 'returns the convention name' do
expect(RenderMailer.template).to eq('render_mailer')
describe ".template" do
describe "when no value is set" do
it "returns the convention name" do
expect(RenderMailer.template).to eq("render_mailer")
end
it 'returns correct namespaced value' do
expect(Users::Welcome.template).to eq('users/welcome')
it "returns correct namespaced value" do
expect(Users::Welcome.template).to eq("users/welcome")
end
end
describe 'when a value is set' do
it 'returns that name' do
expect(InvoiceMailer.template).to eq('invoice')
describe "when a value is set" do
it "returns that name" do
expect(InvoiceMailer.template).to eq("invoice")
end
end
end
describe '.templates' do
describe 'when no value is set' do
it 'returns a set of templates' do
describe ".templates" do
describe "when no value is set" do
it "returns a set of templates" do
template_formats = LazyMailer.templates.keys
expect(template_formats).to match_array(%i[html txt])
end
it 'returns only the template for the given format' do
it "returns only the template for the given format" do
template = LazyMailer.templates(:txt)
expect(template).to be_kind_of(Hanami::Mailer::Template)
expect(template.file).to match(%r{spec/support/fixtures/templates/lazy_mailer.txt.erb\z})
end
end
describe 'when a value is set' do
it 'returns a set of templates' do
describe "when a value is set" do
it "returns a set of templates" do
template_formats = InvoiceMailer.templates.keys
expect(template_formats).to eq([:html])
end
it 'returns only the template for the given format' do
it "returns only the template for the given format" do
template = InvoiceMailer.templates(:html)
expect(template).to be_kind_of(Hanami::Mailer::Template)
expect(template.file).to match(%r{spec/support/fixtures/templates/invoice.html.erb\z})

View File

@ -1,42 +1,44 @@
# frozen_string_literal: true
RSpec.describe Hanami::Mailer do
describe '#render' do
describe 'when template is explicitly declared' do
describe "#render" do
describe "when template is explicitly declared" do
let(:mailer) { InvoiceMailer.new }
it 'renders the given template' do
it "renders the given template" do
expect(mailer.render(:html)).to include(%(<h1>Invoice template</h1>))
end
end
describe 'when template is implicitly declared' do
describe "when template is implicitly declared" do
let(:mailer) { LazyMailer.new }
it 'looks for template with same name with inflected classname and render it' do
it "looks for template with same name with inflected classname and render it" do
expect(mailer.render(:html)).to include(%(Hello World))
expect(mailer.render(:txt)).to include(%(This is a txt template))
end
end
describe 'when mailer defines context' do
describe "when mailer defines context" do
let(:mailer) { WelcomeMailer.new }
it 'renders template with defined context' do
it "renders template with defined context" do
expect(mailer.render(:txt)).to include(%(Ahoy))
end
end
describe 'when locals are parsed in' do
let(:mailer) { RenderMailer.new(user: User.new('Luca')) }
describe "when locals are parsed in" do
let(:mailer) { RenderMailer.new(user: User.new("Luca")) }
it 'renders template with parsed locals' do
it "renders template with parsed locals" do
expect(mailer.render(:html)).to include(%(Luca))
end
end
describe 'with HAML template engine' do
let(:mailer) { TemplateEngineMailer.new(user: User.new('Luca')) }
describe "with HAML template engine" do
let(:mailer) { TemplateEngineMailer.new(user: User.new("Luca")) }
it 'renders template with parsed locals' do
it "renders template with parsed locals" do
expect(mailer.render(:html)).to include(%(<h1>\nLuca\n</h1>\n))
end
end

View File

@ -1,3 +1,5 @@
# frozen_string_literal: true
RSpec.describe "Hanami::Mailer::VERSION" do
it "returns current version" do
expect(Hanami::Mailer::VERSION).to eq("1.3.2")