Skip to content

Commit

Permalink
Merge pull request #79 from mrharshp/invitation
Browse files Browse the repository at this point in the history
email invitation
  • Loading branch information
okwokyk committed Jun 3, 2019
2 parents 3d6e67a + 35ed768 commit ffe478e
Show file tree
Hide file tree
Showing 16 changed files with 110 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Expand Up @@ -34,3 +34,5 @@ group :development, :test do
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'dotenv-rails'
end
gem 'letter_opener', group: :development
gem 'postmark-rails'
15 changes: 15 additions & 0 deletions Gemfile.lock
Expand Up @@ -42,6 +42,8 @@ GEM
i18n (>= 0.7, < 2)
minitest (~> 5.1)
tzinfo (~> 1.1)
addressable (2.6.0)
public_suffix (>= 2.0.2, < 4.0)
arel (9.0.0)
autoprefixer-rails (9.5.1.1)
execjs
Expand Down Expand Up @@ -91,6 +93,11 @@ GEM
concurrent-ruby (~> 1.0)
jbuilder (2.9.1)
activesupport (>= 4.2.0)
json (2.2.0)
launchy (2.4.3)
addressable (~> 2.3)
letter_opener (1.7.0)
launchy (~> 2.2)
listen (3.0.8)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
Expand All @@ -116,6 +123,11 @@ GEM
mini_portile2 (~> 2.4.0)
orm_adapter (0.5.0)
pg (0.21.0)
postmark (1.15.0)
json
postmark-rails (0.19.0)
actionmailer (>= 3.0.0)
postmark (~> 1.15.0)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
Expand All @@ -124,6 +136,7 @@ GEM
pry (~> 0.10)
pry-rails (0.3.9)
pry (>= 0.10.4)
public_suffix (3.1.0)
puma (3.12.1)
pundit (2.0.1)
activesupport (>= 3.0.0)
Expand Down Expand Up @@ -233,8 +246,10 @@ DEPENDENCIES
dotenv-rails
font-awesome-sass (~> 5.6.1)
jbuilder (~> 2.0)
letter_opener
listen (~> 3.0.5)
pg (~> 0.21)
postmark-rails
pry-byebug
pry-rails
puma
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/groups_controller.rb
Expand Up @@ -2,6 +2,7 @@

class GroupsController < ApplicationController
before_action :find_group, only: [:show, :edit, :update]
skip_after_action :verify_authorized, only: :invite
# before_action :footer, only: [:new]

def index
Expand Down Expand Up @@ -35,12 +36,21 @@ def create
end

def edit
@user = current_user
if params[:query].present?
sql_query = "name ILIKE :query OR email ILIKE :query"
@users = policy_scope(User).where(sql_query, query: "%#{params[:query]}%")
else
@users = []
end
end

def invite
if params["friend_email"].present?
@friend_email = params["friend_email"]
@group_id = params["group_id"]
UserMailer.with(friend_email: @friend_email, sender_name: current_user.name, group_id: @group_id).invite.deliver_now
end
end

def update
Expand Down
2 changes: 1 addition & 1 deletion app/mailers/application_mailer.rb
@@ -1,4 +1,4 @@
class ApplicationMailer < ActionMailer::Base
default from: 'from@example.com'
default from: 'hello@haze.global'
layout 'mailer'
end
17 changes: 17 additions & 0 deletions app/mailers/user_mailer.rb
@@ -0,0 +1,17 @@
class UserMailer < ApplicationMailer
# Subject can be set in your I18n file at config/locales/en.yml
# with the following lookup:
#
# en.user_mailer.welcome.subject
def welcome
@user = params[:user] # Instance variable => available in view
mail(to: @user.email, subject: 'Welcome to Haze')
# This will render a view in `app/views/user_mailer`!
end

def invite
@sender_name = params[:sender_name]
@group_name = Group.find(params[:group_id]).name
mail(to: params[:friend_email], subject: "Start Planning Your Trip @HAZE!")
end
end
4 changes: 4 additions & 0 deletions app/policies/group_policy.rb
Expand Up @@ -21,6 +21,10 @@ def edit?
record.users.include?(user)
end

def invite?
record.users.include?(user)
end

