1
0
mirror of https://github.com/danbee/danbarberphoto synced 2025-03-04 08:49:07 +00:00

Merge pull request #4 from danbee/refactor-stuff

Refactor
This commit is contained in:
Daniel Barber 2018-02-14 22:07:20 -05:00 committed by GitHub
commit 93cd1a3585
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
25 changed files with 82 additions and 85 deletions

View File

@ -1,6 +1,3 @@
Metrics/LineLength:
Max: 120
Documentation: Documentation:
Enabled: false Enabled: false

View File

@ -13,6 +13,6 @@ class ApplicationController < ActionController::Base
end end
def render_404 def render_404
render 'errors/not_found', status: :not_found render "errors/not_found", status: :not_found
end end
end end

View File

@ -2,11 +2,11 @@ class CategoriesController < ApplicationController
# GET /categories # GET /categories
# GET /categories.xml # GET /categories.xml
def index def index
@categories = Category.order('sort ASC').page(params[:page]).per(4) @categories = Category.order("sort ASC").page(params[:page]).per(4)
@photos = Photo.featured.limit(2).order('RANDOM()') @photos = Photo.featured.limit(2).order("RANDOM()")
@page_title = 'Portfolio' @page_title = "Portfolio"
@num_blank = 4 - @categories.length @num_blank = 4 - @categories.length

View File

@ -1,16 +1,16 @@
class ContactsController < ApplicationController class ContactsController < ApplicationController
def new def new
@contact = Contact.new(id: 1) @contact = Contact.new(id: 1)
@page_title = 'Contact' @page_title = "Contact"
end end
def create def create
@contact = Contact.new(params[:contact]) @contact = Contact.new(params[:contact])
if @contact.valid? if @contact.valid?
Notifier.contact_notification(@contact).deliver Notifier.contact_notification(@contact).deliver
redirect_to(:new_contact, notice: t('contact.thanks')) redirect_to(:new_contact, notice: t("contact.thanks"))
else else
flash[:alert] = t('contact.invalid') flash[:alert] = t("contact.invalid")
render :new render :new
end end
end end

View File

@ -1,5 +1,5 @@
class HomeController < ApplicationController class HomeController < ApplicationController
def index def index
@photos = Photo.featured.order('RANDOM()').limit(1) @photos = Photo.featured.order("RANDOM()").limit(1)
end end
end end

View File

@ -25,6 +25,6 @@ class PhotosController < ApplicationController
def all def all
@photos = Photo.enabled.order(taken_at: :desc) @photos = Photo.enabled.order(taken_at: :desc)
.page(params[:page]) .page(params[:page])
@page_title = 'All Photos' @page_title = "All Photos"
end end
end end

View File

@ -1,5 +1,5 @@
class SessionsController < ApplicationController class SessionsController < ApplicationController
layout 'admin/login' layout "admin/login"
skip_before_action :require_login, only: [:new, :create], raise: false skip_before_action :require_login, only: [:new, :create], raise: false

View File

@ -2,6 +2,6 @@
class EmailValidator < ActiveModel::EachValidator class EmailValidator < ActiveModel::EachValidator
def validate_each(object, attribute, value) def validate_each(object, attribute, value)
return if value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i return if value =~ /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i
object.errors[attribute] << (options[:message] || 'is not valid') object.errors[attribute] << (options[:message] || "is not valid")
end end
end end

View File

@ -7,10 +7,10 @@
<meta name="keywords" content="photography, images, landscapes" /> <meta name="keywords" content="photography, images, landscapes" />
<meta name="description" content="The photography portfolio of Dan Barber" /> <meta name="description" content="The photography portfolio of Dan Barber" />
<meta name="language" content="English" /> <meta name="language" content="English" />
<%= stylesheet_link_tag 'application' %> <%= stylesheet_link_tag "application" %>
<%= javascript_include_tag 'application' %> <%= javascript_include_tag "application" %>
<%= render "shared/fonts" %> <%= render "shared/fonts" %>
<%= favicon_link_tag 'favicon.ico' %> <%= favicon_link_tag "favicon.ico" %>
<%= csrf_meta_tags %> <%= csrf_meta_tags %>
</head> </head>
@ -23,7 +23,7 @@
<div class="sgParent sg-12"> <div class="sgParent sg-12">
<header class="sg-11"> <header class="sg-11">
<%= link_to image_tag(asset_url('title.svg'), alt: 'Dan Barber Photography', width: 236), '/' %> <%= link_to image_tag(asset_url("title.svg"), alt: "Dan Barber Photography", width: 236), "/" %>
</header> </header>
<%= yield :navigation %> <%= yield :navigation %>
@ -33,12 +33,12 @@
<%= yield %> <%= yield %>
</div> </div>
<%= render 'shared/footer' %> <%= render "shared/footer" %>
</div> </div>
</div> </div>
<%= render 'shared/analytics' if Rails.env.production? %> <%= render "shared/analytics" if Rails.env.production? %>
</body> </body>

