class ViewMailer::ViewMailer

Public Class Methods

create_mocker_class(controller) click to toggle source
# File lib/view_mailer.rb, line 10
def self.create_mocker_class(controller)
  new_class = Class.new(controller) do |klass|
    include ActionController::Rendering
    include ActionController::Redirecting
    include Rails.application.routes.url_helpers
    include  AbstractController::Callbacks
    @@controller = controller

    def content_type
      "text/html"
    end

    def env
      @_env ||= {
        "rack.url_scheme" => "http",
        "rack.input"      => "",
        "rack.session"    => "",
        "REQUEST_METHOD"  => "GET"
      }
    end

    def session
      {}
    end

    def request
      @_request ||= ActionDispatch::Request.new(env)
    end

    def current_user
      @@current_user
    end

    def current_user=(user)
      @@current_user = user
    end

    def params=(given_params)
      @_params = given_params
    end

    def params
      @_params
    end

    helper_method :will_paginate
    def will_paginate(o,x=nil)
      true
    end

    def self.name
      ViewMailer.mock_controller_name(@@controller)
    end
  end
  Object.const_set(mock_controller_name(controller), new_class)
end
define_or_initialize_by_controller(controller) click to toggle source
# File lib/view_mailer.rb, line 67
def self.define_or_initialize_by_controller(controller)
  mock_controller_name = mock_controller_name(controller.to_s)
  create_mocker_class(controller) unless Object.const_defined?(mock_controller_name)
  mock_controller_name.constantize.new
end
mock_controller_name(controller) click to toggle source
# File lib/view_mailer.rb, line 6
def self.mock_controller_name(controller)
  "Mock#{controller.to_s.gsub(/::/, '')}"
end
name() click to toggle source
# File lib/view_mailer.rb, line 60
def self.name
  ViewMailer.mock_controller_name(@@controller)
end

Public Instance Methods

content_type() click to toggle source
# File lib/view_mailer.rb, line 18
def content_type
  "text/html"
end
current_user() click to toggle source
# File lib/view_mailer.rb, line 39
def current_user
  @@current_user
end
current_user=(user) click to toggle source
# File lib/view_mailer.rb, line 43
def current_user=(user)
  @@current_user = user
end
env() click to toggle source
# File lib/view_mailer.rb, line 22
def env
  @_env ||= {
    "rack.url_scheme" => "http",
    "rack.input"      => "",
    "rack.session"    => "",
    "REQUEST_METHOD"  => "GET"
  }
end
mail(options, &block) click to toggle source
Calls superclass method
# File lib/view_mailer.rb, line 73
def mail(options, &block)
  params = Rails.application.routes.recognize_path(options[:path], method: (options[:method] || 'GET'))
  mock_controller = self.class.define_or_initialize_by_controller("#{params[:controller].camelize}Controller".constantize)
  mock_controller.params = params
  mock_controller.current_user = options[:current_user]
  mock_controller.send(params[:action])
  default_options = {:content_type => mock_controller.content_type}

  super(default_options.merge(options).merge(:body => mock_controller.render_to_string(params[:action].to_sym)), &block)
end
params() click to toggle source
# File lib/view_mailer.rb, line 51
def params
  @_params
end
params=(given_params) click to toggle source
# File lib/view_mailer.rb, line 47
def params=(given_params)
  @_params = given_params
end
request() click to toggle source
# File lib/view_mailer.rb, line 35
def request
  @_request ||= ActionDispatch::Request.new(env)
end
session() click to toggle source
# File lib/view_mailer.rb, line 31
def session
  {}
end
will_paginate(o,x=nil) click to toggle source
# File lib/view_mailer.rb, line 56
def will_paginate(o,x=nil)
  true
end