Skip to content

Commit

Permalink
Refactored a lot of the memberships controller and the pending member…
Browse files Browse the repository at this point in the history
…ship messages.
  • Loading branch information
Aimee Daniells committed Apr 18, 2009
1 parent ae8d78e commit dfb30ea
Show file tree
Hide file tree
Showing 10 changed files with 120 additions and 203 deletions.
189 changes: 42 additions & 147 deletions app/controllers/memberships_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
class MembershipsController < ApplicationController

before_filter :login_required
before_filter :find_membership, :only => [:accept, :destroy]
before_filter :edit_access_required, :only => [:accept]
before_filter :delete_access_required, :only => [:destroy]

def new
@invitee = Person.find(params[:person_id])
Expand All @@ -11,170 +14,62 @@ def create
@membership = Membership.new(params[:membership])
@membership.person_id = params[:person_id]
@membership.invited = true
@membership.confirmed = false
if @membership.save
flash[:notice] = 'An invitation will be sent shortly.'
Email.new_membership_invitation(@membership)
Email.new_membership_invitation(@membership.person, @membership.team)
redirect_to(person_path(params[:person_id]))
else
render :new
end
end

def memrequest
if params[:team]
@team = Team.find(params[:team])
@person = session[:person]
validitykey = Person.sha1(@person.name + Time.now.to_s)

@membership = Membership.new(:team_id => @team.id, :person_id => @person.id, :requested => 1, :confirmed => 0, :validity_key => validitykey)
@membership.save

@membership = Membership.new(params[:membership])
@membership.person_id = session[:person].id
@membership.team_id = params[:team_id]
@membership.requested = true
@membership.confirmed = false
if @membership.save
flash[:notice] = 'Your request to join this team was noted.'
redirect_to :controller => 'teams', :action => 'show', :id => @team.id

# Notifier::deliver_memrequest(@team, @person)

# Send an email
@email = Email.new
@email.subject = "New membership request from mychores.co.uk"
@email.message = "Dear " + @team.owner_name + ",
" + @person.name + " (" + @person.login + ") has asked to join your team: " + @team.name + ".
Log into MyChores to view their profile and accept or decline this request. You'll find the links when you log in.
If you have any problems please email contact@mychores.co.uk
http://www.mychores.co.uk"
@email.to = @team.owner_email
@email.save
Email.new_membership_request(@membership.person, @membership.team)
else
flash[:notice] = 'Sorry, there was a problem requesting membership.'
end
redirect_back
end

def invite
@invitee = Person.find(params[:person])

def accept
@membership.confirmed = true
@membership.save
redirect_back
end

def meminvite
if params[:team] && params[:person]
@team = Team.find(params[:team])
@person = Person.find(params[:person])

@check = Membership.find(:first, :conditions => [ "person_id = ? AND team_id = ?", @person.id, @team.id])

if @check.nil?
validitykey = Person.sha1(@person.name + Time.now.to_s)

@membership = Membership.new(:team_id => @team.id, :person_id => @person.id, :invited => 1, :confirmed => 0, :validity_key => validitykey)
@membership.save

flash[:notice] = 'An invitation will be sent shortly.'

# Notifier::deliver_meminvite(@team, @person)

# Send an email
@email = Email.new
@email.subject = "New membership invitation from mychores.co.uk"
@email.message = "Dear " + @person.name + ",
You have been invited to join a team: " + @team.name + ".
Log into MyChores to accept or decline this invitation. You'll find the links when you log in.
If you have any problems please email contact@mychores.co.uk
http://www.mychores.co.uk"
@email.to = @person.email
@email.save

else
flash[:notice] = 'Invitation not saved. Either the person is already a member, or an invitation has already been sent.'
end
redirect_to :controller => 'people', :action => 'show', :id => @person.id
end

def destroy
@membership.destroy
redirect_back
end

def memaccept


protected

def find_membership
@membership = Membership.find(params[:id])
if @membership.validity_key == params[:key]
@membership.confirmed = 1
@membership.save
flash[:notice] = 'Membership successfully updated.'
else
flash[:notice] = 'Error: validity check failed. Membership details not updated.'
end
redirect_to :controller => 'tasks', :action => 'workload'
end

def memdecline
@membership = Membership.find(params[:id])
if @membership.validity_key == params[:key]
@membership.destroy
flash[:notice] = 'Membership declined.'
else
flash[:notice] = 'Error: validity check failed. Membership details not updated.'

