Skip to content

Commit

Permalink
Merge branch 'issue_382'
Browse files Browse the repository at this point in the history
  • Loading branch information
Scott Wainstock committed Oct 4, 2015
2 parents a9add96 + 7de8a35 commit 84b8013
Show file tree
Hide file tree
Showing 9 changed files with 43 additions and 11 deletions.
6 changes: 3 additions & 3 deletions app/assets/javascripts/application.js.erb
Expand Up @@ -103,9 +103,9 @@ function switchSection(newSection) {
$("#" + newSection + "_section_link").toggleClass("active_section_link");
}

function initSearch(region, locationIDs, locationTypeIDs, zoneIDs, machineIDs, cityName, machineGroupId) {
if (locationIDs || locationTypeIDs || zoneIDs || machineIDs || cityName || machineGroupId) {
var url = '/' + region + '/locations?by_location_id=' + locationIDs + ';by_type_id=' + locationTypeIDs + ';by_zone_id=' + zoneIDs + ';by_machine_id=' + machineIDs + ';by_city_id=' + cityName + ';by_machine_group_id=' + machineGroupId;
function initSearch(region, locationIDs, locationTypeIDs, zoneIDs, machineIDs, ipdbIDs, cityName, machineGroupId) {
if (locationIDs || locationTypeIDs || zoneIDs || machineIDs || ipdbIDs || cityName || machineGroupId) {
var url = '/' + region + '/locations?by_location_id=' + locationIDs + ';by_type_id=' + locationTypeIDs + ';by_zone_id=' + zoneIDs + ';by_machine_id=' + machineIDs + ';by_city_id=' + cityName + ';by_machine_group_id=' + machineGroupId + ';by_ipdb_id=' + ipdbIDs;

$('#locations').html(loadingHTML());
$.get(url, function(data) {
Expand Down
6 changes: 4 additions & 2 deletions app/controllers/api/v1/locations_controller.rb
Expand Up @@ -4,7 +4,7 @@ class LocationsController < InheritedResources::Base
include ActionView::Helpers::NumberHelper
before_filter :allow_cors
respond_to :json
has_scope :by_location_name, :by_location_id, :by_machine_id, :by_machine_name, :by_city_id, :by_zone_id, :by_operator_id, :by_type_id, :by_machine_group_id, :by_at_least_n_machines_city, :by_at_least_n_machines_zone, :by_at_least_n_machines_type, :region
has_scope :by_location_name, :by_location_id, :by_machine_id, :by_machine_name, :by_city_id, :by_zone_id, :by_operator_id, :by_type_id, :by_machine_group_id, :by_at_least_n_machines_city, :by_at_least_n_machines_zone, :by_at_least_n_machines_type, :region, :by_ipdb_id

MAX_MILES_TO_SEARCH_FOR_CLOSEST_LOCATION = 50

Expand Down Expand Up @@ -44,6 +44,7 @@ def suggest
param :by_location_name, String, desc: 'Name of location to search for', required: false
param :by_location_id, Integer, desc: 'Location ID to search for', required: false
param :by_machine_id, Integer, desc: 'Machine ID to find in locations', required: false
param :by_ipdb_id, Integer, desc: 'IPDB ID to find in locations', required: false
param :by_machine_name, String, desc: 'Find machine name in locations', required: false
param :by_city_id, String, desc: 'City to search for', required: false
param :by_machine_group_id, String, desc: 'Machine Group to search for', required: false
Expand Down Expand Up @@ -132,7 +133,8 @@ def machine_details
name: m.name,
year: m.year,
manufacturer: m.manufacturer,
ipdb_link: m.ipdb_link
ipdb_link: m.ipdb_link,
ipdb_id: m.ipdb_id
)
end

Expand Down
2 changes: 1 addition & 1 deletion app/controllers/locations_controller.rb
@@ -1,6 +1,6 @@
class LocationsController < InheritedResources::Base
respond_to :xml, :json, :html, :js, :rss
has_scope :by_location_name, :by_location_id, :by_machine_id, :by_machine_name, :by_city_id, :by_machine_group_id, :by_zone_id, :by_operator_id, :by_type_id, :by_at_least_n_machines_city, :by_at_least_n_machines_zone, :by_at_least_n_machines_type, :region
has_scope :by_location_name, :by_location_id, :by_ipdb_id, :by_machine_id, :by_machine_name, :by_city_id, :by_machine_group_id, :by_zone_id, :by_operator_id, :by_type_id, :by_at_least_n_machines_city, :by_at_least_n_machines_zone, :by_at_least_n_machines_type, :region

def autocomplete
render json: @region.locations.select { |l| l.name =~ /#{Regexp.escape params[:term] || ''}/i }.sort_by(&:name).map { |l| { label: l.name, value: l.name, id: l.id } }
Expand Down
5 changes: 5 additions & 0 deletions app/models/location.rb
Expand Up @@ -32,6 +32,11 @@ class Location < ActiveRecord::Base
scope :by_zone_id, ->(id) { where('zone_id in (?)', id.split('_').map(&:to_i)) }
scope :by_city_id, ->(city) { where(city: city) }
scope :by_location_name, ->(name) { where(name: name) }
scope :by_ipdb_id, lambda { |id|
machines = Machine.where('ipdb_id in (?)', id.split('_').map(&:to_i)).map { |m| m.all_machines_in_machine_group }.flatten

joins(:location_machine_xrefs).where('locations.id = location_machine_xrefs.location_id and location_machine_xrefs.machine_id in (?)', machines.map { |m| m.id })
}
scope :by_machine_id, lambda { |id|
machines = Machine.where('id in (?)', id.split('_').map(&:to_i)).map { |m| m.all_machines_in_machine_group }.flatten

Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.haml
Expand Up @@ -23,7 +23,7 @@
= favicon_link_tag 'favicon.ico'
= favicon_link_tag 'apple-touch-icon.png', rel: 'apple-touch-icon', type: 'image/png'

%body{ :onload => defined?(@region) ? "setSearchSections(#{@region.available_search_sections}); switchSection('#{(@region.default_search_type && !@region.default_search_type.empty?) ? @region.default_search_type : 'location'}'); initSearch('#{@region.name.downcase}', '#{params['by_location_id']}', '#{params['by_location_type_id']}', '#{params['by_zone_id']}', '#{params['by_machine_id']}','#{params['by_city_id']}', '#{params['by_machine_group_id']}');" : nil }
%body{ :onload => defined?(@region) ? "setSearchSections(#{@region.available_search_sections}); switchSection('#{(@region.default_search_type && !@region.default_search_type.empty?) ? @region.default_search_type : 'location'}'); initSearch('#{@region.name.downcase}', '#{params['by_location_id']}', '#{params['by_location_type_id']}', '#{params['by_zone_id']}', '#{params['by_machine_id']}','#{params['by_ipdb_id']}','#{params['by_city_id']}', '#{params['by_machine_group_id']}');" : nil }
#container
#page
= render :partial => 'pages/header'
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20150928221835_add_ipdb_id_to_machines.rb
@@ -0,0 +1,5 @@
class AddIpdbIdToMachines < ActiveRecord::Migration
def change
add_column :machines, :ipdb_id, :integer
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150926235940) do
ActiveRecord::Schema.define(version: 20150928221835) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -130,6 +130,7 @@
t.integer "year"
t.string "manufacturer"
t.integer "machine_group_id"
t.integer "ipdb_id"
end

create_table "operators", force: true do |t|
Expand Down
9 changes: 8 additions & 1 deletion spec/features/locations_controller_spec.rb
Expand Up @@ -93,7 +93,7 @@ def handle_js_confirm
@type = FactoryGirl.create(:location_type, name: 'Bar')
@zone = FactoryGirl.create(:zone, region: @region, name: 'DT')
@location = FactoryGirl.create(:location, region: @region, city: 'Portland', name: 'Cleo', zone: @zone, location_type: @type)
@machine = FactoryGirl.create(:machine, name: 'Barb')
@machine = FactoryGirl.create(:machine, name: 'Barb', ipdb_id: 777)
FactoryGirl.create(:location_machine_xref, location: @location, machine: @machine)

FactoryGirl.create(:location, region: @region, name: 'Sass', city: 'Beaverton')
Expand Down Expand Up @@ -174,6 +174,13 @@ def handle_js_confirm
expect(page).to_not have_content('Sass')
end

it 'by_ipdb_id' do
visit '/portland/?by_ipdb_id=' + @machine.ipdb_id.to_s

expect(page).to have_content('Cleo')
expect(page).to_not have_content('Sass')
end

it 'by_machine_id' do
visit '/portland/?by_machine_id=' + @machine.id.to_s

Expand Down
16 changes: 14 additions & 2 deletions spec/requests/api/v1/locations_controller_spec.rb
Expand Up @@ -80,6 +80,16 @@
expect(response.body).to include('777')
expect(response.body).to include('foo bar')
end

it 'respects by_ipdb_id filter' do
lmx = FactoryGirl.create(:location_machine_xref, location: @location, machine: FactoryGirl.create(:machine, id: 777, name: 'Cleo', ipdb_id: 999))
FactoryGirl.create(:machine_condition, location_machine_xref_id: lmx.id, comment: 'foo bar')
get "/api/v1/region/#{@region.name}/locations.json?by_ipdb_id=999"

expect(response.body).to include('Satchmo')
expect(response.body).to include('777')
expect(response.body).to include('foo bar')
end
end

describe '#update' do
Expand Down Expand Up @@ -218,8 +228,8 @@
end

it 'displays details of machines at location' do
FactoryGirl.create(:location_machine_xref, location: @location, machine: FactoryGirl.create(:machine, id: 123, name: 'Cleo', year: 1980, manufacturer: 'Stern', ipdb_link: 'http://www.foo.com'))
FactoryGirl.create(:location_machine_xref, location: @location, machine: FactoryGirl.create(:machine, id: 456, name: 'Sass', year: 1960, manufacturer: 'Bally', ipdb_link: 'http://www.bar.com'))
FactoryGirl.create(:location_machine_xref, location: @location, machine: FactoryGirl.create(:machine, id: 123, name: 'Cleo', year: 1980, manufacturer: 'Stern', ipdb_link: 'http://www.foo.com', ipdb_id: nil))
FactoryGirl.create(:location_machine_xref, location: @location, machine: FactoryGirl.create(:machine, id: 456, name: 'Sass', year: 1960, manufacturer: 'Bally', ipdb_link: 'http://www.bar.com', ipdb_id: 123))

get '/api/v1/locations/' + @location.id.to_s + '/machine_details.json'
expect(response).to be_success
Expand All @@ -231,12 +241,14 @@
expect(machines[0]['year']).to eq(1980)
expect(machines[0]['manufacturer']).to eq('Stern')
expect(machines[0]['ipdb_link']).to eq('http://www.foo.com')
expect(machines[0]['ipdb_id']).to be_nil

expect(machines[1]['id']).to eq(456)
expect(machines[1]['name']).to eq('Sass')
expect(machines[1]['year']).to eq(1960)
expect(machines[1]['manufacturer']).to eq('Bally')
expect(machines[1]['ipdb_link']).to eq('http://www.bar.com')
expect(machines[1]['ipdb_id']).to eq(123)
end
end
end

0 comments on commit 84b8013

Please sign in to comment.