From 6dac0637512f794afbead7ed7d883018f806f9b8 Mon Sep 17 00:00:00 2001 From: Dan Barber Date: Wed, 16 Sep 2015 16:20:11 +0100 Subject: [PATCH] WIP: Rubocop compliance --- .rubocop.yml | 5 +++++ Gemfile | 1 + Gemfile.lock | 15 +++++++++++++ app/controllers/application_controller.rb | 5 ++--- app/controllers/categories_controller.rb | 1 - app/controllers/contacts_controller.rb | 4 ++-- app/controllers/photos_controller.rb | 22 +++++++++++++++----- config/initializers/dragonfly.rb | 16 +++++--------- spec/controllers/contacts_controller_spec.rb | 20 +++++++++--------- spec/controllers/home_controller_spec.rb | 4 ++-- spec/controllers/pages_controller_spec.rb | 8 +++---- spec/controllers/photos_controller_spec.rb | 10 ++++----- spec/factories/categories.rb | 2 +- spec/factories/contacts.rb | 6 +++--- spec/factories/pages.rb | 6 +++--- spec/factories/photos.rb | 4 ++-- spec/features/visitor_navigates_site_spec.rb | 1 - spec/models/category_spec.rb | 6 +++--- spec/models/contact_spec.rb | 6 +++--- spec/models/photo_spec.rb | 4 ++-- spec/spec_helper.rb | 8 +++---- 21 files changed, 89 insertions(+), 65 deletions(-) create mode 100644 .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..91acef6 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,5 @@ +Metrics/LineLength: + Max: 99 + +Documentation: + Enabled: false diff --git a/Gemfile b/Gemfile index 27ad0e9..f8fb7aa 100644 --- a/Gemfile +++ b/Gemfile @@ -47,6 +47,7 @@ end group :test, :development do gem 'rspec-rails' + gem 'rubocop' gem 'pry' end diff --git a/Gemfile.lock b/Gemfile.lock index 649729b..f5b46da 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -35,6 +35,9 @@ GEM tzinfo (~> 1.1) addressable (2.3.8) arel (5.0.1.20140414130214) + ast (2.1.0) + astrolabe (1.3.1) + parser (~> 2.2) aws-sdk (2.1.29) aws-sdk-resources (= 2.1.29) aws-sdk-core (2.1.29) @@ -149,6 +152,8 @@ GEM nokogiri (1.6.6.2) mini_portile (~> 0.6.0) orm_adapter (0.5.0) + parser (2.2.3.0) + ast (>= 1.1, < 3.0) pg (0.18.3) poltergeist (1.7.0) capybara (~> 2.1) @@ -159,6 +164,7 @@ GEM activerecord (>= 3.0) powder (0.3.0) thor (>= 0.11.5) + powerpack (0.1.1) pry (0.10.2) coderay (~> 1.1.0) method_source (~> 0.8.1) @@ -187,6 +193,7 @@ GEM activesupport (= 4.1.13) rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) + rainbow (2.0.0) rake (10.4.2) redcarpet (3.3.3) responders (1.1.2) @@ -208,6 +215,13 @@ GEM rspec-mocks (~> 3.3.0) rspec-support (~> 3.3.0) rspec-support (3.3.0) + rubocop (0.34.2) + astrolabe (~> 1.3) + parser (>= 2.2.2.5, < 3.0) + powerpack (~> 0.1) + rainbow (>= 1.99.1, < 3.0) + ruby-progressbar (~> 1.4) + ruby-progressbar (1.7.5) ruby_parser (3.7.1) sexp_processor (~> 4.1) sass (3.4.19) @@ -300,6 +314,7 @@ DEPENDENCIES rails_12factor redcarpet rspec-rails + rubocop ruby_parser sass-rails (~> 5.0.0) shoulda diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index f83aefc..a4f3f59 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -3,16 +3,15 @@ class ApplicationController < ActionController::Base rescue_from ActiveRecord::RecordNotFound, with: :render_404 - def after_sign_in_path_for(resource_or_scope) + def after_sign_in_path_for(*) admin_dashboard_path end - def after_sign_out_path_for(resource_or_scope) + def after_sign_out_path_for(*) new_admin_user_session_path end def render_404 render 'errors/not_found', status: :not_found end - end diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index 9ada889..a0a9e66 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -26,5 +26,4 @@ class CategoriesController < ApplicationController format.xml { render xml: @category } end end - end diff --git a/app/controllers/contacts_controller.rb b/app/controllers/contacts_controller.rb index 69ef4cb..bd22e8f 100644 --- a/app/controllers/contacts_controller.rb +++ b/app/controllers/contacts_controller.rb @@ -7,9 +7,9 @@ class ContactsController < ApplicationController def create @contact = Contact.new(params[:contact]) if @contact.save - redirect_to(:new_contact, notice: t("contact.thanks")) + redirect_to(:new_contact, notice: t('contact.thanks')) else - flash[:alert] = t("contact.invalid") + flash[:alert] = t('contact.invalid') render :new end end diff --git a/app/controllers/photos_controller.rb b/app/controllers/photos_controller.rb index 91c6b7a..3c09154 100644 --- a/app/controllers/photos_controller.rb +++ b/app/controllers/photos_controller.rb @@ -1,12 +1,9 @@ class PhotosController < ApplicationController def index if params[:category_id] - @category = Category.find_by_id(params[:category_id]) - @photos = @category.photos.enabled.order{taken_at.desc}.paginate(page: params[:page], per_page: 11) - @page_title = @category.name + for_category(params[:category_id]) else - @photos = Photo.enabled.order{taken_at.desc}.paginate(page: params[:page], per_page: 11) - @page_title = 'All Photos' + all end @num_blank = 11 - @photos.length @@ -25,4 +22,19 @@ class PhotosController < ApplicationController head :not_found end end + + private + + def with_category(category_id) + @category = Category.find_by_id(category_id) + @photos = @category.photos.enabled.order { taken_at.desc } + .paginate(page: params[:page], per_page: 11) + @page_title = @category.name + end + + def alls + @photos = Photo.enabled.order { taken_at.desc } + .paginate(page: params[:page], per_page: 11) + @page_title = 'All Photos' + end end diff --git a/config/initializers/dragonfly.rb b/config/initializers/dragonfly.rb index 235c087..edb6e03 100644 --- a/config/initializers/dragonfly.rb +++ b/config/initializers/dragonfly.rb @@ -9,17 +9,11 @@ Dragonfly.app.configure do url_format "/media/:job/:name" - if %w[development test].include? Rails.env - datastore :file, - root_path: Rails.root.join('public/system/dragonfly', Rails.env), - server_root: Rails.root.join('public') - else - datastore :s3, - bucket_name: ENV['AWS_BUCKET'], - access_key_id: ENV['AWS_KEY'], - secret_access_key: ENV['AWS_SECRET'], - region: 'eu-west-1' - end + datastore :s3, + bucket_name: ENV['AWS_BUCKET'], + access_key_id: ENV['AWS_KEY'], + secret_access_key: ENV['AWS_SECRET'], + region: 'eu-west-1' processor :preview do |content| content.process! :thumb, '600x600' end diff --git a/spec/controllers/contacts_controller_spec.rb b/spec/controllers/contacts_controller_spec.rb index 282b813..9a928ff 100644 --- a/spec/controllers/contacts_controller_spec.rb +++ b/spec/controllers/contacts_controller_spec.rb @@ -1,30 +1,30 @@ require 'spec_helper' describe ContactsController, type: :controller do - describe "GET new" do - it "renders the contact form" do + describe 'GET new' do + it 'renders the contact form' do get :new expect(response).to render_template(:new) end end let(:contact_params) do - { name: "Dan Barber", - email: "danbee@gmail.com", - message: "This is a message." } + { name: 'Dan Barber', + email: 'danbee@gmail.com', + message: 'This is a message.' } end - describe "POST create" do - it "saves a new contact" do + describe 'POST create' do + it 'saves a new contact' do expect_any_instance_of(Contact).to receive(:save).once.and_return(true) post :create, contact: contact_params - 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) end - it "re-renders the form if params are missing" do + it 're-renders the form if params are missing' do post :create, 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) end end diff --git a/spec/controllers/home_controller_spec.rb b/spec/controllers/home_controller_spec.rb index 304c68d..6c8f84e 100644 --- a/spec/controllers/home_controller_spec.rb +++ b/spec/controllers/home_controller_spec.rb @@ -1,8 +1,8 @@ require 'spec_helper' describe HomeController, type: :controller do - describe "GET index" do - it "renders the index template" do + describe 'GET index' do + it 'renders the index template' do get :index expect(response).to render_template(:index) end diff --git a/spec/controllers/pages_controller_spec.rb b/spec/controllers/pages_controller_spec.rb index 82b881f..22b7149 100644 --- a/spec/controllers/pages_controller_spec.rb +++ b/spec/controllers/pages_controller_spec.rb @@ -1,16 +1,16 @@ require 'spec_helper' describe PagesController, type: :controller do - describe "GET show" do + describe 'GET show' do let(:test_page) { create(:page) } - it "renders a page" do + it 'renders a page' do get :show, name: test_page.name expect(response).to render_template(:show) end - it "renders 404 for a non existant page" do - get :show, name: "not-a-page" + it 'renders 404 for a non existant page' do + get :show, name: 'not-a-page' expect(response.status).to eql(404) end end diff --git a/spec/controllers/photos_controller_spec.rb b/spec/controllers/photos_controller_spec.rb index c61791f..51728b2 100644 --- a/spec/controllers/photos_controller_spec.rb +++ b/spec/controllers/photos_controller_spec.rb @@ -1,23 +1,23 @@ require 'spec_helper' describe PhotosController, type: :controller do - describe "GET index" do - it "renders the index template" do + describe 'GET index' do + it 'renders the index template' do get :index expect(response).to render_template(:index) end end - describe "GET log_view" do + describe 'GET log_view' do 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 get :log_view, id: photo.id expect(response).to be_successful end - it "responds with not_found if the photo isn't present" do + it 'responds with not_found if the photo is not present' do get :log_view, id: 999 expect(response.status).to eql(404) end diff --git a/spec/factories/categories.rb b/spec/factories/categories.rb index 47c01a1..bfc7ba6 100644 --- a/spec/factories/categories.rb +++ b/spec/factories/categories.rb @@ -2,6 +2,6 @@ FactoryGirl.define do factory :category do - name "Test Category" + name 'Test Category' end end diff --git a/spec/factories/contacts.rb b/spec/factories/contacts.rb index 7dd2aa6..863b5a2 100644 --- a/spec/factories/contacts.rb +++ b/spec/factories/contacts.rb @@ -2,8 +2,8 @@ FactoryGirl.define do factory :contact do - email "test@danbarberphoto.com" - name "Dan Barber" - message "This is a message." + email 'test@danbarberphoto.com' + name 'Dan Barber' + message 'This is a message.' end end diff --git a/spec/factories/pages.rb b/spec/factories/pages.rb index 2e93c52..e74e9a6 100644 --- a/spec/factories/pages.rb +++ b/spec/factories/pages.rb @@ -2,8 +2,8 @@ FactoryGirl.define do factory :page do - name "page" - title "Page" - content "This is a page." + name 'page' + title 'Page' + content 'This is a page.' end end diff --git a/spec/factories/photos.rb b/spec/factories/photos.rb index 3baa1a4..f0716fa 100644 --- a/spec/factories/photos.rb +++ b/spec/factories/photos.rb @@ -2,8 +2,8 @@ FactoryGirl.define do factory :photo do - title "A Photo" - description "A lovely photo of a tree" + title 'A Photo' + description 'A lovely photo of a tree' image Rails.root.join('spec/fixtures/photo.jpg') end end diff --git a/spec/features/visitor_navigates_site_spec.rb b/spec/features/visitor_navigates_site_spec.rb index 59983ae..f7b73b8 100644 --- a/spec/features/visitor_navigates_site_spec.rb +++ b/spec/features/visitor_navigates_site_spec.rb @@ -1,7 +1,6 @@ require 'spec_helper' feature 'visitor navigates site' do - let!(:category) { create(:category) } let!(:photo) { create(:photo, featured: true, categories: [category]) } diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb index 781f025..de64ff4 100644 --- a/spec/models/category_spec.rb +++ b/spec/models/category_spec.rb @@ -7,9 +7,9 @@ describe Category do it { should validate_uniqueness_of(:name) } 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 - expect(category.slug).to eql("a-test-category") + it 'should have a slug generated from name' do + expect(category.slug).to eql('a-test-category') end end diff --git a/spec/models/contact_spec.rb b/spec/models/contact_spec.rb index 03ec286..7d9f241 100644 --- a/spec/models/contact_spec.rb +++ b/spec/models/contact_spec.rb @@ -5,12 +5,12 @@ describe Contact do it { should validate_presence_of(:name) } it { should validate_presence_of(:message) } - it { should allow_value("test@test.com").for(:email) } - it { should_not allow_value("test@test").for(:email) } + it { should allow_value('test@test.com').for(:email) } + it { should_not allow_value('test@test').for(:email) } let(:contact) { build(:contact) } - it "should send an email" do + it 'should send an email' do contact.save expect(ActionMailer::Base.deliveries.last.from).to eql([contact.email]) expect(ActionMailer::Base.deliveries.last.to).to eql(['enquiries@danbarberphoto.com']) diff --git a/spec/models/photo_spec.rb b/spec/models/photo_spec.rb index 21e51aa..b0d0128 100644 --- a/spec/models/photo_spec.rb +++ b/spec/models/photo_spec.rb @@ -7,7 +7,7 @@ describe Photo do let(:photo) { create(:photo) } - it "logs a view" do - expect { photo.log_view }.to change{photo.views}.by(1) + it 'logs a view' do + expect { photo.log_view }.to change { photo.views }.by(1) end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index a109ae7..d9efab3 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,6 +1,6 @@ # This file is copied to spec/ when you run 'rails generate rspec:install' -ENV["RAILS_ENV"] ||= 'test' -require File.expand_path("../../config/environment", __FILE__) +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../../config/environment', __FILE__) require 'rspec/rails' require 'capybara/rspec' require 'capybara/poltergeist' @@ -14,7 +14,7 @@ Capybara.javascript_driver = :poltergeist # Requires supporting ruby files with custom matchers and macros, etc, # in spec/support/ and its subdirectories. -Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f} +Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } RSpec.configure do |config| # ## Mock Framework @@ -46,7 +46,7 @@ RSpec.configure do |config| # order dependency and want to debug it, you can fix the order by providing # the seed, which is printed after each run. # --seed 1234 - config.order = "random" + config.order = 'random' config.before(:suite) do DatabaseCleaner.clean_with(:truncation)