Skip to content
This repository has been archived by the owner on Sep 25, 2024. It is now read-only.

WIP—Email sending #15

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,21 @@ class ApplicationController < ActionController::Base
rescue_from Pundit::NotAuthorizedError,
with: :record_not_authorized
end
helper_method :nobody_signed_in?, :current_user_id, :is_my?, :isnt_my?, :set_event
helper_method :nobody_signed_in?, :current_user_id, :set_event, :custom_authorization

def set_event
@event = Event.friendly.find_by_friendly_id(params[:event_id] || params[:id])
raise ActiveRecord::RecordNotFound unless @event
end

def custom_authorization
skip_authorization
# manually authenticate certain methods, Pundit can't
unless @event.users.include?(current_user) || current_user.admin?
raise Pundit::NotAuthorizedError, 'not allowed to view this action'
end
end

# Keep the current user id in memory
def current_user_id
return unless current_user
Expand Down
1 change: 1 addition & 0 deletions app/controllers/attendee_fields_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class AttendeeFieldsController < ApplicationController
before_action :please_sign_in
before_action :set_event
before_action :set_attendee_field, only: %i[show edit update destroy]
before_action -> { authorize @attendee_field }, only: %i[show edit update destroy]
Expand Down
8 changes: 0 additions & 8 deletions app/controllers/attendees_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@ def index
end
end

def custom_authorization
skip_authorization
# manually authenticate certain methods, Pundit can't
unless @event.users.include?(current_user) || current_user.admin?
raise Pundit::NotAuthorizedError, 'not allowed to view this action'
end
end

def show
@attendee_fields = @event.fields
end
Expand Down
57 changes: 57 additions & 0 deletions app/controllers/emails_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
class EmailsController < ApplicationController
before_action :please_sign_in
before_action :set_event
before_action :set_email, only: %i[show]
before_action :custom_authorization, only: %i[index settings configure]

def index
end

def show
authorize @email
end

def new
@email = @event.emails.new
authorize @email
end

def create
@email = @event.emails.new(email_params)
authorize @email

if @email.save
redirect_to event_emails_path(@event)
flash[:success] = 'Email will be sent shortly.'
else
render :new
end
end

def settings
@email_config = @event.email_config
end

def configure
if @event.email_config.update(email_config_params)
redirect_to event_emails_path(@event)
flash[:success] = 'Email sending has been configured.'
else
render :edit
end
end

private

def set_email
@email = @event.emails.find(params[:id])
end

def email_params
params.require(:email).permit(:recipient, :sender_email, :subject, :body)
end

def email_config_params
params.require(:email_config).permit(:smtp_url, :smtp_port, :authentication, :domain, :sender_email, :username, :password)
end
end
3 changes: 3 additions & 0 deletions app/models/email.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class Email < ApplicationRecord
belongs_to :event
end
11 changes: 11 additions & 0 deletions app/models/email_config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# frozen_string_literal: true

class EmailConfig < ApplicationRecord
belongs_to :event

def enabled?
true unless [
smtp_url, smtp_port, authentication, domain, sender_email, username, password
].any?(&:nil?)
end
end
8 changes: 8 additions & 0 deletions app/models/event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class Event < ApplicationRecord

default_scope { order(id: :asc) }

has_one :email_config, dependent: :destroy
has_many :emails, dependent: :destroy
has_many :attendees, dependent: :destroy
has_many :fields, class_name: 'AttendeeField', dependent: :destroy
has_many :organizer_positions, dependent: :destroy
Expand All @@ -14,6 +16,12 @@ class Event < ApplicationRecord
validates :name, :start_date, :end_date, :location, :user_id, presence: true
validate :permitted_domains_cannot_have_trailing_slash

after_create do
create_email_config!
end

private

def permitted_domains_cannot_have_trailing_slash
permitted_domains.split(',').each do |domain|
if domain[-1] == '/'
Expand Down
15 changes: 15 additions & 0 deletions app/policies/email_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

class EmailPolicy < ApplicationPolicy
def show?
record.event.users.include?(user) || user.admin?
end

def new?
record.event.users.include?(user) || user.admin?
end

def create?
record.event.users.include?(user) || user.admin?
end
end
20 changes: 20 additions & 0 deletions app/views/emails/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<% title "Emails on #{@event.name}" %>
<div class="breadcrumbs">
<%= link_to 'Home', root_url %> › <%= link_to 'Events', events_url %> › <%= link_to @event.name, @event %>
</div>

<h1 class="heading">
Emails
<% if @event.email_config.enabled? %>
<%= link_to 'Send an email', new_event_email_path, class: 'btn bg-success' %>
<%= link_to 'Edit settings', settings_event_emails_path, class: 'btn bg-info' %>
<% else %>
<%= link_to 'Configure email sending', settings_event_emails_path, class: 'btn bg-info' %>
<% end %>
</h1>

<% if @event.email_config.enabled? %>
a
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eep

