Skip to content

Commit

Permalink
Fix last tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Freika committed Dec 27, 2024
1 parent 4f34145 commit 10034fb
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 15 deletions.
16 changes: 14 additions & 2 deletions app/controllers/map_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,20 @@ def index

@countries_and_cities = CountriesAndCities.new(@points).call
@coordinates =
@points.pluck(:latitude, :longitude, :battery, :altitude, :timestamp, :velocity, :id, :country)
.map { [_1.to_f, _2.to_f, _3.to_s, _4.to_s, _5.to_s, _6.to_s, _7.to_s, _8.to_s] }
@points
.select(:latitude, :longitude, :battery, :altitude, :timestamp, :velocity, :id)
.map do |point|
[
point.latitude.to_f,
point.longitude.to_f,
point.battery.to_s,
point.altitude.to_s,
point.timestamp.to_s,
point.velocity.to_s,
point.id.to_s,
point.country_name.to_s
]
end
@distance = distance
@start_at = Time.zone.at(start_at)
@end_at = Time.zone.at(end_at)
Expand Down
14 changes: 5 additions & 9 deletions app/controllers/trips_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ def index
end

def show
@coordinates = @trip.points.pluck(
:latitude, :longitude, :battery, :altitude, :timestamp, :velocity, :id,
:country
).map { [_1.to_f, _2.to_f, _3.to_s, _4.to_s, _5.to_s, _6.to_s, _7.to_s, _8.to_s] }

@photo_previews = Rails.cache.fetch("trip_photos_#{@trip.id}", expires_in: 1.day) do
@trip.photo_previews
end
Expand Down Expand Up @@ -58,10 +53,11 @@ def set_trip
end

def set_coordinates
@coordinates = @trip.points.pluck(
:latitude, :longitude, :battery, :altitude, :timestamp, :velocity, :id,
:country
).map { [_1.to_f, _2.to_f, _3.to_s, _4.to_s, _5.to_s, _6.to_s, _7.to_s, _8.to_s] }
@coordinates =
@trip
.points
.select(:latitude, :longitude, :timestamp)
.map { |point| [point.latitude.to_f, point.longitude.to_f, point.timestamp.to_s] }
end

def trip_params
Expand Down
2 changes: 2 additions & 0 deletions app/models/point.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Point < ApplicationRecord

delegate :name, to: :city, prefix: true, allow_nil: true
delegate :name, to: :country, prefix: true, allow_nil: true
delegate :name, to: :state, prefix: true, allow_nil: true
delegate :name, to: :county, prefix: true, allow_nil: true

enum :battery_status, { unknown: 0, unplugged: 1, charging: 2, full: 3 }, suffix: true
enum :trigger, {
Expand Down
4 changes: 4 additions & 0 deletions app/services/reverse_geocoding/points/fetch_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ def update_point_with_geocoding_data

country = Country.find_or_create_by(name: response.country, iso2_code: response.countrycode)
city = City.find_or_create_by(name: response.city, country: country)
county = County.find_or_create_by(name: response.county, country: country)
state = State.find_or_create_by(name: response.state, country: country)

point.update!(
city: city,
country: country,
county: county,
state: state,
geodata: response.data,
reverse_geocoded_at: Time.current
)
Expand Down
2 changes: 1 addition & 1 deletion app/views/trips/_trip.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class="rounded-lg z-0"
data-controller="trip-map"
data-trip-map-trip-id-value="<%= trip.id %>"
data-trip-map-coordinates-value="<%= trip.points.pluck(:latitude, :longitude, :battery, :altitude, :timestamp, :velocity, :id, :country).to_json %>"
data-trip-map-coordinates-value="<%= trip.points.pluck(:latitude, :longitude, :battery, :altitude, :timestamp, :velocity, :id).to_json %>"
data-trip-map-api-key-value="<%= current_user.api_key %>"
data-trip-map-user-settings-value="<%= current_user.settings.to_json %>"
data-trip-map-timezone-value="<%= Rails.configuration.time_zone %>"
Expand Down
6 changes: 6 additions & 0 deletions app/views/trips/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
<%= render 'trip', trip: trip %>
<% end %>
</div>

<div class="flex justify-center my-5">
<div class='flex'>
<%= paginate @trips %>
</div>
</div>
<% end %>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
it 'migrates point data to columns and tables' do
expect_any_instance_of(DataMigrations::MigratePoint).to receive(:call)

described_class.perform_now(point.id)
described_class.perform_now([point.id])
end
end
5 changes: 4 additions & 1 deletion spec/serializers/point_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@

let(:point) { create(:point) }
let(:expected_json) do
point.attributes.except(*PointSerializer::EXCLUDED_ATTRIBUTES)
point.attributes.except(*PointSerializer::EXCLUDED_ATTRIBUTES).merge(
city: point.city_name,
country: point.country_name
)
end

it 'returns JSON' do
Expand Down
11 changes: 10 additions & 1 deletion spec/services/reverse_geocoding/points/fetch_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
context 'when Geocoder returns city and country' do
before do
allow(Geocoder).to receive(:search).and_return(
[double(city: 'City', country: 'Country', countrycode: 'US', data: { 'address' => 'Address' })]
[double(
city: 'City',
country: 'Country',
countrycode: 'US',
data: { 'address' => 'Address' },
state: 'State',
county: 'County'
)]
)
end

Expand All @@ -19,6 +26,8 @@
expect { fetch_data }.to change { point.reload.city_name }
.from(nil).to('City')
.and change { point.reload.country_name }.from(nil).to('Country')
.and change { point.reload.state_name }.from(nil).to('State')
.and change { point.reload.county_name }.from(nil).to('County')
end

it 'updates point with geodata' do
Expand Down

0 comments on commit 10034fb

Please sign in to comment.