Skip to content

Instantly share code, notes, and snippets.

@sanaumair
Created March 7, 2012 15:19
Show Gist options
  • Save sanaumair/1993754 to your computer and use it in GitHub Desktop.
Save sanaumair/1993754 to your computer and use it in GitHub Desktop.
class Vanilla::ResultItem
attr_reader :fields
def initialize(result_item_hash)
@fields = result_item_hash
end
def item_type
@fields['item_type']
end
# you can override this method in the certian class
# check the Boat
def partial
item_type
end
def id
@fields['id']
end
def url
@fields['url']
end
def modified_at
@fields['modified_at']
end
def created_at
@fields['created_at']
end
def image_by_size
raise "implement me"
end
def all_images_by_size
raise "implement me"
end
def business_id
@fields['business_id']
end
end
__END__
{
"city" => "Bridgehampton",
"zip5" => "11976",
"display_address" => true,
"bedrooms" => 8,
"uri" => "feed:47/adid=29876",
"sale_status" => "For Sale",
"area_sqft" => 20000.0,
"latitude" => 40.915264,
"primary_image" => 0,
"created_at" => "2011-05-09T10:43:46Z",
"business_name" => "Susan Breitenbach, SVP",
"business_parent_name" => "The Corcoran Group",
"postal" => "11976",
"country" => "United States",
"country_code" => "US",
"business_ancestor_ids" => [
[0] 47672,
[1] 47677
],
"updated_at" => "2011-05-09T10:43:46Z",
"url" => "http://www.corcoran.com/SBREITENBA",
"indexed" => 1,
"modified_at" => "2011-05-09T10:43:46Z",
"property_types" => [
[0] "Single family"
],
"street" => "Scuttle Hole Rd",
"global_region" => "Americas",
"id" => "812919",
"business_parent_business_id" => 47677,
"business_mini_logo_url" => "/hub/profile_images/000/001/440/trg-1.gif",
"business_id" => 48105,
"new_development" => false,
"bathrooms" => 12.0,
"state_code" => "NY",
"summary" => "THREE PONDS FARM ESTATE - BRIDGEHAMPTON. Situated on over 60 acres of rolling farmland, this remarkable waterfront compound has been designed and built with a commitment to excellence. A magnificent collaboration between three brilliant architects, each a master of his field. The architect of re...",
"description" => "THREE PONDS FARM ESTATE - BRIDGEHAMPTON. Situated on over 60 acres of rolling farmland, this remarkable waterfront compound has been designed and built with a commitment to excellence. A magnificent collaboration between three brilliant architects, each a master of his field. The architect of record, Allan Greenberg, L.L.D., is regarded as one of the prime interpreters of classicism who has worked on The White House, Princeton University, and Rockefeller Center. The Golf Course (U.S.G.A. rated 18 hole) was designed by renowned golf-course architect Rees Jones, designer of many of the highly-rated courses including Atlantic Golf Club and The Bridge. Landscape architect Edmund D. Hollander, P.C., is among the foremost designers in his field. His inspired transformation of the terrain of this unique property is already legendary. The Gardens, 14 in all, include a traditional English cottage garden, a crabapple alley, a lily walk, a potager (French for vegetable gardens) as well as hydrangea, butterfly and world class rose gardens. Green carpeted grounds are punctuated by three pristine ponds and dock all stocked with bass, perch and pickerel. The 75 ft. flush edge pool with its own pavilion and the grass tennis court and are the definition of leisurely living. Hamptons-based Breitenbach Builders was responsible for executing the plans. The firm incorporated the highest caliber of materials, craftsmanship and unsurpassable quality in this prestigious project. Internationally acclaimed interior designer Michael Christiano added a master's finesse. Outbuildings include a beautiful guest cottage on 4 acres, pool house, storage silo, barns, pro shop and a unique fish house and Orangerie. ",
"longitude" => -72.34715,
"full_address" => "919 Scuttle Hole Rd Bridgehampton Suffolk 11976 New York",
"global_region_code" => "am",
"changed_at" => "2011-05-09T10:43:46Z",
"transaction" => "buy",
"sale_price" => 68000000,
"picture_urls_count" => 0
}
Hash < Object
class ResultItem < Vanilla::ResultItem
%w(available_from available_to beam_m berths boat_category boat_type built_year business_profile cabin_count cabins
capacity_kg city contact_email contact_phone country_code cover_type created_at cruising_speed_kn currency
cylinder_capacity cylinders description designed_for designer displacment_kg draft_m drive_type electric_system_voltage
electronic_equipment engine_count engine_equipment engine_hours engine_location engine_manufacturer engines floor_type
fresh_water_capacity fuel fuel_capacity galley_equipment general_equipment holding_tank_capacity horse_power hours
hull_construction hull_type id is_new item_subtype item_type keel_type loa_m manufacturer marina max_speed_kn model
navigation_equipment operating_voltage original_ref passenger_capacity pdf_url photos_img propeller_type
rig_sails_equipment sale_status skin_material src_fuel src_manufacturer starting_type steering_type system_price_euro
tankage toilets_count transaction_type transom_type updated_at uri vat_included video_url weight).each do |proxy_method|
module_eval %Q?
def #{proxy_method}
@fields.send('[]', "#{proxy_method}") || ''
end
?
end
def price_upon_request
return true if @fields['is_poa']
false
end
def price
# temporary solution until hub correctly sets is_poa for anything below 5
return nil if price_upon_request || system_price_euro.to_f < 5
Price.new(system_price_euro.to_f).exchange_to(I18n.locale)
end
def partial
'boat'
end
def breadcrumb_title
max_size = 35
model.size > max_size ? model[0..(max_size - 1)] + '...' : model
end
def result_heading
heading = (manufacturer.blank? ? src_manufacturer.to_s : manufacturer.to_s)
heading += ((heading.blank? ? '' : ' - ') + model.to_s) if !model.to_s.blank?
return I18n.t(:"untitled", :scope => [:search, :boat]) if heading.blank?
heading
end
# Returns an array of images
# wrap in 'shoot' helper
def photos max = nil
return [] unless self.has_photos?
if max.blank?
self.photos_img
else
max = max - 1 # correct index
self.photos_img[0..max]
end
end
# Should create similar methods for videos
# once we know the name for the videos field
def has_photos?
self.respond_to?(:photos_img) && !self.photos_img.blank? ? true : false
end
def how_many_photos?
self.has_photos? ? self.photos_img.size : 0
end
# returns boat's horse power as kW h
def kwh(precision = 1)
return 0 if !self.respond_to?(:horse_power) || self.horse_power.blank? || self.horse_power <= 0
sprintf('%.1f', (self.horse_power * 0.7457)).to_f
end
def boat_of_week_specs
specs = result_specs(6)
if self.respond_to?(:price) && !self.price.blank?
label = I18n.t(:'price', :scope => [:models, :search, :boat])
specs[label] = self.price
end
specs
end
def home_featured_specs
specs = ActiveSupport::OrderedHash.new
specs['type'] = I18n.t(self.item_type.to_sym, :scope => [:global, :system, :item_types]) if self.respond_to?(:item_type) && !self.item_type.blank?
specs['year'] = self.built_year if self.respond_to?(:built_year) && !self.built_year.blank?
specs['length'] = self.loa_m if self.respond_to?(:loa_m) && !self.loa_m.blank?
specs['country'] = I18n.t(self.country_code.upcase.to_sym, :scope => [:global, :system, :countries]) if self.respond_to?(:country_code) && !self.country_code.blank?
specs['price'] = self.price if self.respond_to?(:price) && !self.price.blank?
specs
end
def result_specs max = nil
generate_specs(result_specs_list, max)
end
def item_details_specs max = nil
generate_specs(item_details_specs_list, max)
end
private
def generate_specs list, max = nil
specs = ActiveSupport::OrderedHash.new
return specs if list.blank?
list.each_with_index do |item, i|
break if max && i >= max
label = I18n.t(item[:label], :scope => 'models.search.boat')
value = spec_value(item)
next if !value
specs[label] = value
end
location = []
location << city if self.respond_to?(:city) && !city.blank?
location << country_code.upcase if self.respond_to?(:country_code) && !country_code.blank?
label = I18n.t(:'location', :scope => 'models.search.boat')
value = location.join(', ')
specs[label] = value unless value.blank?
specs
end
def spec_value item
value = send(item[:property])
return nil if value.blank? || value == 0
# BOSOND-985
return nil if price_upon_request && item[:property] == :vat_included
scope = item[:scope]
if value.kind_of?(TrueClass) || value.kind_of?(FalseClass)
value = (value ? :yes : :no)
scope = 'models.search.boat' unless scope
end
if scope
I18n.t(value, :scope => scope)
else
value
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment