From 18b92965b8b1c0be82229385b2f816912e549ae3 Mon Sep 17 00:00:00 2001 From: andrew Date: Thu, 27 Dec 2007 05:19:13 +0000 Subject: [PATCH] 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 --- app/controllers/application.rb | 9 +++++++++ app/controllers/sessions_controller.rb | 1 + app/models/page.rb | 22 +++++++++++++++++++++- app/models/people.rb | 2 ++ app/views/pages/_page.rhtml | 3 ++- db/migrate/010_page_people_ownership.rb | 19 +++++++++++++++++++ 6 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 db/migrate/010_page_people_ownership.rb diff --git a/app/controllers/application.rb b/app/controllers/application.rb index 35f6983..ec2ed17 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -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 diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index f79c539..aea9802 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -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}" diff --git a/app/models/page.rb b/app/models/page.rb index 30963e4..fbbdfae 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -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. # diff --git a/app/models/people.rb b/app/models/people.rb index a5597af..a377d96 100644 --- a/app/models/people.rb +++ b/app/models/people.rb @@ -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. diff --git a/app/views/pages/_page.rhtml b/app/views/pages/_page.rhtml index c2dca96..b038733 100644 --- a/app/views/pages/_page.rhtml +++ b/app/views/pages/_page.rhtml @@ -4,7 +4,7 @@ <%= @page.title %> <% unless simple -%>
- Posted by Author Name Here + Posted by <%= @page.created_by.title rescue 'That other guy' -%>
<% end -%>
@@ -16,6 +16,7 @@ <% @page.owner.page_attributes.each do |x| -%>
  • <%= x -%>
  • <% end if @page.owner and @page.owner.respond_to?("page_attributes") %> + <% if @page.created_by != @page.updated_by -%>
  • Last updated by: <%= @page.updated_by.title rescue 'That other other guy' -%>
  • <% end %> <% end -%>
    diff --git a/db/migrate/010_page_people_ownership.rb b/db/migrate/010_page_people_ownership.rb new file mode 100644 index 0000000..0a26d04 --- /dev/null +++ b/db/migrate/010_page_people_ownership.rb @@ -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