adding the ability to remember who created and updated a page model

git-svn-id: http://svn.barleysodas.com/barleysodas/trunk@64 0f7b21a7-9e3a-4941-bbeb-ce5c7c368fa7
master
andrew 2007-12-27 05:19:13 +00:00
parent 169fcc0c5e
commit 18b92965b8
6 changed files with 54 additions and 2 deletions

View File

@ -2,7 +2,9 @@ class ApplicationController < ActionController::Base
session :session_key => '_barleysodas_session_id'
append_before_filter :block_prefetching_links
append_before_filter :authorized?
append_before_filter :set_current_people_id
helper_method :logged_in?
cattr_accessor :current_people_id
##
# Ensures that the request was made using an Ajax request.
@ -142,4 +144,11 @@ class ApplicationController < ActionController::Base
end
false
end
##
# Sets a class accessor for the current People.
#
def set_current_people_id
self.current_people_id = session[:people_id]
end
end

View File

@ -8,6 +8,7 @@ class SessionsController < ApplicationController
@people = People.find_by_title(params[:login]) rescue nil
if @people
session[:people_title] = @people.title
session[:people_id] = @people.id
respond_to do |format|
format.html {
flash[:info] = "Welcome, #{@people.title}"

View File

@ -18,13 +18,19 @@ class Page < ActiveRecord::Base
has_many :discussions, :order => 'discussions.created_at ASC',
:dependent => :destroy
belongs_to :created_by, :class_name => 'People', :foreign_key => 'created_by'
belongs_to :updated_by, :class_name => 'People', :foreign_key => 'updated_by'
validates_presence_of :title
validates_uniqueness_of :title, :scope => 'owner_type'
validates_format_of :title, :with => /^([A-Za-z0-9 ])+$/,
:message => 'may only contain letters, numbers and spaces'
before_save :update_html
attr_protected :allow_discussions
before_create :set_created_person
before_save :set_updated_person
attr_protected :allow_discussions, :created_by, :updated_by
##
# Returns an url-friendly title for making links.
@ -63,6 +69,20 @@ class Page < ActiveRecord::Base
protected
##
# Sets the People marker for created_by on creation.
#
def set_created_person
self[:created_by] = ApplicationController.current_people_id
end
##
# Sets the People marker for updated_by on save.
#
def set_updated_person
self[:updated_by] = ApplicationController.current_people_id
end
##
# Updates the HTML chunk from the RedCloth source.
#

View File

@ -5,6 +5,8 @@ class People < ActiveRecord::Base
has_one_tuxwiki_page :owner_class => 'People'
belongs_to :role
attr_protected :role_id
has_many :created_pages, :class_name => 'Page', :foreign_key => 'created_by'
has_many :updated_pages, :class_name => 'Page', :foreign_key => 'updated_by'
##
# Finds the Guest user for the system.

View File

@ -4,7 +4,7 @@
<%= @page.title %>
</h2>
<% unless simple -%><div class="vcard">
Posted by <span class="fn">Author Name Here</span>
Posted by <span class="fn"><%= @page.created_by.title rescue 'That other guy' -%></span>
</div><% end -%>
<br class="clear" />
<div class="entry-content">
@ -16,6 +16,7 @@
<% @page.owner.page_attributes.each do |x| -%>
<li><%= x -%></li>
<% end if @page.owner and @page.owner.respond_to?("page_attributes") %>
<% if @page.created_by != @page.updated_by -%><li>Last updated by: <%= @page.updated_by.title rescue 'That other other guy' -%></li><% end %>
</ul><% end -%>
</div>

View File

@ -0,0 +1,19 @@
class PagePeopleOwnership < ActiveRecord::Migration
def self.up
add_column :pages, :created_by, :integer
add_index :pages, :created_by
add_column :pages, :updated_by, :integer
add_index :pages, :updated_by
add_column :page_versions, :created_by, :integer
add_index :page_versions, :created_by
add_column :page_versions, :updated_by, :integer
add_index :page_versions, :updated_by
end
def self.down
remove_column :pages, :created_by
remove_column :pages, :updated_by
remove_column :page_versions, :created_by
remove_column :page_versions, :updated_by
end
end