Skip to content

Commit

Permalink
Merge pull request #478 from transitland/route_color
Browse files Browse the repository at this point in the history
Route color
  • Loading branch information
drewda committed Mar 7, 2016
2 parents e214542 + 3deb77e commit 2fa1294
Show file tree
Hide file tree
Showing 12 changed files with 873 additions and 795 deletions.
1 change: 1 addition & 0 deletions app/controllers/api/v1/route_stop_patterns_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def index
properties[:stop_pattern] = entity.stop_pattern
properties[:is_generated] = entity.is_generated
properties[:is_modified] = entity.is_modified
properties[:color] = entity.route.color
}
render json: Geojson.from_entity_collection(@rsps, &append)
end
Expand Down
5 changes: 4 additions & 1 deletion app/controllers/api/v1/routes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def index
if params[:bbox].present?
@routes = @routes.stop_within_bbox(params[:bbox])
end
if params[:color].present?
@routes = @routes.where(color: params[:color])
end

@routes = @routes.includes{[
operator,
Expand All @@ -54,7 +57,7 @@ def index
params[:offset],
params[:per_page],
params[:total],
params.slice(:identifier, :identifier_starts_with, :operatedBy, :vehicle_type, :bbox, :onestop_id, :tag_key, :tag_value)
params.slice(:identifier, :identifier_starts_with, :operatedBy, :color, :vehicle_type, :bbox, :onestop_id, :tag_key, :tag_value)
)
end
format.geojson do
Expand Down
1 change: 1 addition & 0 deletions app/models/json_schemas/route.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
},
"name": { "type": "string" },
"tags": { "type": "object" },
"color": { "type": "string" },
"vehicleType": {
"type": ["string", "integer"],
"format": "vehicle-type"
Expand Down
16 changes: 16 additions & 0 deletions app/models/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# geometry :geography({:srid geometry, 4326
# identifiers :string default([]), is an Array
# vehicle_type :integer
# color :string
#
# Indexes
#
Expand Down Expand Up @@ -131,6 +132,20 @@ def before_destroy_making_history(changeset, old_model)
belongs_to :operator

validates :name, presence: true
validate :validate_color_attr

def validate_color_attr
errors.add(:color, "invalid color") unless Route.color_valid?(self.color)
end

def self.color_valid?(color)
return true if color.to_s.empty?
!!(/^[0-9A-F]{6}$/.match(color))
end

def self.color_from_gtfs(route_color)
route_color.upcase if route_color && Route.color_valid?(route_color.upcase)
end

scope :operated_by, -> (model_or_onestop_id) {
if model_or_onestop_id.is_a?(Operator)
Expand Down Expand Up @@ -171,6 +186,7 @@ def self.from_gtfs(entity, attrs={})
onestop_id: onestop_id.to_s,
vehicle_type: entity.route_type.to_i
)
route.color = Route.color_from_gtfs(entity.route_color)
# Copy over GTFS attributes to tags
route.tags ||= {}
route.tags[:route_long_name] = entity.route_long_name
Expand Down
2 changes: 2 additions & 0 deletions app/serializers/route_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# geometry :geography({:srid geometry, 4326
# identifiers :string default([]), is an Array
# vehicle_type :integer
# color :string
#
# Indexes
#
Expand All @@ -31,6 +32,7 @@ class RouteSerializer < CurrentEntitySerializer
:name,
:vehicle_type,
:geometry,
:color,
:tags,
:operated_by_onestop_id,
:operated_by_name,
Expand Down
5 changes: 5 additions & 0 deletions app/serializers/route_stop_pattern_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class RouteStopPatternSerializer < CurrentEntitySerializer
:route_onestop_id,
:stop_pattern,
:geometry,
:color,
:is_generated,
:is_modified,
:created_at,
Expand All @@ -41,4 +42,8 @@ class RouteStopPatternSerializer < CurrentEntitySerializer
def route_onestop_id
object.route.onestop_id
end

def color
object.route.color
end
end
1 change: 1 addition & 0 deletions app/services/gtfs_graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,7 @@ def make_change_route(entity)
{
onestopId: entity.onestop_id,
name: entity.name,
color: entity.color,
identifiedBy: entity.identified_by.uniq,
operatedBy: entity.operator.onestop_id,
vehicleType: entity.vehicle_type,
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20160301214615_add_color_to_routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class AddColorToRoutes < ActiveRecord::Migration
def change
add_column :current_routes, :color, :string
add_column :old_routes, :color, :string
[Route, OldRoute].each do |route|
route.find_each do |r|
r.color = Route.color_from_gtfs(r.tags['route_color'])
r.save!
end
end
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20160219230742) do
ActiveRecord::Schema.define(version: 20160301214615) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -157,6 +157,7 @@
t.geography "geometry", limit: {:srid=>4326, :type=>"geometry", :geographic=>true}
t.string "identifiers", default: [], array: true
t.integer "vehicle_type"
t.string "color"
end

add_index "current_routes", ["created_or_updated_in_changeset_id"], name: "c_route_cu_in_changeset", using: :btree
Expand Down Expand Up @@ -453,6 +454,7 @@
t.geography "geometry", limit: {:srid=>4326, :type=>"geometry", :geographic=>true}
t.string "identifiers", default: [], array: true
t.integer "vehicle_type"
t.string "color"
end

add_index "old_routes", ["created_or_updated_in_changeset_id"], name: "o_route_cu_in_changeset", using: :btree
Expand Down
Loading

0 comments on commit 2fa1294

Please sign in to comment.