Commit 2ec4cada authored by Zac Wood's avatar Zac Wood

Added rubocop config and fixed initial errors

parent 6efb2220
inherit_from: .rubocop_autogen.yml # Some auto-generated Rails code we shouldn't worry about
AllCops:
TargetRubyVersion: 2.5.0
Style/FrozenStringLiteralComment:
Enabled: false # unnecessary
Metrics/AbcSize:
Enabled: false # unnecessary IMO
Metrics/LineLength:
Max: 120
Metrics/MethodLength:
Max: 15 # 10 is a little too low IMO
Exclude:
- "db/excel_loader.rb" # creating objects takes a lot of lines
- "db/migrate/*"
Style/SymbolArray:
EnforcedStyle: brackets # [:symbol1, :symbol2]
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2018-04-09 23:19:33 -0400 using RuboCop version 0.54.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: 2
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle.
# SupportedStyles: empty_lines, no_empty_lines
Layout/EmptyLinesAroundBlockBody:
Exclude:
- 'db/schema.rb'
# Offense count: 2
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, EnforcedStyleForEmptyBrackets.
# SupportedStyles: space, no_space, compact
# SupportedStylesForEmptyBrackets: space, no_space
Layout/SpaceInsideArrayLiteralBrackets:
Exclude:
- 'config/environments/production.rb'
# Offense count: 1
# Configuration parameters: CountComments, ExcludedMethods.
# Metrics/BlockLength:
# Max: 35
Metrics/MethodLength:
Exclude:
- 'db/migrate/*'
Metrics/BlockLength:
Exclude:
- 'db/schema.rb'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: AutoCorrect, EnforcedStyle.
# SupportedStyles: nested, compact
Style/ClassAndModuleChildren:
Exclude:
- 'test/test_helper.rb'
# Offense count: 12
Style/Documentation:
Exclude:
- 'spec/**/*'
- 'test/**/*'
- 'app/helpers/*'
- 'app/mailers/application_mailer.rb'
- 'app/models/application_record.rb'
- 'config/application.rb'
- 'db/migrate/20180407190422_create_semesters.rb'
- 'db/migrate/20180407190502_create_courses.rb'
- 'db/migrate/20180407190750_create_sections.rb'
# Offense count: 4
# Cop supports --auto-correct.
Style/ExpandPathArguments:
Exclude:
- 'bin/bundle'
- 'bin/setup'
- 'bin/update'
- 'test/test_helper.rb'
# Offense count: 2
Style/MixinUsage:
Exclude:
- 'bin/setup'
- 'bin/update'
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: Strict.
Style/NumericLiterals:
MinDigits: 15
# Offense count: 1
# Cop supports --auto-correct.
# Configuration parameters: PreferredDelimiters.
Style/PercentLiteralDelimiters:
Exclude:
- 'config/spring.rb'
# Offense count: 2
# Cop supports --auto-correct.
Style/StderrPuts:
Exclude:
- 'bin/yarn'
# Offense count: 47
# Cop supports --auto-correct.
# Configuration parameters: EnforcedStyle, ConsistentQuotesInMultiline.
# SupportedStyles: single_quotes, double_quotes
Style/StringLiterals:
Exclude:
- 'Gemfile'
- 'bin/yarn'
- 'config/environments/production.rb'
- 'config/puma.rb'
- 'db/schema.rb'
- 'test/application_system_test_case.rb'
Rails/FilePath:
Exclude:
- "config/environments/development.rb" # auto generated file
......@@ -5,7 +5,6 @@ git_source(:github) do |repo_name|
"https://github.com/#{repo_name}.git"
end
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.1.6'
# Use sqlite3 as the database for Active Record
......@@ -43,8 +42,8 @@ end
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'web-console', '>= 3.3.0'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
......@@ -53,4 +52,4 @@ end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'rubyXL'
\ No newline at end of file
gem 'rubyXL'
# Configures the application.
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
end
# Contains all actions having to do with Courses.
class CoursesController < ApplicationController
# Renders JSON of all courses.
def index
@courses = Course.all
render json: @courses
end
end
# Defines actions for the homepage
class HomeController < ApplicationController
def index
end
def index; end
end
# Contains all actions having to do with Sections.
# This is a nested controller -- see +config/routes.rb+ for details
class SectionsController < ApplicationController
# Render JSON of all Sections belonging to a given Course.
def index
@course = Course.find(params[:course_id])
@sections = @course.sections
render json: @sections
end
end
......@@ -2,7 +2,6 @@
#
# TODO: Add more docs
class Course < ApplicationRecord
# Each course belongs to a +Semester+
belongs_to :semester
......@@ -13,6 +12,6 @@ class Course < ApplicationRecord
# Returns all +Section+ objects that belong to this course.
def sections
Section.where :course_id => id
Section.where course_id: id
end
end
......@@ -2,7 +2,6 @@
#
# TODO: Add more docs
class Section < ApplicationRecord
# Each +Section+ belongs to a +Course+.
belongs_to :course
......
......@@ -2,7 +2,6 @@
#
# A +Semester+ is a simple model that consists of a +year+ and a +season+, e.g. "Fall 2018".
class Semester < ApplicationRecord
# Ensure necessary fields are present.
validates :year, presence: true
validates :season, presence: true
......
......@@ -21,7 +21,6 @@ chdir APP_ROOT do
# Install JavaScript dependencies if using Yarn
# system('bin/yarn')
# puts "\n== Copying sample files =="
# unless File.exist?('config/database.yml')
# cp 'config/database.yml.sample', 'config/database.yml'
......
#!/usr/bin/env ruby
VENDOR_PATH = File.expand_path('..', __dir__)
Dir.chdir(VENDOR_PATH) do
begin
exec "yarnpkg #{ARGV.join(" ")}"
rescue Errno::ENOENT
$stderr.puts "Yarn executable was not detected in the system."
$stderr.puts "Download Yarn at https://yarnpkg.com/en/docs/install"
exit 1
end
exec "yarnpkg #{ARGV.join(' ')}"
rescue Errno::ENOENT
$stderr.puts "Yarn executable was not detected in the system."
$stderr.puts "Download Yarn at https://yarnpkg.com/en/docs/install"
exit 1
end
......@@ -25,8 +25,8 @@ class ExcelLoader
# Prints the failure, deletes all data added during loading, and raises the failure error.
def fail(error)
p error.message
p error.backtrace
logger.fatal error.message
logger.fatal error.backtrace
delete_all_records
raise error
end
......@@ -38,12 +38,13 @@ class ExcelLoader
Section.delete_all
end
# Tries to create a course from a given row.
def configure_course?(row)
course_name = row&.cells[1]&.value
course_name = row.cells[1]&.value
course = nil
# Ensure the course name is valid
if course_name && !course_name.empty? && course_name != 'Total'
if course_name.present? && course_name != 'Total'
# Split the name into its two components, i.e. "CS 112" => ["CS", "112"]
name_components = course_name.split(' ')
......@@ -54,50 +55,33 @@ class ExcelLoader
course.semester = @semester
end
return course
course
end
# Tries to create a section from a given row.
def configure_section?(row)
# Get all the info out of the current row
section_name = row&.cells[2]&.value
crn = row&.cells[6]&.value
schedule_type = row&.cells[8]&.value
section_title = row&.cells[11]&.value
instructor = row&.cells[16]&.value
start_date = row&.cells[18]&.value
end_date = row&.cells[21]&.value
days = row&.cells[22]&.value
times = row&.cells[23]&.value
location = row&.cells[25]&.value
# TODO: Add campus, notes, and size limit fields
section = nil
section_name = row.cells[2]&.value
# If there is no valid section name, just continue to the next row
unless !section_name || section_name&.empty? || section_name == 'Total'
# Create the new section
section = Section.new
# Add all fields to the section
section.name = section_name
section.course = @current_course
section.crn = crn
section.section_type = schedule_type
section.title = section_title
section.instructor = instructor
section.start_date = start_date
section.end_date = end_date
section.days = days
unless section_name.blank? || section_name == 'Total'
# The time field in the spreadsheet uses the format "start_time - end_time" i.e. "12:00 PM - 1:15 PM".
# So, split the times string by the - character
times = row.cells[23]&.value
time_strs = times.split('-')
section.start_time = time_strs[0].strip
section.end_time = time_strs[1].strip
section.location = location
# Save the section to the database
section = Section.create name: section_name,
course: @current_course,
crn: row.cells[6]&.value,
section_type: row.cells[8]&.value,
title: row.cells[11]&.value,
instructor: row.cells[16]&.value,
start_date: row.cells[18]&.value,
end_date: row.cells[21]&.value,
days: row.cells[22]&.value,
start_time: time_strs[0].strip,
end_time: time_strs[1].strip,
location: row.cells[25]&.value
# TODO: Add campus, notes, and size limit fields
end
return section
section
end
end
......@@ -8,11 +8,10 @@
require 'rubyXL'
require_relative 'excel_loader'
if Rails.env == 'test'
loader = ExcelLoader.new 'db/data/testdata.xlsx'
loader.load_data
else
loader = ExcelLoader.new 'db/data/allsections.xlsx'
loader.load_data
end
loader = if Rails.env.test?
ExcelLoader.new 'db/data/testdata.xlsx'
else
ExcelLoader.new 'db/data/allsections.xlsx'
end
loader.load_data
require 'test_helper'
class CoursesControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
test 'should get index' do
get url_for controller: 'courses', action: 'index'
assert_response :success
end
end
require 'test_helper'
class HomeControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
test 'should get index' do
get home_index_url
assert_response :success
end
end
require 'test_helper'
class SectionsControllerTest < ActionDispatch::IntegrationTest
test "should get index" do
test 'should get index' do
get url_for controller: 'sections', action: 'index', course_id: 1
assert_response :success
end
......
......@@ -3,11 +3,22 @@ require 'test_helper'
class SectionTest < ActiveSupport::TestCase
test 'fails with improper data' do
assert_raise do
Section.create! name: nil, crn: nil, title: nil, start_date: nil, end_date: nil, days: nil
Section.create! name: nil,
crn: nil,
title: nil,
start_date: nil,
end_date: nil,
days: nil
end
end
test 'succeeds with proper data' do
Section.create! name: 'Test section', crn: '12345', title: 'Test title', start_date: Date.today, end_date: Date.today, days: 'MWF', course_id: 1
Section.create! name: 'Test section',
crn: '12345',
title: 'Test title',
start_date: Time.zone.today,
end_date: Time.zone.today,
days: 'MWF',
course_id: 1
end
end
require 'test_helper'
class SemesterTest < ActiveSupport::TestCase
test "create fails with no data" do
test 'create fails with no data' do
assert_raise do
Semester.create!(season: nil, year: nil)
end
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment