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

Zeitwerk compatibility for Rails 6 #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
43 changes: 0 additions & 43 deletions app/decorators/api/v01/destinations_decorator.rb

This file was deleted.

39 changes: 0 additions & 39 deletions app/decorators/api/v01/stores_decorator.rb

This file was deleted.

Empty file removed app/decorators/controllers/.keep
Empty file.
Empty file removed app/decorators/models/.keep
Empty file.
50 changes: 0 additions & 50 deletions app/decorators/models/customer_decorator.rb

This file was deleted.

44 changes: 44 additions & 0 deletions config/initializers/api/v01/destinations_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# Copyright © Mapotempo, 2015-2016
#
# This file is part of Mapotempo.
#
# Mapotempo is free software. You can redistribute it and/or
# modify since you respect the terms of the GNU Affero General
# Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# Mapotempo is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the Licenses for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Mapotempo. If not, see:
# <http://www.gnu.org/licenses/agpl.html>
#

Rails.application.config.to_prepare do
V01::Destinations.class_eval do
desc 'Fetch customer\'s destinations inside time/distance.',
nickname: 'getDestinationsInsideTimeAndDistance',
is_array: true,
entity: V01::Entities::DestinationId
params do
requires :lat, type: Float, desc: 'Point latitude.'
requires :lng, type: Float, desc: 'Point longitude.'
optional :vehicle_usage_id, type: Integer, desc: 'Vehicle Usage uses in place of default router and speed multiplicator.'
optional :distance, type: Integer, desc: 'Maximum distance in meter.'
optional :time, type: Integer, desc: 'Maximum time in seconds.'
at_least_one_of :time, :distance
end
get :destinations_by_time_and_distance do
position = OpenStruct.new(lat: Float(params[:lat]), lng: Float(params[:lng]))
vehicle_usage = VehicleUsage.joins(:vehicle_usage_set).where(vehicle_usage_sets: {customer_id: current_customer.id}, id: params[:vehicle_usage_id]).first
if params.key?(:vehicle_usage_id) && vehicle_usage.nil?
error! 'VehicleUsage not found', 404
else
destinations = current_customer.destinations_inside_time_distance(position, params[:distance], params[:time], vehicle_usage) || []
present destinations, with: V01::Entities::DestinationId
end
end
end
end
42 changes: 42 additions & 0 deletions config/initializers/api/v01/stores_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Copyright © Mapotempo, 2015-2016
#
# This file is part of Mapotempo.
#
# Mapotempo is free software. You can redistribute it and/or
# modify since you respect the terms of the GNU Affero General
# Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# Mapotempo is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the Licenses for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Mapotempo. If not, see:
# <http://www.gnu.org/licenses/agpl.html>
#

Rails.application.config.to_prepare do
V01::Stores.class_eval do
desc 'Fetch customer\'s stores by distance.',
nickname: 'getStoresByDistance',
is_array: true,
entity: V01::Entities::Store
params do
requires :lat, type: Float, desc: 'Point latitude.'
requires :lng, type: Float, desc: 'Point longitude.'
requires :n, type: Integer, desc: 'Number of results.'
optional :vehicle_usage_id, type: Integer, desc: 'Vehicle Usage uses in place of default router and speed multiplicator.'
end
get :stores_by_distance do
position = OpenStruct.new(lat: Float(params[:lat]), lng: Float(params[:lng]))
vehicle_usage = VehicleUsage.joins(:vehicle_usage_set).where(vehicle_usage_sets: {customer_id: current_customer.id}, id: params[:vehicle_usage_id]).first
if params.key?(:vehicle_usage_id) && vehicle_usage.nil?
error! 'VehicleUsage not found', 404
else
stores = current_customer.stores_by_distance(position, Integer(params[:n]), vehicle_usage)
present stores, with: V01::Entities::Store
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
#
require 'ostruct'

ApiWeb::V01::StoresController.class_eval do
def by_distance
@customer = current_user.customer
@position = OpenStruct.new(lat: Float(params[:lat]), lng: Float(params[:lng]))
@stores = @customer.stores_by_distance(@position, Integer(params[:n]))
Rails.application.config.to_prepare do
ApiWeb::V01::StoresController.class_eval do
def by_distance
@customer = current_user.customer
@position = OpenStruct.new(lat: Float(params[:lat]), lng: Float(params[:lng]))
@stores = @customer.stores_by_distance(@position, Integer(params[:n]))
end
end
end
53 changes: 53 additions & 0 deletions config/initializers/models/customer_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Copyright © Mapotempo, 2015-2016
#
# This file is part of Mapotempo.
#
# Mapotempo is free software. You can redistribute it and/or
# modify since you respect the terms of the GNU Affero General
# Public License as published by the Free Software Foundation,
# either version 3 of the License, or (at your option) any later version.
#
# Mapotempo is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE. See the Licenses for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Mapotempo. If not, see:
# <http://www.gnu.org/licenses/agpl.html>
#

Rails.application.config.to_prepare do
Customer.class_eval do
def stores_by_distance(position, n, vehicle_usage = nil, &matrix_progress)
starts = [[position.lat, position.lng]]
dests = self.stores.select{ |store| !store.lat.nil? && !store.lng.nil? }.collect{ |store| [store.lat, store.lng] }
r = (vehicle_usage && vehicle_usage.vehicle.default_router) || router
d = (vehicle_usage && vehicle_usage.vehicle.default_router_dimension) || router_dimension
options = (vehicle_usage && vehicle_usage.vehicle.default_router_options || router_options).symbolize_keys
options[:geometry] = false
options[:speed_multiplier] = (vehicle_usage && vehicle_usage.vehicle.default_speed_multiplier) || speed_multiplier || 1

distances = r.matrix(starts, dests, :distance, options, &matrix_progress)[0]
stores.select{ |store, distance| !distance.nil? }.zip(distances).sort_by{ |store, distance|
distance
}[0..[n, stores.size].min - 1].collect{ |store, distance| store }
end

def destinations_inside_time_distance(position, distance, time, vehicle_usage = nil, &matrix_progress)
starts = [[position.lat, position.lng]]
dest_with_pos = self.destinations.select{ |d| !d.lat.nil? && !d.lng.nil? }
dests = dest_with_pos.collect{ |d| [d.lat, d.lng] }
r = (vehicle_usage && vehicle_usage.vehicle.default_router) || router
d = (vehicle_usage && vehicle_usage.vehicle.default_router_dimension) || router_dimension
options = (vehicle_usage && vehicle_usage.vehicle.default_router_options || router_options).symbolize_keys
options[:geometry] = false
options[:speed_multiplier] = (vehicle_usage && vehicle_usage.vehicle.default_speed_multiplier) || speed_multiplier || 1

distances = !distance.nil? && r.distance? ? r.matrix(starts, dests, :distance, options, &matrix_progress)[0] : []
times = !time.nil? && r.time? ? r.matrix(starts, dests, :time, options, &matrix_progress)[0] : []
dest_with_pos.zip(distances, times).select{ |dest, dist, t|
(!dist || dist[0] <= distance) && (!t || t[0] <= time)
}.collect{ |dest, d, t| dest }
end
end
end