diff --git a/app/controllers/event_emails_controller.rb b/app/controllers/event_emails_controller.rb index 3f053258..163606de 100644 --- a/app/controllers/event_emails_controller.rb +++ b/app/controllers/event_emails_controller.rb @@ -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 diff --git a/app/views/event_emails/new.html.haml b/app/views/event_emails/new.html.haml index 0a565d13..7afa847c 100644 --- a/app/views/event_emails/new.html.haml +++ b/app/views/event_emails/new.html.haml @@ -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 diff --git a/config/locales/en.yml b/config/locales/en.yml index ee2b9bdf..35a9fcd4 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -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