Files
nairobi/app/models/map_location.rb
Javi Martín 28aafbd4bc Add and apply Style/InvertibleUnlessCondition rule
This rule was added in rubocop 1.44.0. It's useful to avoid accidental
`unless !condition` clauses.

Note we aren't replacing `unless zero?` with `if nonzero?` because we
never use `nonzero?`; using it sounds like `if !zero?`.

Replacing `unless any?` with `if none?` is only consistent if we also replace
`unless present?` with `if blank?`, so we're also adding this case. For
consistency, we're also replacing `unless blank?` with `if present?`.

We're also simplifying code dealing with `> 0` conditions in order to
make the code (hopefully) easier to understand.

Also for consistency, we're enabling the `Style/InverseMethods` rule,
which follows a similar idea.
2023-09-07 19:14:03 +02:00

47 lines
1.2 KiB
Ruby

class MapLocation < ApplicationRecord
belongs_to :proposal, touch: true
belongs_to :investment, class_name: "Budget::Investment", touch: true
validates :longitude, :latitude, :zoom, presence: true, numericality: true
def available?
latitude.present? && longitude.present? && zoom.present?
end
def json_data
{
investment_id: investment_id,
proposal_id: proposal_id,
lat: latitude,
long: longitude
}
end
def self.from_heading(heading)
new(
zoom: Budget::Heading::OSM_DISTRICT_LEVEL_ZOOM,
latitude: (heading.latitude.to_f if heading.latitude.present?),
longitude: (heading.longitude.to_f if heading.longitude.present?)
)
end
def self.investments_json_data(investments)
return [] if investments.none?
budget_id = investments.first.budget_id
data = investments.joins(:map_location)
.with_fallback_translation
.pluck(:id, :title, :latitude, :longitude)
data.map do |values|
{
title: values[1],
link: "/budgets/#{budget_id}/investments/#{values[0]}",
lat: values[2],
long: values[3]
}
end
end
end