Skip to content

Commit

Permalink
added note mode
Browse files Browse the repository at this point in the history
added inline editing
  • Loading branch information
olivM committed Apr 8, 2009
1 parent c2b5ca0 commit 5004cf9
Show file tree
Hide file tree
Showing 23 changed files with 950 additions and 25 deletions.
8 changes: 6 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,12 @@ def user_info

def login_required
if session[:user_id]
@user ||= User.find(session[:user_id])
@access_token ||= OAuth::AccessToken.new(get_consumer, @user.oauth_token, @user.oauth_secret)
begin
@user ||= User.find(session[:user_id])
@access_token ||= OAuth::AccessToken.new(get_consumer, @user.oauth_token, @user.oauth_secret)
rescue
redirect_to :controller => 'session', :action => 'new'
end
else
redirect_to :controller => 'session', :action => 'new'
end
Expand Down
68 changes: 68 additions & 0 deletions app/controllers/note_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
class NoteController < ApplicationController

before_filter :login_required
before_filter :user_info
before_filter :notebooks_info

before_filter :notebook_info
before_filter :note_info, :except => [:index, :new, :create]

def notebook_info
@notebook = Notebook.find(params[:notebook_id])
end

def note_info
@note = Note.find(params[:id])
end

def show
respond_to do |format|
format.html { render :text => @note.display }# show.html.erb
format.xml { render :xml => @notebook }
end
end

def new
@note = Note.new

respond_to do |format|
format.html # new.html.erb
format.xml { render :xml => @note }
end
end

def create
@note = Note.new(params[:note])
@note.notebook = @notebook

respond_to do |format|
if @note.save
flash[:notice] = 'Note was successfully created.'
format.html { redirect_to :action => "show", :id => @note.id }
format.xml { render :xml => @note, :status => :created, :location => @note }
else
format.html { render :action => "new" }
format.xml { render :xml => @note.errors, :status => :unprocessable_entity }
end
end

end

def edit
end

def update
respond_to do |format|
if @note.update_attributes(params[:note])
flash[:notice] = 'Panel was successfully updated.'
format.html { redirect_to :action => "show", :id => @note.id}
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @note.errors, :status => :unprocessable_entity }
end
end
end


end
2 changes: 2 additions & 0 deletions app/helpers/note_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module NoteHelper
end
20 changes: 20 additions & 0 deletions app/models/note.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
class Note < ActiveRecord::Base

belongs_to :notebook

validates_presence_of :notebook_id

def display
"<div class='note' id='note_#{self.id}'><h3>#{self.name}</h3> #{self.content}</div>"
end

def before_create
if self.notebook.notes.length > 0
self.rank = self.notebook.notes.maximum(:rank) + 1
else
self.rank = 0
end
end

end

6 changes: 6 additions & 0 deletions app/models/notebook.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ class Notebook < ActiveRecord::Base
has_many :readers, :through => :annoters, :source => :user, :conditions => "status = 'READER' "
has_many :writers, :through => :annoters, :source => :user, :conditions => "status = 'WRITER' "

has_many :notes

validates_presence_of :name, :creator_id

def after_create
# create the notebook_user entry
self.writers << self.creator
end

def display
self.notes.find(:all, :order => "rank DESC ").collect{ |n| n.display }.join
end

end
4 changes: 4 additions & 0 deletions app/views/layouts/main.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
= javascript_include_tag 'http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js'
= javascript_include_tag 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js'
= javascript_include_tag 'jquery.editinplace.packed'
= javascript_include_tag 'jquery.form'
= javascript_include_tag 'main'
= stylesheet_link_tag 'main'

Expand All @@ -14,6 +15,9 @@
#container

#header
#logo
%h1 Simple Notebook

#account
%a{:href => "/account"}=@user.name

Expand Down
13 changes: 13 additions & 0 deletions app/views/note/edit.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-form_for @note, :url => { :action => "update" }, :html => { :id => "note_form" } do |f|

= f.error_messages

%p
=f.text_field :name

%p
=f.text_area :content, :rows => 4

%p
= submit_tag 'save'

13 changes: 13 additions & 0 deletions app/views/note/new.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-form_for @note, :url => { :action => "create" }, :html => { :id => "note_form" } do |f|

= f.error_messages

%p
=f.text_field :name

%p
=f.text_area :content, :rows => 4

%p
= submit_tag 'save'

3 changes: 0 additions & 3 deletions app/views/notebook/edit.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@
%p
=f.text_field :name

%p
=f.text_area :content

%p
= submit_tag 'save'

3 changes: 0 additions & 3 deletions app/views/notebook/new.haml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@
%p
=f.text_field :name

%p
=f.text_area :content

%p
= submit_tag 'save'

13 changes: 10 additions & 3 deletions app/views/notebook/show.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
%h2=@notebook.name
#notebook_header
%h2=@notebook.name

%a{:href => "/notebook/edit/#{@notebook.id}"} edit
%a{:href => "/notebook/edit/#{@notebook.id}"} edit

#notebook_content
=@notebook.content

%button#new_note new note

#messages

.content
=@notebook.display
6 changes: 5 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
ActionController::Routing::Routes.draw do |map|
# The priority is based upon order of creation: first created -> highest priority.

map.resources :notebooks, :controller => 'notebook'
map.resources :notebooks, :controller => 'notebook' do |notebook|

notebook.resources :notes, :controller => :note

end

# Sample of regular route:
# map.connect 'products/:id', :controller => 'catalog', :action => 'view'
Expand Down
1 change: 0 additions & 1 deletion db/migrate/20090406100933_create_notebooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ def self.up
create_table :notebooks do |t|
t.string :name
t.integer :creator_id
t.text :content

t.timestamps
end
Expand Down
16 changes: 16 additions & 0 deletions db/migrate/20090408074311_create_notes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class CreateNotes < ActiveRecord::Migration
def self.up
create_table :notes do |t|
t.integer :notebook_id
t.string :name
t.text :content
t.integer :rank, :default => 0

t.timestamps
end
end

def self.down
drop_table :notes
end
end
Binary file added public/images/page_white_edit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 5004cf9

Please sign in to comment.