Skip to content

Commit

Permalink
Merge pull request #126 from CaravanTransit/bus-train-distinction
Browse files Browse the repository at this point in the history
Closes #119. Surface bus/train distinction for issue form
  • Loading branch information
SorenSpicknall committed Mar 13, 2019
2 parents 94234d1 + 5369858 commit 37ab637
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 13 deletions.
2 changes: 2 additions & 0 deletions app/controllers/issues_controller.rb
Expand Up @@ -19,6 +19,8 @@ def new
unless user_signed_in?
redirect_to new_user_session_path
end

@line_type = params[:line_type]
@issue = Issue.new(stop_onestop_id: params[:stop_id], line_onestop_id: params[:line_id])
end

Expand Down
35 changes: 33 additions & 2 deletions app/models/line.rb
Expand Up @@ -8,6 +8,11 @@ class Line < ApplicationRecord

self.primary_key = "onestop_id"

# GTFS route types
TRAM_SYSTEM_TYPE = 0
METRO_SYSTEM_TYPE = 1
BUS_SYSTEM_TYPE = 3

# Returns the longest available name
def long_name
route_long_name ? route_long_name : name
Expand All @@ -26,7 +31,7 @@ def bus?
if system_type.nil? # Prefer system_type if present
vehicle_type == "bus"
else
system_type == 3
system_type == BUS_SYSTEM_TYPE
end
end

Expand All @@ -36,7 +41,33 @@ def train?
if system_type.nil?
vehicle_type == "tram" || vehicle_type == "metro"
else
system_type == 0 || system_type == 1
system_type == TRAM_SYSTEM_TYPE || system_type == METRO_SYSTEM_TYPE
end
end

def line_type
if train?
"train"
elsif bus?
"bus"
else
nil
end
end

# Query for bus/train lines.
# line_type can be "train" or "bus".
def self.of_line_type(line_type)
if line_type == "train"
Line.where(system_type: [TRAM_SYSTEM_TYPE, METRO_SYSTEM_TYPE])
.or(Line.where(vehicle_type: ["tram", "metro"]))
.order(:name)
elsif line_type == "bus"
Line.where(system_type: BUS_SYSTEM_TYPE)
.or(Line.where(vehicle_type: "bus"))
.order(:name)
else
Line.order(:name)
end
end
end
3 changes: 2 additions & 1 deletion app/views/issues/_form.html.erb
@@ -1,3 +1,4 @@
<% line_type = nil if local_assigns[:line_type].nil? %>
<%= form_for(issue) do |f| %>
<% if issue.errors.any? %>
<div id="error_explanation">
Expand All @@ -13,7 +14,7 @@

<div class="field-issue">
<%= f.label 'Line' %><br>
<%= f.collection_select(:line_onestop_id, Line.order('vehicle_type DESC, name'), :id, :route_long_name,
<%= f.collection_select(:line_onestop_id, Line.of_line_type(line_type), :id, :route_long_name,
{prompt: 'Select Line'},
{:class => "new-issue-line"}
) %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/issues/edit.html.erb
@@ -1,6 +1,6 @@
<h1>Editing Issue</h1>

<%= render 'form', issue: @issue %>
<%= render 'form', issue: @issue, line_type: @issue.line&.line_type %>
<%= link_to 'Show', @issue %> |
<%= link_to 'Back', issues_path %>
4 changes: 2 additions & 2 deletions app/views/issues/new.html.erb
@@ -1,7 +1,7 @@
<div class="page-container">
<h1>New Issue</h1>

<%= render 'form', issue: @issue %>
<%= render 'form', issue: @issue, line_type: @line_type %>
<%= link_to 'Back', issues_path %>
</div>
</div>
4 changes: 2 additions & 2 deletions app/views/pages/home.html.erb
Expand Up @@ -15,12 +15,12 @@
<div class="heading">Report An Issue</div>

<div class="transit-btn-cont">
<%= link_to(new_issue_path, class: 'blue-btn transit') do %>
<%= link_to(new_issue_path(line_type: 'bus'), class: 'blue-btn transit') do %>
<div>Bus</div>
<%= image_tag "bus_white.svg", alt: '' %>
<% end %>
<%= link_to(new_issue_path, class: 'blue-btn transit') do %>
<%= link_to(new_issue_path(line_type: 'train'), class: 'blue-btn transit') do %>
<div>Train</div>
<%= image_tag "train_white.svg", alt: '' %>
<% end %>
Expand Down
38 changes: 33 additions & 5 deletions test/models/line_test.rb
Expand Up @@ -3,6 +3,15 @@
require "test_helper"

class LineTest < ActiveSupport::TestCase
setup do
Line.create(name: "RouteShort")
Line.create(name: "RouteShort", vehicle_type: "bus")
Line.create(name: "RouteShort", vehicle_type: "train")
Line.create(name: "RouteShort", system_type: Line::TRAM_SYSTEM_TYPE)
Line.create(name: "RouteShort", system_type: Line::METRO_SYSTEM_TYPE)
Line.create(name: "RouteShort", system_type: Line::BUS_SYSTEM_TYPE)
end

test "returns proper long name" do
route_long_name = "RouteLong"
route_short_name = "RouteShort"
Expand Down Expand Up @@ -33,12 +42,12 @@ class LineTest < ActiveSupport::TestCase
line = Line.new(vehicle_type: "bus")
assert line.bus?

line = Line.new(system_type: 3)
line = Line.new(system_type: Line::BUS_SYSTEM_TYPE)
assert line.bus?
end

test "bus? returns false if system type is not bus type" do
line = Line.new(system_type: 1, vehicle_type: "bus")
line = Line.new(system_type: Line::METRO_SYSTEM_TYPE, vehicle_type: "bus")
assert_not line.bus?
end

Expand All @@ -48,10 +57,10 @@ class LineTest < ActiveSupport::TestCase
end

test "train? returns true if system type is tram or metro" do
line = Line.new(system_type: 0)
line = Line.new(system_type: Line::TRAM_SYSTEM_TYPE)
assert line.train?

line = Line.new(system_type: 1)
line = Line.new(system_type: Line::METRO_SYSTEM_TYPE)
assert line.train?
end

Expand All @@ -64,7 +73,7 @@ class LineTest < ActiveSupport::TestCase
end

test "train? returns false if system type is not tram or metro" do
line = Line.new(system_type: 3, vehicle_type: "tram")
line = Line.new(system_type: Line::BUS_SYSTEM_TYPE, vehicle_type: "tram")
assert_not line.train?
end

Expand All @@ -78,4 +87,23 @@ class LineTest < ActiveSupport::TestCase
assert_not line.bus?
assert_not line.train?
end

test "of_line_type(bus) returns all bus lines" do
lines = Line.of_line_type("bus")
assert_not lines.empty?
assert lines.all?(&:bus?)
end

test "of_line_type(train) returns all train lines" do
lines = Line.of_line_type("train")
assert_not lines.empty?
assert lines.all?(&:train?)
end

test "of_line_type(nil) returns all lines" do
line_count = Line.count
lines = Line.of_line_type(nil)
assert_not lines.empty?
assert_equal line_count, lines.count
end
end

0 comments on commit 37ab637

Please sign in to comment.