Skip to content

Commit

Permalink
Merge pull request #215 from hellcp/inherited_resources
Browse files Browse the repository at this point in the history
Migrate event_emails away from inherited resources
  • Loading branch information
hellcp authored Feb 18, 2023
2 parents c24aac3 + 0809fcf commit e0cfc1c
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
67 changes: 53 additions & 14 deletions app/controllers/event_emails_controller.rb
Original file line number Diff line number Diff line change
@@ -1,39 +1,78 @@
# frozen_string_literal: true

class EventEmailsController < InheritedResources::Base
actions :all, except: %i[edit update destroy]
belongs_to :event
class EventEmailsController < ApplicationController
load_and_authorize_resource :event
load_and_authorize_resource :event_email, through: :event
prepend_before_action :set_event
before_action :set_event_email, only: %i[show]
after_action :notify_receivers, only: %i[create]

def preview
@content = params[:content]
end

def index
@event_emails = EventEmail.where(event: @event)
end

def show; end

def new
@event_email = EventEmail.new
end

def create
@event_email = EventEmail.new(event_email_params)
@event_email.event = @event
@event_email.user = current_user
create!(notice: 'Email Delivered')
return unless @event_email.errors.empty?

@event_email[:to].split(',').each do |e|
user = User.find_by(email: e)
EventMailer.notify_to(user, :event_info, @event_email)
respond_to do |format|
if @event_email.save
format.html { redirect_to event_event_emails_url(@event), notice: t(:event_email_create) }
format.json { render :show, status: :created, location: @event_email }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @event_email.errors, status: :unprocessable_entity }
end
end
end

protected

def permitted_params
params.permit(event_email: %i[to subject body])
end

def set_breadcrumbs
@breadcrumbs = [
{ label: 'events', url: events_path },
{ label: @event.name, url: event_path(@event) },
{ label: resource_class.model_name.human(count: 2), url: collection_path }
{ label: EventEmail.model_name.human(count: 2), url: event_event_emails_path(@event) }
]
@breadcrumbs << { label: 'new' } if action_name == 'new'
@breadcrumbs << { label: resource, url: resource_path } if %w[show edit update].include? action_name
@breadcrumbs << { label: @event_email, url: event_event_email_path(@event, @event_email) } if action_name == :show
end

private

def event_email_params
params.require(:event_email).permit(%i[to subject body])
end

def set_event
@event = Event.find(params[:event_id])

redirect_back(fallback_location: events_url) unless @event
end

def set_event_email
@event_email = EventEmail.find(params[:id])

redirect_back(fallback_location: event_emails_url) unless @event_email
end

def notify_receivers
return unless @event_email.errors.empty?

@event_email[:to].split(',').each do |e|
user = User.find_by(email: e)
EventMailer.notify_to(user, :event_info, @event_email)
end
end
end
2 changes: 1 addition & 1 deletion app/views/event_emails/new.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
%input{id: 'state' + state, "type" => 'checkbox', "data-users" => users_for_event(state.downcase) }
%label{'for' => 'state' + state} #{state}
%br
= simple_form_for(resource, url: event_event_emails_path) do |f|
= simple_form_for(@event_email, url: event_event_emails_path) do |f|
= f.input :to, as: :text,readonly: true,placeholder: 'Select the recipients', input_html: {rows: 1}
= f.input :subject
%div
Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,4 @@ en:
event_create: Event was successfully created
event_update: Event was successfully updated
event_destroyed: Event was successfully destroyed
event_email_create: Email Delivered

0 comments on commit e0cfc1c

Please sign in to comment.