Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a Rails Engine to show the letters if you are not running locally #12

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions app/controllers/letter_opener/letters_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module LetterOpener
class LettersController < ApplicationController
def index
@letters = Letter.all
end

def show
@letter = Letter.find_by_name(params[:id])
render :text=>@letter.contents(params[:style])
end
end
end
25 changes: 25 additions & 0 deletions app/models/letter_opener/letter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module LetterOpener
class Letter
attr_accessor :name, :updated_at

def initialize(attributes)
@name = attributes[:name]
@updated_at = attributes[:updated_at]
end

def self.all
letters = Dir.glob("#{LetterOpener.letters_location}/*").map do |folder|
new :name => File.basename(folder), :updated_at => File.mtime(folder)
end
letters.sort_by(&:updated_at).reverse
end

def self.find_by_name(name)
new(:name => name)
end

def contents(style = :plain)
File.read("#{LetterOpener.letters_location}/#{name}/#{style}.html")
end
end
end
8 changes: 8 additions & 0 deletions app/views/letter_opener/letters/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Inbox:

<%- @letters.each do |letter| %>
<div class="letter">
<%= link_to "#{letter.name} - #{letter.updated_at}", letter_opener_letter_path(letter.name, :style=>:plain) %>
</div>
<%- end %>

4 changes: 4 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Rails.application.routes.draw do
match "/letter_opener" => 'letter_opener/letters#index', :as => :letter_opener_letters
match "/letter_opener/:id/:style.html" => 'letter_opener/letters#show', :as => :letter_opener_letter
end
1 change: 1 addition & 0 deletions letter_opener.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ Gem::Specification.new do |s|
s.require_path = "lib"

s.add_dependency 'launchy'
s.add_development_dependency "rails", "~> 3.0"
s.add_development_dependency 'rspec', '~> 2.6.0'
s.add_development_dependency 'mail', '~> 2.3.0'

Expand Down
2 changes: 1 addition & 1 deletion lib/letter_opener.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

require "letter_opener/message"
require "letter_opener/delivery_method"
require "letter_opener/railtie" if defined? Rails
require "letter_opener/engine" if defined? Rails
12 changes: 12 additions & 0 deletions lib/letter_opener/engine.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module LetterOpener
class Engine < Rails::Engine
initializer "letter_opener.add_delivery_method" do
ActionMailer::Base.add_delivery_method :letter_opener, LetterOpener::DeliveryMethod, :location => LetterOpener.letters_location
end
end

def self.letters_location
Rails.root.join("tmp", "letter_opener")
end
end

7 changes: 0 additions & 7 deletions lib/letter_opener/railtie.rb

This file was deleted.

47 changes: 47 additions & 0 deletions spec/app/models/letter_opener/letter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
require "spec_helper"
require File.expand_path('../../../../../app/models/letter_opener/letter.rb', __FILE__)

describe LetterOpener::Letter do
let(:location) { File.expand_path('../../../../../tmp/letter_opener/', __FILE__) }
before(:each) do
LetterOpener.stub(:letters_location).and_return(location)
FileUtils.rm_rf(location)
['1111_1111', '2222_2222'].each do |folder|
FileUtils.mkdir_p("#{location}/#{folder}")
File.open("#{location}/#{folder}/plain.html", 'w') {|f| f.write("I am a plain email for #{folder}") }
File.open("#{location}/#{folder}/rich.html", 'w') {|f| f.write("I am an <b>html<b> email for #{folder}") }
end
end
describe '.all' do
let(:all) { LetterOpener::Letter.all }
describe '2222_2222 is first since it is most recent' do
subject { all.first }
its(:name) { should == '2222_2222' }
its(:contents) { should == 'I am a plain email for 2222_2222' }
it 'should read the rich contents when requested' do
subject.contents(:rich) { should == 'I am an <b>html</b> email for 2222_2222' }
end
it 'should read the plain contents when requested' do
subject.contents(:plain) { should == 'I am a text email for 2222_2222' }
end
end
describe '1111_1111 is last since it is oldest' do
subject { all.last }
its(:name) { should == '1111_1111' }
its(:contents) { should == 'I am a plain email for 1111_1111' }
it 'should read the rich contents when requested' do
subject.contents(:rich) { should == 'I am an <b>html</b> email for 1111_1111' }
end
it 'should read the plain contents when requested' do
subject.contents(:plain) { should == 'I am a text email for 1111_1111' }
end
end
end

describe '.find_by_name' do
subject { LetterOpener::Letter.find_by_name('1111_1111') }
its(:name) { should == '1111_1111' }
its(:contents) { should == 'I am a plain email for 1111_1111' }
end

end
2 changes: 1 addition & 1 deletion spec/letter_opener/delivery_method_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
describe LetterOpener::DeliveryMethod do
before(:each) do
Launchy.stub(:open)
location = File.expand_path('../../../tmp/letter_opener', __FILE__)
location = File.expand_path('../../../../tmp/letter_opener', __FILE__)
FileUtils.rm_rf(location)
Mail.defaults do
delivery_method LetterOpener::DeliveryMethod, :location => location
Expand Down