View File

@ -2,7 +2,7 @@
<div class="sg-5 about"> <div class="sg-5 about">
</div> </div>
<div class="sg-5 portfolio"> <div class="sg-5 portfolio">
<%= link_to content_tag(:span, 'portfolio'), controller: 'categories' %> <%= link_to content_tag(:span, "portfolio"), controller: "categories" %>
</div> </div>
<% end %> <% end %>
@ -12,7 +12,7 @@
</div> </div>
</div> </div>
<div class="sg-11" style="background: url(<%= asset_url('me.jpg') %>);"> <div class="sg-11" style="background: url(<%= asset_url("me.jpg") %>);">
</div> </div>
<div class="sg-5" style="background: #222;"> <div class="sg-5" style="background: #222;">
@ -22,5 +22,5 @@
<div class="sg-5" style="background: #555;"> <div class="sg-5" style="background: #555;">
</div> </div>
<div class="sg-5 contact"> <div class="sg-5 contact">
<%= link_to content_tag(:span, 'contact'), new_contact_path %> <%= link_to content_tag(:span, "contact"), new_contact_path %>
</div> </div>

View File

@ -1,3 +1,3 @@
<% if !Rails.env.test? %> <% if !Rails.env.test? %>
<%= stylesheet_link_tag 'https://cloud.typography.com/7434852/6922572/css/fonts.css' %> <%= stylesheet_link_tag "https://cloud.typography.com/7434852/6922572/css/fonts.css" %>
<% end %> <% end %>

View File

@ -1,6 +1,6 @@
<div class="sg-5 about"> <div class="sg-5 about">
<%= link_to content_tag(:span, 'about'), page_path(:about) %> <%= link_to content_tag(:span, "about"), page_path(:about) %>
</div> </div>
<div class="sg-5 portfolio"> <div class="sg-5 portfolio">
<%= link_to content_tag(:span, 'portfolio'), controller: 'categories' %> <%= link_to content_tag(:span, "portfolio"), controller: "categories" %>
</div> </div>

View File

@ -1,8 +1,8 @@
require 'spec_helper' require "spec_helper"
describe ContactsController, type: :controller do describe ContactsController, type: :controller do
describe 'GET new' do describe "GET new" do
it 'renders the contact form' do it "renders the contact form" do
get :new get :new
expect(response).to render_template(:new) expect(response).to render_template(:new)
end end
@ -10,28 +10,28 @@ describe ContactsController, type: :controller do
let(:contact_params) do let(:contact_params) do
{ {
name: 'Dan Barber', name: "Dan Barber",
email: 'danbee@gmail.com', email: "danbee@gmail.com",
message: 'This is a message.', message: "This is a message.",
} }
end end
describe 'POST create' do describe "POST create" do
it 'saves a new contact' do it "saves a new contact" do
valid_contact = double(valid?: true) valid_contact = double(valid?: true)
allow(Contact).to receive(:new).and_return(valid_contact) allow(Contact).to receive(:new).and_return(valid_contact)
allow(Notifier).to receive(:contact_notification).and_return(double(deliver: true)) allow(Notifier).to receive(:contact_notification).and_return(double(deliver: true))
post :create, params: { contact: contact_params } post :create, params: { contact: contact_params }
expect(Notifier).to have_received(:contact_notification).with(valid_contact) expect(Notifier).to have_received(:contact_notification).with(valid_contact)
expect(flash[:notice]).to eql(I18n.t('contact.thanks')) expect(flash[:notice]).to eql(I18n.t("contact.thanks"))
expect(response).to redirect_to(:new_contact) expect(response).to redirect_to(:new_contact)
end end
it 're-renders the form if params are missing' do it "re-renders the form if params are missing" do
allow(Contact).to receive(:new).and_return(double(valid?: false)) allow(Contact).to receive(:new).and_return(double(valid?: false))
post :create, params: { contact: {} } post :create, params: { contact: {} }
expect(flash[:alert]).to eql(I18n.t('contact.invalid')) expect(flash[:alert]).to eql(I18n.t("contact.invalid"))
expect(response).to render_template(:new) expect(response).to render_template(:new)
end end
end end

