diff --git a/app/assets/javascripts/application.js.erb b/app/assets/javascripts/application.js.erb index 3bc72d8df..14863c610 100644 --- a/app/assets/javascripts/application.js.erb +++ b/app/assets/javascripts/application.js.erb @@ -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) { diff --git a/app/controllers/api/v1/locations_controller.rb b/app/controllers/api/v1/locations_controller.rb index 7e2499533..49c39676d 100644 --- a/app/controllers/api/v1/locations_controller.rb +++ b/app/controllers/api/v1/locations_controller.rb @@ -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 @@ -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 @@ -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 diff --git a/app/controllers/locations_controller.rb b/app/controllers/locations_controller.rb index c22bf6fd1..1e3e91698 100644 --- a/app/controllers/locations_controller.rb +++ b/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 } } diff --git a/app/models/location.rb b/app/models/location.rb index 2421fde4a..6836221ee 100644 --- a/app/models/location.rb +++ b/app/models/location.rb @@ -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 diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 431ff4b92..c652bdb0f 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -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' diff --git a/db/migrate/20150928221835_add_ipdb_id_to_machines.rb b/db/migrate/20150928221835_add_ipdb_id_to_machines.rb new file mode 100644 index 000000000..d455ede23 --- /dev/null +++ b/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 diff --git a/db/schema.rb b/db/schema.rb index 578fb0c39..a0415ccef 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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| diff --git a/spec/features/locations_controller_spec.rb b/spec/features/locations_controller_spec.rb index cf7545b44..2e0cbd5b7 100644 --- a/spec/features/locations_controller_spec.rb +++ b/spec/features/locations_controller_spec.rb @@ -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') @@ -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 diff --git a/spec/requests/api/v1/locations_controller_spec.rb b/spec/requests/api/v1/locations_controller_spec.rb index 2c9c0b155..b7ac26fc5 100644 --- a/spec/requests/api/v1/locations_controller_spec.rb +++ b/spec/requests/api/v1/locations_controller_spec.rb @@ -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 @@ -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 @@ -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