Skip to content

Commit

Permalink
numerous style fixes, guard setup, rubocop tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Ch4s3 committed Oct 10, 2017
1 parent 618a971 commit d611058
Show file tree
Hide file tree
Showing 21 changed files with 97 additions and 115 deletions.
10 changes: 8 additions & 2 deletions .rubocop.yml
Expand Up @@ -11,9 +11,15 @@ Metrics/LineLength:
Max: 100

Metrics/AbcSize:
Max: 17
Max: 20

Metrics/BlockLength:
Max: 30
Exclude:
- 'spec/**/*'
- 'spec/**/*'

Style/Documentation:
Exclude:
- 'app/controllers/application_controller.rb'
- 'app/models/application_record.rb'
- 'app/mailers/application_mailer.rb'
34 changes: 9 additions & 25 deletions Gemfile
@@ -1,7 +1,7 @@
source 'https://rubygems.org'

git_source(:github) do |repo_name|
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/")
repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?('/')
"https://github.com/#{repo_name}.git"
end

Expand All @@ -11,35 +11,20 @@ gem 'rails', '~> 5.1.4'
gem 'devise'
gem 'httparty'
gem 'nokogiri'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.18'
# Use Puma as the app server
gem 'puma', '~> 3.7'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
# Use CoffeeScript for .coffee assets and views
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
gem 'sentry-raven'
gem 'sidekiq'
gem 'skylight'
gem 'turbolinks', '~> 5'
gem 'tzinfo-data'
gem 'uglifier', '>= 1.3.0'
gem 'webpacker'

group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
gem 'byebug'
gem 'faker'
gem 'pry-byebug', require: false
gem 'pry-clipboard'
Expand All @@ -52,7 +37,9 @@ end
group :development do
gem 'brakeman', require: false
gem 'bullet'
gem 'guard-bundler', require: false
gem 'guard-rspec', require: false
gem 'guard-rubocop'
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'pronto'
gem 'pronto-fasterer', require: false
Expand All @@ -68,11 +55,8 @@ end
group :test do
gem 'coveralls', require: false
gem 'fakeredis', require: 'fakeredis/rspec'
gem 'rspec-benchmark'
gem 'mutant-rspec'
gem 'rspec-benchmark'
gem 'rspec-rails', '~> 3.6'
gem 'rspec-sidekiq'
end

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
13 changes: 12 additions & 1 deletion Gemfile.lock
Expand Up @@ -129,11 +129,18 @@ GEM
pry (>= 0.9.12)
shellany (~> 0.0)
thor (>= 0.18.1)
guard-bundler (2.1.0)
bundler (~> 1.0)
guard (~> 2.2)
guard-compat (~> 1.1)
guard-compat (1.2.1)
guard-rspec (4.7.3)
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
guard-rubocop (1.3.0)
guard (~> 2.0)
rubocop (~> 0.20)
httparty (0.15.6)
multi_xml (>= 0.5.2)
i18n (0.8.6)
Expand Down Expand Up @@ -386,6 +393,8 @@ GEM
turbolinks-source (5.0.3)
tzinfo (1.2.3)
thread_safe (~> 0.1)
tzinfo-data (1.2017.2)
tzinfo (>= 1.0.0)
uglifier (3.2.0)
execjs (>= 0.3.0, < 3)
unicode-display_width (1.3.0)
Expand Down Expand Up @@ -430,7 +439,9 @@ DEPENDENCIES
devise
faker
fakeredis
guard-bundler
guard-rspec
guard-rubocop
httparty
listen (>= 3.0.5, < 3.2)
mutant-rspec
Expand Down Expand Up @@ -466,4 +477,4 @@ DEPENDENCIES
yard

BUNDLED WITH
1.16.0.pre.2
1.16.0.pre.3
17 changes: 17 additions & 0 deletions Guardfile
Expand Up @@ -65,3 +65,20 @@ guard :rspec, cmd: 'bundle exec rspec' do
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
end
end

guard :bundler do
require 'guard/bundler'
require 'guard/bundler/verify'
helper = Guard::Bundler::Verify.new

files = ['Gemfile']
files += Dir['*.gemspec'] if files.any? { |f| helper.uses_gemspec?(f) }

# Assume files are symlinked from somewhere
files.each { |file| watch(helper.real_path(file)) }
end

guard :rubocop do
watch(%r{.+\.rb$})
watch(%r{(?:.+/)?\.rubocop(?:_todo)?\.yml$}) { |m| File.dirname(m[0]) }
end
2 changes: 2 additions & 0 deletions app/components/ny_times/builder.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module NyTimes
# A builder for articles that come from the New York Times
class Builder
Expand Down
2 changes: 2 additions & 0 deletions app/components/ny_times/client.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

module NyTimes
# HTTParty client for the NewYork Times that takes an article path
# and get's the text of the article
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/application_controller.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
before_action :set_raven_context
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/articles_controller.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Controller for Articles and related search functionality
class ArticlesController < ApplicationController
before_action :find_article, only: [:show]
Expand Down
2 changes: 2 additions & 0 deletions app/forms/articles_search_form.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# Simple class for bilding search forms
class ArticlesSearchForm < FormObject
attr_accessor :title, :publications, :search_offset
Expand Down
4 changes: 3 additions & 1 deletion app/forms/form_object.rb
@@ -1,4 +1,6 @@
# frozen_string_literal: true

