Skip to content

Commit

Permalink
Add a form for product interest
Browse files Browse the repository at this point in the history
  • Loading branch information
c-lliope committed Sep 20, 2016
1 parent adc2cbf commit 6a8da1c
Show file tree
Hide file tree
Showing 17 changed files with 127 additions and 60 deletions.
18 changes: 18 additions & 0 deletions app/assets/stylesheets/_interest.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.interest {
margin-top: $base-spacing;
}

.new_interest {
display: flex;
align-items: stretch;

.btn {
flex: 0 0 auto;
margin-bottom: $small-spacing;
}

.email {
flex: 1 0 auto;
margin-right: $small-spacing;
}
}
3 changes: 1 addition & 2 deletions app/assets/stylesheets/_timer.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
.timer,
.timer-explanation {
.timer {
font-family: 'Source Sans Pro', sans-serif;
}

Expand Down
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
@import "base/base";
@import "refills/flashes";

@import "interest";
@import "layout";
@import "left_column";
@import "posts";
Expand Down
2 changes: 1 addition & 1 deletion app/assets/stylesheets/base/_buttons.scss
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
-webkit-font-smoothing: antialiased;
font-weight: 600;
line-height: 1;
padding: $small-spacing $base-spacing;
padding: 0 $base-spacing;
text-align: center;
text-decoration: none;
transition: background-color $base-duration $base-timing;
Expand Down
25 changes: 25 additions & 0 deletions app/controllers/interests_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# frozen_string_literal: true

class InterestsController < ApplicationController
def create
@interest = Interest.new(interest_params)

if @interest.save
redirect_back(
fallback_location: root_path,
notice: t("interest.create.success"),
)
else
redirect_back(
fallback_location: root_path,
alert: t("interest.create.failure"),
)
end
end

private

def interest_params
params.require(:interest).permit(:email)
end
end
6 changes: 5 additions & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
module ApplicationHelper
def expiration_time
Post.order(created_at: :desc).first.created_at + 7.days
if Post.any?
Post.order(created_at: :desc).first.created_at + 7.days
else
Time.current
end
end
end
5 changes: 5 additions & 0 deletions app/models/interest.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class Interest < ApplicationRecord
validates :email, presence: true
end
14 changes: 14 additions & 0 deletions app/views/application/_interest.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<div class="interest">
<h2>Want a self-destructing blog of your own?</h2>

<p>
Let me know.
If enough people want a blog of their own like this,
I'll turn it into something that anyone can use.
</p>

<%= simple_form_for(@interest || Interest.new) do |form| %>
<%= form.input :email, label: false, placeholder: "Email" %>
<%= form.button :submit %>
<% end %>
</div>
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<div class="content">
<div class="left-column">
<%= render "application/timer" %>
<%= render "application/interest" %>
</div>

<div class="right-column">
Expand Down
10 changes: 10 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ en:
with_weekday:
"%a %m/%d/%y"

helpers:
submit:
interest:
create: I'll take one, please.

interest:
create:
success: Thanks for your interest! I'll send you an email if there are any updates.
failure: Damn, that didn't work. Try again?

time:
formats:
default:
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Rails.application.routes.draw do
resources :posts
resources :interests, only: [:create]

root to: "posts#index"
end
9 changes: 9 additions & 0 deletions db/migrate/20160920004216_create_interests.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateInterests < ActiveRecord::Migration[5.0]
def change
create_table :interests do |t|
t.string :email, null: false

t.timestamps
end
end
end
8 changes: 7 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160909164244) do
ActiveRecord::Schema.define(version: 20160920004216) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand All @@ -30,6 +30,12 @@
t.index ["priority", "run_at"], name: "delayed_jobs_priority", using: :btree
end

create_table "interests", force: :cascade do |t|
t.string "email", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "posts", force: :cascade do |t|
t.string "title"
t.string "tags"
Expand Down
55 changes: 0 additions & 55 deletions spec/controllers/posts_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,37 +36,6 @@
# PostsController. Be sure to keep this updated too.
let(:valid_session) { {} }

describe "GET #index" do
it "assigns all posts as @posts" do
post = Post.create! valid_attributes
get :index, params: {}, session: valid_session
expect(assigns(:posts)).to eq([post])
end
end

describe "GET #show" do
it "assigns the requested post as @post" do
post = Post.create! valid_attributes
get :show, params: {id: post.to_param}, session: valid_session
expect(assigns(:post)).to eq(post)
end
end

describe "GET #new" do
it "assigns a new post as @post" do
get :new, params: {}, session: valid_session
expect(assigns(:post)).to be_a_new(Post)
end
end

describe "GET #edit" do
it "assigns the requested post as @post" do
post = Post.create! valid_attributes
get :edit, params: {id: post.to_param}, session: valid_session
expect(assigns(:post)).to eq(post)
end
end

describe "POST #create" do
context "with valid params" do
it "creates a new Post" do
Expand All @@ -75,24 +44,13 @@
}.to change(Post, :count).by(1)
end

it "assigns a newly created post as @post" do
post :create, params: {post: valid_attributes}, session: valid_session
expect(assigns(:post)).to be_a(Post)
expect(assigns(:post)).to be_persisted
end

it "redirects to the created post" do
post :create, params: {post: valid_attributes}, session: valid_session
expect(response).to redirect_to(Post.last)
end
end

context "with invalid params" do
it "assigns a newly created but unsaved post as @post" do
post :create, params: {post: invalid_attributes}, session: valid_session
expect(assigns(:post)).to be_a_new(Post)
end

it "re-renders the 'new' template" do
post :create, params: {post: invalid_attributes}, session: valid_session
expect(response).to render_template("new")
Expand All @@ -113,12 +71,6 @@
skip("Add assertions for updated state")
end

it "assigns the requested post as @post" do
post = Post.create! valid_attributes
put :update, params: {id: post.to_param, post: valid_attributes}, session: valid_session
expect(assigns(:post)).to eq(post)
end

it "redirects to the post" do
post = Post.create! valid_attributes
put :update, params: {id: post.to_param, post: valid_attributes}, session: valid_session
Expand All @@ -127,12 +79,6 @@
end

context "with invalid params" do
it "assigns the post as @post" do
post = Post.create! valid_attributes
put :update, params: {id: post.to_param, post: invalid_attributes}, session: valid_session
expect(assigns(:post)).to eq(post)
end

it "re-renders the 'edit' template" do
post = Post.create! valid_attributes
put :update, params: {id: post.to_param, post: invalid_attributes}, session: valid_session
Expand All @@ -155,5 +101,4 @@
expect(response).to redirect_to(posts_url)
end
end

end
3 changes: 3 additions & 0 deletions spec/factories.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
FactoryGirl.define do
factory :interest do
email "MyString"
end
factory :post do
title "MyString"
tags "MyString"
Expand Down
21 changes: 21 additions & 0 deletions spec/features/register_interest_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
require "rails_helper"

feature "Register interest" do
scenario "successfully" do
visit root_path

fill_in "Email", with: "[email protected]"
click_on "I want in."

expect(page).to have_content t("interest.create.success")
expect(Interest.last.email).to eq("[email protected]")
end

scenario "unsuccessfully" do
visit root_path

click_on "I want in."

expect(page).to have_content t("interest.create.failure")
end
end
5 changes: 5 additions & 0 deletions spec/models/interest_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Interest, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit 6a8da1c

Please sign in to comment.