def update?
edit?
end
Expand Down
12 changes: 11 additions & 1 deletion app/views/groups/edit.html.erb
Expand Up @@ -6,7 +6,7 @@
</div>
<div class="form container">
<br>
<h5 style="color: #54AFC3;">Invite your friends!</h5>
<h5 style="color: #54AFC3;">Add Participants</h5>
<%= form_tag edit_group_path(@group), method: :get do %>
<%= text_field_tag :query,
params[:query],
Expand All @@ -15,6 +15,16 @@
%>
<% end %>
<br>
<h5 style="color: #54AFC3;">Invite Friends Via Email!</h5>
<%= form_tag groups_invite_path, method: :post do %>
<%= text_field_tag :friend_email,
params[:friend_email],
class: "form-control",
placeholder: "Enter Email Address"
%>
<%= hidden_field_tag :group_id, params[:id] %>
<% end %>
<br>
<% @users.each do |u| %>
<%= u.name %>, <%= u.email %>
<%= link_to "Add to group", group_group_memberships_path(@group, user: u), method: :post, class: "button-sm" %>
Expand Down
6 changes: 6 additions & 0 deletions app/views/user_mailer/invite.html.erb
@@ -0,0 +1,6 @@
<p>Hi <%= params[:friend_email] %>,</p>

<p><strong><%= @sender_name %></strong> has invited you to join the <strong><%= @group_name %></strong> trip. </p>
<p><a href="https://www.haze.global ">Click here to sign up </a></strong> and start planning for your trip!</p>


7 changes: 7 additions & 0 deletions app/views/user_mailer/invite.text.erb
@@ -0,0 +1,7 @@
<p>Hi <%= params[:email] %></p>

<p>Start planning your next trip @HAZE!</p>

<a href="https://www.haze.global %>">Click here to sign up!</a>


5 changes: 5 additions & 0 deletions app/views/user_mailer/welcome.html.erb
@@ -0,0 +1,5 @@
<h1>User#welcome</h1>

<p>
<%= @greeting %>, find me in app/views/user_mailer/welcome.html.erb
</p>
3 changes: 3 additions & 0 deletions app/views/user_mailer/welcome.text.erb
@@ -0,0 +1,3 @@
User#welcome

<%= @greeting %>, find me in app/views/user_mailer/welcome.text.erb
2 changes: 1 addition & 1 deletion config/environments/development.rb
@@ -1,7 +1,7 @@
Rails.application.configure do
config.action_mailer.default_url_options = { host: "http://localhost:3000" }
# Settings specified here will take precedence over those in config/application.rb.

config.action_mailer.delivery_method = :letter_opener
# In the development environment your application's code is reloaded on
# every request. This slows down response time but is perfect for development
# since you don't have to restart the web server when you make code changes.
Expand Down
5 changes: 5 additions & 0 deletions config/environments/production.rb
Expand Up @@ -96,4 +96,9 @@

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

config.action_mailer.delivery_method = :postmark
config.action_mailer.postmark_settings = { api_token: ENV['POSTMARK_API_TOKEN'] }
config.action_mailer.default_url_options = { host: "hazeglobal.herokuapp.com" }
# or your custom domain name eg. "www.yourdomain.com"
end
2 changes: 2 additions & 0 deletions config/routes.rb
@@ -1,5 +1,7 @@
Rails.application.routes.draw do

get 'votes/create'
post 'groups/invite'
devise_for :users
root to: "pages#home"
# root to: "groups#index"
Expand Down
9 changes: 9 additions & 0 deletions test/mailers/previews/user_mailer_preview.rb
@@ -0,0 +1,9 @@
# Preview all emails at http://localhost:3000/rails/mailers/user_mailer
class UserMailerPreview < ActionMailer::Preview

# Preview this email at http://localhost:3000/rails/mailers/user_mailer/welcome
def welcome
UserMailer.welcome
end

end
12 changes: 12 additions & 0 deletions test/mailers/user_mailer_test.rb
@@ -0,0 +1,12 @@
require 'test_helper'

class UserMailerTest < ActionMailer::TestCase
test "welcome" do
mail = UserMailer.welcome
assert_equal "Welcome", mail.subject
assert_equal ["to@example.org"], mail.to
assert_equal ["from@example.com"], mail.from
assert_match "Hi", mail.body.encoded
end

end

0 comments on commit ffe478e

Please sign in to comment.