Permalink
Browse files

Associated templates (#2)

  • Loading branch information...
inescoelho committed Aug 6, 2015
1 parent 9a6fec7 commit 54585bfbdc537a3784311a3ec1590a767d9e28b4
View
@@ -3,14 +3,15 @@
require 'lotus/mailer/version'
require 'lotus/mailer/configuration'
require 'lotus/mailer/dsl'
require 'lotus/mailer/inheritable'
module Lotus
module Mailer
include Utils::ClassAttribute
class_attribute :configuration
self.configuration = Configuration.new
# Configure the framework.
# It yields the given block in the context of the configuration
#
@@ -209,8 +210,6 @@ def self.duplicate(mod, mailers = 'Mailers', &blk)
#
# @see http://www.ruby-doc.org/core-2.1.2/Module.html#method-i-included
#
# @see Lotus::Mailer::Dsl
#
# @example
# require 'lotus/mailer'
#
@@ -222,7 +221,7 @@ def self.included(base)
conf.add_mailer(base)
base.class_eval do
# extend Inheritable.dup
extend Inheritable.dup
extend Dsl.dup
# extend Rendering.dup
# extend Escape.dup
@@ -243,13 +242,14 @@ def self.included(base)
# @since 0.1.0
# @api private
def self.load!
puts 'here'
configuration.load!
end
# Reset the configuration
def self.reset!
configuration.reset!
end
end
end
@@ -15,7 +15,6 @@ class Configuration
attr_reader :mailers
attr_reader :modules
# Initialize a configuration instance
#
# @return [Lotus::Mailer::Configuration] a new configuration's instance
@@ -274,6 +273,7 @@ def duplicate
# Load the configuration
def load!
mailers.each { |m| m.__send__(:load!) }
freeze
end
View
@@ -54,9 +54,10 @@ def root(value = nil)
# When a value is given, specify the relative path to the template.
# Otherwise, it returns the name that follows Lotus::Mailer conventions.
#
# @param format [Symbol] template format
# @param value [String] relative template path
#
# @return [String] the default template name for this mailer
# @return [Template] the template with the correspondent format for the mailer
#
# @since 0.1.0
#
@@ -69,8 +70,7 @@ def root(value = nil)
# template :json, 'articles/single_article'
# end
#
# Articles::Show.templates # :json => 'articles/single_article'
# Articles::Show.template # 'articles'
# Articles::Show.template(:json) # 'articles'
#
# @example With nested namespace
# require 'lotus/mailer'
@@ -112,7 +112,7 @@ def template(format = nil, value = nil)
# Returns the Hash with all the templates of the mailer
#
# @return [String] the Hash with the templates
# @return [Hash] the Hash with the templates
#
# @since 0.1.0
#
@@ -124,7 +124,12 @@ def template(format = nil, value = nil)
# end
def templates(value = nil)
if value.nil?
@templates
# If no templates are given, use the default templates instead
if @templates.empty?
Mailer::Rendering::TemplatesFinder.new(self).find
else
@templates
end
else
@templates = value
end
@@ -140,7 +145,6 @@ def templates(value = nil)
# @see Lotus::Mailer.load!
def load!
super
mailers.each do |m|
m.root.freeze
m.format.freeze
@@ -0,0 +1,54 @@
module Lotus
module Mailer
# Inheriting mechanisms
#
# @since 0.1.0
module Inheritable
# Register a mailer subclass
#
# @api private
# @since 0.1.0
#
# @example
# require 'lotus/mailer'
#
# class IndexMailer
# include Lotus::Mailer
# end
#
# class JsonIndexMailer < IndexMailer
# end
def inherited(base)
subclasses.add base
end
# Set of registered subclasses
#
# @api private
# @since 0.1.0
def subclasses
@@subclasses ||= Set.new
end
protected
# Loading mechanism hook.
#
# @api private
# @since 0.1.0
#
# @see Lotus::Mailer.load!
def load!
subclasses.freeze
mailers.freeze
end
# Registered mailers
#
# @api private
# @since 0.1.0
def mailers
@@mailers ||= [ self ] + subclasses.to_a
end
end
end
end
View
@@ -1,8 +1,13 @@
class InvoiceMailer
include Lotus::Mailer
template :html, 'invoice.html.erb'
#Lotus::Mailer.load!
end
class RenderMailer
include Lotus::Mailer
end
class LazyMailer
include Lotus::Mailer
end
@@ -0,0 +1,5 @@
<html>
<body>
<h1>Hello World!</h1>
</body>
</html>
View
@@ -14,4 +14,13 @@
end
end
end
describe '#templates' do
describe 'finds all the templates with the same name' do
it 'has the template in the hash' do
templateTest = LazyMailer.templates[:html]
templateTest.file.must_equal("#{ LazyMailer.root }/lazy_mailer.html.erb")
end
end
end
end

0 comments on commit 54585bf

Please sign in to comment.