From 594c9b02b19910d562a8a11d22cd9af3678bd255 Mon Sep 17 00:00:00 2001
From: Ilyaaaaaaaaaaaaa Zhitomirskiy <ilya@joindiaspora.com>
Date: Mon, 11 Jul 2011 12:20:22 -0700
Subject: [PATCH] A admin is now able to allow open invitations

---
 app/controllers/invitations_controller.rb     |  4 +--
 app/controllers/services_controller.rb        |  2 +-
 app/views/aspects/index.html.haml             |  7 +++---
 app/views/shared/_invitations.haml            | 25 ++++++++-----------
 config/application.yml.example                |  5 ++++
 .../invitations_controller_spec.rb            | 22 ++++++++++++----
 spec/controllers/services_controller_spec.rb  | 18 +++++++++++++
 7 files changed, 58 insertions(+), 25 deletions(-)

diff --git a/app/controllers/invitations_controller.rb b/app/controllers/invitations_controller.rb
index 02899e3692..5e8423ae48 100644
--- a/app/controllers/invitations_controller.rb
+++ b/app/controllers/invitations_controller.rb
@@ -12,7 +12,7 @@ class InvitationsController < Devise::InvitationsController
   end
 
   def create
-      if current_user.invites == 0
+    if !AppConfig[:open_invitations] && current_user.invites == 0
         flash[:error] = I18n.t 'invitations.create.no_more'
         redirect_to :back
         return
@@ -34,7 +34,7 @@ class InvitationsController < Devise::InvitationsController
         end
       end
 
-      good_emails.each{|e| Resque.enqueue(Job::InviteUserByEmail, current_user.id, e, aspect, message)}
+      good_emails.each{|e| pp Resque.enqueue(Job::InviteUserByEmail, current_user.id, e, aspect, message)}
 
       if bad_emails.any?
         flash[:error] = I18n.t('invitations.create.sent') + good_emails.join(', ') + " "+ I18n.t('invitations.create.rejected') + bad_emails.join(', ')
diff --git a/app/controllers/services_controller.rb b/app/controllers/services_controller.rb
index ae855f4aeb..b9ea7f3d6a 100644
--- a/app/controllers/services_controller.rb
+++ b/app/controllers/services_controller.rb
@@ -51,7 +51,7 @@ class ServicesController < ApplicationController
   end
 
   def inviter
-    if current_user.invites == 0
+    if !AppConfig[:open_invitations] && current_user.invites == 0
       flash[:error] = I18n.t 'invitations.create.no_more'
       redirect_to :back
       return
diff --git a/app/views/aspects/index.html.haml b/app/views/aspects/index.html.haml
index 32d910cbad..aa6a26ca6b 100644
--- a/app/views/aspects/index.html.haml
+++ b/app/views/aspects/index.html.haml
@@ -82,14 +82,15 @@
           %br= link_to service.titleize, "/auth/#{service}" 
 
 
-  - if @invites > 0
+  - unless AppConfig[:invites_off]
     .section
       .title
 
         = image_tag('/images/icons/plus.png')
         %h5
-          .right
-            = t('shared.invitations.invitations_left', :count => @invites)
+          - unless AppConfig[:open_invitations]
+            .right
+              = t('shared.invitations.invitations_left', :count => @invites)
           = t('shared.invitations.invite_your_friends')
       .content
         = render "shared/invitations", :invites => @invites
diff --git a/app/views/shared/_invitations.haml b/app/views/shared/_invitations.haml
index dc536f143f..6ebe21c52a 100644
--- a/app/views/shared/_invitations.haml
+++ b/app/views/shared/_invitations.haml
@@ -1,16 +1,13 @@
-- if AppConfig[:invites_off]
-  = t('.invites_closed')
--else
-  -if invites > 0
-    - if SERVICES['facebook']['app_id'] !=""
-      - if defined? remote
-        = link_to t('.from_facebook'), friend_finder_path('facebook', :remote => remote), :rel => 'facebox'
-      -else
-        = link_to t('.from_facebook'), friend_finder_path('facebook'), :rel => 'facebox'
-      %br
+-if AppConfig[:open_invitations] || (invites > 0)
+  - if SERVICES['facebook']['app_id'] !=""
+    - if defined? remote
+      = link_to t('.from_facebook'), friend_finder_path('facebook', :remote => remote), :rel => 'facebox'
+    -else
+      = link_to t('.from_facebook'), friend_finder_path('facebook'), :rel => 'facebox'
+    %br
 
