FEATURE: backend for inviting a user to a group

This commit is contained in:
Sam 2014-05-08 16:45:49 +10:00
parent 77d68ccf08
commit 34d1668f9f
14 changed files with 110 additions and 68 deletions

View File

@ -22,6 +22,5 @@ end
# #
# Indexes # Indexes
# #
# index_badges_on_badge_type_id (badge_type_id) # index_badges_on_name (name) UNIQUE
# index_badges_on_name (name) UNIQUE
# #

View File

@ -6,12 +6,12 @@ end
# #
# Table name: category_custom_fields # Table name: category_custom_fields
# #
# id :integer not null, primary key # id :integer not null, primary key
# category_id :integer not null # category_id :integer not null
# name :string(256) not null # name :string(256) not null
# value :text # value :text
# created_at :datetime # created_at :datetime
# updated_at :datetime # updated_at :datetime
# #
# Indexes # Indexes
# #

View File

@ -49,3 +49,16 @@ class ColorScheme < ActiveRecord::Base
end end
end end
# == Schema Information
#
# Table name: color_schemes
#
# id :integer not null, primary key
# name :string(255) not null
# enabled :boolean default(FALSE), not null
# versioned_id :integer
# version :integer default(1), not null
# created_at :datetime
# updated_at :datetime
#

View File

@ -97,3 +97,20 @@ class ColorSchemeColor < ActiveRecord::Base
bookmarkColor: "00aaff" bookmarkColor: "00aaff"
} }
end end
# == Schema Information
#
# Table name: color_scheme_colors
#
# id :integer not null, primary key
# name :string(255) not null
# hex :string(255) not null
# opacity :integer default(100), not null
# color_scheme_id :integer not null
# created_at :datetime
# updated_at :datetime
#
# Indexes
#
# index_color_scheme_colors_on_color_scheme_id (color_scheme_id)
#

View File

@ -243,6 +243,7 @@ end
# automatic :boolean default(FALSE), not null # automatic :boolean default(FALSE), not null
# user_count :integer default(0), not null # user_count :integer default(0), not null
# alias_level :integer default(0) # alias_level :integer default(0)
# visible :boolean default(TRUE), not null
# #
# Indexes # Indexes
# #

View File

@ -6,12 +6,12 @@ end
# #
# Table name: group_custom_fields # Table name: group_custom_fields
# #
# id :integer not null, primary key # id :integer not null, primary key
# group_id :integer not null # group_id :integer not null
# name :string(256) not null # name :string(256) not null
# value :text # value :text
# created_at :datetime # created_at :datetime
# updated_at :datetime # updated_at :datetime
# #
# Indexes # Indexes
# #

View File

@ -5,6 +5,8 @@ class Invite < ActiveRecord::Base
belongs_to :topic belongs_to :topic
belongs_to :invited_by, class_name: 'User' belongs_to :invited_by, class_name: 'User'
has_many :invited_groups
has_many :groups, through: :invited_groups
has_many :topic_invites has_many :topic_invites
has_many :topics, through: :topic_invites, source: :topic has_many :topics, through: :topic_invites, source: :topic
validates_presence_of :email validates_presence_of :email

View File

@ -34,6 +34,7 @@ InviteRedeemer = Struct.new(:invite) do
def process_invitation def process_invitation
add_to_private_topics_if_invited add_to_private_topics_if_invited
add_user_to_invited_topics add_user_to_invited_topics
add_user_to_groups
send_welcome_message send_welcome_message
approve_account_if_needed approve_account_if_needed
notify_invitee notify_invitee
@ -75,6 +76,12 @@ InviteRedeemer = Struct.new(:invite) do
end end
end end
def add_user_to_groups
invite.groups.each do |g|
invited_user.group_users.create(group_id: g.id)
end
end
def send_welcome_message def send_welcome_message
if Invite.where(['email = ?', invite.email]).update_all(['user_id = ?', invited_user.id]) == 1 if Invite.where(['email = ?', invite.email]).update_all(['user_id = ?', invited_user.id]) == 1
invited_user.send_welcome_message = true invited_user.send_welcome_message = true

