Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Initial Import

  • Loading branch information...
commit f39bb31fae04761b959c7496696afea6ab244a17 0 parents
mikedemers authored
20 LICENSE
... ... @@ -0,0 +1,20 @@
  1 +Copyright (c) 2009 Mike Demers <mike_(at)_9astronauts.com>
  2 +
  3 +Permission is hereby granted, free of charge, to any person obtaining
  4 +a copy of this software and associated documentation files (the
  5 +"Software"), to deal in the Software without restriction, including
  6 +without limitation the rights to use, copy, modify, merge, publish,
  7 +distribute, sublicense, and/or sell copies of the Software, and to
  8 +permit persons to whom the Software is furnished to do so, subject to
  9 +the following conditions:
  10 +
  11 +The above copyright notice and this permission notice shall be
  12 +included in all copies or substantial portions of the Software.
  13 +
  14 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15 +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16 +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17 +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18 +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19 +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20 +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
70 README
... ... @@ -0,0 +1,70 @@
  1 += CachingMailer
  2 +
  3 +Enables the use of fragment caching in ActionMailer views.
  4 +
  5 +Shares the cache configuration of ActionController::Base.
  6 +
  7 +
  8 +== Resources
  9 +
  10 +* Source: http://github.com/mikedemers/caching-mailer/tree/master
  11 +* Support: http://github.com/mikedemers/caching-mailer/issues
  12 +
  13 +
  14 +== Installation
  15 +
  16 + script/plugin install git://github.com/mikedemers/caching-mailer.git
  17 +
  18 +
  19 +== Usage
  20 +
  21 +Configure fragment caching in "config/environment.rb" (or in the
  22 +configuration file for a specific environment such as
  23 +"config/environments/production.rb"):
  24 +
  25 + config.action_controller.perform_caching = true
  26 + config.cache_store = :file_store, File.join(Rails.root, 'tmp', 'cache')
  27 +
  28 +
  29 +For the mailer "app/models/hello_mailer.rb":
  30 +
  31 + class HelloMailer < ActionMailer::Base
  32 +
  33 + include_fragment_caching
  34 +
  35 + def hello
  36 + subject "Hello, World"
  37 + recipients "you@foo.com"
  38 + from "me@bar.com"
  39 + sent_on Time.now
  40 + end
  41 + end
  42 +
  43 +And the template "app/views/hello_mailer/hello.text.plain.erb":
  44 +
  45 + Hello, World!
  46 +
  47 + This is an computationally expensive list:
  48 +
  49 + <% cache("mail/expensive-list") do %>
  50 + <% ExpensiveOperation.results.each do |result| %>
  51 + * Item #<%= result.id %>: "<%= result.title %>"
  52 + <% end %>
  53 + <% end %>
  54 +
  55 +The computationally expensive method `ExpensiveOperation.results` will only
  56 +be invoked once. Subsequent emails will use the cached result.
  57 +
  58 +For automatic expiration, try including a time value in the cache key. This
  59 +example will generate a new cache entry every 2 hours (removal of stale
  60 +entries is left as an exercise to the reader):
  61 +
  62 + <% cache("mail/expensive-list-#{Time.now.to_i / 2.hours.to_i}") do %>
  63 + <% ExpensiveOperation.results.each do |result| %>
  64 + * Item #<%= result.id %>: "<%= result.title %>"
  65 + <% end %>
  66 + <% end %>
  67 +
  68 +
  69 +Copyright (c) 2009 Mike Demers <mike_(at)_9astronauts.com>, released under
  70 +the MIT license.
