Skip to content

Commit

Permalink
[Foundation] Changed profiles, added public profiles, bugfixed, updat…
Browse files Browse the repository at this point in the history
…ed tests.

* Changed profile to not track email, and consequently rewrote referral handling
* Fixed invites to work properly * Added friendly_id migration and hooked it up for users
* Added options for public profile in the settings
* Fixed up specs and features, added new specs for public profiles
* Updated the frozen new relic gem
* Updated the todo list
  • Loading branch information
queso committed Sep 19, 2009
1 parent 2fe69bc commit 73a4e90
Show file tree
Hide file tree
Showing 185 changed files with 20,271 additions and 151 deletions.
7 changes: 6 additions & 1 deletion TODO.textile
@@ -1,3 +1,8 @@
h1. Todo list

* Add breadcrumb support
* Add breadcrumb support (AS, please confirm you want this)
* Add invite cucumber features
* User generation with temp passwords
* Remove profiles tab in admin, add referrals tab
* Add admin controller/views generator
* Fix form builder so that it doesn't pass bogus html attributes
4 changes: 2 additions & 2 deletions app/controllers/invites_controller.rb
Expand Up @@ -7,10 +7,10 @@ def new
end

def create
@invite = Invite.create(:email => params[:invite][:email], :approved => false)
@invite = Invite.new(:email => params[:invite][:email], :approved => false)
@invite.add_inviter(current_user) if signed_in?
respond_to do |format|
if @invite.errors.empty?
if @invite.save
flash[:notice] = "Your invite request has been sent to a site admin." if @invite.unapproved?
flash[:notice] = "Your invite has been sent" if @invite.approved?
format.html { redirect_to root_url }
Expand Down
11 changes: 6 additions & 5 deletions app/controllers/referrals_controller.rb
@@ -1,19 +1,20 @@
class ReferralsController < ApplicationController
before_filter :enabled?

# layout "sessions.html.haml"
before_filter :require_user

def index
redirect_to new_referral_path
end

def new
@referrer_profile = current_user.profile if signed_in?
@referral = Referral.new(:referrer => current_user) if signed_in?
end

def create
referrer = Profile.find_or_create_by_email(:email => params[:referral][:email], :first_name => params[:referral][:first_name], :last_name => params[:referral][:last_name])
referrer.add_referrals(params[:referral][:friends_email], params[:referral][:email_text])
params[:referral][:email_list].split().each do |email|
Referral.create(:email_address => email, :email_text => params[:referral][:email_text], :referrer => current_user)
end
flash[:notice] = "Your emails have been sent."
redirect_to root_url
end

Expand Down
15 changes: 13 additions & 2 deletions app/controllers/users_controller.rb
@@ -1,6 +1,7 @@
class UsersController < ApplicationController
before_filter :require_no_user, :only => [:new, :create]
before_filter :require_user, :only => [:show, :edit, :update]
before_filter :require_user, :only => [:edit, :update]
before_filter :check_for_public_profiles, :only => :show

def new
@user = User.new
Expand All @@ -17,7 +18,11 @@ def create
end

def show
@user = @current_user
if params[:id].blank?
@user = current_user
else
@user = User.find(params[:id])
end
end

def edit
Expand All @@ -34,4 +39,10 @@ def update
end
end

private

def check_for_public_profiles
require_user unless @site.public_profiles?
end

end
2 changes: 1 addition & 1 deletion app/models/invite.rb
Expand Up @@ -4,7 +4,7 @@ class Invite < ActiveRecord::Base
belongs_to :user, :class_name => "User", :foreign_key => "user_id"

validates_presence_of :email, :on => :create, :message => "can't be blank"
validates_uniqueness_of :email, :on => :create, :message => "must be unique"
validates_uniqueness_of :email, :on => :create, :message => "has already been invited"
validate :ensure_new_user
validate :ensure_inviter_has_invites, :if => Proc.new {|invite| !invite.inviter_id.nil?}

Expand Down
57 changes: 25 additions & 32 deletions app/models/profile.rb
Expand Up @@ -2,13 +2,6 @@ class Profile < ActiveRecord::Base
liquid_methods :email

belongs_to :user
belongs_to :referral, :class_name => "Profile", :foreign_key => "referral_id"

validates_uniqueness_of :email, :case_sensitive => false
validates_presence_of :email
validates_length_of :email, :within => 3..100

named_scope :referrers, :conditions => {:referral_id => nil}

has_attached_file :avatar, :styles => {:thumb => "16x16>", :small => "48x48>", :large => "100x100>", :xlarge => "150x150>"}, :default_url => "/images/foundation/default_:style_avatar.png"

Expand All @@ -17,30 +10,30 @@ def fullname
"#{first_name} #{last_name}"
end

