Skip to content

Commit

Permalink
Fix RuboCop offenses
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Mar 29, 2014
1 parent c6814a9 commit a491e0f
Show file tree
Hide file tree
Showing 30 changed files with 87 additions and 91 deletions.
1 change: 1 addition & 0 deletions .travis.yml
@@ -1,6 +1,7 @@
---
before_install: gem update bundler
before_script: bundle exec rake db:create db:schema:load
after_script: bundle exec rubocop -R
bundler_args: --without assets:development:production
language: ruby
rvm:
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/addresses_controller.rb
Expand Up @@ -3,10 +3,10 @@ class AddressesController < ApplicationController

def show
@address = Address.geocode("#{params[:address]}, #{params[:city_state]}")
unless @address.blank?
respond_with @address
if @address.blank?
render(json: {errors: {address: [t('errors.not_found', thing: t('defaults.address'))]}}, status: 404)
else
render(json: {errors: {address: [t("errors.not_found", thing: t("defaults.address"))]}}, status: 404)
respond_with @address
end
end
end
8 changes: 4 additions & 4 deletions app/controllers/application_controller.rb
Expand Up @@ -2,8 +2,8 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
before_filter :set_flash_from_params
before_filter :set_locale
before_action :set_flash_from_params
before_action :set_locale

protected

Expand All @@ -16,8 +16,8 @@ def set_flash_from_params
end

def set_locale
available_languages = Dir.glob(Rails.root + "config/locales/??.yml").map do |file|
File.basename(file, ".yml")
available_languages = Dir.glob(Rails.root + 'config/locales/??.yml').collect do |file|
File.basename(file, '.yml')
end
I18n.locale = http_accept_language.compatible_language_from(available_languages) || I18n.default_locale
end
Expand Down
8 changes: 4 additions & 4 deletions app/controllers/info_window_controller.rb
Expand Up @@ -3,15 +3,15 @@ def index
@thing = Thing.find_by_id(params[:thing_id])
if @thing.adopted?
if user_signed_in? && current_user == @thing.user
render("users/thank_you")
render('users/thank_you')
else
render("users/profile")
render('users/profile')
end
else
if user_signed_in?
render("things/adopt")
render('things/adopt')
else
render("users/sign_in")
render('users/sign_in')
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/passwords_controller.rb
Expand Up @@ -12,7 +12,7 @@ def create
def edit
self.resource = resource_class.new
resource.reset_password_token = params[:reset_password_token]
render("edit", layout: "info_window")
render('edit', layout: 'info_window')
end

def update
Expand All @@ -22,7 +22,7 @@ def update
resource.unlock_access! if unlockable?(resource)
sign_in(resource_name, resource)
end
redirect_to(controller: "main", action: "index")
redirect_to(controller: 'main', action: 'index')
end

private
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/sessions_controller.rb
@@ -1,5 +1,5 @@
class SessionsController < Devise::SessionsController
skip_before_filter :verify_authenticity_token, only: [:destroy]
skip_before_action :verify_authenticity_token, only: [:destroy]

def new
redirect_to(root_path)
Expand All @@ -12,7 +12,7 @@ def create
yield resource if block_given?
render(json: resource)
else
render(json: {errors: {password: [t("errors.password")]}}, status: 401)
render(json: {errors: {password: [t('errors.password')]}}, status: 401)
end
end

Expand Down
8 changes: 4 additions & 4 deletions app/controllers/things_controller.rb
Expand Up @@ -3,10 +3,10 @@ class ThingsController < ApplicationController

def show
@things = Thing.find_closest(params[:lat], params[:lng], params[:limit] || 10)
unless @things.blank?
respond_with @things
if @things.blank?
render(json: {errors: {address: [t('errors.not_found', thing: t('defaults.thing'))]}}, status: 404)
else
render(json: {errors: {address: [t("errors.not_found", thing: t("defaults.thing"))]}}, status: 404)
respond_with @things
end
end

Expand All @@ -19,7 +19,7 @@ def update
end
end

private
private

