From a39b0711eefb07fb75294a68d635fb1323d24cf3 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 21 Oct 2023 12:36:21 -0400 Subject: Quotes can be submitted, although it is not encouraged --- app/controllers/quotes_controller.rb | 15 +++++++++++++-- app/mailers/quote_mailer.rb | 7 +++++++ app/views/quote_mailer/pending_quote_email.html.haml | 12 ++++++++++++ app/views/quote_mailer/pending_quote_email.text.erb | 15 +++++++++++++++ 4 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 app/mailers/quote_mailer.rb create mode 100644 app/views/quote_mailer/pending_quote_email.html.haml create mode 100644 app/views/quote_mailer/pending_quote_email.text.erb (limited to 'app') diff --git a/app/controllers/quotes_controller.rb b/app/controllers/quotes_controller.rb index 58af7bb..f7445da 100644 --- a/app/controllers/quotes_controller.rb +++ b/app/controllers/quotes_controller.rb @@ -63,6 +63,12 @@ class QuotesController < ApplicationController @quote = Quote.new(quote_params) @quote.submitter = nil if @quote.submitter.empty? + unless @quote.valid? + flash.alert = "Error submitting quote." + render :new + return + end + if user_signed_in? @quote.state = :published @quote.save! @@ -70,8 +76,13 @@ class QuotesController < ApplicationController flash[:notice] = "Thank you for submitting your quote!" redirect_to @quote else - flash[:error] = "There was an error." - render :new + @quote.state = :pending + @quote.save! + + QuoteMailer.with(quote: @quote).pending_quote_email.deliver_later + + flash[:notice] = "Your quote has been submitted and is pending moderation." + redirect_to new_quote_url end end diff --git a/app/mailers/quote_mailer.rb b/app/mailers/quote_mailer.rb new file mode 100644 index 0000000..1f3bddd --- /dev/null +++ b/app/mailers/quote_mailer.rb @@ -0,0 +1,7 @@ +class QuoteMailer < ApplicationMailer + def pending_quote_email + @quote = params[:quote] + # again, this is odd + mail(to: User.first.email, subject: "[Four Island] New quote pending moderation") + end +end diff --git a/app/views/quote_mailer/pending_quote_email.html.haml b/app/views/quote_mailer/pending_quote_email.html.haml new file mode 100644 index 0000000..fff825d --- /dev/null +++ b/app/views/quote_mailer/pending_quote_email.html.haml @@ -0,0 +1,12 @@ +%p + A new quote has been submitted for moderation. +- unless @quote.submitter.nil? + %p + It was submitted by: + = succeed "." do + = @quote.submitter +%p The body of the quote: +%pre= @quote.content +%p + Submitted: + = @quote.created_at.strftime("%B #{@quote.created_at.day.ordinalize}, %Y at %-I:%M:%S%P") diff --git a/app/views/quote_mailer/pending_quote_email.text.erb b/app/views/quote_mailer/pending_quote_email.text.erb new file mode 100644 index 0000000..818fc4a --- /dev/null +++ b/app/views/quote_mailer/pending_quote_email.text.erb @@ -0,0 +1,15 @@ +A new quote has been submitted for moderation. + +<% unless @quote.submitter.nil? %> +It was submitted by: <%= @quote.submitter %>. + +<% end %> +Here is the quote: + +--- + +<%= @quote.content %> + +--- + +Posted: <%= @quote.created_at.strftime("%B #{@quote.created_at.day.ordinalize}, %Y at %-I:%M:%S%P") %> -- cgit 1.4.1