def add_referrals(friends_list, email_text)
emails = Profile.parse_friends_email(friends_list)
referrals = emails.collect do |email|
Profile.find_or_create_by_email(:email => email, :referral => self)
end
Profile.send_referral_emails(self, referrals, email_text)
referrals
end

def self.parse_friends_email(email_list = "")
if email_list.include?("\r\n")
emails = email_list.split(/\r\n/)
else
emails = email_list.split(/,/)
end
emails
end

def self.send_referral_emails(sender, referrals, text)
referrals.each do |profile|
ReferralMailer.send_later(:deliver_referral, profile, text)
end
ReferralMailer.send_later(:deliver_confirmation, sender)
ReferralMailer.send_later(:deliver_admin_confirmation, sender, referrals)
end
# def add_referrals(friends_list, email_text)
# emails = Profile.parse_friends_email(friends_list)
# referrals = emails.collect do |email|
# #Profile.find_or_create_by_email(:email => email, :referral => self)
# end
# Profile.send_referral_emails(self, referrals, email_text)
# referrals
# end
#
# def self.parse_friends_email(email_list = "")
# if email_list.include?("\r\n")
# emails = email_list.split(/\r\n/)
# else
# emails = email_list.split(/,/)
# end
# emails
# end
#
# def self.send_referral_emails(sender, referrals, text)
# referrals.each do |profile|
# ReferralMailer.send_later(:deliver_referral, profile, text)
# end
# ReferralMailer.send_later(:deliver_confirmation, sender)
# ReferralMailer.send_later(:deliver_admin_confirmation, sender, referrals)
# end

end
28 changes: 28 additions & 0 deletions app/models/referral.rb
@@ -0,0 +1,28 @@
class Referral < ActiveRecord::Base
attr_accessor :email_list

#Associations
belongs_to :referrer, :class_name => "User", :foreign_key => "referrer_id"

#Validations
validates_format_of :email_address, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
validates_uniqueness_of :email_address, :on => :create, :message => "has already been invited to the site"
validates_presence_of :email_text
validate :email_address_isnt_a_user

#Callbacks
after_create :send_referral_emails


private

def send_referral_emails
ReferralMailer.send_later(:deliver_referral, email_address, email_text)
ReferralMailer.send_later(:deliver_confirmation, referrer.email, email_text) if referrer.email
end

def email_address_isnt_a_user
errors.add("email_address", "has already signed up for the site") if User.find_by_email(email_address)
end

end
20 changes: 10 additions & 10 deletions app/models/referral_mailer.rb
@@ -1,20 +1,20 @@
class ReferralMailer < Mailer

def referral(profile, body)
setup_template('referral', profile.email) do |options|
def referral(email, body)
setup_template('referral', email) do |options|
options['note'] = body
end
end

def confirmation(profile)
setup_template('confirmation', profile.email)
def confirmation(email)
setup_template('confirmation', email)
end

def admin_confirmation(profile, referrals)
setup_template('admin_confirmation', SiteSetting.first.admin_email) do |options|
options['referrer'] = profile
options['referrals'] = referrals
end
end
# def admin_confirmation(profile, email_address)
# setup_template('admin_confirmation', SiteSetting.first.admin_email) do |options|
# options['referrer'] = profile
# options['referrals'] = referrals
# end
# end

end
26 changes: 25 additions & 1 deletion app/models/user.rb
@@ -1,15 +1,31 @@
class User < ActiveRecord::Base
acts_as_authentic
acts_as_authentic do
login_field :email
validate_login_field :false
end

has_friendly_id :login, :use_slug => true

liquid_methods :display_name

attr_protected :admin

#Validations
validates_uniqueness_of :email, :case_sensitive => false
validates_presence_of :email
validates_presence_of :login
validates_length_of :email, :within => 3..100

#Associations
has_one :profile

#Callbacks
before_validation_on_create :make_login
before_create :make_first_admin
after_create :create_profile

#Nested Attribuets
accepts_nested_attributes_for :profile, :allow_destroy => true

def self.invite_count
User.sum(:invites)
Expand All @@ -19,6 +35,14 @@ def make_first_admin
self.admin = true if first_user?
end

# def create_profile
# profile.create
# end

def make_login
self.login = self.email.split("@")[0] if self.login.blank? && !self.email.blank?
end