# Top level object for all form objects
class FormObject
include ActiveModel::Model
end
end
3 changes: 0 additions & 3 deletions app/helpers/application_helper.rb

This file was deleted.

13 changes: 12 additions & 1 deletion app/models/article.rb
@@ -1,3 +1,5 @@
# frozen_string_literal: true

# AcriveRecord model for articles, core to the app
class Article < ApplicationRecord
belongs_to :publication, optional: true
Expand All @@ -8,9 +10,18 @@ class Article < ApplicationRecord
#
# @param article_hash [Hash]
# @param article_id [Integer]
# @return [Article|nil]
# @return [Boolean|nil]
def self.update_with_hash_by_id(article_hash, article_id)
article = Article.find_by(id: article_id)
article.update(article_hash) if article
end

# Selects 10 rows of`:title`, `:id`, & `:uuid` from a collection of
# articles and offsets them by a given ammount, defaulting to 0
#
# @param offset [Integer]
# @return [Article::ActiveRecord_Relation]
def self.ten_ids_with_offset(offset = 0)
select(:title, :id, :uuid).order('id desc').limit(10).offset(offset)
end
end
3 changes: 3 additions & 0 deletions app/models/publication.rb
@@ -1,3 +1,6 @@
# frozen_string_literal: true

# Model for publications which own articles e.g. New York Times
class Publication < ApplicationRecord
has_many :articles
scope :new_york_times, -> { find_by(name: 'New York Times') }
Expand Down
3 changes: 3 additions & 0 deletions app/models/user.rb
@@ -1,3 +1,6 @@
# frozen_string_literal: true

# Basic user model generated by `devise`
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
Expand Down
7 changes: 2 additions & 5 deletions app/services/articles/finder.rb
Expand Up @@ -13,14 +13,11 @@ def self.find(publication_name, article_title, offset = 0)
cache_key = "articles/#{publication_name}/#{article_title}/offset=#{offset}"
Rails.cache.fetch(cache_key, expires_in: 10.minutes) do
if publication_name.strip.empty?
Article.fuzzy_title(article_title)
.select(:title, :id, :uuid).order('id desc')
.limit(10).offset(offset)
Article.fuzzy_title(article_title).ten_ids_with_offset(offset)
else
Article.joins(:publication).fuzzy_title(article_title)
.where('publications.name = ?', publication_name)
.select(:title, :id, :uuid).order('id desc')
.limit(10).offset(offset)
.ten_ids_with_offset(offset)
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions app/workers/article_worker.rb
@@ -1,10 +1,12 @@
# A background worker that builds new Articles from links
# or updates existing articles with excerpts if they
# have been created by the ui
class ArticleWorker
include Sidekiq::Worker

def perform(url, article_id = nil)
uri = URI(url)
case
when uri.host =~ /nytimes.com/
if uri.host =~ /nytimes.com/
client = NyTimes::Client.new(uri.path, article_id)
client.article
end
Expand Down
4 changes: 2 additions & 2 deletions spec/controllers/articles_controller_spec.rb
@@ -1,7 +1,6 @@
require 'rails_helper'

RSpec.describe ArticlesController, type: :controller do

describe 'GET #new' do
let(:user) { User.create(email: 'test@is.test', password: '1234Password') }

Expand All @@ -22,7 +21,8 @@
let(:post_params) do
{ article: {
title: 'Some Title', link: 'www.test.test',
publication: { publication_id: 1 } } }
publication: { publication_id: 1 }
} }
end
it 'returns http success for signed in users' do
sign_in(user)
Expand Down
11 changes: 10 additions & 1 deletion spec/models/article_spec.rb
Expand Up @@ -14,11 +14,20 @@
describe '#update_with_hash_by_id' do
let!(:article) { Article.create(title: 'Bluth Banana Stand Burns') }
let(:new_hash) { { title: 'Bluth Bananas Burn', link: 'https://news.test.test/bananas' } }
let(:subject) { Article.update_with_hash_by_id(new_hash, article.id) }
let(:id) { article.id }
let(:subject) { Article.update_with_hash_by_id(new_hash, id) }
it 'finds and updates an article wih a valid hash of data' do
expect(subject).to eq(true)
expect(article.reload.title).to eq(new_hash[:title])
expect(article.reload.link).to eq(new_hash[:link])
end

context 'with out nonexistent id' do
let(:id) { Article.order(:id).max.id + 1 }

it 'returns nil' do
expect(subject).to eq(nil)
end
end
end
end
2 changes: 1 addition & 1 deletion spec/rails_helper.rb
Expand Up @@ -7,7 +7,7 @@
ENV['RAILS_ENV'] ||= 'test'
require File.expand_path('../../config/environment', __FILE__)
# Prevent database truncation if the environment is production
abort("The Rails environment is running in production mode!") if Rails.env.production?
abort('The Rails environment is running in production mode!') if Rails.env.production?
require 'rspec/rails'
require 'sidekiq/testing'
# Add additional requires below this line. Rails is not loaded until this point!
Expand Down

0 comments on commit d611058

Please sign in to comment.