diff --git a/Gemfile b/Gemfile index 4de752927c..5fef02966a 100644 --- a/Gemfile +++ b/Gemfile @@ -32,6 +32,7 @@ gem 'chili', :github => 'eaterprises/chili' gem 'deface', :github => 'spree/deface' gem 'paperclip' gem 'geocoder' +gem 'gmaps4rails' # Gems used only for assets and not required # in production environments by default. diff --git a/Gemfile.lock b/Gemfile.lock index 7531905c0c..f070db7595 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -264,6 +264,7 @@ GEM formatador (0.2.4) fssm (0.2.10) geocoder (1.1.8) + gmaps4rails (1.5.6) haml (3.1.6) highline (1.6.18) hike (1.2.3) @@ -463,6 +464,7 @@ DEPENDENCIES factory_girl_rails faker geocoder + gmaps4rails haml jquery-rails letter_opener diff --git a/app/assets/javascripts/search/all.js b/app/assets/javascripts/search/all.js index 39e41b5732..648dc7f440 100644 --- a/app/assets/javascripts/search/all.js +++ b/app/assets/javascripts/search/all.js @@ -10,4 +10,4 @@ //= require foundation //= require_tree . -$(function(){ $(document).foundation(); }); +//$(function(){ $(document).foundation(); }); diff --git a/app/assets/stylesheets/search/all.css b/app/assets/stylesheets/search/all.css index e4409decee..15cb708dde 100644 --- a/app/assets/stylesheets/search/all.css +++ b/app/assets/stylesheets/search/all.css @@ -4,5 +4,6 @@ * the top of the compiled file, but it's generally better to create a new file per style scope. *= require_self *= require ./foundation_and_overrides - *= require_tree . + *= require ./gmaps4rails + *= require_tree . */ \ No newline at end of file diff --git a/app/assets/stylesheets/search/enteprise_search.css.scss b/app/assets/stylesheets/search/enteprise_search.css.scss new file mode 100644 index 0000000000..0a9311ca8c --- /dev/null +++ b/app/assets/stylesheets/search/enteprise_search.css.scss @@ -0,0 +1,11 @@ +@import "foundation/variables"; +@import "foundation/components/global"; + +.search-result { + min-height: emCalc(50); + + .secondary-info { + font-size: 0.8em; + color: #b7b7b7; + } +} \ No newline at end of file diff --git a/app/assets/stylesheets/search/home.css.scss b/app/assets/stylesheets/search/home.css.scss index 520fc86fec..51e895d4f4 100644 --- a/app/assets/stylesheets/search/home.css.scss +++ b/app/assets/stylesheets/search/home.css.scss @@ -87,4 +87,8 @@ $adjust: true; @include panel($bg, $padding, $adjust); +} + +.centered { + text-align: center; } \ No newline at end of file diff --git a/app/controllers/enterprises_controller.rb b/app/controllers/enterprises_controller.rb index 54d4859a0b..d45b316dcd 100644 --- a/app/controllers/enterprises_controller.rb +++ b/app/controllers/enterprises_controller.rb @@ -34,7 +34,8 @@ class EnterprisesController < BaseController def search suburb = Suburb.find_by_postcode(params[:postcode]) - @enterpsises = Enterprise.all + @enterpsises = Enterprise.find_near(suburb) + @enterprises_json = @enterpsises.to_gmaps4rails render :layout => "landing_page" end end diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index d3e8ace54d..b325257bc6 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -1,4 +1,8 @@ class Enterprise < ActiveRecord::Base + ENTERPRISE_SEARCH_RADIUS = 100 + + acts_as_gmappable :process_geocoding => false + has_many :supplied_products, :class_name => 'Spree::Product', :foreign_key => 'supplier_id' has_many :distributed_orders, :class_name => 'Spree::Order', :foreign_key => 'distributor_id' belongs_to :address, :class_name => 'Spree::Address' @@ -7,6 +11,8 @@ class Enterprise < ActiveRecord::Base has_many :enterprise_roles has_many :users, through: :enterprise_roles + delegate :latitude, :longitude, :city, :state_name, :to => :address + accepts_nested_attributes_for :address validates_presence_of :name @@ -65,8 +71,15 @@ class Enterprise < ActiveRecord::Base count(distinct: true) end - def self.search_near(suburb) - Enterprise.near [suburb.latitude, suburb.longitude] + def self.find_near(suburb) + enterprises = [] + + unless suburb.nil? + addresses = Spree::Address.near([suburb.latitude, suburb.longitude], ENTERPRISE_SEARCH_RADIUS, :units => :km).limit(10) + enterprises = addresses.collect(&:enterprise) + end + + enterprises end def has_supplied_products_on_hand? diff --git a/app/models/spree/address_decorator.rb b/app/models/spree/address_decorator.rb index 54938b25f4..b007f3621b 100644 --- a/app/models/spree/address_decorator.rb +++ b/app/models/spree/address_decorator.rb @@ -1,7 +1,11 @@ Spree::Address.class_eval do + has_one :enterprise + geocoded_by :full_address after_validation :geocode + delegate :name, :to => :state, :prefix => true + def full_address full_address = [address1, address2, zipcode, city, country.name, state.name] filtered_address = full_address.select{ |field| !field.nil? && field != '' } diff --git a/app/views/enterprises/search.html.haml b/app/views/enterprises/search.html.haml index 08e5b63c5d..e6ccd6b2cf 100644 --- a/app/views/enterprises/search.html.haml +++ b/app/views/enterprises/search.html.haml @@ -1,4 +1,17 @@ -- @enterpsises.each do |enterprise| +- if @enterpsises.any? + .row.full-width + .large-4.columns + - @enterpsises.each do |enterprise| + .search-result + .large-12.columns + = enterprise.name + %span.secondary-info= "#{enterprise.city}, #{enterprise.state_name}" + .large-8.columns + = gmaps4rails(@enterprises_json) + +- else .row - .large-12.columns - = enterprise.name \ No newline at end of file + .large-12.large-centered.columns + .centered + = "No distribution hubs found within #{Enterprise::ENTERPRISE_SEARCH_RADIUS} kilometer radius" + = link_to "Try again...", new_landing_page_path \ No newline at end of file diff --git a/app/views/layouts/landing_page.html.haml b/app/views/layouts/landing_page.html.haml index c7e902665c..726c7d38e4 100644 --- a/app/views/layouts/landing_page.html.haml +++ b/app/views/layouts/landing_page.html.haml @@ -30,3 +30,5 @@ %li.divider %li= link_to "Farmers", "#" = yield + = yield :scripts +