def first_user?
User.count == 0
end
Expand Down
1 change: 1 addition & 0 deletions app/views/admin/site_settings/edit.html.haml
Expand Up @@ -12,6 +12,7 @@
= f.text_field :admin_email, :label => "Outgoing email address", :example => "hello@app.gethandcrafted.com"
= f.check_box :user_avatars, :label => "Allow users to upload avatar pictures?"
= f.check_box :beta_invites, :label => "Enable beta invites?"
= f.check_box :public_profiles, :label => "Enable public profile viewing?"
= f.check_box :referrals, :label => "Enable tell a friend?"
= f.submit "Update" do
- link_to "Cancel", admin_dashboard_url, :class => "cancel"
5 changes: 5 additions & 0 deletions app/views/invites/_invite.html.haml
@@ -0,0 +1,5 @@
- form_for(Invite.new, :url => invites_path, :builder => HandcraftedFormBuilder ) do |f|
- field_set_tag do
= f.text_field :email
= f.submit "Request Invite" do
- link_to "Cancel", root_url, :class => "red"
6 changes: 1 addition & 5 deletions app/views/invites/new.html.haml
Expand Up @@ -9,8 +9,4 @@

#page_body.wide
= error_messages_for :invite
- form_for(@invite, :url => invites_path, :builder => HandcraftedFormBuilder ) do |f|
- field_set_tag do
= f.text_field :email
= f.submit "Request Invite" do
- link_to "Cancel", root_url, :class => "red"
= render :partial => "invite"
12 changes: 7 additions & 5 deletions app/views/referrals/new.html.haml
Expand Up @@ -6,10 +6,12 @@

#page_body.wide
- form_for :referral, :url => referrals_url, :builder => HandcraftedFormBuilder do |f|
- field_set_tag do
= f.first_last :first_name, :last_name
= f.text_field :email, :example => "johnsmith@gmail.com"
= f.text_area :friends_email, :rows => 5
%fieldset
- unless signed_in?
= f.first_last :first_name, :last_name
= f.text_field :email, :example => "johnsmith@gmail.com"
= f.text_area :email_list, :rows => 5, :label => "Emails"
= f.text_area :email_text
= f.submit "Tell your friends" do
- link_to "Cancel", root_url, :class => "red"
- link_to "Cancel", root_url, :class => "red"

2 changes: 1 addition & 1 deletion app/views/shared/_notice_invites.html.haml
@@ -1,4 +1,4 @@
- if @site.beta_invites?
- if @site.beta_invites? && signed_in?
.notice.invites
.padding
%h4
Expand Down
1 change: 1 addition & 0 deletions app/views/users/edit.html.haml
Expand Up @@ -7,6 +7,7 @@
- form_for :user, :url => user_path(@user), :builder => HandcraftedFormBuilder, :html => {:method => :put, :multipart => true} do |f|
- field_set_tag do
= f.text_field :email, :example => "johnsmith@gmail.com"
= f.text_field :login, :label => "Profile url", :example => "#{@site.url}/profile_url"
= f.time_zone_select :time_zone, ActiveSupport::TimeZone.us_zones
- fields_for @user.profile, :builder => HandcraftedFormBuilder do |p|
= p.text_field :first_name
Expand Down
1 change: 1 addition & 0 deletions app/views/users/new.html.haml
Expand Up @@ -10,6 +10,7 @@
%fieldset
%legend Sign up for an Account
= f.text_field :email, :example => "johnsmith@gmail.com"
= f.text_field :login, :label => "Profile url"
= f.password_field :password
= f.password_field :password_confirmation
= f.submit "Sign up" do
Expand Down
11 changes: 6 additions & 5 deletions app/views/users/show.html.haml
@@ -1,14 +1,15 @@
#page_header
%h2{:style => "position: relative;"}
= @user.email
- if false#@site.user_avatar_upload?
- if @site.user_avatars?
= image_tag(@user.profile.avatar.url(:thumb), :style => "position: absolute; top: 0; right: 0;")

#page_body
%p
Click here to
= link_to "edit your account", edit_account_url
- if signed_in? && current_user == @user
Click here to
= link_to "edit your account", edit_account_url

-if false#@site.beta_invites?
-if @site.beta_invites? && signed_in?
= render :partial => "shared/notice_invites"
= render :partial => "invite"
= render :partial => "invites/invite"
2 changes: 1 addition & 1 deletion config/routes.rb
Expand Up @@ -24,7 +24,7 @@
map.signin "/signin", :controller => "user_sessions", :action => "new"
# APP MARKER - Place app specific routes below this line

map.static_page "/:id", :controller => "pages", :action => "show"

map.root :page
map.profile "/:id", :controller => "users", :action => "show"
end
1 change: 1 addition & 0 deletions db/migrate/20090709041139_create_users.rb
@@ -1,6 +1,7 @@
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :login
t.string :email, :null => false # optional, you can use login instead, or both
t.string :crypted_password, :null => false # optional, see below
t.string :password_salt, :null => false # optional, but highly recommended
Expand Down

0 comments on commit 73a4e90

Please sign in to comment.