Sha256: d6ef9e9c712b95d1a91c30a0dd7da0160faeaa5108f91577d52ee8c54fc95856

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 KB

Contents

class Airport < ActiveRecord::Base
  set_primary_key :iata_code

  acts_as_mappable :default_units => :nms,
                   :lat_column_name => :latitude,
                   :lng_column_name => :longitude
  
  class << self
    def loose_search_columns
      @_loose_search_columns ||= [primary_key, :city]
    end

    # search by name
    def loose_right_reader
      @_loose_right_reader ||= lambda { |record| record[1] }
    end
  end
  
  def name_and_location
    [ name.to_s.titleize, city, country.andand.name ].select(&:present?).join ', '
  end
  
  # --------------------------------
  # virtual has_many association
  # has_many :segments won't work because there's no general way to specify the correct conditions
  # even if you get clever with it, like
  # has_many  :segments,
  #           :class_name => 'FlightSegment',
  #           :foreign_key => 'origin_airport_id',
  #           :conditions => 'flight_segments.destination_airport_id = #{id}'
  # you get queries like "`flight_segments`.origin_airport_id = 3654 AND (flight_segments.destination_airport_id = 3654))"
  # in which you notice the AND which must be an OR
  # and you can't just do finder_sql, because that breaks any other :select
  def segments
    FlightSegment.scoped :conditions => ['origin_airport_id = ? OR destination_airport_id = ?', id, id]
  end
  # --------------------------------
  
  belongs_to :country, :foreign_key => 'country_iso_3166_code'

  def all_flights_from_here_domestic?
    !international_origin?
  end

  def all_flights_to_here_domestic?
    !international_destination?
  end
  
  def united_states?
    country == Country.united_states
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
flight-0.0.5 lib/test_support/data_models/airport.rb
flight-0.0.4 lib/test_support/data_models/airport.rb