View File

@ -1,8 +1,8 @@
require 'spec_helper' require "spec_helper"
describe HomeController, type: :controller do describe HomeController, type: :controller do
describe 'GET index' do describe "GET index" do
it 'renders the index template' do it "renders the index template" do
get :index get :index
expect(response).to render_template(:index) expect(response).to render_template(:index)
end end

View File

@ -1,16 +1,16 @@
require 'spec_helper' require "spec_helper"
describe PagesController, type: :controller do describe PagesController, type: :controller do
describe 'GET show' do describe "GET show" do
let(:test_page) { create(:page) } let(:test_page) { create(:page) }
it 'renders a page' do it "renders a page" do
get :show, params: { name: test_page.name } get :show, params: { name: test_page.name }
expect(response).to render_template(:show) expect(response).to render_template(:show)
end end
it 'renders 404 for a non existant page' do it "renders 404 for a non existant page" do
get :show, params: { name: 'not-a-page' } get :show, params: { name: "not-a-page" }
expect(response.status).to eql(404) expect(response.status).to eql(404)
end end
end end

View File

@ -1,8 +1,8 @@
require 'spec_helper' require "spec_helper"
describe PhotosController, type: :controller do describe PhotosController, type: :controller do
describe 'GET index' do describe "GET index" do
it 'renders the index template' do it "renders the index template" do
get :index get :index
expect(response).to render_template(:index) expect(response).to render_template(:index)
end end

View File

@ -1,16 +1,16 @@
require 'spec_helper' require "spec_helper"
RSpec.describe ViewsController, type: :controller do RSpec.describe ViewsController, type: :controller do
describe 'POST create' do describe "POST create" do
let(:photo) { create(:photo) } let(:photo) { create(:photo) }
it 'logs a photo view' do it "logs a photo view" do
expect_any_instance_of(Photo).to receive(:log_view).once expect_any_instance_of(Photo).to receive(:log_view).once
post :create, params: { photo_id: photo.id } post :create, params: { photo_id: photo.id }
expect(response).to be_successful expect(response).to be_successful
end end
it 'responds with not_found if the photo is not present' do it "responds with not_found if the photo is not present" do
post :create, params: { photo_id: 999 } post :create, params: { photo_id: 999 }
expect(response.status).to eql(404) expect(response.status).to eql(404)
end end

View File

@ -2,8 +2,8 @@
FactoryGirl.define do FactoryGirl.define do
factory :contact do factory :contact do
email 'test@danbarberphoto.com' email "test@danbarberphoto.com"
name 'Dan Barber' name "Dan Barber"
message 'This is a message.' message "This is a message."
end end
end end

View File

@ -2,8 +2,8 @@
FactoryGirl.define do FactoryGirl.define do
factory :page do factory :page do
name 'page' name "page"
title 'Page' title "Page"
content 'This is a page.' content "This is a page."
end end
end end

View File

@ -2,8 +2,8 @@
FactoryGirl.define do FactoryGirl.define do
factory :photo do factory :photo do
title 'A Photo' title "A Photo"
description 'A lovely photo of a tree' description "A lovely photo of a tree"
image Rails.root.join('spec/fixtures/photo.jpg') image Rails.root.join("spec/fixtures/photo.jpg")
end end
end end

View File

