Skip to content
Browse files

Refactor

  • Loading branch information...
1 parent 4dd5179 commit 389a8d4011ecf65b4f9f1151ecd39a8c6d52cb72 @topfunky committed Sep 11, 2008
View
13 app/controllers/application.rb
@@ -7,9 +7,16 @@ class ApplicationController < ActionController::Base
# See ActionController::RequestForgeryProtection for details
# Uncomment the :secret if you're not using the cookie session store
protect_from_forgery # :secret => '1473d82c87ff75589db6723e090f26ac'
-
- # See ActionController::Base for details
+
+ # See ActionController::Base for details
# Uncomment this to filter the contents of submitted sensitive data parameters
- # from your application log (in this case, all fields with names like "password").
+ # from your application log (in this case, all fields with names like "password").
# filter_parameter_logging :password
+
+ protected
+
+ def database_name
+ ["script", "topfunky", Rails.env].join("_")
+ end
+
end
View
26 app/controllers/notes_controller.rb
@@ -1,40 +1,40 @@
class NotesController < ApplicationController
def index
- @notes = Note.init_from_rows(db.view("notes/by_title-map"))
+ @notes = Note.view(database_name, "notes/by_title-map")
respond_to do |wants|
wants.html
wants.json { render :json => @notes.rows.to_json }
end
end
def show
- @note = Note.new(db.get(params[:id]))
+ @note = Note.find(database_name, params[:id])
respond_to do |wants|
wants.html
wants.json { render :json => @note.attributes.to_json }
end
end
def new
- @note = Note.new
+ @note = Note.new(database_name)
end
def create
- note = Note.new
- result = db.save(note.update(params[:note]))
+ note = Note.new(database_name)
+ result = note.save(params[:note])
respond_to do |wants|
wants.html { redirect_to note_url(result["id"]) }
end
end
def edit
- @note = Note.new(db.get(params[:id]))
+ @note = Note.find(database_name, params[:id])
end
def update
- @note = Note.new(db.get(params[:id]))
- if db.save(@note.update(params[:note]))
+ @note = Note.find(database_name, params[:id])
+ if @note.save(params[:note])
respond_to do |wants|
wants.html { redirect_to note_url(@note) }
end
@@ -47,15 +47,5 @@ def update
def destroy
end
-
- private
-
- def db
- @@couchrest ||= CouchRest.new(COUCHDB_SERVER)
- # TODO Run creation tasks, load views, etc.
- db_name = ["travel", "topfunky", Rails.env].join("_")
- @@couchrest.create_db(db_name) rescue nil
- @db = @@couchrest.database(db_name)
- end
end
View
80 app/models/basic_model.rb
@@ -1,3 +1,5 @@
+require 'couchrest'
+
##
# A simple class to help use CouchDB and CouchRest with Rails.
#
@@ -15,6 +17,19 @@ class BasicModel
attr_accessor :attributes
+ def self.db(database_name)
+ puts "Getting #{database_name}"
+ @@couchrest = CouchRest.new(COUCHDB_SERVER)
+ begin
+ @@couchrest.create_db(database_name)
+ file_manager = CouchRest::FileManager.new(database_name)
+ file_manager.push_views(File.join(Rails.root, "couchdb_views"))
+ rescue
+ nil
+ end
+ @@couchrest.database(database_name)
+ end
+
##
# Takes a record from CouchRest ID call and turns it into something
# usable in Rails.
@@ -24,53 +39,68 @@ class BasicModel
# note._rev
# note.new_record?
# note.title # Any field from the record
-
- def initialize(attributes={})
- @attributes = attributes
+
+ def initialize(database_name, attributes={})
+ @database_name = database_name
+ @attributes = default_attributes.merge(attributes)
end
##
- # Takes a set of results from a CouchRest view call and turns the
+ # To be overridden by subclasses.
+
+ def default_attributes
+ {}
+ end
+
+ ##
+ # Get a document by its _id.
+
+ def self.find(database_name, id)
+ new(database_name, self.db(database_name).get(id))
+ end
+
+ ##
+ # Takes a set of results from a CouchRest view call and turns the
# rows into Rails-friendly objects.
#
# notes = Note.init_from_rows(db.view("notes/by_title"))
# notes.rows.each {|row| row.id ... }
-
- def self.init_from_rows(couchdb_results=[])
- results = self.new(couchdb_results)
+
+ def self.view(database_name, view_name)
+ results = new(database_name, self.db(database_name).view(view_name))
results.rows.each_with_index do |row, index|
- results.rows[index] = self.new(row['value'])
+ results.rows[index] = new(database_name, row['value'])
end
results
end
-
+
##
# Takes a Hash, merges with existing attributes, and returns them with
# the intent that they will be serialized to JSON.
#
# Useful for sending to CouchRest's db.save method.
-
- def update(attributes)
+
+ def save(attributes)
@attributes = @attributes.merge(attributes)
self.type = self.class.name
if new_record?
self.created_at = Time.now
end
self.updated_at = Time.now
self.on_update if self.respond_to?(:on_update)
- @attributes
+ self.class.db(@database_name).save(@attributes)
end
##
# Returns the ID so Rails can use it for forms.
-
+
def id
_id rescue nil
end
alias_method :to_param, :id
-
+
def new_record?
- (_id && _rev).nil?
+ (_rev).nil?
rescue NameError
true
end
@@ -81,19 +111,19 @@ def new_record?
# record._rev
# record.title
# record.title = "Streetside bratwurst vendor"
-
+
def method_missing(method_symbol, *arguments)
method_name = method_symbol.to_s
-
+
case method_name[-1..-1]
- when "="
- @attributes[method_name[0..-2]] = arguments.first
- when "?"
- @attributes[method_name[0..-2]] == true
- else
- # Returns nil on failure so forms will work
- @attributes.has_key?(method_name) ? @attributes[method_name] : nil
+ when "="
+ @attributes[method_name[0..-2]] = arguments.first
+ when "?"
+ @attributes[method_name[0..-2]] == true
+ else
+ # Returns nil on failure so forms will work
+ @attributes.has_key?(method_name) ? @attributes[method_name] : nil
end
end
-
+
end
View
10 app/models/note.rb
@@ -1,8 +1,12 @@
class Note < BasicModel
- def initialize(attributes={})
- defaults = {"title" => nil, "description" => nil, "tags" => []}
- super(defaults.merge(attributes))
+ def default_attributes
+ {
+ "title" => nil,
+ "description" => nil,
+ "tags" => [],
+ "visited_on" => Time.now.strftime('%Y/%m/%d')
+ }
end
##
View
1 app/views/notes/index.html.erb
@@ -5,5 +5,6 @@
<% @notes.rows.each do |note| %>
<h2><%= link_to note.title, note_path(note) %></h2>
<p><%= note.description %></p>
+ <p><em>Tags:</em> <%= note.tags.join(", ") %></p>
<p><%= link_to "Edit", edit_note_path(note) %></p>
<% end %>
View
2 app/views/notes/show.html.erb
@@ -4,4 +4,6 @@
<p><%= @note.description %></p>
+<p><em>Tags:</em> <%= @note.tags.join(", ") %></p>
+
<p><%= link_to "Edit", edit_note_path(@note) %></p>
View
0 views/lib.js → couchdb_views/lib.js
File renamed without changes.
View
5 couchdb_views/notes/by_title-map.js
@@ -0,0 +1,5 @@
+function(doc) {
+ if (doc.type == "Note") {
+ emit(doc.title, doc);
+ }
+}
View
0 views/notes/by_date-map.js → couchdb_views/notes/by_updated_at-map.js
File renamed without changes.
View
5 couchdb_views/notes/by_visited_on-map.js
@@ -0,0 +1,5 @@
+function(doc) {
+ if (doc.type == "Note" && doc.visited_on) {
+ emit(doc.visited_on, doc);
+ }
+}
View
0 views/notes/lib.js → couchdb_views/notes/lib.js
File renamed without changes.
View
3 views/notes/by_title-map.js
@@ -1,3 +0,0 @@
-function(doc) {
- emit(doc.title, doc);
-}

0 comments on commit 389a8d4

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