Permalink
Browse files

Added some extra actions in the admin: new, edit, destroy, unsubscrib…

…e and resubscribe; with corresponding views.
  • Loading branch information...
1 parent 8071ded commit 4c84364cea4fd268e72b46662fbb7d9864173296 @nelstrom committed Jun 16, 2008
@@ -1,6 +1,8 @@
class SubscribersController < ApplicationController
require 'fastercsv'
-
+
+ before_filter :find_subscriber_list, :only => [:new, :edit, :destroy]
+
def index
@lists = Page.find_all_by_class_name("SubscriberListPage")
end
@@ -10,6 +12,60 @@ def list
@subscribers = Subscriber.find_active_subscribers_by_subscriber_list(@list)
@unsubscribers = Subscriber.find_unsubscribers_by_subscriber_list(@list)
end
+
+ def new
+ @subscriber = Subscriber.new
+ end
+
+ def create
+ params[:subscriber][:subscribed_at] = Time.now()
+ @subscriber = Subscriber.new(params[:subscriber])
+ if @subscriber.save
+ flash[:notice] = 'Subscriber has been saved correctly.'
+ redirect_to :action => 'list', :id => @subscriber.subscriber_list_id
+ else
+ flash[:error] = 'Validation errors occurred while processing this form. Please take a moment to review the form and correct any input errors before continuing.'
+ render :action => 'new'
+ end
+ end
+
+ def edit
+ @subscriber = Subscriber.find(params[:id])
+ end
+
+ def update
+ @subscriber = Subscriber.find(params[:id])
+ @subscriber_list = Page.find(params[:subscriber][:subscriber_list_id])
+ if @subscriber.update_attributes(params[:subscriber])
+ flash[:notice] = 'Subscriber has been updated correctly.'
+ redirect_to :action => 'list', :id => @subscriber.subscriber_list_id
+ else
+ flash[:error] = 'Validation errors occurred while processing this form. Please take a moment to review the form and correct any input errors before continuing.'
+ render :action => 'edit'
+ end
+ end
+
+
+ def unsubscribe
+ if @subscriber = Subscriber.find(params[:id])
+ @subscriber.update_attributes({:unsubscribed_at => Time.now})
+ redirect_to :action => :list, :id => @subscriber.subscriber_list_id
+ end
+ end
+
+ def resubscribe
+ if @subscriber = Subscriber.find(params[:id])
+ @subscriber.update_attributes({:unsubscribed_at => nil})
+ redirect_to :action => :list, :id => @subscriber.subscriber_list_id
+ end
+ end
+
+ def delete_subscriber
+ if @subscriber = Subscriber.find(params[:id])
+ @subscriber.destroy
+ redirect_to :action => :list, :id => @subscriber.subscriber_list_id
+ end
+ end
def export
@list = Page.find(params[:id])
@@ -44,6 +100,13 @@ def stream_csv
yield csv
}
end
-
+
+
+ def find_subscriber_list
+ @subscriber_list = Page.find(params[:subscriber_list_id])
+ redirect_to('/admin/') and return if @subscriber_list.class_name != 'SubscriberListPage'
+ rescue ActiveRecord::RecordNotFound
+ redirect_to('/admin')
+ end
end
@@ -0,0 +1,11 @@
+<%= subscriber.hidden_field :subscriber_list_id, :value => @subscriber_list.id %>
+<div class="form-area">
+ <p class="title">
+ <label for="subscriber_name">Name</label>
+ <%= subscriber.text_field :name, :class => 'textbox' %>
+ </p>
+ <p class="title">
+ <label for="subscriber_email">Email</label>
+ <%= subscriber.text_field :email, :class => 'textbox' %>
+ </p>
+</div>
@@ -0,0 +1,9 @@
+<h1>Edit subscribers</h1>
+
+<% form_for(:subscriber, :url => {:action => 'update', :id => @subscriber.id} ) do |subscriber| -%>
+ <%= render :partial => 'form', :locals => {:subscriber => subscriber} %>
+ <p class="buttons">
+ <%= submit_tag 'Update subscriber', :class => 'button' %>
+ or <%= link_to 'Cancel', :action => 'index' %>
+ </p>
+<% end -%>
@@ -4,18 +4,23 @@
<% if @subscribers and !@subscribers.empty? %>
<h2>Subscribers</h2>
+<p><%= link_to "Add a new subscriber", :action => :new, :subscriber_list_id => @list.id %></p>
<p><%= link_to "Export as CSV", :action => :export, :id => @list.id %></p>
<table class="index">
<tr>
+ <th>#id</th>
<th>email</th>
<th>name</th>
<th>Subscribed on</th>
+ <th></th>
</tr>
<%- @subscribers.each do |subscriber| -%>
<tr>
- <td><%= subscriber.email %></td>
+ <td><%= subscriber.id %></td>
+ <td><%= link_to subscriber.email, :action => "edit", :id => subscriber.id, :subscriber_list_id => @list.id %></td>
<td><%= subscriber.name.blank? ? "-" : subscriber.name %></td>
<td><%= subscriber.subscribed_at.strftime("%d %b, %Y") %></td>
+ <td><%= link_to "Unsubscribe", :action => "unsubscribe", :id => subscriber.id %></td>
</tr>
<%- end -%>
</table>
@@ -32,12 +37,16 @@
<th>email</th>
<th>name</th>
<th>Cancelled on</th>
+ <th></th>
+ <th></th>
</tr>
<%- @unsubscribers.each do |unsubscriber| -%>
<tr>
<td><%= unsubscriber.email %></td>
<td><%= unsubscriber.name.blank? ? "-" : unsubscriber.name %></td>
<td><%= unsubscriber.subscribed_at.strftime("%d %b, %Y") %></td>
+ <td><%= link_to "Resubscribe", :action => "resubscribe", :id => unsubscriber.id %></td>
+ <td><%= link_to "Destroy", :action => "delete_subscriber", :id => unsubscriber.id %></td>
</tr>
<%- end -%>
</table>
@@ -0,0 +1,9 @@
+<h1>New subscriber</h1>
+
+<% form_for(:subscriber, :url => {:action => 'create'} ) do |subscriber| -%>
+ <%= render :partial => 'form', :locals => {:subscriber => subscriber} %>
+ <p class="buttons">
+ <%= submit_tag 'Create subscriber', :class => 'button' %>
+ or <%= link_to 'Cancel', :action => 'index' %>
+ </p>
+<% end -%>
View
@@ -0,0 +1,5 @@
+✓ Add/Edit subscriber actions
+- Break up the subscribers_controller in two:
+ - one to control the subscriber_list
+ - the other for managing subscribers within a list (with nested RESTful routes)
+- Add a "Create new Subscriber list" button on /subscribers/index, which does the equivalent to the task: rake radiant:extensions:subscriber_lists:page:new

0 comments on commit 4c84364

Please sign in to comment.