Skip to content

Commit

Permalink
Rename Email Template statuses #242
Browse files Browse the repository at this point in the history
  • Loading branch information
Frederike Ramin committed Jan 24, 2017
1 parent 716c58f commit 65d1d57
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 33 deletions.
19 changes: 13 additions & 6 deletions app/controllers/emails_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ def show
authorize! :send_email, Email
@event = Event.find(params[:event_id])

@templates = EmailTemplate.with_status(get_status)
@addresses = @event.email_addresses_of_type(get_status)
@templates = EmailTemplate.with_status(get_email_template_status)
@addresses = @event.email_addresses_of_type(get_corresponding_application_letter_status)

@email = Email.new(hide_recipients: true, reply_to: 'workshop.portal@hpi.de', recipients: @addresses,
subject: '', content: '')
Expand Down Expand Up @@ -35,7 +35,7 @@ def send_email

redirect_to @event, notice: t('.sending_successful')
else
@templates = EmailTemplate.with_status(get_status)
@templates = EmailTemplate.with_status(get_email_template_status)

flash.now[:alert] = t('.sending_failed')
render :email
Expand All @@ -45,7 +45,7 @@ def send_email
def save_template
@email = Email.new(email_params)

@template = EmailTemplate.new({ status: get_status, hide_recipients: @email.hide_recipients,
@template = EmailTemplate.new({ status: get_email_template_status, hide_recipients: @email.hide_recipients,
subject: @email.subject, content: @email.content })

if @email.validates_presence_of(:subject, :content) && @template.save
Expand All @@ -54,15 +54,22 @@ def save_template
flash.now[:alert] = t('.saving_failed')
end
@event = Event.find(params[:event_id])
@templates = EmailTemplate.with_status(get_status)
@templates = EmailTemplate.with_status(get_email_template_status)

render :email
end

def get_status
def get_email_template_status
params[:status] ? params[:status].to_sym : :default
end

def get_corresponding_application_letter_status
return :accepted if params[:status] == "acceptance"
return :rejected if params[:status] == "rejection"
# default value
return :accepted
end

# Only allow a trusted parameter "white list" through.
def email_params
params.require(:email).permit(:hide_recipients, :recipients, :reply_to, :subject, :content)
Expand Down
3 changes: 2 additions & 1 deletion app/models/email_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
#
class EmailTemplate < ActiveRecord::Base

enum status: { default: 0, accepted: 1, rejected: 2 }
enum status: { default: 0, acceptance: 1, rejection: 2 }

validates_inclusion_of :status, in: statuses.keys
validates_inclusion_of :hide_recipients, in: [ true, false ]
validates_presence_of :subject, :content
Expand Down
4 changes: 2 additions & 2 deletions app/views/events/_applicants_overview.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@
<div class="btn-group pull-right" role="group">
<%= link_to t('events.badges.print_button_label'), event_badges_path(@event), :class => 'btn btn-default'%>
<% if can? :send_email, Email %>
<%= link_to t('.sending_acceptances'), event_email_show_path(@event, status: :accepted), class: 'send-emails-button btn btn-default' %>
<%= link_to t('.sending_rejections'), event_email_show_path(@event, status: :rejected), class: 'send-emails-button btn btn-default' %>
<%= link_to t('.sending_acceptances'), event_email_show_path(@event, status: :acceptance), class: 'send-emails-button btn btn-default' %>
<%= link_to t('.sending_rejections'), event_email_show_path(@event, status: :rejection), class: 'send-emails-button btn btn-default' %>
<% end %>
</div>
<% end %>
Expand Down
20 changes: 10 additions & 10 deletions spec/controllers/email_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,35 +16,35 @@
context "with valid accepted applications" do
before :each do
@application = FactoryGirl.create(:application_letter_accepted, event: @event, user: FactoryGirl.build(:user))
@template = FactoryGirl.create(:email_template_accepted)
@template = FactoryGirl.create(:email_template_acceptance)
end

it "sets @email with the email of the accepted application" do
get :show, event_id: @event.id, status: :accepted
get :show, event_id: @event.id, status: :acceptance
expect(assigns(:email)).to be_a(Email)
expect(assigns(:email).recipients).to eq(@application.user.email)
end

it "sets @template with template for accepted emails" do
get :show, event_id: @event.id, status: :accepted
get :show, event_id: @event.id, status: :acceptance
expect(assigns(:templates)).to eq([@template])
end
end

context "with valid rejected applications" do
before :each do
@application = FactoryGirl.create(:application_letter_rejected, event: @event, user: FactoryGirl.build(:user))
@template = FactoryGirl.create(:email_template_rejected)
@template = FactoryGirl.create(:email_template_rejection)
end

it "sets @email with the email of the rejected application" do
get :show, event_id: @event.id, status: :rejected
get :show, event_id: @event.id, status: :rejection
expect(assigns(:email)).to be_a(Email)
expect(assigns(:email).recipients).to eq(@application.user.email)
end

it "sets @template with template for rejected emails" do
get :show, event_id: @event.id, status: :rejected
get :show, event_id: @event.id, status: :rejection
expect(assigns(:templates)).to eq([@template])
end
end
Expand Down Expand Up @@ -102,12 +102,12 @@

it "saves the current email as template" do
expect {
post :submit, save: I18n.t('.emails.email_form.save_template'), event_id: @event.id, email: @email, status: :accepted
post :submit, save: I18n.t('.emails.email_form.save_template'), event_id: @event.id, email: @email, status: :acceptance
}.to change(EmailTemplate, :count).by(1)
end

it "shows success message" do
post :submit, save: I18n.t('.emails.email_form.save_template'), event_id: @event.id, email: @email, status: :accepted
post :submit, save: I18n.t('.emails.email_form.save_template'), event_id: @event.id, email: @email, status: :acceptance
expect(flash[:success]).to eq(I18n.t('.emails.submit.saving_successful'))
end
end
Expand All @@ -119,12 +119,12 @@

it "does not save the current email as template" do
expect {
post :submit, save: I18n.t('.emails.email_form.save_template'), event_id: @event.id, email: @email, status: :accepted
post :submit, save: I18n.t('.emails.email_form.save_template'), event_id: @event.id, email: @email, status: :acceptance
}.to change(EmailTemplate, :count).by(0)
end

it "shows error message" do
post :submit, save: I18n.t('.emails.email_form.save_template'), event_id: @event.id, email: @email, status: :accepted
post :submit, save: I18n.t('.emails.email_form.save_template'), event_id: @event.id, email: @email, status: :acceptance
expect(flash[:alert]).to eq(I18n.t('.emails.submit.saving_failed'))
end
end
Expand Down
14 changes: 9 additions & 5 deletions spec/factories/email_templates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@
#
FactoryGirl.define do
factory :email_template do
status :accepted
status :acceptance
hide_recipients false
subject "EmailTemplate-Subject"
content "EmailTemplate-Content"
end

factory :email_template_accepted, parent: :email_template do
status :accepted
factory :email_template_acceptance, parent: :email_template do
status :acceptance
end

factory :email_template_rejected, parent: :email_template do
status :rejected
factory :email_template_rejection, parent: :email_template do
status :rejection
end

factory :email_template_default, parent: :email_template do
status :default
end
end
8 changes: 4 additions & 4 deletions spec/features/email_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@
scenario "logged in as Organizer I can send emails to the applicants" do
login(:organizer)

visit event_email_show_path(@event, status: :accepted)
visit event_email_show_path(@event, status: :acceptance)
fill_in :email_subject, with: "Subject Accepted"
fill_in :email_content, with: "Content Accepted"
click_button I18n.t('.emails.email_form.send')

expect(page).to have_text(I18n.t('.emails.submit.sending_successful'))

visit event_email_show_path(@event, status: :rejected)
visit event_email_show_path(@event, status: :rejection)
fill_in :email_subject, with: "Subject Rejected"
fill_in :email_content, with: "Content Rejected"
click_button I18n.t('.emails.email_form.send')
Expand All @@ -32,7 +32,7 @@
@event.application_status_locked = false
@event.save

visit event_email_show_path(@event, status: :accepted)
visit event_email_show_path(@event, status: :acceptance)
fill_in :email_subject, with: "Subject"
fill_in :email_content, with: "Content"
click_button I18n.t('.emails.email_form.send')
Expand All @@ -46,7 +46,7 @@
@template_subject = "Template Subject"
@template_content = "Template Content"

visit event_email_show_path(@event, status: :accepted)
visit event_email_show_path(@event, status: :acceptance)
fill_in :email_subject, with: @template_subject
fill_in :email_content, with: @template_content
click_button I18n.t('.emails.email_form.save_template')
Expand Down
12 changes: 7 additions & 5 deletions spec/models/email_template_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@
end

it "accepts valid status values" do
expect(FactoryGirl.build(:email_template, status: :accepted)).to be_valid
expect(FactoryGirl.build(:email_template, status: :rejected)).to be_valid
expect(FactoryGirl.build(:email_template_acceptance)).to be_valid
expect(FactoryGirl.build(:email_template_rejection)).to be_valid
expect(FactoryGirl.build(:email_template_default)).to be_valid
end

it "rejects invalid status values" do
Expand All @@ -33,8 +34,9 @@
end

it "returns correct templates by status" do
@accepted_template = FactoryGirl.create(:email_template, status: :accepted)
@rejected_template = FactoryGirl.create(:email_template, status: :rejected)
expect(EmailTemplate.with_status(:accepted)).to eq([@accepted_template])
@accepted_template = FactoryGirl.create(:email_template_acceptance)
@rejected_template = FactoryGirl.create(:email_template_rejection)
expect(EmailTemplate.with_status(:acceptance)).to eq([@accepted_template])
expect(EmailTemplate.with_status(:rejection)).to eq([@rejected_template])
end
end

0 comments on commit 65d1d57

Please sign in to comment.