Merge pull request #966 from TildeWill/skip_invitation_code

Adds the ability to allow public signup without invite code (retry)

Andrew Cantino 9 ans auparavant
Parent
Commettre
16e9504a88

+ 3 - 0
.env.example

@@ -47,6 +47,9 @@ FORCE_SSL=false
47 47
 # You can see its use in user.rb.  PLEASE CHANGE THIS!
48 48
 INVITATION_CODE=try-huginn
49 49
 
50
+# If you don't want to require new users to have an invitation code in order to sign up, set this to true.
51
+SKIP_INVITATION_CODE=false
52
+
50 53
 #############################
51 54
 #    Email Configuration    #
52 55
 #############################

+ 5 - 1
app/models/user.rb

@@ -18,7 +18,7 @@ class User < ActiveRecord::Base
18 18
   validates_presence_of :username
19 19
   validates_uniqueness_of :username
20 20
   validates_format_of :username, :with => /\A[a-zA-Z0-9_-]{3,15}\Z/, :message => "can only contain letters, numbers, underscores, and dashes, and must be between 3 and 15 characters in length."
21
-  validates_inclusion_of :invitation_code, :on => :create, :in => INVITATION_CODES, :message => "is not valid"
21
+  validates_inclusion_of :invitation_code, :on => :create, :in => INVITATION_CODES, :message => "is not valid", if: ->{ User.using_invitation_code? }
22 22
 
23 23
   has_many :user_credentials, :dependent => :destroy, :inverse_of => :user
24 24
   has_many :events, -> { order("events.created_at desc") }, :dependent => :delete_all, :inverse_of => :user
@@ -40,4 +40,8 @@ class User < ActiveRecord::Base
40 40
       where(conditions).first
41 41
     end
42 42
   end
43
+
44
+  def self.using_invitation_code?
45
+    ENV['SKIP_INVITATION_CODE'] != 'true'
46
+  end
43 47
 end

+ 8 - 6
app/views/devise/registrations/new.html.erb

@@ -30,13 +30,15 @@ bin/setup_heroku
30 30
           </div>
31 31
           <% end %>
32 32
 
33
-          <div class="form-group">
34
-            <%= f.label :invitation_code, class: 'col-md-4 control-label' %>
35
-            <div class="col-md-6">
36
-              <%= f.text_field :invitation_code, class: 'form-control' %>
37
-              <span class="help-inline">We are not yet open to the public.  If you have an invitation code, please enter it here.</span>
33
+          <% if User.using_invitation_code? %>
34
+            <div class="form-group">
35
+              <%= f.label :invitation_code, class: 'col-md-4 control-label' %>
36
+              <div class="col-md-6">
37
+                <%= f.text_field :invitation_code, class: 'form-control' %>
38
+                <span class="help-inline">We are not yet open to the public.  If you have an invitation code, please enter it here.</span>
39
+              </div>
38 40
             </div>
39
-          </div>
41
+          <% end %>
40 42
 
41 43
           <div class="form-group">
42 44
             <%= f.label :email, class: 'col-md-4 control-label' %>

+ 5 - 0
db/migrate/20150808115436_remove_requirement_from_users_invitation_code.rb

@@ -0,0 +1,5 @@
1
+class RemoveRequirementFromUsersInvitationCode < ActiveRecord::Migration
2
+  def change
3
+    change_column_null :users, :invitation_code, true, ENV['INVITATION_CODE'].presence || 'try-huginn'
4
+  end
5
+end

+ 2 - 2
db/schema.rb

@@ -11,7 +11,7 @@
11 11
 #
12 12
 # It's strongly recommended that you check this file into your version control system.
13 13
 
14
-ActiveRecord::Schema.define(version: 20150507153436) do
14
+ActiveRecord::Schema.define(version: 20150808115436) do
15 15
 
16 16
   create_table "agent_logs", force: :cascade do |t|
17 17
     t.integer  "agent_id",          limit: 4,                 null: false
@@ -176,7 +176,7 @@ ActiveRecord::Schema.define(version: 20150507153436) do
176 176
     t.string   "unlock_token",           limit: 255
177 177
     t.datetime "locked_at"
178 178
     t.string   "username",               limit: 191,                 null: false, charset: "utf8mb4", collation: "utf8mb4_unicode_ci"
179
-    t.string   "invitation_code",        limit: 255,                 null: false,                     collation: "utf8_bin"
179
+    t.string   "invitation_code",        limit: 255,                                                  collation: "utf8_bin"
180 180
     t.integer  "scenario_count",         limit: 4,   default: 0,     null: false
181 181
   end
182 182
 

+ 3 - 0
deployment/site-cookbooks/huginn_production/files/default/env.example

@@ -39,6 +39,9 @@ FORCE_SSL=false
39 39
 # You can see its use in user.rb.  PLEASE CHANGE THIS!
40 40
 INVITATION_CODE=try-huginn
41 41
 
42
+# If you don't want to require users to have an invitation code, set this to true
43
+SKIP_INVITATION_CODE=false
44
+
42 45
 #############################
43 46
 #    Email Configuration    #
44 47
 #############################

+ 25 - 7
spec/models/users_spec.rb

@@ -3,15 +3,33 @@ require 'spec_helper'
3 3
 describe User do
4 4
   describe "validations" do
5 5
     describe "invitation_code" do
6
-      it "only accepts valid invitation codes" do
7
-        User::INVITATION_CODES.each do |v|
8
-          is_expected.to allow_value(v).for(:invitation_code)
6
+      context "when configured to use invitation codes" do
7
+        before do
8
+          stub(User).using_invitation_code? {true}
9
+        end
10
+        
11
+        it "only accepts valid invitation codes" do
12
+          User::INVITATION_CODES.each do |v|
13
+            is_expected.to allow_value(v).for(:invitation_code)
14
+          end
15
+        end
16
+  
17
+        it "can reject invalid invitation codes" do
18
+          %w['foo', 'bar'].each do |v|
19
+            is_expected.not_to allow_value(v).for(:invitation_code)
20
+          end
9 21
         end
10 22
       end
11
-
12
-      it "can reject invalid invitation codes" do
13
-        %w['foo', 'bar'].each do |v|
14
-          is_expected.not_to allow_value(v).for(:invitation_code)
23
+      
24
+      context "when configured not to use invitation codes" do
25
+        before do
26
+          stub(User).using_invitation_code? {false}
27
+        end
28
+        
29
+        it "skips this validation" do
30
+          %w['foo', 'bar', nil, ''].each do |v|
31
+            is_expected.to allow_value(v).for(:invitation_code)
32
+          end
15 33
         end
16 34
       end
17 35
     end