diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 82a8f7cd1d552aa9c4d55099f42f040919970fb0..65b57b2526f279ad4203025afcea735d50e8b2e1 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -9,6 +9,7 @@ class ApplicationController < ActionController::Base before_filter :set_friends_and_status, :except => [:create, :update] before_filter :count_requests before_filter :fb_user_info + before_filter :set_invites layout :layout_by_resource @@ -37,6 +38,12 @@ class ApplicationController < ActionController::Base @request_count = Request.for_user(current_user).size if current_user end + def set_invites + if current_user + @invites = current_user.invites + end + end + def fb_user_info if current_user @access_token = warden.session[:access_token] diff --git a/app/controllers/invitations_controller.rb b/app/controllers/invitations_controller.rb index e7b2c02626d90b76248faf3fa4a26688c1355907..92da135d8f95f2266f22b363a08922c0a1ddb13c 100644 --- a/app/controllers/invitations_controller.rb +++ b/app/controllers/invitations_controller.rb @@ -4,14 +4,19 @@ class InvitationsController < Devise::InvitationsController def create - self.resource = current_user.invite_user(params[resource_name]) - - if resource.errors.empty? - set_flash_message :notice, :send_instructions#, :email => self.resource.email - redirect_to after_sign_in_path_for(resource_name) - else - render_with_scope :new + begin + self.resource = current_user.invite_user(params[resource_name]) + flash[:notice] = I18n.t 'invitations.create.sent' + rescue RuntimeError => e + if e.message == "You have no invites" + flash[:error] = I18n.t 'invitations.create.no_more' + elsif e.message == "You already invited this person" + flash[:error] = I18n.t 'invitations.create.already_sent' + else + raise e + end end + redirect_to after_sign_in_path_for(resource_name) end def update diff --git a/app/models/user.rb b/app/models/user.rb index 152591f1f8b107d2b05cebf775a5c49200f45ef6..795d610cbb088941933d1bf4a0892879a141b001 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -27,6 +27,7 @@ class User key :username, :unique => true key :serialized_private_key, String + key :invites, Integer, :default => 5 key :invitation_token, String key :invitation_sent_at, DateTime key :inviter_ids, Array @@ -266,17 +267,25 @@ class User ###Invitations############ def invite_user( opts = {} ) - invited_user = User.invite!(:email => opts[:email], :inviter => self) - #invited_user.inviters << self - #invited_user.save! - invited_user + if self.invites > 0 + invited_user = User.invite!(:email => opts[:email], :inviter => self) + self.invites = self.invites - 1 + self.save! + invited_user + else + raise "You have no invites" + end end def self.invite!(attributes={}) inviter = attributes.delete(:inviter) invitable = find_or_initialize_with_error_by(:email, attributes.delete(:email)) invitable.attributes = attributes - invitable.inviters << inviter + if invitable.inviters.include?(inviter) + raise "You already invited this person" + else + invitable.inviters << inviter + end if invitable.new_record? invitable.errors.clear if invitable.email.try(:match, Devise.email_regexp) diff --git a/app/views/aspects/manage.html.haml b/app/views/aspects/manage.html.haml index 85296669f75171b60015ee56d4adf6b6c2748d97..472d8241fb833598b1b7eb6af17b476ffc2bb92d 100644 --- a/app/views/aspects/manage.html.haml +++ b/app/views/aspects/manage.html.haml @@ -26,6 +26,7 @@ %li.grey Drag to ignore/remove %h3= link_to "Invite a friend!", "#invite_user_pane", :id => "invite_user_button", :class => "invite_user_button", :title => "Invite a friend" + %h3= "You have #{@invites} invites." .yo{ :style => "display:none;"} #invite_user_pane diff --git a/app/views/devise/mailer/invitation.html.haml b/app/views/devise/mailer/invitation.html.haml index a6c82a3b0468e15d273c46174f92408fc64748f9..03b6a5d2665ec801f5be61cd3788a97077343a7b 100644 --- a/app/views/devise/mailer/invitation.html.haml +++ b/app/views/devise/mailer/invitation.html.haml @@ -1,7 +1,7 @@ %p Hello #{@resource.email}! %p - #{(@resource.inviters.count == 1)? ( @resource.inviters.first.real_name " has") : (@resource.inviters.map{|inv| inv.real_name}.join(",") + " have")} invited you to #{root_url}, you can accept it through the link below. + #{(@resource.inviters.count == 1)? ( @resource.inviters.first.real_name + " has") : (@resource.inviters.map{|inv| inv.real_name}.join(",") + " have")} invited you to #{root_url}, you can accept it through the link below. %p= link_to 'Accept invitation', accept_invitation_url(@resource, :invitation_token => @resource.invitation_token) %p If you don't want to accept the invitation, please ignore this email. diff --git a/app/views/shared/_aspect_friends.haml b/app/views/shared/_aspect_friends.haml index 343272dff25ad8dda21bbd2a499465ea14388f36..a37068577829ac7bae60a078ff96f003993ed3ac 100644 --- a/app/views/shared/_aspect_friends.haml +++ b/app/views/shared/_aspect_friends.haml @@ -24,6 +24,8 @@ %br = link_to "Invite a friend!", "#invite_user_pane", :id => "invite_user_button", :class => "invite_user_button", :title => "Invite a friend" +%br += "You have #{@invites} invites." .yo{ :style => "display:none;"} #invite_user_pane = render "invitations/new" diff --git a/config/locales/diaspora/en.yml b/config/locales/diaspora/en.yml index 554f8bcb8ce16dda05e64abf05f079b7ea951eb0..1dd33bb3f97eeaf53ef359afa81b6b70003db2a9 100644 --- a/config/locales/diaspora/en.yml +++ b/config/locales/diaspora/en.yml @@ -154,6 +154,14 @@ en: sign_up: "Sign up" create: success: "You've joined Diaspora!" + invitations: + create: + sent: 'Your invitation has been sent.' + no_more: 'You have no more invitations.' + already_sent: 'You already invited this person.' + invitation_token_invalid: 'The invitation token provided is not valid!' + updated: 'Your password was set successfully. You are now signed in.' + status_messages: new_status_message: tell_me_something_good: "tell me something good" diff --git a/spec/controllers/invitations_controller_spec.rb b/spec/controllers/invitations_controller_spec.rb deleted file mode 100644 index ff87b4bcc8e08fd30e8268e8b9587db5a07a3f27..0000000000000000000000000000000000000000 --- a/spec/controllers/invitations_controller_spec.rb +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright (c) 2010, Diaspora Inc. This file is -# licensed under the Affero General Public License version 3 or later. See -# the COPYRIGHT file. - -require 'spec_helper' - -describe InvitationsController do - render_views - let(:user) {Factory.create :user} - - before do - sign_in :user, user - end - - context 'inviting another user' do - it 'should create an invited user and add keep track of an invitor' do - debugger - params = {"user" => {"email" => "test@example.com"}} - post :create, params - #invitee = inviter.invite_user(:email => "test@example.com") - #invitee.inviters.includes?(inviter).should be true - end - end - -end diff --git a/spec/models/user/invite_spec.rb b/spec/models/user/invite_spec.rb index 25d08e89767941b8a7e30fd940e24f49fe7f3439..117bb5b37c8401c7be92a97d66459b4142ca25dc 100644 --- a/spec/models/user/invite_spec.rb +++ b/spec/models/user/invite_spec.rb @@ -6,7 +6,11 @@ require 'spec_helper' describe User do let(:inviter) {Factory.create :user} - let!(:invited_user) { create_user_with_invitation("abc", :email => "email@example.com", :inviter => inviter)} + let(:inviter_with_3_invites) {Factory.create :user, :invites => 3} + let!(:invited_user) { create_user_with_invitation("abc", :email => "email@example.com", :inviter => inviter)} + let(:invited_user1) { create_user_with_invitation("abc", :email => "email@example.com", :inviter => inviter_with_3_invites)} + let(:invited_user2) { create_user_with_invitation("abc", :email => "email@example.com", :inviter => inviter_with_3_invites)} + let(:invited_user3) { create_user_with_invitation("abc", :email => "email@example.com", :inviter => inviter_with_3_invites)} context "creating invites" do it 'should invite the user' do @@ -23,6 +27,23 @@ describe User do end end + context "limit on invites" do + it 'does not invite users after 3 invites' do + User.stub!(:invite!).and_return(invited_user1,invited_user2,invited_user3) + inviter_with_3_invites.invite_user(:email => "email1@example.com") + inviter_with_3_invites.invite_user(:email => "email2@example.com") + inviter_with_3_invites.invite_user(:email => "email3@example.com") + proc{inviter_with_3_invites.invite_user(:email => "email4@example.com")}.should raise_error /You have no invites/ + end + + it 'does not invite people I already invited' do + pending "this is really weird to test without the actual method working" + User.stub!(:invite!).and_return(invited_user1,invited_user1) + inviter_with_3_invites.invite_user(:email => "email1@example.com") + proc{inviter_with_3_invites.invite_user(:email => "email1@example.com")}.should raise_error /You already invited that person/ + end + end + context "the acceptance of an invitation" do it "should create the person with the passed in params" do person_count = Person.count