Module: Blue::Template

Included in:
Box
Defined in:
lib/blue/template.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/blue/template.rb', line 4

def self.included(klass)
  klass.class_eval do
    def local_template_dir
      @local_template_dir ||= Pathname.new(Blue.rails_current + '/app/manifests/templates')
    end

    def local_template(pathname)
     (local_template_dir + pathname.basename).expand_path
    end

    # Render the ERB template located at <tt>pathname</tt>. If a template exists
    # with the same basename at <tt>RAILS_ROOT/app/manifests/templates</tt>, it
    # is used instead. This is useful to override templates provided by plugins
    # to customize application configuration files.
    def template(pathname, b = binding)
      pathname = Pathname.new(pathname) unless pathname.kind_of?(Pathname)

      pathname = if local_template(pathname).exist?
                   local_template(pathname)
                 elsif pathname.exist?
                   pathname
                 else
                   raise LoadError, "Can't find template #{pathname}"
                 end
      erb = ERB.new(pathname.read)
      erb.filename = pathname.to_s
      erb.result(b)
    end
  end
end

Instance Method Details

#template(pathname, b = binding) ⇒ Object



35
36
37
# File 'lib/blue/template.rb', line 35

def template(pathname, b = binding)
  self.class.template(pathname, b)
end