fga-gpp-mds/2017.1-OndeE-UnB

View on GitHub
darcyWeb/app/controllers/map/search_controller.rb

Summary

Maintainability
A
1 hr
Test Coverage
class Map::SearchController < MapController
  def locations
    puts params[:search]
    buildings = search_buildings(params[:search])
    rooms = search_rooms(params[:search])
    buildings + rooms
  end

  def search
    render json: locations
  end

  def search_geo
    render json: geo(locations)
  end

  def geo(features)
    geo_json = []
    features.each do |feature|

      title = ''
      description = ''
      image = ''
      case feature.class.name
      when 'Building'
        title = feature.acronym
        description = feature.title
        image = 'fa-building'
      when 'Room'
        title = feature.title
        description = feature.translated_room_type
        image = 'fa-room'
      end

      properties = {
        popupContent: '',
        title: title,
        description: description,
        image: image,
        latitude: feature.latitude,
        longitude: feature.longitude
      }

      geo_data = JSON.parse feature.geo_data
      geo_data['features'].each do |geo_feature|
        geo_feature[:properties] = properties
        geo_json.push(geo_feature)
      end
    end

    json = {
     type: 'FeatureCollection',
     features: geo_json
    }

    json.to_json
  end

  def search_rooms(search)
    search = "%#{search}%"
    Room.joins(:location).where('locations.title ILIKE ? OR acronym ILIKE ? ', search, search)
  end

  def search_buildings(search)
    search = "%#{search}%"
    Building.joins(:location).where('locations.title ILIKE ? OR acronym ILIKE ? ', search, search)
  end
end