diff --git a/.hound.yml b/.hound.yml new file mode 100644 index 0000000..5d0ff60 --- /dev/null +++ b/.hound.yml @@ -0,0 +1,2 @@ +ruby: + config_file: .rubocop.yml diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..332bcc3 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,17 @@ +inherit_from: .rubocop_todo.yml + +AllCops: + Exclude: + - 'db/schema.rb' + DisplayCopNames: true + ExtraDetails: true + TargetRubyVersion: 2.3 + +Rails: + Enabled: true + +Style/FrozenStringLiteralComment: + Enabled: false + +Style/StringLiterals: + EnforcedStyle: single_quotes diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..54338a3 --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,26 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2016-06-20 19:08:22 -0400 using RuboCop version 0.40.0. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +Metrics/AbcSize: + Max: 16 + +# Offense count: 102 +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes. +# URISchemes: http, https +Metrics/LineLength: + Max: 184 + +# Offense count: 2 +# Configuration parameters: CountComments. +Metrics/MethodLength: + Max: 17 + +# Offense count: 17 +Style/Documentation: + Enabled: false diff --git a/Gemfile b/Gemfile index 449f4c9..a44f3e5 100644 --- a/Gemfile +++ b/Gemfile @@ -32,7 +32,7 @@ gem 'jbuilder', '~> 2.0' gem 'rollbar' gem 'pry' gem 'aws-sdk', '~> 2.3' -gem 'paperclip', :git=> 'https://github.com/thoughtbot/paperclip', :ref => '523bd46c768226893f23889079a7aa9c73b57d68' +gem 'paperclip', git: 'https://github.com/thoughtbot/paperclip', ref: '523bd46c768226893f23889079a7aa9c73b57d68' gem 'dotenv-rails' gem 'gmaps4rails' diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 4c68dc5..b809d60 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -4,11 +4,11 @@ class ApplicationController < ActionController::Base protect_from_forgery with: :exception, prepend: true before_action :configure_permitted_parameters, if: :devise_controller? - rescue_from CanCan::AccessDenied do |exception| - respond_to do |format| - format.html { redirect_to root_path, :alert => "Unauthorized"} - end + rescue_from CanCan::AccessDenied do |_exception| + respond_to do |format| + format.html { redirect_to root_path, alert: 'Unauthorized' } end + end before_action :configure_permitted_parameters, if: :devise_controller? protected diff --git a/app/controllers/observations_controller.rb b/app/controllers/observations_controller.rb index 77b63e8..e338c81 100644 --- a/app/controllers/observations_controller.rb +++ b/app/controllers/observations_controller.rb @@ -70,20 +70,21 @@ def destroy def gmaps_hash @hash = Gmaps4rails.build_markers(@observations) do |observation, marker| - marker.lat observation.latitude - marker.lng observation.longitude - marker.infowindow observation.location - end + marker.lat observation.latitude + marker.lng observation.longitude + marker.infowindow observation.location + end end private - # Use callbacks to share common setup or constraints between actions. - def set_observation - @observation = Observation.find(params[:id]) - end - # Never trust parameters from the scary internet, only allow the white list through. - def observation_params - params.require(:observation).permit(:sighted_at, :location, :latitude, :longitude, :num_bands, :photo) - end + # Use callbacks to share common setup or constraints between actions. + def set_observation + @observation = Observation.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def observation_params + params.require(:observation).permit(:sighted_at, :location, :latitude, :longitude, :num_bands, :photo) + end end diff --git a/app/controllers/users/invitations_controller.rb b/app/controllers/users/invitations_controller.rb index f353d0b..04d8d62 100644 --- a/app/controllers/users/invitations_controller.rb +++ b/app/controllers/users/invitations_controller.rb @@ -1,13 +1,16 @@ -class Users::InvitationsController < Devise::InvitationsController - before_action :configure_permitted_parameters, if: :devise_controller? - before_action :check_authorization +module Users + class InvitationsController < Devise::InvitationsController + before_action :configure_permitted_parameters, if: :devise_controller? + before_action :check_authorization - private - def configure_permitted_parameters - devise_parameter_sanitizer.permit(:invite, keys: [:role]) - end + private + + def configure_permitted_parameters + devise_parameter_sanitizer.permit(:invite, keys: [:role]) + end - def check_authorization - can? :invite, User + def check_authorization + can? :invite, User + end end end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index f911462..96ab3ce 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,7 +1,6 @@ class UsersController < ApplicationController load_and_authorize_resource - def index @title = 'All Users' @users = User.all @@ -9,7 +8,7 @@ def index def guests @title = 'Pending Approval' - @users = User.where("role" => "guest") + @users = User.where('role' => 'guest') render 'index' end @@ -25,10 +24,10 @@ def edit def update @user = User.find(params[:id]) redirect_to users_path, alert: 'Success!' if @user.update_attributes(user_params) - end private + def user_params params.require(:user).permit(:email, :name, :phone, :role) end diff --git a/app/models/ability.rb b/app/models/ability.rb index ce01217..5b91ec9 100644 --- a/app/models/ability.rb +++ b/app/models/ability.rb @@ -3,8 +3,6 @@ class Ability def initialize(user) user ||= User.new - if user.is_admin? - can :manage, :all - end + can :manage, :all if user.admin? end end diff --git a/app/models/observation.rb b/app/models/observation.rb index 113d945..a19c6d3 100644 --- a/app/models/observation.rb +++ b/app/models/observation.rb @@ -1,5 +1,5 @@ class Observation < ApplicationRecord - validates_inclusion_of :num_bands, in: 0..2 + validates :num_bands, inclusion: 0..2 has_attached_file :photo, styles: { thumb: '100x100>', @@ -7,5 +7,5 @@ class Observation < ApplicationRecord medium: '300x300>' } - validates_attachment_content_type :photo, :content_type => /\Aimage\/.*\Z/ + validates_attachment_content_type :photo, content_type: %r{\Aimage/.*\Z} end diff --git a/app/models/user.rb b/app/models/user.rb index 9d4c84b..804a01e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,17 +3,17 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable and :omniauthable devise :invitable, :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable - ROLES = ['admin', 'researcher', 'scientist'] + ROLES = %w(admin researcher scientist).freeze - def is_admin? - self.role == "admin" + def admin? + role == 'admin' end - def is_researcher? - self.role == "researcher" + def researcher? + role == 'researcher' end - def is_scientist? - self.role == "scientist" + def scientist? + role == 'scientist' end end diff --git a/config/application.rb b/config/application.rb index 51afc3e..854b907 100644 --- a/config/application.rb +++ b/config/application.rb @@ -1,15 +1,15 @@ require_relative 'boot' -require "rails" +require 'rails' # Pick the frameworks you want: -require "active_model/railtie" -require "active_job/railtie" -require "active_record/railtie" -require "action_controller/railtie" -require "action_mailer/railtie" -require "action_view/railtie" -require "action_cable/engine" -require "sprockets/railtie" +require 'active_model/railtie' +require 'active_job/railtie' +require 'active_record/railtie' +require 'action_controller/railtie' +require 'action_mailer/railtie' +require 'action_view/railtie' +require 'action_cable/engine' +require 'sprockets/railtie' # require "rails/test_unit/railtie" # Require the gems listed in Gemfile, including any gems diff --git a/config/environments/development.rb b/config/environments/development.rb index 4a6c1dd..c306abc 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -61,7 +61,7 @@ bucket: ENV.fetch('S3_BUCKET_NAME'), access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'), secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'), - s3_region: ENV.fetch('AWS_REGION'), + s3_region: ENV.fetch('AWS_REGION') } } end diff --git a/config/environments/production.rb b/config/environments/production.rb index ac0c1cc..9f03711 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -49,7 +49,7 @@ config.log_level = :debug # Prepend all log lines with the following tags. - config.log_tags = [ :request_id ] + config.log_tags = [:request_id] # Use a different cache store in production. # config.cache_store = :mem_cache_store @@ -91,7 +91,7 @@ # require 'syslog/logger' # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') - if ENV["RAILS_LOG_TO_STDOUT"].present? + if ENV['RAILS_LOG_TO_STDOUT'].present? logger = ActiveSupport::Logger.new(STDOUT) logger.formatter = config.log_formatter config.logger = ActiveSupport::TaggedLogging.new(logger) @@ -106,7 +106,7 @@ bucket: ENV.fetch('S3_BUCKET_NAME'), access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'), secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'), - s3_region: ENV.fetch('AWS_REGION'), + s3_region: ENV.fetch('AWS_REGION') } } end diff --git a/config/environments/test.rb b/config/environments/test.rb index 9482646..627382d 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -46,7 +46,7 @@ bucket: ENV.fetch('S3_BUCKET_NAME'), access_key_id: ENV.fetch('AWS_ACCESS_KEY_ID'), secret_access_key: ENV.fetch('AWS_SECRET_ACCESS_KEY'), - s3_region: ENV.fetch('AWS_REGION'), + s3_region: ENV.fetch('AWS_REGION') } } end diff --git a/config/initializers/rollbar.rb b/config/initializers/rollbar.rb index 6b0265c..a90be5e 100644 --- a/config/initializers/rollbar.rb +++ b/config/initializers/rollbar.rb @@ -5,9 +5,7 @@ config.access_token = ENV['ROLLBAR_ACCESS_TOKEN'] # Here we'll disable in 'test': - if Rails.env.test? - config.enabled = false - end + config.enabled = false if Rails.env.test? # By default, Rollbar will try to call the `current_user` controller method # to fetch the logged-in user object, and then call that object's `id`, diff --git a/config/puma.rb b/config/puma.rb index c7f311f..786af49 100644 --- a/config/puma.rb +++ b/config/puma.rb @@ -4,16 +4,16 @@ # the maximum value specified for Puma. Default is set to 5 threads for minimum # and maximum, this matches the default thread size of Active Record. # -threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 }.to_i +threads_count = ENV.fetch('RAILS_MAX_THREADS') { 5 }.to_i threads threads_count, threads_count # Specifies the `port` that Puma will listen on to receive requests, default is 3000. # -port ENV.fetch("PORT") { 3000 } +port ENV.fetch('PORT') { 3000 } # Specifies the `environment` that Puma will run in. # -environment ENV.fetch("RAILS_ENV") { "development" } +environment ENV.fetch('RAILS_ENV') { 'development' } # Specifies the number of `workers` to boot in clustered mode. # Workers are forked webserver processes. If using threads and workers together diff --git a/config/routes.rb b/config/routes.rb index 1a4ff1b..45d71b6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,8 +1,8 @@ Rails.application.routes.draw do root 'welcome#index' resources :observations - devise_for :users, :controllers => { :invitations => 'users/invitations' } - scope "/admin" do + devise_for :users, controllers: { invitations: 'users/invitations' } + scope '/admin' do resources :users get 'guests', to: 'users#guests' end diff --git a/db/migrate/20160617174555_devise_create_users.rb b/db/migrate/20160617174555_devise_create_users.rb index f942b33..bc17255 100644 --- a/db/migrate/20160617174555_devise_create_users.rb +++ b/db/migrate/20160617174555_devise_create_users.rb @@ -2,10 +2,10 @@ class DeviseCreateUsers < ActiveRecord::Migration[5.0] def change create_table :users do |t| ## Database authenticatable - t.string :email, null: false, default: "" - t.string :encrypted_password, null: false, default: "" - t.string :name, null: false, default: "" - t.string :phone, null: false, default: "" + t.string :email, null: false, default: '' + t.string :encrypted_password, null: false, default: '' + t.string :name, null: false, default: '' + t.string :phone, null: false, default: '' ## Recoverable t.string :reset_password_token @@ -32,7 +32,6 @@ def change # t.string :unlock_token # Only if unlock strategy is :email or :both # t.datetime :locked_at - t.timestamps null: false end diff --git a/db/migrate/20160617180935_create_observations.rb b/db/migrate/20160617180935_create_observations.rb index 1b1c37c..e7e960b 100644 --- a/db/migrate/20160617180935_create_observations.rb +++ b/db/migrate/20160617180935_create_observations.rb @@ -2,8 +2,8 @@ class CreateObservations < ActiveRecord::Migration[5.0] def change create_table :observations do |t| t.datetime :sighted_at - t.string :location, {precision: 10, scale: 6} - t.decimal :latitude, {precision: 10, scale: 6} + t.string :location, precision: 10, scale: 6 + t.decimal :latitude, precision: 10, scale: 6 t.decimal :longitude t.integer :num_bands diff --git a/db/seeds.rb b/db/seeds.rb index 3f03534..c30ac88 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,9 +5,9 @@ # # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) # Character.create(name: 'Luke', movie: movies.first) -puts "Adding a Whole bunch of Observations" +Rails.logger.info 'Adding a Whole bunch of Observations' -Observation.all.each {|o| o.delete} +Observation.all.each(&:delete) -Observation.create(sighted_at: DateTime.now, location: "Burke Lake Park, Fairfax Station, VA", latitude: 38, longitude: -77, num_bands: 1) -Observation.create(sighted_at: DateTime.now, location: "someplace cool", latitude: 37, longitude: -78, num_bands: 1) \ No newline at end of file +Observation.create(sighted_at: DateTime.current, location: 'Burke Lake Park, Fairfax Station, VA', latitude: 38, longitude: -77, num_bands: 1) +Observation.create(sighted_at: DateTime.current, location: 'someplace cool', latitude: 37, longitude: -78, num_bands: 1) diff --git a/spec/controllers/invitations_controller_spec.rb b/spec/controllers/invitations_controller_spec.rb index 45b7a38..378a88d 100644 --- a/spec/controllers/invitations_controller_spec.rb +++ b/spec/controllers/invitations_controller_spec.rb @@ -1,34 +1,33 @@ require 'rails_helper' RSpec.describe Users::InvitationsController, type: :controller do - describe "GET #new" do - - context "improper authorization" do - it "redirects to login page" do - @request.env["devise.mapping"] = Devise.mappings[:user] + describe 'GET #new' do + context 'improper authorization' do + it 'redirects to login page' do + @request.env['devise.mapping'] = Devise.mappings[:user] get :new expect(response).to redirect_to(new_user_session_path) end end end - context "proper authorization" do - let (:admin_params) { + context 'proper authorization' do + let(:admin_params) do { - email: "admin@gmail.com", - name: "Mr Admin", - role: "admin", - password: "password", - password_confirmation: "password" + email: 'admin@gmail.com', + name: 'Mr Admin', + role: 'admin', + password: 'password', + password_confirmation: 'password' } - } + end - let(:admin) { + let(:admin) do User.create!(admin_params) - } + end - it "loads invitation form" do - @request.env["devise.mapping"] = Devise.mappings[:user] + it 'loads invitation form' do + @request.env['devise.mapping'] = Devise.mappings[:user] sign_in admin get :new expect(response.status).to eq(200) diff --git a/spec/controllers/observations_controller_spec.rb b/spec/controllers/observations_controller_spec.rb index f1223bc..1a66f2e 100644 --- a/spec/controllers/observations_controller_spec.rb +++ b/spec/controllers/observations_controller_spec.rb @@ -19,21 +19,20 @@ # that an instance is receiving a specific message. RSpec.describe ObservationsController, type: :controller do - # This should return the minimal set of attributes required to create a valid # Observation. As you add validations to Observation, be sure to # adjust the attributes here as well. - let(:valid_attributes) { + let(:valid_attributes) do { - sighted_at:DateTime.new(2016, 5, 10, 10, 30, 0, '-5'), + sighted_at: DateTime.new(2016, 5, 10, 10, 30, 0, '-5'), location: 'Near my moms house', latitude: 38.918167, longitude: -78.194445, num_bands: 1 } - } + end - let(:invalid_attributes) { + let(:invalid_attributes) do { sighted_at: DateTime.new(2016, 5, 10, 10, 30, 0, '-5'), location: 'Near my moms house', @@ -41,96 +40,96 @@ longitude: -78.194445, num_bands: 3 } - } + end - let (:admin_params) { + let(:admin_params) do { - email: "admin@gmail.com", - name: "Mr Admin", - role: "admin", - password: "password", - password_confirmation: "password" + email: 'admin@gmail.com', + name: 'Mr Admin', + role: 'admin', + password: 'password', + password_confirmation: 'password' } - } + end - let(:admin) { + let(:admin) do User.create!(admin_params) - } + end # This should return the minimal set of values that should be in the session # in order to pass any filters (e.g. authentication) defined in # ObservationsController. Be sure to keep this updated too. let(:valid_session) { {} } - describe "GET #index" do - it "assigns all observations as @observations" do - @request.env["devise.mapping"] = Devise.mappings[:user] + describe 'GET #index' do + it 'assigns all observations as @observations' do + @request.env['devise.mapping'] = Devise.mappings[:user] sign_in admin - observation = Observation.create! valid_attributes - get :index, params: {}, session: valid_session - expect(assigns(:observations)).to eq(Observation.all) + Observation.create! valid_attributes + get :index, params: {}, session: valid_session + expect(assigns(:observations)).to eq(Observation.all) end end - describe "GET #show" do - it "assigns the requested observation as @observation" do + describe 'GET #show' do + it 'assigns the requested observation as @observation' do observation = Observation.create! valid_attributes - get :show, params: {id: observation.to_param}, session: valid_session - expect(assigns(:observation)).to eq(observation) + get :show, params: { id: observation.to_param }, session: valid_session + expect(assigns(:observation)).to eq(observation) end end - describe "GET #new" do - it "assigns a new observation as @observation" do - get :new, params: {}, session: valid_session - expect(assigns(:observation)).to be_a_new(Observation) + describe 'GET #new' do + it 'assigns a new observation as @observation' do + get :new, params: {}, session: valid_session + expect(assigns(:observation)).to be_a_new(Observation) end end - describe "GET #edit" do - it "assigns the requested observation as @observation" do + describe 'GET #edit' do + it 'assigns the requested observation as @observation' do observation = Observation.create! valid_attributes - get :edit, params: {id: observation.to_param}, session: valid_session - expect(assigns(:observation)).to eq(observation) + get :edit, params: { id: observation.to_param }, session: valid_session + expect(assigns(:observation)).to eq(observation) end end - describe "POST #create" do - context "with valid params" do - it "creates a new Observation" do - expect { - post :create, params: {observation: valid_attributes}, session: valid_session - }.to change(Observation, :count).by(1) + describe 'POST #create' do + context 'with valid params' do + it 'creates a new Observation' do + expect do + post :create, params: { observation: valid_attributes }, session: valid_session + end.to change(Observation, :count).by(1) end - it "assigns a newly created observation as @observation" do - post :create, params: {observation: valid_attributes}, session: valid_session - expect(assigns(:observation)).to be_a(Observation) + it 'assigns a newly created observation as @observation' do + post :create, params: { observation: valid_attributes }, session: valid_session + expect(assigns(:observation)).to be_a(Observation) expect(assigns(:observation)).to be_persisted end - it "redirects to the created observation" do - post :create, params: {observation: valid_attributes}, session: valid_session - expect(response).to redirect_to(Observation.last) + it 'redirects to the created observation' do + post :create, params: { observation: valid_attributes }, session: valid_session + expect(response).to redirect_to(Observation.last) end end - context "with invalid params" do - it "assigns a newly created but unsaved observation as @observation" do - post :create, params: {observation: invalid_attributes}, session: valid_session - expect(assigns(:observation)).to be_a_new(Observation) + context 'with invalid params' do + it 'assigns a newly created but unsaved observation as @observation' do + post :create, params: { observation: invalid_attributes }, session: valid_session + expect(assigns(:observation)).to be_a_new(Observation) end it "re-renders the 'new' template" do - post :create, params: {observation: invalid_attributes}, session: valid_session - expect(response).to render_template("new") + post :create, params: { observation: invalid_attributes }, session: valid_session + expect(response).to render_template('new') end end end - describe "PUT #update" do - context "with valid params" do - let(:new_attributes) { + describe 'PUT #update' do + context 'with valid params' do + let(:new_attributes) do { sighted_at: DateTime.new(2016, 5, 10, 10, 30, 0, '-5'), location: 'Near my dads house', @@ -138,9 +137,9 @@ longitude: -78.194445, num_bands: 1 } - } + end - let(:valid_attributes) { + let(:valid_attributes) do { sighted_at: DateTime.new(2016, 5, 10, 10, 30, 0, '-5'), location: 'Near my moms house', @@ -148,62 +147,61 @@ longitude: -78.194445, num_bands: 1 } - } + end let(:valid_session) { {} } - it "updates the requested observation" do + it 'updates the requested observation' do observation = Observation.create! valid_attributes - put :update, params: {id: observation.to_param, observation: new_attributes}, session: valid_session - observation.reload - expect(observation.location).to eql('Near my dads house') - expect(observation.sighted_at.utc.to_i).to eql(DateTime.new(2016, 5, 10, 10, 30, 0, '-5').to_i) - expect(observation.latitude).to eql(38.918167) - expect(observation.longitude).to eql(-78.194445) - expect(observation.num_bands).to eql(1) + put :update, params: { id: observation.to_param, observation: new_attributes }, session: valid_session + observation.reload + expect(observation.location).to eql('Near my dads house') + expect(observation.sighted_at.utc.to_i).to eql(DateTime.new(2016, 5, 10, 10, 30, 0, '-5').to_i) + expect(observation.latitude).to eql(38.918167) + expect(observation.longitude).to eql(-78.194445) + expect(observation.num_bands).to eql(1) end - it "assigns the requested observation as @observation" do + it 'assigns the requested observation as @observation' do observation = Observation.create! valid_attributes - put :update, params: {id: observation.to_param, observation: valid_attributes}, session: valid_session - expect(assigns(:observation)).to eq(observation) + put :update, params: { id: observation.to_param, observation: valid_attributes }, session: valid_session + expect(assigns(:observation)).to eq(observation) end - it "redirects to the observation" do + it 'redirects to the observation' do observation = Observation.create! valid_attributes - put :update, params: {id: observation.to_param, observation: valid_attributes}, session: valid_session - expect(response).to redirect_to(observation) + put :update, params: { id: observation.to_param, observation: valid_attributes }, session: valid_session + expect(response).to redirect_to(observation) end end - context "with invalid params" do - it "assigns the observation as @observation" do + context 'with invalid params' do + it 'assigns the observation as @observation' do observation = Observation.create! valid_attributes - put :update, params: {id: observation.to_param, observation: invalid_attributes}, session: valid_session - expect(assigns(:observation)).to eq(observation) + put :update, params: { id: observation.to_param, observation: invalid_attributes }, session: valid_session + expect(assigns(:observation)).to eq(observation) end it "re-renders the 'edit' template" do observation = Observation.create! valid_attributes - put :update, params: {id: observation.to_param, observation: invalid_attributes}, session: valid_session - expect(response).to render_template("edit") + put :update, params: { id: observation.to_param, observation: invalid_attributes }, session: valid_session + expect(response).to render_template('edit') end end end - describe "DELETE #destroy" do - it "destroys the requested observation" do + describe 'DELETE #destroy' do + it 'destroys the requested observation' do observation = Observation.create! valid_attributes - expect { - delete :destroy, params: {id: observation.to_param}, session: valid_session - }.to change(Observation, :count).by(-1) + expect do + delete :destroy, params: { id: observation.to_param }, session: valid_session + end.to change(Observation, :count).by(-1) end - it "redirects to the observations list" do + it 'redirects to the observations list' do observation = Observation.create! valid_attributes - delete :destroy, params: {id: observation.to_param}, session: valid_session - expect(response).to redirect_to(observations_url) + delete :destroy, params: { id: observation.to_param }, session: valid_session + expect(response).to redirect_to(observations_url) end end - end diff --git a/spec/controllers/users_controller_spec.rb b/spec/controllers/users_controller_spec.rb index 985d8ce..50ce6c8 100644 --- a/spec/controllers/users_controller_spec.rb +++ b/spec/controllers/users_controller_spec.rb @@ -1,53 +1,51 @@ require 'rails_helper' RSpec.describe UsersController, type: :controller do - - describe "Admin users should GET index access" do - let(:admin_params) { + describe 'Admin users should GET index access' do + let(:admin_params) do { - email: "admin@gmail.com", - name: "Mr Admin", - role: "admin", - password: "password", - password_confirmation: "password" + email: 'admin@gmail.com', + name: 'Mr Admin', + role: 'admin', + password: 'password', + password_confirmation: 'password' } - } + end - let(:admin) { + let(:admin) do User.create!(admin_params) - } + end - before { + before do allow(controller).to receive(:current_user).and_return(admin) - } + end - it "admin users have access to user index" do + it 'admin users have access to user index' do get :index, params: {} expect(response.status).to eq(200) end end - describe "NonAdmin users should not GET index access" do - - let(:nonadmin_params) { + describe 'NonAdmin users should not GET index access' do + let(:nonadmin_params) do { - email: "nonadmin@gmail.com", - name: "Mr User", - role: "scientist", - password: "password", - password_confirmation: "password" + email: 'nonadmin@gmail.com', + name: 'Mr User', + role: 'scientist', + password: 'password', + password_confirmation: 'password' } - } + end - let(:nonadmin) { + let(:nonadmin) do User.create!(nonadmin_params) - } + end - before { + before do allow(controller).to receive(:current_user).and_return(nonadmin) - } + end - it "nonadmin users should not have access to user index" do + it 'nonadmin users should not have access to user index' do get :index, params: {} expect(response.status).to eq(302) end diff --git a/spec/controllers/welcome_controller_spec.rb b/spec/controllers/welcome_controller_spec.rb index 332a230..e560f50 100644 --- a/spec/controllers/welcome_controller_spec.rb +++ b/spec/controllers/welcome_controller_spec.rb @@ -1,5 +1,4 @@ require 'rails_helper' RSpec.describe WelcomeController, type: :controller do - end diff --git a/spec/models/ability_spec.rb b/spec/models/ability_spec.rb index df7b32b..69f7756 100644 --- a/spec/models/ability_spec.rb +++ b/spec/models/ability_spec.rb @@ -2,13 +2,13 @@ require 'cancan/matchers' RSpec.describe Ability, type: :model do - it "can manage users when admin" do - user = User.new :role => "admin" + it 'can manage users when admin' do + user = User.new role: 'admin' ability = Ability.new(user) expect(ability).to be_able_to(:manage, User.new) end - it "cannot manage user when not admin" do + it 'cannot manage user when not admin' do user = User.new ability = Ability.new(user) expect(ability).not_to be_able_to(:manage, User.new) diff --git a/spec/models/observation_spec.rb b/spec/models/observation_spec.rb index 06c0c72..a719726 100644 --- a/spec/models/observation_spec.rb +++ b/spec/models/observation_spec.rb @@ -1,10 +1,9 @@ require 'rails_helper' RSpec.describe Observation, type: :model do - - it "can be valid" do + it 'can be valid' do observation = Observation.new( - location: "Location", + location: 'Location', latitude: 10.99, longitude: 11.99, num_bands: 0 @@ -12,9 +11,9 @@ expect(observation).to be_valid end - it "should have num bands greater than 0" do + it 'should have num bands greater than 0' do observation = Observation.new( - location: "Location", + location: 'Location', latitude: 10.99, longitude: 11.99, num_bands: -1 @@ -22,9 +21,9 @@ expect(observation).to_not be_valid end - it "should have num bands less than 3" do + it 'should have num bands less than 3' do observation = Observation.new( - location: "Location", + location: 'Location', latitude: 10.99, longitude: 11.99, num_bands: 3 diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 707d039..65cd8ee 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,27 +1,25 @@ require 'rails_helper' RSpec.describe User, type: :model do - - it "can be valid" do + it 'can be valid' do user = User.new( - email: "spacejame@gmail.com", - name: "Michael Jordan", - role: "admin", - password: "password", - password_confirmation: "password" + email: 'spacejame@gmail.com', + name: 'Michael Jordan', + role: 'admin', + password: 'password', + password_confirmation: 'password' ) expect(user).to be_valid end - it "can be an admin" do + it 'can be an admin' do user = User.new( - email: "spacejame@gmail.com", - name: "Michael Jordan", - role: "admin", - password: "password", - password_confirmation: "password" + email: 'spacejame@gmail.com', + name: 'Michael Jordan', + role: 'admin', + password: 'password', + password_confirmation: 'password' ) - expect(user.is_admin?).to be(true) + expect(user).to be_admin end - end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 95d8675..d7ccfa2 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -2,7 +2,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 'spec_helper' require 'rspec/rails' # Add additional requires below this line. Rails is not loaded until this point! @@ -29,7 +29,7 @@ RSpec.configure do |config| # configure devise rspec helpers - config.include Devise::TestHelpers, :type => :controller + config.include Devise::TestHelpers, type: :controller # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{::Rails.root}/spec/fixtures" diff --git a/spec/requests/observations_spec.rb b/spec/requests/observations_spec.rb index 7e0be64..c85b37c 100644 --- a/spec/requests/observations_spec.rb +++ b/spec/requests/observations_spec.rb @@ -1,16 +1,16 @@ require 'rails_helper' -RSpec.describe "Observations", type: :request do - describe "GET /observations" do - it "can get list" do +RSpec.describe 'Observations', type: :request do + describe 'GET /observations' do + it 'can get list' do get observations_path expect(response).to have_http_status(200) end end - describe "POST /observations" do - it "should create observation" do - post "/observations", params: {observation: {sighted_at: DateTime.new(2016, 5, 10, 10, 30, 0, '-5'), location: "somewhere", latitude: 40.00, longitude: -78.00, num_bands: 1}} + describe 'POST /observations' do + it 'should create observation' do + post '/observations', params: { observation: { sighted_at: DateTime.new(2016, 5, 10, 10, 30, 0, '-5'), location: 'somewhere', latitude: 40.00, longitude: -78.00, num_bands: 1 } } expect(response.code).to eql('302') expect(response).to redirect_to(assigns(:observation)) end diff --git a/spec/routing/observations_routing_spec.rb b/spec/routing/observations_routing_spec.rb index 868aa7d..719899b 100644 --- a/spec/routing/observations_routing_spec.rb +++ b/spec/routing/observations_routing_spec.rb @@ -1,39 +1,37 @@ -require "rails_helper" +require 'rails_helper' RSpec.describe ObservationsController, type: :routing do - describe "routing" do - - it "routes to #index" do - expect(:get => "/observations").to route_to("observations#index") + describe 'routing' do + it 'routes to #index' do + expect(get: '/observations').to route_to('observations#index') end - it "routes to #new" do - expect(:get => "/observations/new").to route_to("observations#new") + it 'routes to #new' do + expect(get: '/observations/new').to route_to('observations#new') end - it "routes to #show" do - expect(:get => "/observations/1").to route_to("observations#show", :id => "1") + it 'routes to #show' do + expect(get: '/observations/1').to route_to('observations#show', id: '1') end - it "routes to #edit" do - expect(:get => "/observations/1/edit").to route_to("observations#edit", :id => "1") + it 'routes to #edit' do + expect(get: '/observations/1/edit').to route_to('observations#edit', id: '1') end - it "routes to #create" do - expect(:post => "/observations").to route_to("observations#create") + it 'routes to #create' do + expect(post: '/observations').to route_to('observations#create') end - it "routes to #update via PUT" do - expect(:put => "/observations/1").to route_to("observations#update", :id => "1") + it 'routes to #update via PUT' do + expect(put: '/observations/1').to route_to('observations#update', id: '1') end - it "routes to #update via PATCH" do - expect(:patch => "/observations/1").to route_to("observations#update", :id => "1") + it 'routes to #update via PATCH' do + expect(patch: '/observations/1').to route_to('observations#update', id: '1') end - it "routes to #destroy" do - expect(:delete => "/observations/1").to route_to("observations#destroy", :id => "1") + it 'routes to #destroy' do + expect(delete: '/observations/1').to route_to('observations#destroy', id: '1') end - end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8f698be..ef76a62 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -47,53 +47,51 @@ # triggering implicit auto-inclusion in groups with matching metadata. config.shared_context_metadata_behavior = :apply_to_host_groups -# The settings below are suggested to provide a good initial experience -# with RSpec, but feel free to customize to your heart's content. -=begin - # This allows you to limit a spec run to individual examples or groups - # you care about by tagging them with `:focus` metadata. When nothing - # is tagged with `:focus`, all examples get run. RSpec also provides - # aliases for `it`, `describe`, and `context` that include `:focus` - # metadata: `fit`, `fdescribe` and `fcontext`, respectively. - config.filter_run_when_matching :focus - - # Allows RSpec to persist some state between runs in order to support - # the `--only-failures` and `--next-failure` CLI options. We recommend - # you configure your source control system to ignore this file. - config.example_status_persistence_file_path = "spec/examples.txt" - - # Limits the available syntax to the non-monkey patched syntax that is - # recommended. For more details, see: - # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ - # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ - # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode - config.disable_monkey_patching! - - # Many RSpec users commonly either run the entire suite or an individual - # file, and it's useful to allow more verbose output when running an - # individual spec file. - if config.files_to_run.one? - # Use the documentation formatter for detailed output, - # unless a formatter has already been configured - # (e.g. via a command-line flag). - config.default_formatter = 'doc' - end - - # Print the 10 slowest examples and example groups at the - # end of the spec run, to help surface which specs are running - # particularly slow. - config.profile_examples = 10 - - # Run specs in random order to surface order dependencies. If you find an - # 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 - - # Seed global randomization in this process using the `--seed` CLI option. - # Setting this allows you to use `--seed` to deterministically reproduce - # test failures related to randomization by passing the same `--seed` value - # as the one that triggered the failure. - Kernel.srand config.seed -=end + # The settings below are suggested to provide a good initial experience + # with RSpec, but feel free to customize to your heart's content. + # # This allows you to limit a spec run to individual examples or groups + # # you care about by tagging them with `:focus` metadata. When nothing + # # is tagged with `:focus`, all examples get run. RSpec also provides + # # aliases for `it`, `describe`, and `context` that include `:focus` + # # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + # config.filter_run_when_matching :focus + # + # # Allows RSpec to persist some state between runs in order to support + # # the `--only-failures` and `--next-failure` CLI options. We recommend + # # you configure your source control system to ignore this file. + # config.example_status_persistence_file_path = "spec/examples.txt" + # + # # Limits the available syntax to the non-monkey patched syntax that is + # # recommended. For more details, see: + # # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + # config.disable_monkey_patching! + # + # # Many RSpec users commonly either run the entire suite or an individual + # # file, and it's useful to allow more verbose output when running an + # # individual spec file. + # if config.files_to_run.one? + # # Use the documentation formatter for detailed output, + # # unless a formatter has already been configured + # # (e.g. via a command-line flag). + # config.default_formatter = 'doc' + # end + # + # # Print the 10 slowest examples and example groups at the + # # end of the spec run, to help surface which specs are running + # # particularly slow. + # config.profile_examples = 10 + # + # # Run specs in random order to surface order dependencies. If you find an + # # 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 + # + # # Seed global randomization in this process using the `--seed` CLI option. + # # Setting this allows you to use `--seed` to deterministically reproduce + # # test failures related to randomization by passing the same `--seed` value + # # as the one that triggered the failure. + # Kernel.srand config.seed end diff --git a/spec/views/observations/edit.html.erb_spec.rb b/spec/views/observations/edit.html.erb_spec.rb index bc54392..a4e5ce7 100644 --- a/spec/views/observations/edit.html.erb_spec.rb +++ b/spec/views/observations/edit.html.erb_spec.rb @@ -1,31 +1,29 @@ require 'rails_helper' -RSpec.describe "observations/edit", type: :view do +RSpec.describe 'observations/edit', type: :view do before(:each) do @observation = assign(:observation, Observation.create!( - :location => "MyString", - :latitude => "9.99", - :longitude => "9.99", - :num_bands => 1 + location: 'MyString', + latitude: '9.99', + longitude: '9.99', + num_bands: 1 )) end - it "renders the edit observation form" do + it 'renders the edit observation form' do render - assert_select "form[action=?][method=?]", observation_path(@observation), "post" do + assert_select 'form[action=?][method=?]', observation_path(@observation), 'post' do + assert_select 'input#observation_location[name=?]', 'observation[location]' - assert_select "input#observation_location[name=?]", "observation[location]" + assert_select 'input#observation_latitude[name=?]', 'observation[latitude]' - assert_select "input#observation_latitude[name=?]", "observation[latitude]" + assert_select 'input#observation_longitude[name=?]', 'observation[longitude]' - assert_select "input#observation_longitude[name=?]", "observation[longitude]" - - assert_select "input#observation_num_bands[name=?]", "observation[num_bands]" + assert_select 'input#observation_num_bands[name=?]', 'observation[num_bands]' end end - it 'creates a div due to google maps' do render # TODO: fix this test diff --git a/spec/views/observations/index.html.erb_spec.rb b/spec/views/observations/index.html.erb_spec.rb index 5c7c45a..8480142 100644 --- a/spec/views/observations/index.html.erb_spec.rb +++ b/spec/views/observations/index.html.erb_spec.rb @@ -1,32 +1,32 @@ require 'rails_helper' -RSpec.describe "observations/index", type: :view do +RSpec.describe 'observations/index', type: :view do before(:each) do assign(:observations, [ - Observation.create!( - :location => "Location", - :sighted_at => DateTime.new(2016, 5, 10, 10, 30, 0, '-5'), - :latitude => 10.99, - :longitude => 11.99, - :num_bands => 0 - ), - Observation.create!( - :location => "here", - :sighted_at => DateTime.new(2014, 5, 10, 10, 40, 0, '-5'), - :latitude => 12.99, - :longitude => 13.99, - :num_bands => 1 - ) - ]) + Observation.create!( + location: 'Location', + sighted_at: DateTime.new(2016, 5, 10, 10, 30, 0, '-5'), + latitude: 10.99, + longitude: 11.99, + num_bands: 0 + ), + Observation.create!( + location: 'here', + sighted_at: DateTime.new(2014, 5, 10, 10, 40, 0, '-5'), + latitude: 12.99, + longitude: 13.99, + num_bands: 1 + ) + ]) end - it "renders a list of observations" do + it 'renders a list of observations' do render - assert_select "tr>td.sighted_at_date", :text => "2016-05-10", :count => 1 - assert_select "tr>td.sighted_at_time", :text => "15:30", :count => 1 - assert_select "tr>td.longitude", :text => "11.99".to_s, :count => 1 - assert_select "tr>td.latitude", :text => "10.99".to_s, :count => 1 - assert_select "tr>td.num_bands", :text => "1".to_s, :count => 1 + assert_select 'tr>td.sighted_at_date', text: '2016-05-10', count: 1 + assert_select 'tr>td.sighted_at_time', text: '15:30', count: 1 + assert_select 'tr>td.longitude', text: '11.99'.to_s, count: 1 + assert_select 'tr>td.latitude', text: '10.99'.to_s, count: 1 + assert_select 'tr>td.num_bands', text: '1'.to_s, count: 1 end it 'creates a div due to google maps' do diff --git a/spec/views/observations/new.html.erb_spec.rb b/spec/views/observations/new.html.erb_spec.rb index ebbf044..a0644c8 100644 --- a/spec/views/observations/new.html.erb_spec.rb +++ b/spec/views/observations/new.html.erb_spec.rb @@ -1,27 +1,26 @@ require 'rails_helper' -RSpec.describe "observations/new", type: :view do +RSpec.describe 'observations/new', type: :view do before(:each) do assign(:observation, Observation.new( - :location => "MyString", - :latitude => "9.99", - :longitude => "9.99", - :num_bands => 1 + location: 'MyString', + latitude: '9.99', + longitude: '9.99', + num_bands: 1 )) end - it "renders new observation form" do + it 'renders new observation form' do render - assert_select "form[action=?][method=?]", observations_path, "post" do + assert_select 'form[action=?][method=?]', observations_path, 'post' do + assert_select 'input#observation_location[name=?]', 'observation[location]' - assert_select "input#observation_location[name=?]", "observation[location]" + assert_select 'input#observation_latitude[name=?]', 'observation[latitude]' - assert_select "input#observation_latitude[name=?]", "observation[latitude]" + assert_select 'input#observation_longitude[name=?]', 'observation[longitude]' - assert_select "input#observation_longitude[name=?]", "observation[longitude]" - - assert_select "input#observation_num_bands[name=?]", "observation[num_bands]" + assert_select 'input#observation_num_bands[name=?]', 'observation[num_bands]' end end end diff --git a/spec/views/observations/show.html.erb_spec.rb b/spec/views/observations/show.html.erb_spec.rb index 8f357b8..46e22fe 100644 --- a/spec/views/observations/show.html.erb_spec.rb +++ b/spec/views/observations/show.html.erb_spec.rb @@ -1,16 +1,16 @@ require 'rails_helper' -RSpec.describe "observations/show", type: :view do +RSpec.describe 'observations/show', type: :view do before(:each) do @observation = assign(:observation, Observation.create!( - :location => "Location", - :latitude => "9.99", - :longitude => "9.99", - :num_bands => 2 + location: 'Location', + latitude: '9.99', + longitude: '9.99', + num_bands: 2 )) end - it "renders attributes in
" do + it 'renders attributes in
' do render expect(rendered).to match(/Location/) expect(rendered).to match(/9.99/) @@ -24,4 +24,3 @@ # assert_select "div.gm-style" end end -