diff --git a/.env.example b/.env.example index b8177b8..3b6904b 100644 --- a/.env.example +++ b/.env.example @@ -37,3 +37,6 @@ MAILER_SENDER_ADDRESS=noreply@example.com # comma separated list of IP adresses # IP_WHITELIST=127.0.0.1 + +# Admin email adress +ADMIN_EMAIL=admin@example.com diff --git a/app/controllers/feedbacks_controller.rb b/app/controllers/feedbacks_controller.rb new file mode 100644 index 0000000..bf09d1c --- /dev/null +++ b/app/controllers/feedbacks_controller.rb @@ -0,0 +1,17 @@ +class FeedbacksController < ApplicationController + expose(:feedback) + + def new + end + + def create + ApplicationMailer.feedback(feedback).deliver_now! if feedback.save + respond_with(feedback, location: root_path) + end + + private + + def feedback_params + params.require(:feedback).permit(:email, :name, :text) + end +end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb index 47cdc62..239a552 100644 --- a/app/mailers/application_mailer.rb +++ b/app/mailers/application_mailer.rb @@ -1,3 +1,8 @@ class ApplicationMailer < ActionMailer::Base layout "mailer" + + def feedback(feedback) + @feedback = feedback + mail(subject: "Feedback", from: feedback.email, to: ENV.fetch("ADMIN_EMAIL")) + end end diff --git a/app/models/feedback.rb b/app/models/feedback.rb new file mode 100644 index 0000000..a5abed6 --- /dev/null +++ b/app/models/feedback.rb @@ -0,0 +1,13 @@ +class Feedback + include ActiveModel::Model + + attr_accessor :email, :name, :text + + validates :email, presence: true, format: Devise.email_regexp + validates :name, presence: true + validates :text, presence: true + + def save + valid? + end +end diff --git a/app/views/application_mailer/feedback.html.slim b/app/views/application_mailer/feedback.html.slim new file mode 100644 index 0000000..8272377 --- /dev/null +++ b/app/views/application_mailer/feedback.html.slim @@ -0,0 +1,11 @@ +header + | Hello, this feedback is from #{@feedback.name} + +hr +blockquote + p + = @feedback.text +hr + +footer + | from: #{@feedback.email} diff --git a/app/views/application_mailer/feedback.text.erb b/app/views/application_mailer/feedback.text.erb new file mode 100644 index 0000000..a478767 --- /dev/null +++ b/app/views/application_mailer/feedback.text.erb @@ -0,0 +1,15 @@ +
+ Hello, this feedback is from <%= @feedback.name %> +
+ +
+
+

+ <%= @feedback.text %> +

+
+
+ + diff --git a/app/views/feedbacks/new.html.slim b/app/views/feedbacks/new.html.slim new file mode 100644 index 0000000..4442447 --- /dev/null +++ b/app/views/feedbacks/new.html.slim @@ -0,0 +1,14 @@ +.row + .medium-5.columns + = simple_form_for(feedback) do |f| + + legend + | Send Your Feedback + + .form-inputs + = f.input :email, required: true, autofocus: true + = f.input :name, required: true + = f.input :text, as: :text, required: true + + .form-actions + = f.button :submit, "Submit feedback" diff --git a/config/locales/flash.en.yml b/config/locales/flash.en.yml index 345e2f3..a379e84 100644 --- a/config/locales/flash.en.yml +++ b/config/locales/flash.en.yml @@ -19,3 +19,7 @@ en: destroy: notice: '%{resource_name} was successfully destroyed.' alert: '%{resource_name} could not be destroyed.' + feedbacks: + create: + notice: Feedback was successfully send! + alert: Your feedback has not been sent. diff --git a/config/routes.rb b/config/routes.rb index 1f1ea15..dbdc552 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Rails.application.routes.draw do devise_for :users, controllers: { registrations: "users/registrations" } root to: "pages#home" + + resources :feedbacks, only: %i[new create] end diff --git a/spec/factories/feedback.rb b/spec/factories/feedback.rb new file mode 100644 index 0000000..fde8645 --- /dev/null +++ b/spec/factories/feedback.rb @@ -0,0 +1,7 @@ +FactoryGirl.define do + factory :feedback do + email + name { Faker::Name.name } + text { Faker::Lorem.paragraph } + end +end diff --git a/spec/features/visitor/feedback/create_spec.rb b/spec/features/visitor/feedback/create_spec.rb new file mode 100644 index 0000000..6f45cc7 --- /dev/null +++ b/spec/features/visitor/feedback/create_spec.rb @@ -0,0 +1,32 @@ +require "rails_helper" + +feature "Create Feedback" do + let(:feedback_attributes) { attributes_for(:feedback) } + + scenario "Visitor creates his feedback" do + visit new_feedback_path + + fill_form :feedback, feedback_attributes + + click_button "Submit feedback" + + open_email(ENV.fetch("ADMIN_EMAIL")) + + expect(current_email).to have_subject("Feedback") + expect(current_email).to be_delivered_from(feedback_attributes[:email]) + + expect(current_email).to have_body_text(feedback_attributes[:name]) + expect(current_email).to have_body_text(feedback_attributes[:email]) + expect(current_email).to have_body_text(feedback_attributes[:text]) + + expect(page).to have_content("Feedback was successfully send!") + end + + scenario "Visitor cannot creates his feedback" do + visit new_feedback_path + + click_button "Submit feedback" + + expect(page).to have_content("Your feedback has not been sent.") + end +end diff --git a/spec/models/feedback_spec.rb b/spec/models/feedback_spec.rb new file mode 100644 index 0000000..94ae09f --- /dev/null +++ b/spec/models/feedback_spec.rb @@ -0,0 +1,7 @@ +require "rails_helper" + +describe Feedback do + it { is_expected.to validate_presence_of :name } + it { is_expected.to validate_presence_of :email } + it { is_expected.to validate_presence_of :text } +end