Skip to content
Browse files

Search wiki.

  • Loading branch information...
1 parent d99a045 commit a3281e17347f2de8cfcc7a39bc5434bdb21b4def @pwnall committed Jul 18, 2010
View
11 app/controllers/home_controller.rb
@@ -0,0 +1,11 @@
+class HomeController < ApplicationController
+ def index
+ if ListingPin.where('score > 0').count
+ redirect_to listing_pins_path
+ elsif Listing.count
+ redirect_to listings_path
+ else
+ redirect_to scrape_orders_path
+ end
+ end
+end
View
13 app/controllers/listing_pins_controller.rb
@@ -0,0 +1,13 @@
+class ListingPinsController < ApplicationController
+ # GET /listing_pins
+ # GET /listing_pins.xml
+ def index
+ @listing_pins = ListingPin.where('score > 0').order('score DESC').all.
+ group_by(&:rough_score)
+
+ respond_to do |format|
+ format.html # index.html.erb
+ format.xml { render :xml => @listing_pins }
+ end
+ end
+end
View
74 app/controllers/listings_controller.rb
@@ -20,26 +20,33 @@ def search
radius = params[:radius].to_f
end
- rel = Listing
- if params[:rooms_min]
+ rel = Listing.includes(:pin).order('posted_at DESC')
+ unless params[:rooms_min].blank?
rel = rel.where('rooms >= ?', params[:rooms_min].to_i)
end
- if params[:rooms_max]
+ unless params[:rooms_max].blank?
rel = rel.where('rooms <= ?', params[:rooms_max].to_i)
end
@listings = rel.all.select do |listing|
- if params[:room_price_max]
- if (listing.price / listing.rooms.to_f) > params[:room_price_max].to_f
- next false
- end
+ next false if listing.pin && listing.pin.hide?
+
+ room_price = listing.price / listing.rooms.to_f
+ if !params[:room_price_max].blank? &&
+ room_price > params[:room_price_max].to_f
+ next false
end
+ if !params[:room_price_min].blank? &&
+ room_price < params[:room_price_min].to_f
+ next false
+ end
+
if location
if radius < listing.distance_to(location, :units => :miles)
next false
end
end
true
- end
+ end
respond_to do |format|
format.html # search.html.erb
@@ -58,53 +65,20 @@ def show
end
end
- # GET /listings/new
- # GET /listings/new.xml
- def new
- @listing = Listing.new
-
- respond_to do |format|
- format.html # new.html.erb
- format.xml { render :xml => @listing }
- end
- end
-
- # GET /listings/1/edit
- def edit
+ # PUT /listings/1/pin?score=-1
+ def pin
@listing = Listing.find(params[:id])
- end
-
- # POST /listings
- # POST /listings.xml
- def create
- @listing = Listing.new(params[:listing])
-
- respond_to do |format|
- if @listing.save
- format.html { redirect_to(@listing, :notice => 'Listing was successfully created.') }
- format.xml { render :xml => @listing, :status => :created, :location => @listing }
- else
- format.html { render :action => "new" }
- format.xml { render :xml => @listing.errors, :status => :unprocessable_entity }
- end
+ if @listing.pin
+ @listing.pin.update_attributes! :score => params[:score]
+ else
+ @listing.pin = ListingPin.create! :listing => @listing, :score => params[:score]
end
- end
-
- # PUT /listings/1
- # PUT /listings/1.xml
- def update
- @listing = Listing.find(params[:id])
-
+
respond_to do |format|
- if @listing.update_attributes(params[:listing])
- format.html { redirect_to(@listing, :notice => 'Listing was successfully updated.') }
- format.xml { head :ok }
- else
- format.html { render :action => "edit" }
- format.xml { render :xml => @listing.errors, :status => :unprocessable_entity }
- end
+ format.js # pin.js.rjs
end
end
+
# DELETE /listings/1
# DELETE /listings/1.xml
View
2 app/helpers/home_helper.rb
@@ -0,0 +1,2 @@
+module HomeHelper
+end
View
14 app/helpers/listing_pins_helper.rb
@@ -0,0 +1,14 @@
+module ListingPinsHelper
+ def enable_listing_pins
+ @_enable_listing_pins ||=
+ enable_listing_search && ListingPin.where('score > 0').count > 0
+ end
+
+ def emphasize_listing_pins
+ @_emphasize_scrape_orders ||= enable_listing_pins
+ end
+
+ def pin_score_description(score)
+ {-1 => 'Hidden', 1 => 'Meh', 2 => 'Like', 3 => 'Love'}[score.to_i]
+ end
+end
View
12 app/helpers/listings_helper.rb
@@ -1,2 +1,14 @@
module ListingsHelper
+ def enable_listing_search
+ @_enable_listing_search ||= enable_scrape_orders && Listing.count != 0
+ end
+
+ def emphasize_listing_search
+ @_emphasize_scrape_orders ||=
+ enable_listing_search && ListingPin.where('score > 0').count == 0
+ end
+
+ def google_maps_link(location)
+ link_to location, "http://maps.google.com?q=loc:#{URI.encode location}"
+ end
end
View
7 app/helpers/scrape_orders_helper.rb
@@ -1,2 +1,9 @@
module ScrapeOrdersHelper
+ def enable_scrape_orders
+ @_enable_scrape_orders ||= true
+ end
+
+ def emphasize_scrape_orders
+ @_emphasize_scrape_orders ||= Listing.count == 0
+ end
end
View
2 app/models/listing.rb
@@ -1,6 +1,8 @@
class Listing < ActiveRecord::Base
acts_as_mappable
+ has_one :pin, :class_name => 'ListingPin', :dependent => :destroy
+
def location=(new_location)
super
geocode = GeocodeFetch.get location, city
View
11 app/models/listing_pin.rb
@@ -0,0 +1,11 @@
+class ListingPin < ActiveRecord::Base
+ belongs_to :listing
+
+ def hide?
+ score < 0.0
+ end
+
+ def rough_score
+ score.to_i
+ end
+end
View
5 app/models/scrape_order.rb
@@ -2,7 +2,8 @@ class ScrapeOrder < ActiveRecord::Base
def run
starting_time = Time.now
ListingPopulator.new.run start_url, page_depth
- self.last_ran_at = Time.now
- self.last_runtime = last_ran_at - starting_time
+ self.ran_last_at = Time.now
+ self.last_runtime = ran_last_at - starting_time
+ save!
end
end
View
3 app/views/layouts/_footer.html.erb
@@ -0,0 +1,3 @@
+<div id="footer">
+ <p>Hacked together by <a href="http://www.costan.us">Victor Costan</a></p>
+</div>
View
30 app/views/layouts/_header.html.erb
@@ -0,0 +1,30 @@
+<div id="header">
+ <h1>
+ CraigsList Housing Helper
+ <ol>
+ <% if enable_scrape_orders %>
+ <li class="<%= emphasize_scrape_orders ? 'emphasized' : '' %>">
+ <%= link_to 'Scrape Craigslist', scrape_orders_path %>
+ </li>
+ <% else %>
+ <li class="disabled">Scrape Craigslist</li>
+ <% end %>
+
+ <% if enable_listing_search %>
+ <li class="<%= emphasize_listing_search ? 'emphasized' : '' %>">
+ <%= link_to 'Search Listings', listings_path %>
+ </li>
+ <% else %>
+ <li class="disabled">Search Listings</li>
+ <% end %>
+
+ <% if enable_listing_pins %>
+ <li class="<%= emphasize_listing_pins ? 'emphasized' : '' %>">
+ <%= link_to 'Pursue Listings', listing_pins_path %>
+ </li>
+ <% else %>
+ <li class="disabled">Pursue Listings</li>
+ <% end %>
+ </ol>
+ </h1>
+</div>
View
12 app/views/layouts/application.html.erb
@@ -7,8 +7,14 @@
<%= csrf_meta_tag %>
</head>
<body>
-
-<%= yield %>
-
+ <div id="page_wrapper">
+ <%= render 'layouts/header' -%>
+ <div id="content_wrapper">
+ <div id="content">
+ <%= yield -%>
+ </div>
+ </div>
+ <%= render 'layouts/footer' -%>
+ </div>
</body>
</html>
View
8 app/views/listing_pins/index.html.erb
@@ -0,0 +1,8 @@
+<h2>Rated Listings</h2>
+
+<% @listing_pins.keys.sort.reverse.each do |score| %>
+<h3><%= pin_score_description(score) %></h3>
+<% @listing_pins[score].each do |listing_pin| %>
+<%= render 'listings/search_result', :listing => listing_pin.listing %>
+<% end %>
+<% end %>
View
10 app/views/listings/_search_result.html.erb
@@ -1,8 +1,8 @@
-<div class="search_result">
+<div class="search_result" id="search_result_listing_<%= listing.id %>">
<div class="header">
<span class="price">$<%= listing.price %></span>
<span class="rooms"><%= pluralize listing.rooms, 'room' %></span>
- <span class="location"><%= listing.location %></span>
+ <span class="location"><%= google_maps_link listing.location %></span>
</div>
<div class="description">
<%= listing.title %>
@@ -12,7 +12,9 @@
<%= link_to listing.cl_url, listing.cl_url %>
</div>
<div class="actions">
- <%= link_to 'Show', listing %>
- <%= link_to 'Destroy', listing, :confirm => 'Are you sure?', :method => :delete %>
+ <%= link_to 'Hide', pin_listing_path(listing, :score => -1), :method => :put, :remote => true %>
+ <%= link_to 'Meh', pin_listing_path(listing, :score => 1), :method => :put, :remote => true %>
+ <%= link_to 'Like', pin_listing_path(listing, :score => 2), :method => :put, :remote => true %>
+ <%= link_to 'Love', pin_listing_path(listing, :score => 3), :method => :put, :remote => true %>
</div>
</div>
View
BIN app/views/listings/index.html.erb
Binary file not shown.
View
1 app/views/listings/pin.js.rjs
@@ -0,0 +1 @@
+page['search_result_listing_' + @listing.id.to_s].fade
View
2 app/views/listings/search.html.erb
@@ -1,4 +1,4 @@
-<h1>Listings</h1>
+<h2>Search Results</h2>
<% @listings.each do |listing| %>
<%= render 'listings/search_result', :listing => listing %>
View
2 app/views/scrape_orders/index.html.erb
@@ -1,4 +1,4 @@
-<h1>Scrape Orders</h1>
+<h2>Scrape Orders</h2>
<table>
<tr>
View
5 config/routes.rb
@@ -1,4 +1,6 @@
Clh::Application.routes.draw do
+ resources :listing_pins
+
resources :scrape_orders do
member { post :run }
end
@@ -9,6 +11,7 @@
resources :listings do
collection { get :search }
+ member { put :pin }
end
# The priority is based upon order of creation:
@@ -60,7 +63,7 @@
# You can have the root of your site routed with "root"
# just remember to delete public/index.html.
- root :to => "listings#index"
+ root :to => "home#index"
# See how all your routes lay out with "rake routes"
View
14 db/migrate/20100718025129_create_listing_pins.rb
@@ -0,0 +1,14 @@
+class CreateListingPins < ActiveRecord::Migration
+ def self.up
+ create_table :listing_pins do |t|
+ t.integer :listing_id, :null => false
+ t.float :score, :null => false
+ end
+ add_index :listing_pins, :listing_id, :unique => true, :null => false
+ add_index :listing_pins, :score, :unique => false, :null => false
+ end
+
+ def self.down
+ drop_table :listing_pins
+ end
+end
View
10 db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20100718005836) do
+ActiveRecord::Schema.define(:version => 20100718025129) do
create_table "geocode_fetches", :force => true do |t|
t.string "location", :limit => 128, :null => false
@@ -22,6 +22,14 @@
add_index "geocode_fetches", ["location", "city"], :name => "index_geocode_fetches_on_location_and_city", :unique => true
+ create_table "listing_pins", :force => true do |t|
+ t.integer "listing_id", :null => false
+ t.float "score", :null => false
+ end
+
+ add_index "listing_pins", ["listing_id"], :name => "index_listing_pins_on_listing_id", :unique => true
+ add_index "listing_pins", ["score"], :name => "index_listing_pins_on_score"
+
create_table "listings", :force => true do |t|
t.string "cl_url", :limit => 128, :null => false
t.integer "price", :null => false
View
9 public/stylesheets/footer.css
@@ -0,0 +1,9 @@
+#footer {
+ margin: 0;
+ padding: 5px 5px 20px 5px;
+ text-align: center;
+}
+#footer p {
+ margin: 0;
+ padding: 0;
+}
View
16 public/stylesheets/generic.css
@@ -0,0 +1,16 @@
+th {
+ text-align: left;
+}
+tr:nth-child(odd) {
+ background-color: #dddddd;
+}
+tr:nth-child(even) {
+ background-color: #ffffff;
+}
+
+table.form tr:nth-child(odd) {
+ background: inherit;
+}
+table.form tr:nth-child(even) {
+ background: inherit;
+}
View
31 public/stylesheets/header.css
@@ -0,0 +1,31 @@
+#header {
+ border-bottom: 1px solid #8080FF;
+ vertical-align: baseline;
+}
+
+h1 {
+ font-size: 11pt;
+ margin: 0;
+}
+h1 ol {
+ display: inline-block;
+ margin: 0;
+ font-size: 11pt;
+ font-weight: normal;
+}
+h1 ol li {
+ float: left;
+ padding-right: 40px;
+ margin: 0;
+}
+h1 ol li.emphasized {
+ font-weight: bold;
+}
+
+#header a {
+ text-decoration: none;
+}
+
+h2 {
+ margin: 0 0 0.5em 0;
+}
View
36 public/stylesheets/layout.css
@@ -0,0 +1,36 @@
+body {
+ margin: 0;
+ padding: 0;
+ border: none;
+ font-size: 10px;
+}
+
+#page_wrapper {
+ margin: 0;
+ padding: 0;
+ min-width: 1000px;
+}
+
+#header {
+ padding: 0 2em;
+}
+
+#content_wrapper {
+ margin: 0 auto;
+ padding: 5px;
+ width: 1000px;
+ overflow: hidden;
+}
+#content {
+ margin: 0;
+ padding: 0;
+}
+
+#footer {
+ font-size: 1.3em;
+}
+
+#content {
+ font-size: 10pt;
+ line-height: 18px;
+}
View
20 public/stylesheets/scaffold.css
@@ -1,20 +1,6 @@
-body { background-color: #fff; color: #333; }
-
-body, p, ol, ul, td {
- font-family: verdana, arial, helvetica, sans-serif;
- font-size: 13px;
- line-height: 18px;
-}
-
-pre {
- background-color: #eee;
- padding: 10px;
- font-size: 11px;
-}
-
-a { color: #000; }
-a:visited { color: #666; }
-a:hover { color: #fff; background-color:#000; }
+a { color: #0000ff; }
+a:visited { color: #4444ff; }
+a:hover { color: #000066; }
div.field, div.actions {
margin-bottom: 10px;
View
9 test/fixtures/listing_pins.yml
@@ -0,0 +1,9 @@
+# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
+
+one:
+ listing_id: 1
+ hide: false
+
+two:
+ listing_id: 1
+ hide: false
View
8 test/functional/home_controller_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class HomeControllerTest < ActionController::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end
View
49 test/functional/listing_pins_controller_test.rb
@@ -0,0 +1,49 @@
+require 'test_helper'
+
+class ListingPinsControllerTest < ActionController::TestCase
+ setup do
+ @listing_pin = listing_pins(:one)
+ end
+
+ test "should get index" do
+ get :index
+ assert_response :success
+ assert_not_nil assigns(:listing_pins)
+ end
+
+ test "should get new" do
+ get :new
+ assert_response :success
+ end
+
+ test "should create listing_pin" do
+ assert_difference('ListingPin.count') do
+ post :create, :listing_pin => @listing_pin.attributes
+ end
+
+ assert_redirected_to listing_pin_path(assigns(:listing_pin))
+ end
+
+ test "should show listing_pin" do
+ get :show, :id => @listing_pin.to_param
+ assert_response :success
+ end
+
+ test "should get edit" do
+ get :edit, :id => @listing_pin.to_param
+ assert_response :success
+ end
+
+ test "should update listing_pin" do
+ put :update, :id => @listing_pin.to_param, :listing_pin => @listing_pin.attributes
+ assert_redirected_to listing_pin_path(assigns(:listing_pin))
+ end
+
+ test "should destroy listing_pin" do
+ assert_difference('ListingPin.count', -1) do
+ delete :destroy, :id => @listing_pin.to_param
+ end
+
+ assert_redirected_to listing_pins_path
+ end
+end
View
4 test/unit/helpers/home_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class HomeHelperTest < ActionView::TestCase
+end
View
4 test/unit/helpers/listing_pins_helper_test.rb
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class ListingPinsHelperTest < ActionView::TestCase
+end
View
8 test/unit/listing_pin_test.rb
@@ -0,0 +1,8 @@
+require 'test_helper'
+
+class ListingPinTest < ActiveSupport::TestCase
+ # Replace this with your real tests.
+ test "the truth" do
+ assert true
+ end
+end

0 comments on commit a3281e1

Please sign in to comment.
Something went wrong with that request. Please try again.