<% else %>
<p class="muted no-margin">Email sending hasn’t been configured.</p>
<% end %>
63 changes: 63 additions & 0 deletions app/views/emails/settings.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<% title "Editing email settings on #{@event.name}" %>
<div class="breadcrumbs">
<%= link_to 'Home', root_url %> › <%= link_to 'Events', events_url %> › <%= link_to @event.name, @event %> › <%= link_to 'Emails', event_emails_path(@event) %>
</div>
<h1>Email Configuration</h1>

<%= form_with(model: @email_config, local: true, url: configure_event_emails_path) do |form| %>
<% if @email_config.errors.any? %>
<div id="error_explanation">
<h2><%= pluralize(@email_config.errors.count, "error") %> prohibited this event from being saved:</h2>

<ul>
<% @email_config.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
</div>
<% end %>

<div class="field">
<%= form.label :smtp_url %>
<%= form.text_field :smtp_url %>
</div>

<div class="field">
<%= form.label :smtp_port %>
<%= form.text_field :smtp_port %>
</div>

<div class="field">
<%= form.label :authentication %>
<%= form.text_field :authentication, placeholder: 'PLAIN' %>
</div>

<div class="field">
<%= form.label :smtp_url %>
<%= form.text_field :smtp_url %>
</div>

<div class="field">
<%= form.label :domain %>
<%= form.text_field :domain %>
</div>

<div class="field">
<%= form.label :sender_email %>
<%= form.text_field :sender_email %>
</div>

<div class="field">
<%= form.label :username %>
<%= form.text_field :username %>
</div>

<div class="field">
<%= form.label :password %>
<%= form.password_field :password %>
</div>

<div class="actions">
<%= form.submit 'Configure' %>
</div>
<% end %>
5 changes: 5 additions & 0 deletions app/views/events/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
Hardware<span class="badge"><%= @event.hardwares.count %></span>
</li>
<% end %>
<%= link_to event_emails_path(@event) do %>
<li style="display: flex; align-items: center;">
Emails<span class="badge"><%= @event.email_config.enabled? ? @event.emails.count : 'Disabled' %></span>
</li>
<% end %>
</ul>

<div id="team-members" style="margin: 0 0 1rem;">
Expand Down
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
end
end
resources :attendee_fields, path: 'fields'
resources :emails do
collection do
get 'settings', to: 'emails#settings', as: :settings
patch 'configure', to: 'emails#configure', as: :configure
end
end

resources :organizer_position_invites, path: 'invites' do
post 'accept'
Expand Down
16 changes: 16 additions & 0 deletions db/migrate/20181213014752_create_email_configs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateEmailConfigs < ActiveRecord::Migration[5.2]
def change
create_table :email_configs do |t|
t.string :smtp_url
t.string :smtp_port
t.string :authentication
t.string :domain
t.string :sender_email
t.string :username
t.string :password
t.belongs_to :event, index: true

t.timestamps
end
end
end
15 changes: 15 additions & 0 deletions db/migrate/20181213030449_create_emails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class CreateEmails < ActiveRecord::Migration[5.2]
def change
create_table :emails do |t|
t.string :recipient
t.string :sender_email
t.text :subject
t.text :body
t.string :read_receipt_img_url
t.string :read_receipt_stats_url
t.belongs_to :event, index: true

t.timestamps
end
end
end
29 changes: 28 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2018_12_04_044905) do
ActiveRecord::Schema.define(version: 2018_12_13_030449) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -74,6 +74,33 @@
t.index ["event_id"], name: "index_attendees_on_event_id"
end

create_table "email_configs", force: :cascade do |t|
t.string "smtp_url"
t.string "smtp_port"
t.string "authentication"
t.string "domain"
t.string "sender_email"
t.string "username"
t.string "password"
t.bigint "event_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["event_id"], name: "index_email_configs_on_event_id"
end

create_table "emails", force: :cascade do |t|
t.string "recipient"
t.string "sender_email"
t.text "subject"
t.text "body"
t.string "read_receipt_img_url"
t.string "read_receipt_stats_url"
t.bigint "event_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["event_id"], name: "index_emails_on_event_id"
end

create_table "events", force: :cascade do |t|
t.string "name"
t.datetime "start_date"
Expand Down
21 changes: 21 additions & 0 deletions test/fixtures/email_configs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
enabled: false
smtp_url: MyString
smtp_port: MyString
authentication: MyString
domain: MyString
sender_email: MyString
username: MyString
password: MyString

two:
enabled: false
smtp_url: MyString
smtp_port: MyString
authentication: MyString
domain: MyString
sender_email: MyString
username: MyString
password: MyString
17 changes: 17 additions & 0 deletions test/fixtures/emails.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html

one:
recipient: MyString
sender_email: MyString
subject: MyText
body: MyText
read_receipt_img_url: MyString
read_receipt_stats_url: MyString

two:
recipient: MyString
sender_email: MyString
subject: MyText
body: MyText
read_receipt_img_url: MyString
read_receipt_stats_url: MyString
7 changes: 7 additions & 0 deletions test/models/email_config_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class EmailConfigTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end
7 changes: 7 additions & 0 deletions test/models/email_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class EmailTest < ActiveSupport::TestCase
# test "the truth" do
# assert true
# end
end