# app/controllers/entries_controller.rb
class EntriesController < ApplicationController
def create
if @entry.save
AdminMailer.new_entry(@entry).deliver_later
CompanyMailer.new_entry(@entry).deliver_later
if current_user.entries.count.zero?
UserMailer.new_entry_with_description(@entry).deliver_later
else
UserMailer.new_entry(@entry).deliver_later
end
else
render :new
end
end
end
class DeliverAfterCreateEntryService
def self.call(user:, entry:)
new(user: user, entry: entry).call
end
def initialize(user:, entry:)
@user = user
@entry = entry
end
def call
deliver_to_admin
deliver_to_company
deliver_to_user
end
private
attr_reader :user, :entry
def deliver_to_admin
AdminMailer.new_entry(entry).deliver_later
end
def deliver_to_company
CompanyMailer.new_entry(entry).deliver_later
end
def deliver_to_user
if initial_entry?
UserMailer.new_entry_with_description(entry).deliver_later
else
UserMailer.new_entry(entry).deliver_later
end
end
def initial_entry?
user.entries.count.zero? # 条件的にちょっとおかしい気がするけど元のコードを尊重
end
end
class EntriesController < ApplicationController
def create
if @entry.save
DeliverAfterCreateEntryService.call(user: current_user, entry: @entry)
else
render :new
end
end
end
# app/controllers/entries_controller.rb
class EntriesController < ApplicationController
class Entry < ::Entry
after_create do
AdminMailer.new_entry(self).deliver_later
CompanyMailer.new_entry(self).deliver_later
UserMailer.new_entry(self).deliver_later
end
end
class InitialEntry < ::Entry
self.table_name = "entries"
after_create do
AdminMailer.new_entry(self).deliver_later
CompanyMailer.new_entry(self).deliver_later
UserMailer.new_entry_with_description(self).deliver_later
end
end
def create
if current_user.entries.count.zero?
@entry = InitialEntry.new(new_entry_params)
else
@entry = Entry.new(new_entry_params)
end
unless @entry.save
render :new and return
end
end
end
# config/routes.rb
post "/entries/1", to: 'initial_entry#create'
resources :entries
# app/controllers/initial_entry_controller.rb
class InitialEntryController < ApplicationController
class InitialEntry < ::Entry
self.table_name = "entries"
after_create do
AdminMailer.new_entry(self).deliver_later
CompanyMailer.new_entry(self).deliver_later
UserMailer.new_entry_with_description(self).deliver_later
end
end
before_action do
if current_user.entries.count.nonzero?
raise
end
end
def create
@entry = InitialEntry.new(new_entry_params)
if @entry.save
redirect_to survey_path
else
render :new
end
end
end
# app/controllers/entries_controller.rb
class EntriesController < ApplicationController
class Entry < ::Entry
after_create do
AdminMailer.new_entry(self).deliver_later
CompanyMailer.new_entry(self).deliver_later
UserMailer.new_entry(self).deliver_later
end
end
before_action do
if current_user.entries.count.zero?
raise
end
end
def create
if @entry.save
redirect_to entry_path(@entry)
else
render :new
end
end
end