def thing_params
params.require(:thing).permit(:name, :user_id)
Expand Down
8 changes: 3 additions & 5 deletions app/controllers/users_controller.rb
@@ -1,16 +1,15 @@
class UsersController < Devise::RegistrationsController
def edit
render("sidebar/edit_profile", layout: "sidebar")
render('sidebar/edit_profile', layout: 'sidebar')
end

def update
self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email)
if update_resource(resource, account_update_params)
yield resource if block_given?
sign_in(resource_name, resource, bypass: true)
flash[:notice] = "Profile updated!"
redirect_to(controller: "sidebar", action: "search")
flash[:notice] = 'Profile updated!'
redirect_to(controller: 'sidebar', action: 'search')
else
clean_up_passwords(resource)
render(json: {errors: resource.errors}, status: 500)
Expand Down Expand Up @@ -38,5 +37,4 @@ def sign_up_params
def account_update_params
params.require(:user).permit(:address_1, :address_2, :city, :current_password, :email, :name, :organization, :password, :password_confirmation, :remember_me, :sms_number, :state, :voice_number, :zip)
end

end
2 changes: 1 addition & 1 deletion app/helpers/application_helper.rb
@@ -1,5 +1,5 @@
module ApplicationHelper
def us_states
def us_states # rubocop:disable MethodLength
[
['Massachusetts', 'MA'],
['Alabama', 'AL'],
Expand Down
9 changes: 2 additions & 7 deletions app/mailers/thing_mailer.rb
@@ -1,14 +1,9 @@
class ThingMailer < ActionMailer::Base
default from: "adoptahydrant@cityofboston.gov"
default from: 'adoptahydrant@cityofboston.gov'

def reminder(thing)
@thing = thing
@user = thing.user
mail(
{
to: thing.user.email,
subject: ["Remember to shovel", thing.name].compact.join(' '),
}
)
mail(to: thing.user.email, subject: ['Remember to shovel', thing.name].compact.join(' '))
end
end
2 changes: 1 addition & 1 deletion app/models/address.rb
Expand Up @@ -2,6 +2,6 @@ class Address
include Geokit::Geocoders

def self.geocode(address)
MultiGeocoder.geocode(address).ll.split(',').map{|s| s.to_f}
MultiGeocoder.geocode(address).ll.split(',').collect { |s| s.to_f }
end
end
8 changes: 5 additions & 3 deletions app/models/reminder.rb
@@ -1,7 +1,9 @@
class Reminder < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
validates_presence_of :from_user, :to_user, :thing
belongs_to :from_user, class_name: "User"
belongs_to :to_user, class_name: "User"
belongs_to :from_user, class_name: 'User'
belongs_to :thing
belongs_to :to_user, class_name: 'User'
validates :from_user, presence: true
validates :thing, presence: true
validates :to_user, presence: true
end
7 changes: 4 additions & 3 deletions app/models/thing.rb
Expand Up @@ -2,12 +2,13 @@

class Thing < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
validates_uniqueness_of :city_id, allow_nil: true
validates_presence_of :lat, :lng
belongs_to :user
has_many :reminders
validates :city_id, uniqueness: true, allow_nil: true
validates :lat, presence: true
validates :lng, presence: true

def self.find_closest(lat, lng, limit=10)
def self.find_closest(lat, lng, limit = 10)
query = <<-SQL
SELECT *, (3959 * ACOS(COS(RADIANS(?)) * COS(RADIANS(lat)) * COS(RADIANS(lng) - RADIANS(?)) + SIN(RADIANS(?)) * SIN(RADIANS(lat)))) AS distance
FROM things
Expand Down
20 changes: 9 additions & 11 deletions app/models/user.rb
@@ -1,21 +1,19 @@
class User < ActiveRecord::Base
include ActiveModel::ForbiddenAttributesProtection
# Include default devise modules. Others available are:
# :token_authenticatable, :confirmable,
# :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable,
:trackable, :validatable
:trackable, :validatable
before_validation :remove_non_digits_from_phone_numbers
has_many :reminders_from, class_name: 'Reminder', foreign_key: 'from_user_id'
has_many :reminders_to, class_name: 'Reminder', foreign_key: 'to_user_id'
has_many :things
validates :name, presence: true
validates_formatting_of :email, using: :email
validates_formatting_of :sms_number, using: :us_phone, allow_blank: true
validates_formatting_of :voice_number, using: :us_phone, allow_blank: true
validates_formatting_of :zip, using: :us_zip, allow_blank: true
validates_presence_of :name
has_many :reminders_to, class_name: "Reminder", foreign_key: "to_user_id"
has_many :reminders_from, class_name: "Reminder", foreign_key: "from_user_id"
has_many :things
before_validation :remove_non_digits_from_phone_numbers

def remove_non_digits_from_phone_numbers
self.sms_number = self.sms_number.to_s.gsub(/\D/, '').to_i if self.sms_number.present?
self.voice_number = self.voice_number.to_s.gsub(/\D/, '').to_i if self.voice_number.present?
self.sms_number = sms_number.to_s.gsub(/\D/, '').to_i if sms_number.present?
self.voice_number = voice_number.to_s.gsub(/\D/, '').to_i if voice_number.present?
end
end
2 changes: 1 addition & 1 deletion config/boot.rb
@@ -1,4 +1,4 @@
# Set up gems listed in the Gemfile.
ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__)

