Skip to content

Commit

Permalink
First import
Browse files Browse the repository at this point in the history
  • Loading branch information
topfunky committed Sep 10, 2008
0 parents commit 3a96ffc
Show file tree
Hide file tree
Showing 66 changed files with 8,823 additions and 0 deletions.
10 changes: 10 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require(File.join(File.dirname(__FILE__), 'config', 'boot'))

require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

require 'tasks/rails'
15 changes: 15 additions & 0 deletions app/controllers/application.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
helper :all # include all helpers, all the time

# 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
# Uncomment this to filter the contents of submitted sensitive data parameters
# from your application log (in this case, all fields with names like "password").
# filter_parameter_logging :password
end
2 changes: 2 additions & 0 deletions app/controllers/fields_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class FieldsController < ApplicationController
end
60 changes: 60 additions & 0 deletions app/controllers/notes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
class NotesController < ApplicationController

def index
@notes = Note.init_from_rows(db.view("notes/by_title"))
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]))
respond_to do |wants|
wants.html
wants.json { render :json => @note.attributes.to_json }
end
end

def new
@note = Note.new
end

def create
result = db.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]))
end

def update
@note = Note.new(db.get(params[:id]))
@note.updated_at = DateTime.now
if db.save(@note.update(params[:note]))
respond_to do |wants|
wants.html { redirect_to note_url(@note) }
end
else
respond_to do |wants|
wants.html { render :action => "edit" }
end
end
end

def destroy
end

private

def db
@@couchrest ||= CouchRest.new(COUCHDB_SERVER)
# TODO Run creation tasks, load views, etc.
@@couchrest.create_db("travel_topfunky") rescue nil
@db = @@couchrest.database("travel_topfunky")
end

end
3 changes: 3 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
end
2 changes: 2 additions & 0 deletions app/helpers/fields_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module FieldsHelper
end
9 changes: 9 additions & 0 deletions app/helpers/notes_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module NotesHelper

def couchdb_rev_field(form, record)
unless record.new_record?
form.hidden_field("_rev")
end
end

end
91 changes: 91 additions & 0 deletions app/models/basic_model.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
##
# A simple class to help use CouchDB and CouchRest with Rails.
#
# You should subclass this so routes are properly generated when making forms.
#
# class Note < BasicModel; end
#
# couch_rest = CouchRest.new('http://localhost:5984')
# db = couch_rest.database('my_db')
# note = Note.new(db.get('22323232'))
# result = db.save(note.attributes)
#

class BasicModel

attr_accessor :attributes

##
# Takes a record from CouchRest ID call and turns it into something
# usable in Rails.
#
# note = Note.new(db.get('283934927362'))
# note.id
# note._rev
# note.new_record?
# note.title # Any field from the record