View File

@ -0,0 +1,15 @@
class InvitedGroup < ActiveRecord::Base
belongs_to :group
belongs_to :invite
end
# == Schema Information
#
# Table name: invited_groups
#
# id :integer not null, primary key
# group_id :integer
# invite_id :integer
# created_at :datetime
# updated_at :datetime
#

View File

@ -6,12 +6,12 @@ end
# #
# Table name: post_custom_fields # Table name: post_custom_fields
# #
# id :integer not null, primary key # id :integer not null, primary key
# post_id :integer not null # post_id :integer not null
# name :string(256) not null # name :string(256) not null
# value :text # value :text
# created_at :datetime # created_at :datetime
# updated_at :datetime # updated_at :datetime
# #
# Indexes # Indexes
# #

View File

@ -6,12 +6,12 @@ end
# #
# Table name: topic_custom_fields # Table name: topic_custom_fields
# #
# id :integer not null, primary key # id :integer not null, primary key
# topic_id :integer not null # topic_id :integer not null
# name :string(256) not null # name :string(256) not null
# value :text # value :text
# created_at :datetime # created_at :datetime
# updated_at :datetime # updated_at :datetime
# #
# Indexes # Indexes
# #

View File

@ -777,6 +777,9 @@ end
# primary_group_id :integer # primary_group_id :integer
# locale :string(10) # locale :string(10)
# profile_background :string(255) # profile_background :string(255)
# email_hash :string(255)
# registration_ip_address :inet
# last_redirected_to_top_at :datetime
# #
# Indexes # Indexes
# #

View File

@ -0,0 +1,9 @@
class AddInvitedGroups < ActiveRecord::Migration
def change
create_table :invited_groups do |t|
t.integer :group_id
t.integer :invite_id
t.timestamps
end
end
end

View File

