Permalink
Browse files

Let's make this an idiomatic Sunspot application.

  • Loading branch information...
1 parent beef4a4 commit 6aab1a75d1c746a6031b46e766bfb191977208b0 @nz nz committed Jul 23, 2011
View
@@ -6,3 +6,4 @@ tmp/
*.rbc
solr/data
vendor/bundle
+log/*.lck
View
@@ -14,12 +14,13 @@ gem 'jquery-rails'
gem 'pjax-rails' # just for the controller stuff for now
# Everything else
-gem 'rsolr'
+gem 'sunspot_rails', :git => 'git://github.com/nz/sunspot'
group :development, :test do
gem 'foreman'
gem 'growl'
gem 'guard-rspec'
+ gem 'rb-fsevent'
gem 'rspec-rails'
gem 'sqlite3'
end
View
@@ -1,4 +1,16 @@
GIT
+ remote: git://github.com/nz/sunspot
+ revision: ad0153ceff8863bf59b547060c2ce868a3b057ba
+ specs:
+ sunspot (1.2.1)
+ escape (= 0.0.4)
+ pr_geohash (~> 1.0)
+ rsolr (= 0.12.1)
+ sunspot_rails (1.2.1)
+ nokogiri
+ sunspot (= 1.2.1)
+
+GIT
remote: git://github.com/rails/rails.git
revision: 0988da62e24abad8ce745bb6ac5431740d02b5ec
branch: 3-1-stable
@@ -60,6 +72,7 @@ GEM
coffee-script-source (1.1.1)
diff-lcs (1.1.2)
erubis (2.7.0)
+ escape (0.0.4)
execjs (1.1.0)
multi_json (~> 1.0)
foreman (0.18.0)
@@ -83,10 +96,12 @@ GEM
treetop (~> 1.4.8)
mime-types (1.16)
multi_json (1.0.3)
+ nokogiri (1.5.0)
pg (0.11.0)
pjax-rails (0.1.4)
jquery-rails
polyglot (0.3.1)
+ pr_geohash (1.0.0)
rack (1.3.1)
rack-cache (1.0.2)
rack (>= 0.4)
@@ -98,8 +113,9 @@ GEM
rack (>= 1.0)
raindrops (0.7.0)
rake (0.9.2)
+ rb-fsevent (0.4.1)
rdoc (3.8)
- rsolr (1.0.1)
+ rsolr (0.12.1)
builder (>= 2.1.2)
rspec (2.5.0)
rspec-core (~> 2.5.0)
@@ -147,9 +163,10 @@ DEPENDENCIES
pg
pjax-rails
rails!
- rsolr
+ rb-fsevent
rspec-rails
sass
sqlite3
+ sunspot_rails!
uglifier
unicorn
View
@@ -1,2 +1,2 @@
-solr: rake solr:run
+solr: rake sunspot:solr:run
web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
@@ -1,10 +1,11 @@
class WebhookController < ApplicationController
def gem
- Rubygem.create({
- id: json['name']
- }.merge(json))
- head 200
+ if Rubygem.create(json)
+ head 200
+ else
+ head 400
+ end
end
protected
View
@@ -1,148 +1,33 @@
-class Rubygem #< Solr::Document
-
- attr_accessor :attributes
-
- def initialize(attributes={})
- @attributes = {}
- attributes.each do |name, val|
- send "#{name}=", val
- end
- end
-
- def self.index(name, options={})
- name = name.to_sym
- type = name == :id ? nil : (options.delete(:as) || :text).to_sym
- solr_field_name = [name, type].compact.join("_").to_sym
- (@@fields||={})[name] = {
- as: type,
- solr_field_name: solr_field_name
- }.merge(options)
- define_method "#{name}=" do |value|
- @attributes[name] = value
- end
- alias_method "#{solr_field_name}=", "#{name}="
- define_method "#{name}" do
- @attributes[name]
- end
- end
-
- DYNAMIC_FIELD = /dynamicField name="\*_([^"]+)".*type="([^"]+)"/
- FIELD = /field name="([^_][^"]*)".*type="([^"]+)"/
-
- class << self
- Rails.root.join('solr/conf/schema.xml').open do |schema|
- @@fields ||= {}
- schema.readlines.each do |line|
- case line
- when DYNAMIC_FIELD
- # define a class method to specify other fields of this type
- field_suffix = $1
- field_type = $2
- define_method field_suffix do |name, options={}|
- index name, {as: field_suffix}
- end
- end
- end
- end
- end
-
- Rails.root.join('solr/conf/schema.xml').open do |schema|
- @@fields ||= {}
- schema.readlines.each do |line|
- case line
- when FIELD
- # define an instance getter and setter for this specific field
- field_name = $1
- field_type = $2
- index field_name, as: field_type
- end
- end
- end
-
- def to_solr
- solr_doc = {}
- solr_doc[:__id] = "#{attributes[:id]} #{Rails.env}"
- solr_doc[:__env] = Rails.env.to_s
- @@fields.each do |name, options|
- if @attributes[name] # || options[:proc]
- solr_doc[options[:solr_field_name]] = attributes[name]
- end
- end
- solr_doc
- end
-
- text :info
- text :authors
-
- string :bug_tracker_uri
- string :documentation_uri
- string :gem_uri
- string :homepage_uri
- string :mailing_list_uri
- string :project_uri
- string :source_code_uri
- string :version
- string :wiki_uri
-
- name :name, boost: 2
- name :runtime_dependencies
- name :development_dependencies
-
- sint :downloads
- sint :version_downloads
-
- def self.find(*args)
- if args.length == 1
- json = JSON.parse(
- $solr.get 'select', params: {
- q: "__id:[#{args.first} #{Rails.env}]",
- wt: "json"
- }
- )
- json ||= {'response' => {'docs' => []}}
- json.collect do |doc|
- new(doc)
- end
- end
- end
-
- def self.create(attributes={})
- new(attributes).tap do |rubygem|
- rubygem.save
- end
- end
-
- def dependencies=(dependencies)
- runtime_dependencies = dependencies['runtime']
- development_dependencies = dependencies['development']
- end
-
- def save
- $solr.add(to_solr)
- $solr.commit
- end
-
- def self.dismax_qf
- @@fields.collect do |name, opts|
- [ opts[:solr_field_name],
- opts[:boost] ].compact.join("^")
- end
+class Rubygem < ActiveRecord::Base
+
+ attr_accessible :downloads, :version_downloads,
+ :name, :authors, :info,
+ :bug_tracker_uri, :documentation_uri, :gem_uri, :homepage_uri,
+ :mailing_list_uri, :project_uri, :source_code_uri, :wiki_uri
+
+ searchable do
+ integer :downloads
+ integer :version_downloads
+
+ text :name
+ text :authors
+ text :info
+
+ string :bug_tracker_uri
+ string :documentation_uri
+ string :gem_uri
+ string :homepage_uri
+ string :mailing_list_uri
+ string :project_uri
+ string :source_code_uri
+ string :wiki_uri
end
def self.search(params={})
- json = JSON.parse(
- $solr.get("select", params: {
- defType: "dismax",
- wt: "json",
- qf: dismax_qf
- }.merge(params))
- )
- json ||= {'response' => {'docs' => []}}
- json['response']['docs'].collect! do |doc|
- d = doc.dup.delete_if{|k,v| %w(__id __env).include?(k) }
- new(d)
+ solr_response = solr_search do
+ keywords params[:q]
end
- json
+ solr_response.results
end
-end
+end
@@ -0,0 +1,23 @@
+class CreateRubygems < ActiveRecord::Migration
+ def change
+ create_table :rubygems do |t|
+ t.integer :downloads
+ t.integer :version_downloads
+
+ t.string :name
+ t.string :authors
+ t.string :info
+
+ t.string :bug_tracker_uri
+ t.string :documentation_uri
+ t.string :gem_uri
+ t.string :homepage_uri
+ t.string :mailing_list_uri
+ t.string :project_uri
+ t.string :source_code_uri
+ t.string :wiki_uri
+
+ t.timestamps
+ end
+ end
+end
View
@@ -0,0 +1,33 @@
+# This file is auto-generated from the current state of the database. Instead
+# of editing this file, please use the migrations feature of Active Record to
+# incrementally modify your database, and then regenerate this schema definition.
+#
+# Note that this schema.rb definition is the authoritative source for your
+# database schema. If you need to create the application database on another
+# system, you should be using db:schema:load, not running all the migrations
+# from scratch. The latter is a flawed and unsustainable approach (the more migrations
+# you'll amass, the slower it'll run and the greater likelihood for issues).
+#
+# It's strongly recommended to check this file into your version control system.
+
+ActiveRecord::Schema.define(:version => 20110723192421) do
+
+ create_table "rubygems", :force => true do |t|
+ t.integer "downloads"
+ t.integer "version_downloads"
+ t.string "name"
+ t.string "authors"
+ t.string "info"
+ t.string "bug_tracker_uri"
+ t.string "documentation_uri"
+ t.string "gem_uri"
+ t.string "homepage_uri"
+ t.string "mailing_list_uri"
+ t.string "project_uri"
+ t.string "source_code_uri"
+ t.string "wiki_uri"
+ t.datetime "created_at"
+ t.datetime "updated_at"
+ end
+
+end
View
@@ -5,3 +5,6 @@
#
# cities = City.create([{ :name => 'Chicago' }, { :name => 'Copenhagen' }])
# Mayor.create(:name => 'Emanuel', :city => cities.first)
+
+Rails.root.join('tmp/rubygem-payloads.json').open.readlines.each do |payload|
+end
View
@@ -6,7 +6,7 @@ namespace :solr do
data_dir = Rails.root.join("solr/data", Rails.env)
solr_logs = Rails.root.join("solr/conf/logging.properties")
jetty_logs = Rails.root.join("log")
- port = 8982
+ port = Rails.env.test? ? 8981 : 8982
system %(
cd vendor/solr &&
View
@@ -1,31 +0,0 @@
-<!--
- Licensed to the Apache Software Foundation (ASF) under one or more
- contributor license agreements. See the NOTICE file distributed with
- this work for additional information regarding copyright ownership.
- The ASF licenses this file to You under the Apache License, Version 2.0
- (the "License"); you may not use this file except in compliance with
- the License. You may obtain a copy of the License at
-
- http://www.apache.org/licenses/LICENSE-2.0
-
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
--->
-
-<!-- The content of this page will be statically included into the top
-of the admin page. Uncomment this as an example to see there the content
-will show up.
-
-<hr>
-<i>This line will appear before the first table</i>
-<tr>
-<td colspan="2">
-This row will be appended to the end of the first table
-</td>
-</tr>
-<hr>
-
--->
Oops, something went wrong.

0 comments on commit 6aab1a7

Please sign in to comment.