@ -1,7 +1,7 @@
require 'spec_helper' require "spec_helper"
feature 'visitor navigates site' do feature "visitor navigates site" do
it 'shows the featured image on the home page' do it "shows the featured image on the home page" do
category = create(:category) category = create(:category)
photo = create(:photo, featured: true, categories: [category]) photo = create(:photo, featured: true, categories: [category])
@ -9,7 +9,7 @@ feature 'visitor navigates site' do
expect(page).to have_selector("a[data-id='#{photo.id}']") expect(page).to have_selector("a[data-id='#{photo.id}']")
end end
it 'increments the view counter when an image is displayed', js: true do it "increments the view counter when an image is displayed", js: true do
category = create(:category) category = create(:category)
photo = create(:photo, featured: true, categories: [category]) photo = create(:photo, featured: true, categories: [category])
@ -19,7 +19,7 @@ feature 'visitor navigates site' do
page.find(selector).click page.find(selector).click
expect(page).to have_selector('img.fancybox-image') expect(page).to have_selector("img.fancybox-image")
wait_for_ajax wait_for_ajax
photo.reload photo.reload
@ -27,20 +27,20 @@ feature 'visitor navigates site' do
expect(photo.views).to eq(1) expect(photo.views).to eq(1)
end end
it 'shows the categories' do it "shows the categories" do
categories = create_list(:category, 5) categories = create_list(:category, 5)
visit root_path visit root_path
click_link 'portfolio' click_link "portfolio"
expect(page).to have_link(categories.first.name.downcase) expect(page).to have_link(categories.first.name.downcase)
click_link '→' click_link ""
expect(page).to have_link(categories.last.name.downcase) expect(page).to have_link(categories.last.name.downcase)
end end
it 'shows the photos for the category' do it "shows the photos for the category" do
category = create(:category) category = create(:category)
photo = create(:photo, featured: true, categories: [category]) photo = create(:photo, featured: true, categories: [category])
@ -52,13 +52,13 @@ feature 'visitor navigates site' do
expect(page).to have_selector(selector) expect(page).to have_selector(selector)
end end
it 'shows the second page of photos' do it "shows the second page of photos" do
category = create(:category) category = create(:category)
photos = create_list(:photo, 12, featured: true, categories: [category]) photos = create_list(:photo, 12, featured: true, categories: [category])
visit url_for([category, :photos]) visit url_for([category, :photos])
click_link '→' click_link ""
selector = "a[data-id='#{photos.last.id}']" selector = "a[data-id='#{photos.last.id}']"
expect(page).to have_selector(selector) expect(page).to have_selector(selector)

View File

@ -1,4 +1,4 @@
require 'spec_helper' require "spec_helper"
describe Category do describe Category do
it { should have_and_belong_to_many(:photos) } it { should have_and_belong_to_many(:photos) }
@ -7,9 +7,9 @@ describe Category do
it { should validate_uniqueness_of(:name) } it { should validate_uniqueness_of(:name) }
it { should validate_uniqueness_of(:slug) } it { should validate_uniqueness_of(:slug) }
let(:category) { create(:category, name: 'A Test Category') } let(:category) { create(:category, name: "A Test Category") }
it 'should have a slug generated from name' do it "should have a slug generated from name" do
expect(category.slug).to eql('a-test-category') expect(category.slug).to eql("a-test-category")
end end
end end

View File

@ -1,12 +1,12 @@
require 'spec_helper' require "spec_helper"
describe Contact do describe Contact do
it { should validate_presence_of(:email) } it { should validate_presence_of(:email) }
it { should validate_presence_of(:name) } it { should validate_presence_of(:name) }
it { should validate_presence_of(:message) } it { should validate_presence_of(:message) }
it { should allow_value('test@test.com').for(:email) } it { should allow_value("test@test.com").for(:email) }
it { should_not allow_value('test@test').for(:email) } it { should_not allow_value("test@test").for(:email) }
let(:contact) { build(:contact) } let(:contact) { build(:contact) }
end end

View File

@ -1,4 +1,4 @@
require 'spec_helper' require "spec_helper"
describe Photo do describe Photo do
it { should have_and_belong_to_many(:categories) } it { should have_and_belong_to_many(:categories) }
@ -7,7 +7,7 @@ describe Photo do
let(:photo) { create(:photo) } let(:photo) { create(:photo) }
it 'logs a view' do it "logs a view" do
expect { photo.log_view }.to change { photo.views }.by(1) expect { photo.log_view }.to change { photo.views }.by(1)
end end
end end

View File

@ -1,12 +1,12 @@
require 'spec_helper' require "spec_helper"
RSpec.describe User, type: :model do RSpec.describe User, type: :model do
it { is_expected.to validate_presence_of(:email) } it { is_expected.to validate_presence_of(:email) }
it { is_expected.to validate_presence_of(:password_digest) } it { is_expected.to validate_presence_of(:password_digest) }
it 'validates uniqueness of email' do it "validates uniqueness of email" do
create(:user, email: 'test@example.com') create(:user, email: "test@example.com")
user = User.new(email: 'test@example.com') user = User.new(email: "test@example.com")
expect(user).to validate_uniqueness_of(:email) expect(user).to validate_uniqueness_of(:email)
end end