Skip to content

Commit

Permalink
Merge pull request #1120 from transitland/stop-min-platforms
Browse files Browse the repository at this point in the history
Stop: min platforms query
  • Loading branch information
irees committed Jun 29, 2017
2 parents 308de28 + dfc6f72 commit fb0dbfc
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 1 deletion.
7 changes: 7 additions & 0 deletions app/controllers/api/v1/stop_stations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def index_query
if params[:wheelchair_boarding].present?
@collection = @collection.where(wheelchair_boarding: AllowFiltering.to_boolean(params[:wheelchair_boarding] ))
end
if params[:min_platforms].present?
@collection = @collection.with_min_platforms(params[:min_platforms].to_i)
end
end

def index_includes
Expand Down Expand Up @@ -92,6 +95,10 @@ def query_params
wheelchair_boarding: {
desc: "Wheelchair boarding",
type: "boolean"
},
min_platforms: {
desc: "Mininum number of platforms",
type: "integer"
}
})
end
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/concerns/json_collection_pagination.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ def paginated_collection(collection)
end

if include_total
meta[:total] = collection.count
total = collection.count
(total = total.size) if total.is_a?(Hash)
meta[:total] = total
end
# Return results + meta
data_on_page = data_on_page.empty? ? collection.model.none : data_on_page
Expand Down
7 changes: 7 additions & 0 deletions app/models/stop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,13 @@ def parent_stop
where('last_conflated_at <= ?', last_conflated_at)
}

scope :with_min_platforms, -> (min_platform_count) {
where(type: nil)
.joins("INNER JOIN current_stops AS current_stop_platforms ON current_stop_platforms.type = 'StopPlatform' AND current_stop_platforms.parent_stop_id = current_stops.id")
.group('current_stops.id, current_stop_platforms.parent_stop_id')
.having('COUNT(current_stop_platforms.id) >= ?', min_platform_count || 1)
}

def coordinates
g = geometry(as: :wkt)
[g.lon, g.lat]
Expand Down
18 changes: 18 additions & 0 deletions config/initializers/squeel.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,21 @@ def within *list
end
end
end

# WARNING: a monkey patch from
# https://stackoverflow.com/questions/31860441/deprecation-warning-modifying-already-cached-relation-on-postgres-max-greates
# to handle:
# DEPRECATION WARNING: Modifying already cached Relation. The cache will be reset.
module Squeel
module Adapters
module ActiveRecord
module RelationExtensions

def execute_grouped_calculation(operation, column_name, distinct)
super
end

end
end
end
end
24 changes: 24 additions & 0 deletions spec/models/stop_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,30 @@
end
end

context '.with_min_platforms' do
before(:each) do
@s1 = create(:stop)
@s2 = create(:stop)
@s2p1 = create(:stop_platform, parent_stop: @s2)
@s3 = create(:stop)
@s3p1 = create(:stop_platform, parent_stop: @s3)
@s3p2 = create(:stop_platform, parent_stop: @s3)
end

it 'returns only Stops with StopPlatforms' do
expect(Stop.with_min_platforms(1)).to match_array([@s2, @s3])
end

it 'returns minimum StopPlatforms' do
expect(Stop.with_min_platforms(2)).to match_array([@s3])
end

it 'works with count' do
# Handled in JSON pagination concern
expect(Stop.with_min_platforms(1).count.size).to eq(2)
end
end

context 'served_by' do
before(:each) do
@bart = create(:operator, name: 'BART')
Expand Down

0 comments on commit fb0dbfc

Please sign in to comment.