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

Remove single email. #23

Merged
merged 5 commits into from
Apr 7, 2014
Merged
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
6 changes: 6 additions & 0 deletions app/controllers/letter_opener_web/letters_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,11 @@ def clear
Letter.destroy_all
redirect_to letters_path
end

def destroy
letter = Letter.find(params[:id])
letter.delete
redirect_to letters_path
end
end
end
4 changes: 4 additions & 0 deletions app/models/letter_opener_web/letter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def attachments
end
end

def delete
FileUtils.rm_rf("#{letters_location}/#{self.id}")
end

private

def base_dir
Expand Down
15 changes: 15 additions & 0 deletions app/views/letter_opener_web/letters/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
<tr>
<th>ID</th>
<th>Sent at</th>
<th>Action</th>
</tr>
</thead>
<tbody>
Expand All @@ -26,6 +27,13 @@
<%= link_to(first_letter.id, letter_path(first_letter, style: first_letter.default_style), target: 'mail') %>
</td>
<td><%= first_letter.sent_at %></td>
<td>
<%= link_to delete_letter_path(first_letter), method: 'delete', data: { confirm: 'Are you sure you want to delete this email?' } do %>
<span class="pull-right">
<i class="icon-remove-circle" title="Delete"></i>
</span>
<% end %>
</td>
</tr>
<% end %>
<% @letters.each do |letter| %>
Expand All @@ -34,6 +42,13 @@
<%= link_to(letter.id, letter_path(letter, style: letter.default_style), target: 'mail') %>
</td>
<td><%= letter.sent_at %></td>
<td>
<%= link_to delete_letter_path(letter), :method => 'delete', data: { confirm: 'Are you sure you want to delete this email?' } do %>
<span class="pull-right">
<i class="icon-remove-circle"></i>
</span>
<% end %>
</td>
</tr>
<% end %>
</tbody>
Expand Down
7 changes: 4 additions & 3 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
LetterOpenerWeb::Engine.routes.draw do
delete 'clear' => 'letters#clear', :as => :clear_letters
get '/' => 'letters#index', :as => :letters
get ':id(/:style)' => 'letters#show', :as => :letter
delete 'clear' => 'letters#clear', :as => :clear_letters
delete ':id/delete' => 'letters#destroy', :as => :delete_letter
get '/' => 'letters#index', :as => :letters
get ':id(/:style)' => 'letters#show', :as => :letter
get ':id/attachments/:file' => 'letters#attachment'
end
8 changes: 8 additions & 0 deletions spec/controllers/letter_opener_web/letters_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,12 @@
response.should redirect_to(letters_path)
end
end

describe 'DELETE destroy' do
let(:id) { 'an-id' }
it 'removes the selected letter' do
LetterOpenerWeb::Letter.any_instance.should_receive(:delete)
delete :destroy, :id => id, :use_route => :letter_opener_web
end
end
end
1 change: 1 addition & 0 deletions spec/internal/config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Rails.application.routes.draw do
delete 'clear' => 'letter_opener_web/letters#clear'
delete ':id/delete' => 'letters#destroy', :as => :delete_letter
get '/' => 'letter_opener_web/letters#index', :as => :letters
get ':id(/:style)' => 'letter_opener_web/letters#show', :as => :letter
get ':id/attachments/:file' => 'letter_opener_web/letters#attachment'
Expand Down
12 changes: 12 additions & 0 deletions spec/models/letter_opener_web/letter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,16 @@ def rich_text(mail_id)
Dir["#{location}/**/*"].should be_empty
end
end

describe '#delete' do
let(:id) { '1111_1111' }
subject { described_class.new(:id => id).delete }

it'removes the letter with given id' do
subject
directories = Dir["#{location}/*"]
directories.count.should eql(1)
directories.first.should_not match(id)
end
end
end