def initialize(attributes={})
@attributes = attributes
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)
results.rows.each_with_index do |row, index|
results.rows[index] = self.new(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.

def update(attributes)
@attributes.merge(attributes)
end

def id
_id rescue nil
end
alias_method :to_param, :id

def new_record?
(_id && _rev).nil?
rescue NameError
true
end

# def self.json_create(o)
# new(*o['data'])
# end
#
# def to_json(*a)
# {
# 'json_class' => self.class.name,
# 'data' => @attributes
# }.to_json(*a)
# end

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
end
end

end
8 changes: 8 additions & 0 deletions app/models/note.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Note < BasicModel

def initialize(attributes={})
defaults = {"title" => nil, "description" => nil}
super(defaults.merge(attributes))
end

end
22 changes: 22 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html;charset=UTF-8" />
<title>Notes: <%= controller.action_name %></title>
<%= stylesheet_link_tag 'scaffold', 'application' %>
<%= javascript_include_tag :defaults, "lowpro", "date_selector" %>
</head>
<body>

<p style="color: green"><%= flash[:notice] %></p>

<div class="container">
<%= yield %>

<div id="home"><%= link_to "Home", root_url %></div>
</div>

</body>
</html>
19 changes: 19 additions & 0 deletions app/views/notes/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<% form_for @note do |f| %>
<%= couchdb_rev_field f, @note %>
<%= f.label "Title" %>
<%= f.text_field :title %>
<%= f.label "Description" %>
<%= f.text_area :description %>
<%= f.label "Tags (separated by spaces)" %>
<%= f.text_field "tags" %>
<%= f.label "Visited On" %>
<%= f.text_field "visited_on", :id => "date" %>
<%= f.submit "Save" %>
<% end %>
3 changes: 3 additions & 0 deletions app/views/notes/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1 id="title"><%= @note.title %></h1>

<%= render :partial => "form" %>
7 changes: 7 additions & 0 deletions app/views/notes/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1>Your Notes</h1>

<% @notes.rows.each do |note| %>
<h2><%= link_to note.title, note_path(note) %></h2>
<p><%= note.description %></p>
<p><%= link_to "Edit", edit_note_path(note) %></p>
<% end %>
3 changes: 3 additions & 0 deletions app/views/notes/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h1 id="title">Note</h1>

<%= render :partial => "form" %>
7 changes: 7 additions & 0 deletions app/views/notes/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<h1 id="title"><%= @note.title %></h1>

<small>Updated <%= @note.updated_at.to_s(:rfc822) rescue "?" %></small>

<p><%= @note.description %></p>

<p><%= link_to "Edit", edit_note_path(@note) %></p>
109 changes: 109 additions & 0 deletions config/boot.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
# Don't change this file!
# Configure your app in config/environment.rb and config/environments/*.rb

RAILS_ROOT = "#{File.dirname(__FILE__)}/.." unless defined?(RAILS_ROOT)

module Rails
class << self
def boot!
unless booted?
preinitialize
pick_boot.run
end
end

def booted?
defined? Rails::Initializer
end

def pick_boot
(vendor_rails? ? VendorBoot : GemBoot).new
end

def vendor_rails?
File.exist?("#{RAILS_ROOT}/vendor/rails")
end

def preinitialize
load(preinitializer_path) if File.exist?(preinitializer_path)
end

def preinitializer_path
"#{RAILS_ROOT}/config/preinitializer.rb"
end
end

class Boot
def run
load_initializer
Rails::Initializer.run(:set_load_path)
end
end

class VendorBoot < Boot
def load_initializer
require "#{RAILS_ROOT}/vendor/rails/railties/lib/initializer"
Rails::Initializer.run(:install_gem_spec_stubs)
end
end

class GemBoot < Boot
def load_initializer
self.class.load_rubygems
load_rails_gem
require 'initializer'
end

def load_rails_gem
if version = self.class.gem_version
gem 'rails', version
else
gem 'rails'
end
rescue Gem::LoadError => load_error
$stderr.puts %(Missing the Rails #{version} gem. Please `gem install -v=#{version} rails`, update your RAILS_GEM_VERSION setting in config/environment.rb for the Rails version you do have installed, or comment out RAILS_GEM_VERSION to use the latest version installed.)
exit 1
end

class << self
def rubygems_version
Gem::RubyGemsVersion if defined? Gem::RubyGemsVersion
end

def gem_version
if defined? RAILS_GEM_VERSION
RAILS_GEM_VERSION
elsif ENV.include?('RAILS_GEM_VERSION')
ENV['RAILS_GEM_VERSION']
else
parse_gem_version(read_environment_rb)
end
end

def load_rubygems
require 'rubygems'

unless rubygems_version >= '0.9.4'
$stderr.puts %(Rails requires RubyGems >= 0.9.4 (you have #{rubygems_version}). Please `gem update --system` and try again.)
exit 1
end

rescue LoadError
$stderr.puts %(Rails requires RubyGems >= 0.9.4. Please install RubyGems and try again: http://rubygems.rubyforge.org)
exit 1
end

def parse_gem_version(text)
$1 if text =~ /^[^#]*RAILS_GEM_VERSION\s*=\s*["']([!~<>=]*\s*[\d.]+)["']/
end

private
def read_environment_rb
File.read("#{RAILS_ROOT}/config/environment.rb")
end
end
end
end

# All that for this:
Rails.boot!
Loading

0 comments on commit 3a96ffc

Please sign in to comment.