-      = link_to t('.by_email'), new_user_invitation_path, :title => t('.invite_someone'), :rel => 'facebox'
-    - else
-      = link_to t('.by_email'), new_user_invitation_path, :title => t('.invite_someone'), :rel => 'facebox'
+    = link_to t('.by_email'), new_user_invitation_path, :title => t('.invite_someone'), :rel => 'facebox'
   - else
-    = t('.dont_have_now')
+    = link_to t('.by_email'), new_user_invitation_path, :title => t('.invite_someone'), :rel => 'facebox'
+- else
+  = t('.dont_have_now')
diff --git a/config/application.yml.example b/config/application.yml.example
index 68692de60d..0be3aa1258 100644
--- a/config/application.yml.example
+++ b/config/application.yml.example
@@ -14,6 +14,9 @@ defaults: &defaults
   # Set this to true to prevent users from sending invitations.
   invites_off: false
 
+  # Set this to true if you want users to invite as many people as they want
+  open_invitations: true
+
   #
   # Logging setup
   #
@@ -148,4 +151,6 @@ test:
   pod_url: "http://localhost:9887"
   socket_port: 8081
   enable_splunk_logging: false
+  open_invitations: false
+
 
diff --git a/spec/controllers/invitations_controller_spec.rb b/spec/controllers/invitations_controller_spec.rb
index 920290a1bd..6bcb9d23c7 100644
--- a/spec/controllers/invitations_controller_spec.rb
+++ b/spec/controllers/invitations_controller_spec.rb
@@ -52,21 +52,33 @@ describe InvitationsController do
     it "doesn't invite anyone if you have 0 invites" do
       @user.invites = 0
       @user.save!
-      lambda {
-        post :create, :user => @invite.merge(:email => "mbs@gmail.com, foo@bar.com, foo.com, lala@foo, cool@bar.com")
-      }.should_not change(User, :count)
+
+      Resque.should_not_receive(:enqueue)
+      post :create, :user => @invite.merge(:email => "mbs@gmail.com, foo@bar.com, foo.com, lala@foo, cool@bar.com")
+    end
+
+    it "allows invitations without limit if invitations are open" do
+      open_bit = AppConfig[:open_invitations]
+      AppConfig[:open_invitations] = true
+      @user.invites = 0
+      @user.save!
+
+      Resque.should_receive(:enqueue).once
+      post :create, :user => @invite
+
+      AppConfig[:open_invitations] = open_bit
     end
 
     it 'returns to the previous page on success' do
       post :create, :user => @invite
       response.should redirect_to("http://test.host/cats/foo")
     end
-    
+
     it 'strips out your own email' do
       lambda {
         post :create, :user => @invite.merge(:email => @user.email)
       }.should_not change(User, :count)
-      
+
       Resque.should_receive(:enqueue).once
       post :create, :user => @invite.merge(:email => "hello@example.org, #{@user.email}")
     end
diff --git a/spec/controllers/services_controller_spec.rb b/spec/controllers/services_controller_spec.rb
index 45b0ffbd1d..06cd95ed81 100644
--- a/spec/controllers/services_controller_spec.rb
+++ b/spec/controllers/services_controller_spec.rb
@@ -140,5 +140,23 @@ describe ServicesController do
         put :inviter, @invite_params
       }.should_not change(Invitation, :count)
     end
+
+    it' does not crete an invitation if the user has no invitations' do
+      @user.invites = 0
+      lambda {
+        put :inviter, @invite_params
+      }.should_not change(Invitation, :count)
+    end
+
+    it 'disregares the amount of invites if open_invitations are anabled' do
+      open_bit = AppConfig[:open_invitations]
+      AppConfig[:open_invitations] = true
+      @user.invites = 0
+
+      lambda {
+        put :inviter, @invite_params
+      }.should change(Invitation, :count).by(1)
+      AppConfig[:open_invitations] = open_bit
+    end
   end
 end
-- 
GitLab