Skip to content

Commit

Permalink
Merge pull request #68 from omnilord/trashcan
Browse files Browse the repository at this point in the history
Adding functionality to view trashed items and restore them when needed.
  • Loading branch information
miklb authored Oct 25, 2018
2 parents fa1a25e + a5543b5 commit 241dfc0
Show file tree
Hide file tree
Showing 14 changed files with 349 additions and 96 deletions.
3 changes: 2 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ def setup_main_menu
{ text: 'Users', path: users_path },
{ text: 'AmazonProducts', path: amazon_products_path },
{ text: 'Charitable Organizations', path: charitable_organizations_path },
{ text: 'Pages', path: pages_path }
{ text: 'Pages', path: pages_path },
{ text: 'Trash', path: trash_index_path }
] }
end
end
Expand Down
28 changes: 28 additions & 0 deletions app/controllers/trash_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class TrashController < ApplicationController
before_action :authenticate_admin!, only: [:destroy]
before_action :set_trash, only: [:show, :destroy]

def index
@trash = Trash.all
end

def show
@columns = @resource.class::ColumnNames
@headers = @resource.class::HeaderNames
end

def destroy
if resource = @trash.restore!
redirect_to resource
else
redirect_to trash_path, notice: "Could not restore '#{@resource.name}' (#{@trash.trashable_type}/#{@trash.trashable_id})."
end
end

private

def set_trash
@trash = Trash.find(params[:id])
@resource = @trash.resource
end
end
4 changes: 4 additions & 0 deletions app/models/distribution_point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ def self.to_csv
end
end

def name
facility_name
end

def outdated?
updated_at < 4.hours.ago
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/shelter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ def self.to_csv
end
end

def name
shelter
end

def outdated?
updated_at < 4.hours.ago
end
Expand Down
23 changes: 16 additions & 7 deletions app/models/trash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,24 @@ class Trash < ApplicationRecord
belongs_to :user
belongs_to :trashable, polymorphic: true, optional: true

after_initialize :set_resource

def restore!
restoring = nil
restored = false
transaction do
restoring = trashable_type.constantize.create(data)
unless restoring && self.destroy
restoring = nil
raise ActiveRecord::Rollback
end
restored = @resource.save && self.destroy
raise ActiveRecord::Rollback unless restored
end
restoring
restored ? @resource : nil
end

def resource
@resource
end

private

def set_resource
@resource = trashable_type.constantize.new(data)
end
end
22 changes: 22 additions & 0 deletions app/views/distribution_points/_distribution_point.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<h1><%= resource.facility_name %></h1>

<%= content_for :resource_command_list %>

<div class="row">
<div class="column">
<%= render 'shared/show', record: resource, headers: @headers, columns: @columns %>
</div>
<div class="column">
<div id="map" style="height: 300px; width: 100%"></div>
<script>
$(window).ready(function(){
simpleMap({
selector: "#map",
name: "<%= j resource.facility_name %>",
lat: <%= resource.latitude || 0 %>,
lng: <%= resource.longitude || 0 %>
})
})
</script>
</div>
</div>
70 changes: 26 additions & 44 deletions app/views/distribution_points/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,50 +1,32 @@
<h1><%= @distribution_point.facility_name %></h1>


<% if @distribution_point.archived %>
<%= link_to 'Back', archived_distribution_points_path, class: 'button button-clear' %>
<% else %>
<%= link_to 'Update', [:edit, @distribution_point], class: 'button button-clear' %> |
<%= link_to 'Back', distribution_points_path, class: 'button button-clear' %>
<% end %>
<% if admin? %> |
<% content_for :resource_command_list do %>
<% if @distribution_point.archived %>
<%= link_to 'Unarchive', unarchive_distribution_point_path(@distribution_point),
method: :delete,
data: { confirm: 'Are you sure?' },
class: 'button button-clear' %>
<%= link_to 'Back', archived_distribution_points_path, class: 'button button-clear' %>
<% else %>
<%= link_to 'Archive', archive_distribution_point_path(@distribution_point),
<%= link_to 'Update', edit_distribution_point_path(@distribution_point), class: 'button button-clear' %> |
<%= link_to 'Back', distribution_points_path, class: 'button button-clear' %>
<% end %>
<% if admin? %> |
<% if @distribution_point.archived %>
<%= link_to 'Unarchive', unarchive_distribution_point_path(@distribution_point),
method: :delete,
data: { confirm: 'Are you sure?' },
class: 'button button-clear' %>
<% else %>
<%= link_to 'Archive', archive_distribution_point_path(@distribution_point),
method: :post,
data: { confirm: 'Are you sure?' },
class: 'button button-clear' %>
<% end %> |
<%= link_to 'Delete', @distribution_point,
method: :delete,
data: { confirm: 'Are you sure?' },
class: 'button button-clear' %>
<% end %>
<% if user_signed_in? && @distribution_point.outdated? %> |
<%= link_to 'Mark Current', mark_current_distribution_point_path(@distribution_point),
method: :post,
data: { confirm: 'Are you sure?' },
class: 'button button-clear' %>
<% end %> |
<%= link_to 'Delete', @distribution_point,
method: :delete,
data: { confirm: 'Are you sure?' },
class: 'button button-clear' %>
<% end %>
<% if user_signed_in? && @distribution_point.outdated? %> |
<%= link_to 'Mark Current', mark_current_distribution_point_path(@distribution_point),
method: :post,
class: 'button button-clear' %>
<% end %>
<% end %>