@ -2,10 +2,6 @@ require 'spec_helper'
describe Invite do describe Invite do
it { should belong_to :user }
it { should have_many :topic_invites }
it { should belong_to :invited_by }
it { should have_many :topics }
it { should validate_presence_of :email } it { should validate_presence_of :email }
it { should validate_presence_of :invited_by_id } it { should validate_presence_of :invited_by_id }
@ -65,9 +61,6 @@ describe Invite do
it 'belongs to the topic' do it 'belongs to the topic' do
topic.invites.should == [@invite] topic.invites.should == [@invite]
end
it 'has a topic' do
@invite.topics.should == [topic] @invite.topics.should == [topic]
end end
@ -77,27 +70,16 @@ describe Invite do
it 'returns a different invite' do it 'returns a different invite' do
new_invite.should_not == @invite new_invite.should_not == @invite
end
it 'has a different key' do
new_invite.invite_key.should_not == @invite.invite_key new_invite.invite_key.should_not == @invite.invite_key
end
it 'has the topic relationship' do
new_invite.topics.should == [topic] new_invite.topics.should == [topic]
end end
end end
context 'when adding a duplicate' do context 'when adding a duplicate' do
it 'returns the original invite' do it 'returns the original invite' do
topic.invite_by_email(inviter, 'iceking@adventuretime.ooo').should == @invite topic.invite_by_email(inviter, 'iceking@adventuretime.ooo').should == @invite
end
it 'matches case insensitively for the domain part' do
topic.invite_by_email(inviter, 'iceking@ADVENTURETIME.ooo').should == @invite topic.invite_by_email(inviter, 'iceking@ADVENTURETIME.ooo').should == @invite
end
it 'matches case sensitively for the local part' do
topic.invite_by_email(inviter, 'ICEKING@adventuretime.ooo').should_not == @invite topic.invite_by_email(inviter, 'ICEKING@adventuretime.ooo').should_not == @invite
end end
@ -114,21 +96,13 @@ describe Invite do
context 'when adding to another topic' do context 'when adding to another topic' do
let!(:another_topic) { Fabricate(:topic, user: topic.user) } let!(:another_topic) { Fabricate(:topic, user: topic.user) }
before do
@new_invite = another_topic.invite_by_email(inviter, iceking)
end
it 'should be the same invite' do it 'should be the same invite' do
@new_invite = another_topic.invite_by_email(inviter, iceking)
@new_invite.should == @invite @new_invite.should == @invite
end
it 'belongs to the new topic' do
another_topic.invites.should == [@invite] another_topic.invites.should == [@invite]
end
it 'has references to both topics' do
@invite.topics.should =~ [topic, another_topic] @invite.topics.should =~ [topic, another_topic]
end end
end end
end end
end end
@ -172,7 +146,18 @@ describe Invite do
invite.redeem.should be_blank invite.redeem.should be_blank
end end
context 'invite trust levels' do context "when inviting to groups" do
it "add the user to the correct groups" do
group = Fabricate(:group)
invite.invited_groups.build(group_id: group.id)
invite.save
user = invite.redeem
user.groups.count.should == 1
end
end
context "invite trust levels" do
it "returns the trust level in default_invitee_trust_level" do it "returns the trust level in default_invitee_trust_level" do
SiteSetting.stubs(:default_invitee_trust_level).returns(TrustLevel.levels[:leader]) SiteSetting.stubs(:default_invitee_trust_level).returns(TrustLevel.levels[:leader])
invite.redeem.trust_level.should == TrustLevel.levels[:leader] invite.redeem.trust_level.should == TrustLevel.levels[:leader]
@ -183,7 +168,6 @@ describe Invite do
it 'correctly activates accounts' do it 'correctly activates accounts' do
SiteSetting.stubs(:must_approve_users).returns(true) SiteSetting.stubs(:must_approve_users).returns(true)
user = invite.redeem user = invite.redeem
user.approved?.should == true user.approved?.should == true
end end
end end
@ -257,13 +241,7 @@ describe Invite do
it 'adds the user to the topic_users of the first topic' do it 'adds the user to the topic_users of the first topic' do
topic.allowed_users.include?(user).should be_true topic.allowed_users.include?(user).should be_true
end
it 'adds the user to the topic_users of the second topic' do
another_topic.allowed_users.include?(user).should be_true another_topic.allowed_users.include?(user).should be_true
end
it 'does not redeem the second invite' do
another_invite.reload another_invite.reload
another_invite.should_not be_redeemed another_invite.should_not be_redeemed
end end
@ -276,9 +254,6 @@ describe Invite do
it 'returns the same user' do it 'returns the same user' do
@result.should == user @result.should == user
end
it 'marks the second invite as redeemed' do
another_invite.reload another_invite.reload
another_invite.should be_redeemed another_invite.should be_redeemed
end end
@ -303,7 +278,7 @@ describe Invite do
context 'with user that has not invited' do context 'with user that has not invited' do
it 'does not return invites' do it 'does not return invites' do
user = Fabricate(:user) user = Fabricate(:user)
invite = Fabricate(:invite) Fabricate(:invite)
invites = Invite.find_all_invites_from(user) invites = Invite.find_all_invites_from(user)
@ -315,12 +290,13 @@ describe Invite do
describe '.find_redeemed_invites_from' do describe '.find_redeemed_invites_from' do
it 'returns redeemed invites only' do it 'returns redeemed invites only' do
inviter = Fabricate(:user) inviter = Fabricate(:user)
pending_invite = Fabricate( Fabricate(
:invite, :invite,
invited_by: inviter, invited_by: inviter,
user_id: nil, user_id: nil,
email: 'pending@example.com' email: 'pending@example.com'
) )
redeemed_invite = Fabricate( redeemed_invite = Fabricate(
:invite, :invite,
invited_by: inviter, invited_by: inviter,