23 Rakefile
... ... @@ -0,0 +1,23 @@
  1 +require 'rake'
  2 +require 'rake/testtask'
  3 +require 'rake/rdoctask'
  4 +
  5 +desc 'Default: run unit tests.'
  6 +task :default => :test
  7 +
  8 +desc 'Test the caching_mailer plugin.'
  9 +Rake::TestTask.new(:test) do |t|
  10 + t.libs << 'lib'
  11 + t.libs << 'test'
  12 + t.pattern = 'test/**/*_test.rb'
  13 + t.verbose = true
  14 +end
  15 +
  16 +desc 'Generate documentation for the caching_mailer plugin.'
  17 +Rake::RDocTask.new(:rdoc) do |rdoc|
  18 + rdoc.rdoc_dir = 'rdoc'
  19 + rdoc.title = 'CachingMailer'
  20 + rdoc.options << '--line-numbers' << '--inline-source'
  21 + rdoc.rdoc_files.include('README')
  22 + rdoc.rdoc_files.include('lib/**/*.rb')
  23 +end
5 init.rb
... ... @@ -0,0 +1,5 @@
  1 +require 'nine_astronauts/caching_mailer'
  2 +
  3 +ActionMailer::Base.class_eval do
  4 + include NineAstronauts::CachingMailer
  5 +end
62 lib/nine_astronauts/caching_mailer.rb
... ... @@ -0,0 +1,62 @@
  1 +module NineAstronauts
  2 + module CachingMailer
  3 +
  4 + module VERSION #:nodoc:
  5 + MAJOR = 1
  6 + MINOR = 0
  7 + TINY = 0
  8 +
  9 + STRING = [MAJOR, MINOR, TINY].join('.')
  10 + end
  11 +
  12 + def self.included(base) #:nodoc:
  13 + base.extend CachingMethods
  14 + end
  15 +
  16 + module CachingMethods
  17 +
  18 + # Brings Rails' fragment caching functionality into ActionMailer
  19 + #
  20 + # Usage:
  21 + #
  22 + # class HelloMailer < ActionMailer::Base
  23 + #
  24 + # include_fragment_caching
  25 + #
  26 + # def hello
  27 + # subject "Hello, World"
  28 + # recipients "you@foo.com"
  29 + # from "me@bar.com"
  30 + # sent_on Time.now
  31 + # end
  32 + # end
  33 + #
  34 + def include_fragment_caching
  35 + self.extend(ClassMethods)
  36 + self.send(:include, InstanceMethods)
  37 + self.send(:include, ActionController::Caching::Fragments)
  38 + end
  39 + end
  40 +
  41 + module ClassMethods #:nodoc:
  42 + delegate :cache_store, :perform_caching, :cache_configured?, :benchmark, :silence, :to => ActionController::Base
  43 + end
  44 +
  45 + module InstanceMethods #:nodoc:
  46 + def self.included(base)
  47 + base.delegate :cache_store, :perform_caching, :cache_configured?, :to => base
  48 + end
  49 + protected
  50 + ### Copied from "action_controller/caching.rb", line 57-64 (in 2.2.2):
  51 + # Convenience accessor
  52 + def cache(key, options = {}, &block)
  53 + if cache_configured?
  54 + cache_store.fetch(ActiveSupport::Cache.expand_cache_key(key, :controller), options, &block)
  55 + else
  56 + yield
  57 + end
  58 + end
  59 + end
  60 +
  61 + end
  62 +end
4 tasks/caching_mailer_tasks.rake
... ... @@ -0,0 +1,4 @@
  1 +# desc "Explaining what the task does"
  2 +# task :caching_mailer do
  3 +# # Task goes here
  4 +# end
8 test/caching_mailer_test.rb
... ... @@ -0,0 +1,8 @@
  1 +require 'test_helper'
  2 +
  3 +class CachingMailerTest < ActiveSupport::TestCase
  4 + # Replace this with your real tests.
  5 + test "the truth" do
  6 + assert true
  7 + end
  8 +end
3  test/test_helper.rb
... ... @@ -0,0 +1,3 @@
  1 +require 'rubygems'
  2 +require 'active_support'
  3 +require 'active_support/test_case'

0 comments on commit f39bb31

Please sign in to comment.
Something went wrong with that request. Please try again.