<div class="row">
<div class="column">
<%= render 'shared/show', record: @distribution_point, headers: @headers, columns: @columns %>
</div>
<div class="column">
<div id="map" style="height: 300px; width: 100%"></div>
<script>
$(window).ready(function(){
simpleMap({
selector: "#map",
name: "<%= j @distribution_point.facility_name %>",
lat: <%= @distribution_point.latitude || 0 %>,
lng: <%= @distribution_point.longitude || 0 %>
})
})
</script>
</div>
</div>
<%= render @distribution_point, resource: @distribution_point %>
22 changes: 22 additions & 0 deletions app/views/shelters/_shelter.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<h1><%= resource.name %></h1>

<%= content_for :resource_command_list %>

<div class="row">
<div class="column">
<%= render 'shared/show', record: resource, headers: headers, columns: columns %>
</div>
<div class="column">
<div id="map" style="height: 300px; width: 100%"></div>
<script>
$(window).ready(function(){
simpleMap({
selector: "#map",
name: "<%= j resource.name %>",
lat: <%= resource.latitude || 0 %>,
lng: <%= resource.longitude || 0 %>
})
})
</script>
</div>
</div>
73 changes: 29 additions & 44 deletions app/views/shelters/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,49 +1,34 @@
<h1><%= @shelter.shelter %></h1>

<% if @shelter.active %>
<%= link_to 'Update', [:edit, @shelter], class: 'button button-clear' %> |
<%= link_to 'Back', shelters_path, class: 'button button-clear' %>
<% else %>
<%= link_to 'Back', archived_shelters_path, class: 'button button-clear' %>
<% end %>
<% if admin? %> |
<% content_for :resource_command_list do %>
<% if @shelter.active %>
<%= link_to 'Archive', archive_shelter_path(@shelter),
method: :post,
data: { confirm: 'Are you sure?' },
class: 'button button-clear' %>
<%= link_to 'Update', [:edit, @shelter], class: 'button button-clear' %> |
<%= link_to 'Back', shelters_path, class: 'button button-clear' %>
<% else %>
<%= link_to 'Unarchive', unarchive_shelter_path(@shelter),
method: :delete,
data: { confirm: 'Are you sure?' },
class: 'button button-clear' %>
<% end %> |
<%= link_to 'Delete', @shelter,
method: :delete,
data: { confirm: 'Are you sure?' },
<%= link_to 'Back', archived_shelters_path, class: 'button button-clear' %>
<% end %>
<% if admin? %> |
<% if @shelter.active %>
<%= link_to 'Archive', archive_shelter_path(@shelter),
method: :post,
data: { confirm: 'Are you sure?' },
class: 'button button-clear' %>
<% else %>
<%= link_to 'Unarchive', unarchive_shelter_path(@shelter),
method: :delete,
data: { confirm: 'Are you sure?' },
class: 'button button-clear' %>
<% end %> |
<%= link_to 'Delete', @shelter,
method: :delete,
data: { confirm: 'Are you sure?' },
class: 'button button-clear' %>
<% end %>
<% if user_signed_in? && @shelter.outdated? %> |
<%= link_to 'Mark Current', mark_current_shelter_path(@shelter),
method: :post,
class: 'button button-clear' %>
<% end %>
<% if user_signed_in? && @shelter.outdated? %> |
<%= link_to 'Mark Current', mark_current_shelter_path(@shelter),
method: :post,
class: 'button button-clear' %>
<% end %>
<% end %>

<div class="row">
<div class="column">
<%= render 'shared/show', record: @shelter, headers: @headers, columns: @columns %>
</div>
<div class="column">
<div id="map" style="height: 300px; width: 100%"></div>
<script>
$(window).ready(function(){
simpleMap({
selector: "#map",
name: "<%= j @shelter.shelter %>",
lat: <%= @shelter.latitude || 0 %>,
lng: <%= @shelter.longitude || 0 %>
})
})
</script>
</div>
</div>
<%= render @shelter, resource: @shelter,
headers: @headers,
columns: @columns %>
43 changes: 43 additions & 0 deletions app/views/trash/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<h1>Trashed Records</h1>


<table id="data-table">
<thead>
<tr>
<th></th>
<th>Type/ID</th>
<th>Record Name</th>
<th>Deleted At</th>
<th>Deleted By</th>
<th>Reason</th>
</tr>
</thead>

<tbody>
<% @trash.each do |row| %>
<tr>
<td class="controlsColumn">
<%= link_to 'Show', row, class: 'button button-clear' %>
<% if admin? %>
<br/>
<%= link_to 'Restore', row,
method: :delete,
data: { confirm: 'Are you sure?' },
class: 'button button-clear' %>
<% end %>
</td>
<td><%= "#{row.trashable_type}/#{row.trashable_id}" %></td>
<td><%= sanitize(row.resource.name) %></td>
<td><%= time_ago_in_words(row.created_at) %> ago</td>
<td><%= row.user&.email %></td>
<td><%= sanitize(row.reason) %></td>
</tr>
<% end %>
</tbody>
</table>

<script>
window.onload = function() {
$('#data-table').DataTable();
};
</script>
9 changes: 9 additions & 0 deletions app/views/trash/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<% content_for :resource_command_list do %>
<%= link_to 'Restore', trash_path(@trash),
method: :delete,
data: { confirm: 'Are you sure?' },
class: 'button button-clear' %>
<% end %>
<%= render @resource, resource: @resource,
headers: @headers,
columns: @columns %>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
post :accept, on: :member
end

resources :trashes, as: :trash, path: '/trash', controller: 'trash', only: [:index, :show, :destroy]

resources :amazon_products, except: [:new, :create, :destroy]

namespace :connect do
Expand Down
Loading

0 comments on commit 241dfc0

Please sign in to comment.