Permalink
Browse files

Created a subscriber_list_page, which can process posted subscription…

… requests. Not yet unsubscription requests.

Generated a basic subscriber model, with validation for email format and presence of subscriber_list_id.
Created a rake task for creating a Subscriber List page with the necessary page parts:
* body
* subscribe
* subscribed
* unsubscribe
* unsubscribed
  • Loading branch information...
1 parent ba19476 commit d345b8cf48e8bda33ab731881e686bb237f9ded0 @nelstrom committed Jun 14, 2008
View
@@ -0,0 +1,10 @@
+class Subscriber < ActiveRecord::Base
+ validates_presence_of :email
+ # validates_uniqueness_of :email
+ validates_presence_of :subscriber_list_id
+
+ #TODO: add validation to newsletter.Does the newsletter page exists?
+
+ validates_format_of :email, :with => /^$|^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :if => Proc.new{|ns| !ns.email.blank? }
+
+end
@@ -0,0 +1,82 @@
+class SubscriberListPage < Page
+
+ ACTIONS = {
+ :public => %w[ subscribe activate unsubscribe confirm_unsubscription ],
+ :private => %w[ subscribed activated unsubscribed ]
+ }
+
+ def find_by_url(url, live = true, clean = false)
+ url = clean_url(url) if clean
+ if url =~ /^#{self.url}(#{ACTIONS[:public].join("|")})\/([a-zA-Z0-9]{40,})?\/?$/
+ @subscriber_list_action = $1
+ @code = $2
+ self
+ else
+ super
+ end
+ end
+
+ def cache?
+ false
+ end
+
+ def process(request, response)
+ @request, @response = request, response
+
+ if request.post?
+ if @subscriber_list_action == 'subscribe'
+ parameters = request.parameters[:subscriber] || {}
+ parameters[:subscriber_list_id] = self.id
+ parameters[:subscribed_at] = Time.now
+ @subscriber = Subscriber.new
+ if @subscriber.update_attributes(parameters)
+ @subscriber_list_action = 'subscribed'
+ else
+ @subscriber_list_errors = true
+ end
+
+ elsif @subscriber_list_action == 'unsubscribe'
+ # todo: process unsubscription
+ end
+ else # request.get?
+ if @subscriber_list_action == 'unsubscribe'
+
+ end
+ end
+
+ super(request, response)
+ end
+
+
+ tag 'subscriber_list' do |tag|
+ tag.expand
+ end
+
+ tag 'subscriber_list:unless_actions' do |tag|
+ tag.expand unless (ACTIONS[:private] + ACTIONS[:public]).include?(@subscriber_list_action)
+ end
+
+ tag 'subscriber_list:if_actions' do |tag|
+ tag.expand if (ACTIONS[:private] + ACTIONS[:public]).include?(@subscriber_list_action)
+ end
+
+ tag 'subscriber_list:if_form_errors' do |tag|
+ tag.expand if @subscriber_list_errors
+ end
+
+ # todo: tag 'subscriber_list:form_errors'
+ # tag 'subscriber_list:form_errors:each'
+ # tag 'subscriber_list:form_errors:each:error_message'
+
+
+ tag 'subscriber_list:unless_action' do |tag|
+ action_name = tag.attr['name']
+ tag.expand unless action_name == @subscriber_list_action
+ end
+
+ tag 'subscriber_list:if_action' do |tag|
+ action_name = tag.attr['name']
+ tag.expand if action_name == @subscriber_list_action
+ end
+
+end
@@ -0,0 +1,17 @@
+class CreateSubscribers < ActiveRecord::Migration
+ def self.up
+ create_table :subscribers do |t|
+ t.string :email
+ t.string :name
+ t.integer :subscriber_list_id
+ t.datetime :subscribed_at
+ t.datetime :unsubscribed_at
+
+ t.timestamps
+ end
+ end
+
+ def self.down
+ drop_table :subscribers
+ end
+end
@@ -0,0 +1,58 @@
+require 'yaml'
+
+namespace :radiant do
+ namespace :extensions do
+ namespace :subscriber_lists do
+ namespace :page do
+ # rake radiant:extensions:subscriber_lists:page:new
+ desc %Q{Creates a new Subscriber List page, with all necessary page parts
+ You can set the parent for the Subscriber List page by passing PARENT_ID= id_of_parent_page. By default, it will use the home page as parent.
+ }
+ task :new => :environment do
+ pages_fixture = File.join(SubscriberListsExtension.root, 'spec', 'fixtures', 'pages.yml')
+ page_parts_fixture = File.join(SubscriberListsExtension.root, 'spec', 'fixtures', 'page_parts.yml')
+ pages = YAML.load_file(pages_fixture)
+ page_parts = YAML.load_file(page_parts_fixture)
+ newsletter_page_attributes = pages["newsletter"]
+
+ newsletter_page_attributes["parent_id"] = ENV["PARENT_ID"] if ENV["PARENT_ID"]
+ newsletter_page_attributes["title"] = ENV["TITLE"] if ENV["TITLE"]
+ if ENV["SLUG"]
+ newsletter_page_attributes["slug"] = ENV["SLUG"]
+ else
+ newsletter_page_attributes["slug"] = newsletter_page_attributes["title"].downcase.gsub(/[^-a-z0-9~\s\.:;+=_]/, '').strip.gsub(/[\s\.:;=+]+/, '-')
+ end
+
+ if ENV["BREADCRUMB"]
+ newsletter_page_attributes["breadcrumb"] = ENV["BREADCRUMB"]
+ else
+ newsletter_page_attributes["breadcrumb"] = newsletter_page_attributes["title"]
+ end
+
+ page = Page.new
+ if page.update_attributes(newsletter_page_attributes)
+ puts "Page has been created succesfully"
+ puts "Creating page parts..."
+ %w[
+ newsletter_body
+ newsletter_subscribe newsletter_subscribed
+ newsletter_unsubscribe newsletter_unsubscribed
+ ].each do |part_name|
+ print '.'
+ page_part_attributes = page_parts[part_name]
+ page_part_attributes["page_id"] = page.id
+ page.parts.create(page_part_attributes)
+ end
+ puts;puts "Page parts have been created"
+ else
+ puts "There are some errors:"
+ page.errors.each{|k, v| puts " * #{v}"}
+ end
+
+
+ end
+
+ end
+ end
+ end
+end
@@ -22,7 +22,7 @@ namespace :radiant do
mkdir_p RAILS_ROOT + directory
cp file, RAILS_ROOT + path
end
- end
+ end
end
end
end
@@ -0,0 +1,61 @@
+home_body:
+ id: 1
+ name: body
+ content: This is the home page
+ page_id: 1
+newsletter_body:
+ id: 2
+ name: body
+ content: |
+ <r:subscriber_list:unless_actions>
+ Subscribe to our newsletter:
+ <r:content part="subscribe" />
+ </r:subscriber_list:unless_actions>
+
+ <r:subscriber_list:if_actions>
+ <r:subscriber_list:if_action name="subscribe">
+ <r:content part="subscribe" />
+ </r:subscriber_list:if_action>
+
+ <r:subscriber_list:if_action name="subscribed">
+ <r:content part="subscribed" />
+ </r:subscriber_list:if_action>
+
+ <r:subscriber_list:if_action name="unsubscribe">
+ <r:content part="unsubscribe" />
+ </r:subscriber_list:if_action>
+
+ <r:subscriber_list:if_action name="unsubscribed">
+ <r:content part="unsubscribed" />
+ </r:subscriber_list:if_action>
+
+ </r:subscriber_list:if_actions>
+ page_id: 2
+newsletter_subscribe:
+ id: 3
+ name: subscribe
+ content: |
+ <r:subscriber_list:if_form_errors>
+ All fields are required
+ </r:subscriber_list:if_form_errors>
+ <form action="/newsletter/subscribe/" method="post"><input type="text" name="subscriber[email]" /><input type="submit" value="Join"/></form>
+ page_id: 2
+newsletter_subscribed:
+ id: 4
+ name: subscribed
+ content: The email address EMAIL_ADDRESS has been successfully added to our mailing list. Thanks for your interest.
+ page_id: 2
+newsletter_unsubscribe:
+ id: 7
+ name: unsubscribe
+ content: |
+ <r:subscriber_list:if_form_errors>
+ Sorry, there is no active subscriber with that email address
+ </r:subscriber_list:if_form_errors>
+ <form action="/newsletter/unsubscribe/" method="post"><input type="text" name="subscriber[email]" /><input type="submit" value="Remove me"/></form>
+ page_id: 2
+newsletter_unsubscribed:
+ id: 8
+ name: unsubscribed
+ content: You will receive an email with your unsubscription code
+ page_id: 2
View
@@ -0,0 +1,17 @@
+homepage:
+ id: 1
+ title: Home Page
+ breadcrumb: Home
+ slug: /
+ status_id: 100
+ parent_id:
+ published_at: <%= 1.day.ago.to_s :db %>
+newsletter:
+ id: 2
+ title: Newsletter
+ breadcrumb: newsletter
+ slug: newsletter
+ status_id: 100
+ parent_id: 1
+ class_name: SubscriberListPage
+ published_at: <%= 1.day.ago.to_s :db %>
@@ -0,0 +1,11 @@
+require File.dirname(__FILE__) + '/../spec_helper'
+
+describe Subscriber do
+ before(:each) do
+ @subscriber = Subscriber.new
+ end
+
+ it "should be valid" do
+ @subscriber.should be_valid
+ end
+end
@@ -11,11 +11,12 @@ class SubscriberListsExtension < Radiant::Extension
# end
def activate
- # admin.tabs.add "Subscriber Lists", "/admin/subscriber_lists", :after => "Layouts", :visibility => [:all]
+ SubscriberListPage
+ admin.tabs.add "Subscribers", "/admin/subscriber_lists", :after => "Layouts", :visibility => [:all]
end
def deactivate
- # admin.tabs.remove "Subscriber Lists"
+ admin.tabs.remove "Subscribers"
end
end

0 comments on commit d345b8c

Please sign in to comment.