Permalink
Browse files

Minimal admin interface scaffolding

  • Loading branch information...
1 parent 449dca9 commit 9c7bcbe02f829307e777854bd4a59eb0cd4cdfdf Martin Kleppmann committed Oct 18, 2009
Showing with 80 additions and 1 deletion.
  1. +7 −0 README.md
  2. +33 −1 app/controllers/customers_controller.rb
  3. +26 −0 app/views/customers/index.html.erb
  4. +14 −0 app/views/customers/show.html.erb
View
@@ -29,8 +29,13 @@ Add the following to `config/environment.rb` just after the `require File.join(F
and in the same file inside the `Rails::Initializer.run` block:
config.gem "rubyist-aasm", :lib => 'aasm', :source => "http://gems.github.com"
+ config.gem "fastercsv"
config.active_record.observers = :mailing_observer
+Add the following to `config/initializers/mime_types.rb`:
+
+ Mime::Type.register "text/csv", :csv
+
Add the following to `config/environments/development.rb`:
# Do not redirect to HTTPS URLs during development
@@ -100,6 +105,8 @@ by a controller called `AboutController`):
map.welcome '/welcome', :controller => 'customers', :action => 'dashboard'
map.forgotten_password '/forgotten_password', :controller => 'users', :action => 'forgotten_password'
+ map.resources :customers, :collection => {:welcome => :any, :choose => :get}
+
map.resources :users, :member => {:impersonate => :any}
map.resource :session
@@ -1,6 +1,8 @@
class CustomersController < ApplicationController
- before_filter :customer_login_required, :except => [:new, :choose]
+ admin_actions = [:index, :show]
+ before_filter :admin_required, :only => admin_actions
+ before_filter :customer_login_required, :except => [:new, :choose] + admin_actions
before_filter :login_required, :only => :choose
def new
@@ -50,4 +52,34 @@ def choose
end
end
+
+ ######## Admin stuff
+
+ def index
+ if params[:search]
+ @customers = Customer.all(:conditions => ["subdomain LIKE ? OR name LIKE ?", "%#{params[:search]}%", "%#{params[:search]}%"])
+ else
+ @customers = Customer.all
+ end
+
+ respond_to do |format|
+ format.html
+
+ format.csv do
+ send_data(FasterCSV.generate do |csv|
+ csv << ['Subdomain', 'Name', 'Signed Up']
+ @customers.each do |customer|
+ csv << [
+ customer.subdomain, customer.name, customer.created_at.strftime('%Y-%m-%d')
+ ]
+ end
+ end, :filename => 'customers.csv', :type => :csv, :disposition => 'inline')
+ end
+ end
+ end
+
+ def show
+ @customer = Customer.find_by_subdomain(params[:id]) or raise ActiveRecord::RecordNotFound
+ end
+
end
@@ -0,0 +1,26 @@
+<h1>Customers</h1>
+
+<form method="GET">
+ <p>
+ <input type="text" name="search" size="20" value="<%= h params[:search] %>" />
+ <input type="submit" name="btn" value="Search" />
+ </p>
+</form>
+
+<table>
+ <tr>
+ <th>Subdomain</th>
+ <th>Name</th>
+ <th>Signed up</th>
+ </tr>
+
+ <% for customer in @customers %>
+ <tr>
+ <td><%= link_to h(customer.subdomain), "/customers/#{customer.subdomain}" %></td>
+ <td><%= h customer.name %></td>
+ <td><%= h customer.created_at.strftime('%Y-%m-%d') %></td>
+ </tr>
+ <% end %>
+</table>
+
+<p><%= link_to 'Download CSV', :format => :csv, :search => params[:search] %></p>
@@ -0,0 +1,14 @@
+<h1><%= h @customer.name %> (<%= h @customer.subdomain %>)</h1>
+
+<h2>Users</h2>
+
+<ul>
+ <% for user in @customer.users %>
+ <li>
+ <% if user.is_admin_for? @customer %><b><% end %>
+ <%= h user.name %> &lt;<a href="mailto:<%= h user.email %>"><%= h user.email %></a>&gt; &mdash;
+ <%= link_to 'Impersonate', impersonate_user_path(user, :subdomain => @customer.subdomain) %>
+ <% if user.is_admin_for? @customer %></b><% end %>
+ </li>
+ <% end %>
+</ul>

0 comments on commit 9c7bcbe

Please sign in to comment.