def edit_access_required
if !@membership.editable_by?(session[:person])
flash[:notice] = "Sorry, you don't have permission to do that"
redirect_back
end
redirect_to :controller => 'tasks', :action => 'workload'
end

def index
redirect_to :controller => 'tasks', :action => 'workload'
end

def list
redirect_to :controller => 'tasks', :action => 'workload'
end



def remove
@membership = Membership.find(params[:id])

@person = @membership.person
@team = @membership.team

if params[:returnto] then
returnto = params[:returnto]
else
returnto = "team"
end

allowedtodelete = false

if @person.id == session[:person].id then
allowedtodelete = true # allowed to remove yourself from a team
elsif @team.person.id == session[:person].id then
allowedtodelete = true # allowed to remove someone from a team you created
end

if allowedtodelete == true then
@membership.destroy

# now we've got to find if any tasks were assigned to them in the team
@tasks = Task.find(:all, :conditions => [ "list_id in (select id from lists where team_id = ?) and person_id = ?", @team.id, @person.id ])
for task in @tasks
task.person_id = nil
# yes, i could work out the next person in the rotation, but it's complicated.
# people aren't often removed from teams so we can just turn off rotation.
task.rotate = 0
task.save
end

flash[:notice] = @person.name + " has been successfully removed from " + @team.name + "."
else
flash[:notice] = "You are not able to remove that person from that team."
returnto = "workload"
end

if returnto == "person"
# return to person
redirect_to :controller => 'people', :action => 'show', :id => @person.id
elsif returnto == "workload"
redirect_to :controller => 'tasks', :action => 'workload'
else
# return to team
redirect_to :controller => 'teams', :action => 'show', :id => @team.id

def delete_access_required
if !@membership.deletable_by?(session[:person])
flash[:notice] = "Sorry, you don't have permission to do that"
redirect_back
end
end



end
9 changes: 0 additions & 9 deletions app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -400,9 +400,6 @@ def workload
end



@messages = Membership.find_by_sql ["select * from memberships where (confirmed is null or confirmed = 0) and (person_id = ? or team_id in (select team_id from memberships where person_id = ? and confirmed = 1))", @person.id, @person.id]

@number_overdue = Task.count_by_sql ["select count(*) from tasks where status='active' and next_due < ? and list_id in (select id from lists where team_id in (select id from teams where id in (select team_id from memberships where person_id = ? and confirmed = 1))) and (person_id = ? or person_id is null)", @datetoday, @person.id, @person.id]


Expand Down Expand Up @@ -481,8 +478,6 @@ def collage



@messages = Membership.find_by_sql ["select * from memberships where (confirmed is null or confirmed = 0) and (person_id = ? or team_id in (select team_id from memberships where person_id = ? and confirmed = 1))", @person.id, @person.id]

@number_overdue = Task.count_by_sql ["select count(*) from tasks where status='active' and next_due < ? and list_id in (select id from lists where team_id in (select id from teams where id in (select team_id from memberships where person_id = ? and confirmed = 1))) and (person_id = ? or person_id is null)", @datetoday, @person.id, @person.id]


Expand Down Expand Up @@ -527,8 +522,6 @@ def matrix



@messages = Membership.find_by_sql ["select * from memberships where (confirmed is null or confirmed = 0) and (person_id = ? or team_id in (select team_id from memberships where person_id = ? and confirmed = 1))", @person.id, @person.id]

@number_overdue = Task.count_by_sql ["select count(*) from tasks where status='active' and next_due < ? and list_id in (select id from lists where team_id in (select id from teams where id in (select team_id from memberships where person_id = ? and confirmed = 1))) and (person_id = ? or person_id is null)", @datetoday, @person.id, @person.id]


Expand Down Expand Up @@ -564,7 +557,6 @@ def statistics
@refreshrate = @preference.workload_refresh
end

@messages = Membership.find_by_sql ["select * from memberships where (confirmed is null or confirmed = 0) and (person_id = ? or team_id in (select team_id from memberships where person_id = ? and confirmed = 1))", @person.id, @person.id]

@number_overdue = Task.count_by_sql ["select count(*) from tasks where status='active' and next_due < ? and list_id in (select id from lists where team_id in (select id from teams where id in (select team_id from memberships where person_id = ? and confirmed = 1))) and (person_id = ? or person_id is null)", @datetoday, @person.id, @person.id]

Expand Down Expand Up @@ -687,7 +679,6 @@ def calendar
end


