Skip to content

Commit

Permalink
Merge pull request huginn#966 from TildeWill/skip_invitation_code
Browse files Browse the repository at this point in the history
Adds the ability to allow public signup without invite code (retry)
  • Loading branch information
cantino committed Aug 14, 2015
2 parents 9e8f9bf + 570030c commit 16e9504
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 16 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ FORCE_SSL=false
# You can see its use in user.rb. PLEASE CHANGE THIS!
INVITATION_CODE=try-huginn

# If you don't want to require new users to have an invitation code in order to sign up, set this to true.
SKIP_INVITATION_CODE=false

#############################
# Email Configuration #
#############################
Expand Down
6 changes: 5 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class User < ActiveRecord::Base
validates_presence_of :username
validates_uniqueness_of :username
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."
validates_inclusion_of :invitation_code, :on => :create, :in => INVITATION_CODES, :message => "is not valid"
validates_inclusion_of :invitation_code, :on => :create, :in => INVITATION_CODES, :message => "is not valid", if: ->{ User.using_invitation_code? }

has_many :user_credentials, :dependent => :destroy, :inverse_of => :user
has_many :events, -> { order("events.created_at desc") }, :dependent => :delete_all, :inverse_of => :user
Expand All @@ -40,4 +40,8 @@ def self.find_first_by_auth_conditions(warden_conditions)
where(conditions).first
end
end

def self.using_invitation_code?
ENV['SKIP_INVITATION_CODE'] != 'true'
end
end
14 changes: 8 additions & 6 deletions app/views/devise/registrations/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@ bin/setup_heroku
</div>
<% end %>

<div class="form-group">
<%= f.label :invitation_code, class: 'col-md-4 control-label' %>
<div class="col-md-6">
<%= f.text_field :invitation_code, class: 'form-control' %>
<span class="help-inline">We are not yet open to the public. If you have an invitation code, please enter it here.</span>
<% if User.using_invitation_code? %>
<div class="form-group">
<%= f.label :invitation_code, class: 'col-md-4 control-label' %>
<div class="col-md-6">
<%= f.text_field :invitation_code, class: 'form-control' %>
<span class="help-inline">We are not yet open to the public. If you have an invitation code, please enter it here.</span>
</div>
</div>
</div>
<% end %>

<div class="form-group">
<%= f.label :email, class: 'col-md-4 control-label' %>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RemoveRequirementFromUsersInvitationCode < ActiveRecord::Migration
def change
change_column_null :users, :invitation_code, true, ENV['INVITATION_CODE'].presence || 'try-huginn'
end
end
4 changes: 2 additions & 2 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150507153436) do
ActiveRecord::Schema.define(version: 20150808115436) do

create_table "agent_logs", force: :cascade do |t|
t.integer "agent_id", limit: 4, null: false
Expand Down Expand Up @@ -176,7 +176,7 @@
t.string "unlock_token", limit: 255
t.datetime "locked_at"
t.string "username", limit: 191, null: false, charset: "utf8mb4", collation: "utf8mb4_unicode_ci"
t.string "invitation_code", limit: 255, null: false, collation: "utf8_bin"
t.string "invitation_code", limit: 255, collation: "utf8_bin"
t.integer "scenario_count", limit: 4, default: 0, null: false
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ FORCE_SSL=false
# You can see its use in user.rb. PLEASE CHANGE THIS!
INVITATION_CODE=try-huginn

# If you don't want to require users to have an invitation code, set this to true
SKIP_INVITATION_CODE=false

#############################
# Email Configuration #
#############################
Expand Down
32 changes: 25 additions & 7 deletions spec/models/users_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,33 @@
describe User do
describe "validations" do
describe "invitation_code" do
it "only accepts valid invitation codes" do
User::INVITATION_CODES.each do |v|
is_expected.to allow_value(v).for(:invitation_code)
context "when configured to use invitation codes" do
before do
stub(User).using_invitation_code? {true}
end

it "only accepts valid invitation codes" do
User::INVITATION_CODES.each do |v|
is_expected.to allow_value(v).for(:invitation_code)
end
end

it "can reject invalid invitation codes" do
%w['foo', 'bar'].each do |v|
is_expected.not_to allow_value(v).for(:invitation_code)
end
end
end

it "can reject invalid invitation codes" do
%w['foo', 'bar'].each do |v|
is_expected.not_to allow_value(v).for(:invitation_code)

context "when configured not to use invitation codes" do
before do
stub(User).using_invitation_code? {false}
end

it "skips this validation" do
%w['foo', 'bar', nil, ''].each do |v|
is_expected.to allow_value(v).for(:invitation_code)
end
end
end
end
Expand Down

0 comments on commit 16e9504

Please sign in to comment.