Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route color #478

Merged
merged 7 commits into from
Mar 7, 2016
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
13 changes: 12 additions & 1 deletion 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,16 @@ 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

scope :operated_by, -> (model_or_onestop_id) {
if model_or_onestop_id.is_a?(Operator)
Expand Down Expand Up @@ -171,12 +182,12 @@ def self.from_gtfs(entity, attrs={})
onestop_id: onestop_id.to_s,
vehicle_type: entity.route_type.to_i
)
route.color = entity.route_color.upcase if entity.route_color && Route.color_valid?(entity.route_color.upcase)
# Copy over GTFS attributes to tags
route.tags ||= {}
route.tags[:route_long_name] = entity.route_long_name
route.tags[:route_desc] = entity.route_desc
route.tags[:route_url] = entity.route_url
route.tags[:route_color] = entity.route_color
route.tags[:route_text_color] = entity.route_text_color
route
end
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 @@ -410,6 +410,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
6 changes: 6 additions & 0 deletions db/migrate/20160301214615_add_color_to_routes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class AddColorToRoutes < ActiveRecord::Migration
def change
add_column :current_routes, :color, :string
add_column :old_routes, :color, :string
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