@messages = Membership.find_by_sql ["select * from memberships where (confirmed is null or confirmed = 0) and (person_id = ? or team_id in (select team_id from memberships where person_id = ? and confirmed = 1))", @person.id, @person.id]

@number_overdue = Task.count_by_sql ["select count(*) from tasks where status='active' and next_due < ? and list_id in (select id from lists where team_id in (select id from teams where id in (select team_id from memberships where person_id = ? and confirmed = 1))) and (person_id = ? or person_id is null)", @datetoday, @person.id, @person.id]

Expand Down
24 changes: 20 additions & 4 deletions app/models/email.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,34 @@
class Email < ActiveRecord::Base

def self.new_membership_invitation(membership)
def self.new_membership_invitation(person, team)
@email = Email.new
@email.subject = "New membership invitation from mychores.co.uk"
@email.message = "Dear #{membership.person_name},
@email.message = "Dear #{person.name},
You have been invited to join a team: #{membership.team_name}.
You have been invited to join a team: #{team.name}.
Log into MyChores to accept or decline this invitation. You'll find the links when you log in.
If you have any problems please email contact@mychores.co.uk
http://www.mychores.co.uk"
@email.to = membership.person_email
@email.to = person.email
@email.save
end

def self.new_membership_request(person, team)
@email = Email.new
@email.subject = "New membership request from mychores.co.uk"
@email.message = "Dear #{team.owner_name},
#{person.name} (#{person.login}) has asked to join your team: #{team.name}.
Log into MyChores to view their profile and accept or decline this request. You'll find the links when you log in.
If you have any problems please email contact@mychores.co.uk
http://www.mychores.co.uk"
@email.to = team.owner_email
@email.save
end

Expand Down
11 changes: 11 additions & 0 deletions app/models/membership.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ class Membership < ActiveRecord::Base
belongs_to(:team) # ... of teams

named_scope :confirmed, :conditions => {:confirmed => true}
named_scope :unconfirmed, :conditions => {:confirmed => false}

def person_name
return '' if person.nil?
Expand All @@ -19,5 +20,15 @@ def team_name
return '' if team.nil?
team.name
end

def editable_by?(person)
return true if self.person == person && invited?
team.member?(person)
end

def deletable_by?(person)
return true if self.person == person
team.member?(person)
end

end
9 changes: 7 additions & 2 deletions app/models/person.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ class Person < ActiveRecord::Base

has_many(:completions) # has completed tasks
has_many(:memberships) # may be a member of teams
has_many(:tasks) # may have task assignments
has_many(:teams) # is owner/creator of teams
has_many(:invitations) # may have sent invitations to many people
has_many(:tips) # may have contributed one or more tips
has_many(:pictures) # any pictures this person has uploaded
Expand Down Expand Up @@ -305,6 +303,13 @@ def fellow_team_members
confirmed_teams.map(&:confirmed_members).flatten.uniq - [self]
end

def outstanding_memberships
memberships.unconfirmed
end

def team_outstanding_memberships
Membership.find(:all, :conditions => {:confirmed => false, :team_id => memberships.confirmed.map(&:team_id)})
end



Expand Down
4 changes: 4 additions & 0 deletions app/models/team.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def owner_email
return 'contact@mychores.co.uk' if person.nil?
person.email
end

def outstanding_memberships
Membership.find(:all, :conditions => {:confirmed => false, :team_id => id})
end


protected
Expand Down
30 changes: 30 additions & 0 deletions app/views/tasks/_messages.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
- session[:person].outstanding_memberships.each do |membership|
- if membership.invited?
%p
You have been invited to join
= link_to_team(membership.team, 'picturelink')
= link_to('Accept', accept_membership_path(membership), :method => :put, :class => 'action')
|
= link_to('Decline', membership_path(membership), :method => :delete, :class => 'action')

- if membership.requested?
%p
Your request to join
= link_to_team(membership.team, 'picturelink')
is pending

- session[:person].team_outstanding_memberships.each do |membership|
- if membership.invited?
%p
= link_to_person(membership.person, 'picturelink')
has not yet joined
= link_to_team(membership.team, 'picturelink')

- if membership.requested?
%p
= link_to_person(membership.person, 'picturelink')
wants to join
= link_to_team(membership.team, 'picturelink')
= link_to('Accept', accept_membership_path(membership), :method => :put, :class => 'action')
|
= link_to('Decline', membership_path(membership), :method => :delete, :class => 'action')

0 comments on commit dfb30ea

Please sign in to comment.