require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE'])
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
4 changes: 2 additions & 2 deletions config/environments/production.rb
Expand Up @@ -82,8 +82,8 @@
end

ActionMailer::Base.smtp_settings = {
address: "smtp.sendgrid.net",
port: "25",
address: 'smtp.sendgrid.net',
port: '25',
authentication: :plain,
user_name: ENV['SENDGRID_USERNAME'],
password: ENV['SENDGRID_PASSWORD'],
Expand Down
2 changes: 1 addition & 1 deletion config/environments/test.rb
Expand Up @@ -14,7 +14,7 @@

# Configure static asset server for tests with Cache-Control for performance.
config.serve_static_assets = true
config.static_cache_control = "public, max-age=3600"
config.static_cache_control = 'public, max-age=3600'

# Show full error reports and disable caching.
config.consider_all_requests_local = true
Expand Down
6 changes: 3 additions & 3 deletions config/initializers/devise.rb
Expand Up @@ -41,12 +41,12 @@
# Configure which authentication keys should be case-insensitive.
# These keys will be downcased upon creating or modifying a user and when used
# to authenticate or find a user. Default is :email.
config.case_insensitive_keys = [ :email ]
config.case_insensitive_keys = [:email]

# Configure which authentication keys should have whitespace stripped.
# These keys will have whitespace before and after removed upon creating or
# modifying a user and when used to authenticate or find a user. Default is :email.
config.strip_whitespace_keys = [ :email ]
config.strip_whitespace_keys = [:email]

# Tell if authentication through request.params is enabled. True by default.
# It can be set to an array that will enable params authentication only for the
Expand Down Expand Up @@ -95,7 +95,7 @@
config.stretches = Rails.env.test? ? 1 : 10

# Setup a pepper to generate the encrypted password.
config.pepper = "d0ce05a602094357144e8d2ce90258904f8cb26fb943cefd6fe0b824752616a9254fadabed3a47ba5c0de66a359513768ab1ab233d9cfef893f376a9b5ebcf68"
config.pepper = 'd0ce05a602094357144e8d2ce90258904f8cb26fb943cefd6fe0b824752616a9254fadabed3a47ba5c0de66a359513768ab1ab233d9cfef893f376a9b5ebcf68'

# ==> Configuration for :confirmable
# A period that the user is allowed to access the website even without
Expand Down
2 changes: 1 addition & 1 deletion config/initializers/rails_admin.rb
@@ -1,5 +1,5 @@
RailsAdmin.config do |config|
config.authenticate_with do
redirect_to(main_app.root_path, flash: {warning: "You must be signed-in as an administrator to access that page"}) unless signed_in? && current_user.admin?
redirect_to(main_app.root_path, flash: {warning: 'You must be signed-in as an administrator to access that page'}) unless signed_in? && current_user.admin?
end
end
2 changes: 1 addition & 1 deletion config/initializers/secret_token.rb
Expand Up @@ -8,7 +8,7 @@
# You can use `rake secret` to generate a secure secret key.

if Rails.env.production? && ENV['SECRET_TOKEN'].blank?
raise 'The SECRET_TOKEN environment variable is not set.\n
fail 'The SECRET_TOKEN environment variable is not set.\n
To generate it, run "rake secret", then set it with "heroku config:set SECRET_TOKEN=the_token_you_generated"'
end

Expand Down
5 changes: 2 additions & 3 deletions db/migrate/00000000000002_add_devise_to_users.rb
Expand Up @@ -3,7 +3,7 @@ def up
change_table(:users) do |t|
## Database authenticatable
# t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
t.string :encrypted_password, null: false, default: ''

## Recoverable
t.string :reset_password_token
Expand All @@ -30,7 +30,6 @@ def up
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at


# Uncomment below if timestamps were not included in your original model.
# t.timestamps
end
Expand All @@ -44,6 +43,6 @@ def up
def down
# By default, we don't want to make any assumption about how to roll back a migration when your
# model already existed. Please edit below which fields you would like to remove in this migration.
raise ActiveRecord::IrreversibleMigration
fail ActiveRecord::IrreversibleMigration
end
end
Expand Up @@ -10,6 +10,6 @@ def change
t.timestamps
end

add_index(:rails_admin_histories, [:item, :table, :month, :year], name: 'index_rails_admin_histories' )
add_index(:rails_admin_histories, [:item, :table, :month, :year], name: 'index_rails_admin_histories')
end
end
14 changes: 7 additions & 7 deletions test/controllers/addresses_controller_test.rb
Expand Up @@ -2,19 +2,19 @@

class AddressesControllerTest < ActionController::TestCase
test 'should return latitude and longitude for a valid address' do
stub_request(:get, "http://maps.google.com/maps/api/geocode/json").
with(query: {address: "City Hall, Boston, MA", sensor: "false"}).
stub_request(:get, 'http://maps.google.com/maps/api/geocode/json').
with(query: {address: 'City Hall, Boston, MA', sensor: 'false'}).
to_return(body: File.read(File.expand_path('../../fixtures/city_hall.json', __FILE__)))
get :show, address: 'City Hall', city_state: "Boston, MA", format: 'json'
get :show, address: 'City Hall', city_state: 'Boston, MA', format: 'json'
assert_not_nil assigns :address
end

test 'should return an error for an invalid address' do
stub_request(:get, "http://maps.google.com/maps/api/geocode/json").
with(query: {address: ", ", sensor: "false"}).
stub_request(:get, 'http://maps.google.com/maps/api/geocode/json').
with(query: {address: ', ', sensor: 'false'}).
to_return(body: File.read(File.expand_path('../../fixtures/unknown_address.json', __FILE__)))
stub_request(:get, "http://geocoder.us/service/csv/geocode").
with(query: {address: ", "}).
stub_request(:get, 'http://geocoder.us/service/csv/geocode').
with(query: {address: ', '}).
to_return(body: File.read(File.expand_path('../../fixtures/unknown_address.json', __FILE__)))
get :show, address: '', city_state: '', format: 'json'
assert_response :missing
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/info_window_controller_test.rb
Expand Up @@ -17,7 +17,7 @@ class InfoWindowControllerTest < ActionController::TestCase
assert_template 'users/thank_you'
assert_select 'h2', 'Thank you for adopting this hydrant!'
assert_select 'form#abandon_form' do
assert_select '[action=?]', "/things"
assert_select '[action=?]', '/things'
assert_select '[method=?]', 'post'
end
assert_select 'input[name="_method"]' do
Expand Down Expand Up @@ -48,7 +48,7 @@ class InfoWindowControllerTest < ActionController::TestCase
assert_template :adopt
assert_select 'h2', 'Adopt this Hydrant'
assert_select 'form#adoption_form' do
assert_select '[action=?]', "/things"
assert_select '[action=?]', '/things'
assert_select '[method=?]', 'post'
end
assert_select 'input[name="_method"]' do
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/main_controller_test.rb
Expand Up @@ -4,7 +4,7 @@ class MainControllerTest < ActionController::TestCase
include Devise::TestHelpers

setup do
request.env["devise.mapping"] = Devise.mappings[:user]
request.env['devise.mapping'] = Devise.mappings[:user]
@user = users(:erik)
end

Expand Down

0 comments on commit a491e0f

Please sign in to comment.