diff --git a/app/jobs/regular/invite_email.rb b/app/jobs/regular/invite_email.rb index cd8ecb91af0..f373134a2bd 100644 --- a/app/jobs/regular/invite_email.rb +++ b/app/jobs/regular/invite_email.rb @@ -9,7 +9,7 @@ module Jobs raise Discourse::InvalidParameters.new(:invite_id) unless args[:invite_id].present? invite = Invite.find_by(id: args[:invite_id]) - message = InviteMailer.send_invite(invite, args[:custom_message]) + message = InviteMailer.send_invite(invite) Email::Sender.new(message, :invite).send end diff --git a/app/mailers/invite_mailer.rb b/app/mailers/invite_mailer.rb index 36141237162..35798bdfc16 100644 --- a/app/mailers/invite_mailer.rb +++ b/app/mailers/invite_mailer.rb @@ -8,7 +8,7 @@ class InviteMailer < ActionMailer::Base include EmailHelper end - def send_invite(invite, custom_message=nil) + def send_invite(invite) # Find the first topic they were invited to first_topic = invite.topics.order(:created_at).first @@ -27,7 +27,7 @@ class InviteMailer < ActionMailer::Base end template = 'invite_mailer' - if custom_message.present? + if invite.custom_message.present? template = 'custom_invite_mailer' end @@ -46,10 +46,10 @@ class InviteMailer < ActionMailer::Base topic_excerpt: topic_excerpt, site_description: SiteSetting.site_description, site_title: SiteSetting.title, - user_custom_message: custom_message) + user_custom_message: invite.custom_message) else template = 'invite_forum_mailer' - if custom_message.present? + if invite.custom_message.present? template = 'custom_invite_forum_mailer' end @@ -60,7 +60,7 @@ class InviteMailer < ActionMailer::Base invite_link: "#{Discourse.base_url}/invites/#{invite.invite_key}", site_description: SiteSetting.site_description, site_title: SiteSetting.title, - user_custom_message: custom_message) + user_custom_message: invite.custom_message) end end diff --git a/app/models/invite.rb b/app/models/invite.rb index 4749baa87bc..a6c63677551 100644 --- a/app/models/invite.rb +++ b/app/models/invite.rb @@ -122,6 +122,7 @@ class Invite < ActiveRecord::Base if !invite create_args = { invited_by: invited_by, email: lower_email } create_args[:moderator] = true if opts[:moderator] + create_args[:custom_message] = custom_message if custom_message invite = Invite.create!(create_args) end @@ -143,7 +144,7 @@ class Invite < ActiveRecord::Base end end - Jobs.enqueue(:invite_email, invite_id: invite.id, custom_message: custom_message) if send_email + Jobs.enqueue(:invite_email, invite_id: invite.id) if send_email invite.reload invite @@ -295,6 +296,7 @@ end # deleted_by_id :integer # invalidated_at :datetime # moderator :boolean default(FALSE), not null +# custom_message :text # # Indexes # diff --git a/db/migrate/20170630083540_add_custom_message_to_invite.rb b/db/migrate/20170630083540_add_custom_message_to_invite.rb new file mode 100644 index 00000000000..ce482c1be38 --- /dev/null +++ b/db/migrate/20170630083540_add_custom_message_to_invite.rb @@ -0,0 +1,5 @@ +class AddCustomMessageToInvite < ActiveRecord::Migration + def change + add_column :invites, :custom_message, :text + end +end diff --git a/spec/mailers/invite_mailer_spec.rb b/spec/mailers/invite_mailer_spec.rb index 68ce2b4a4a9..ce21d2c957b 100644 --- a/spec/mailers/invite_mailer_spec.rb +++ b/spec/mailers/invite_mailer_spec.rb @@ -5,9 +5,9 @@ describe InviteMailer do describe "send_invite" do context "invite to site" do - let(:invite) { Fabricate(:invite) } context "default invite message" do + let(:invite) { Fabricate(:invite) } let(:invite_mail) { InviteMailer.send_invite(invite) } it 'renders the invitee email' do @@ -36,9 +36,10 @@ describe InviteMailer do end context "custom invite message" do + let(:invite) { Fabricate(:invite, custom_message: "Hey, you should join this forum!") } context "custom message includes invite link" do - let(:custom_invite_mail) { InviteMailer.send_invite(invite, "Hey, you should join this forum!") } + let(:custom_invite_mail) { InviteMailer.send_invite(invite) } it 'renders the invitee email' do expect(custom_invite_mail.to).to eql([invite.email]) @@ -76,9 +77,9 @@ describe InviteMailer do context "invite to topic" do let(:trust_level_2) { build(:user, trust_level: 2) } let(:topic) { Fabricate(:topic, excerpt: "Topic invite support is now available in Discourse!", user: trust_level_2) } - let(:invite) { topic.invite(topic.user, 'name@example.com') } context "default invite message" do + let(:invite) { topic.invite(topic.user, 'name@example.com') } let(:invite_mail) { InviteMailer.send_invite(invite) } it 'renders the invitee email' do @@ -123,7 +124,8 @@ describe InviteMailer do end context "custom invite message" do - let(:custom_invite_mail) { InviteMailer.send_invite(invite, "Hey, I thought you might enjoy this topic!") } + let(:invite) { topic.invite(topic.user, 'name@example.com', nil, "Hey, I thought you might enjoy this topic!") } + let(:custom_invite_mail) { InviteMailer.send_invite(invite) } it 'renders custom_message' do expect(custom_invite_mail.body.encoded).to match("Hey, I thought you might enjoy this topic!")