Skip to content

Commit

Permalink
Basic functionality for discussions almost complete.
Browse files Browse the repository at this point in the history
  • Loading branch information
astjohn committed Aug 15, 2011
1 parent cda9780 commit ed24131
Show file tree
Hide file tree
Showing 69 changed files with 1,640 additions and 330 deletions.
4 changes: 2 additions & 2 deletions Gemfile
Expand Up @@ -5,8 +5,8 @@ gemspec
#gem 'rails', '3.1.0.rc4'

# Bundle edge Rails instead:
gem 'rails', :git => 'git://github.com/rails/rails.git',
:branch => '3-1-stable'
#gem 'rails', :git => 'git://github.com/rails/rails.git',
# :branch => '3-1-stable'



Expand Down
105 changes: 52 additions & 53 deletions Gemfile.lock
@@ -1,62 +1,42 @@
GIT
remote: git://github.com/rails/rails.git
revision: 66610db03722d7b5aa6d8f1c8e8c569d5d2f1068
branch: 3-1-stable
PATH
remote: .
specs:
cornerstone (0.0.1)
rails (>= 3.1.0.rc5)

GEM
remote: http://rubygems.org/
specs:
actionmailer (3.1.0.rc4)
actionpack (= 3.1.0.rc4)
actionmailer (3.1.0.rc5)
actionpack (= 3.1.0.rc5)
mail (~> 2.3.0)
actionpack (3.1.0.rc4)
activemodel (= 3.1.0.rc4)
activesupport (= 3.1.0.rc4)
actionpack (3.1.0.rc5)
activemodel (= 3.1.0.rc5)
activesupport (= 3.1.0.rc5)
builder (~> 3.0.0)
erubis (~> 2.7.0)
i18n (~> 0.6)
rack (~> 1.3.1)
rack-cache (~> 1.0.2)
rack-mount (~> 0.8.1)
rack-test (~> 0.6.0)
sprockets (~> 2.0.0.beta.10)
activemodel (3.1.0.rc4)
activesupport (= 3.1.0.rc4)
sprockets (~> 2.0.0.beta.12)
activemodel (3.1.0.rc5)
activesupport (= 3.1.0.rc5)
bcrypt-ruby (~> 2.1.4)
builder (~> 3.0.0)
i18n (~> 0.6)
activerecord (3.1.0.rc4)
activemodel (= 3.1.0.rc4)
activesupport (= 3.1.0.rc4)
arel (~> 2.1.3)
activerecord (3.1.0.rc5)
activemodel (= 3.1.0.rc5)
activesupport (= 3.1.0.rc5)
arel (~> 2.1.4)
tzinfo (~> 0.3.29)
activeresource (3.1.0.rc4)
activemodel (= 3.1.0.rc4)
activesupport (= 3.1.0.rc4)
activesupport (3.1.0.rc4)
activeresource (3.1.0.rc5)
activemodel (= 3.1.0.rc5)
activesupport (= 3.1.0.rc5)
activesupport (3.1.0.rc5)
multi_json (~> 1.0)
rails (3.1.0.rc4)
actionmailer (= 3.1.0.rc4)
actionpack (= 3.1.0.rc4)
activerecord (= 3.1.0.rc4)
activeresource (= 3.1.0.rc4)
activesupport (= 3.1.0.rc4)
bundler (~> 1.0)
railties (= 3.1.0.rc4)
railties (3.1.0.rc4)
actionpack (= 3.1.0.rc4)
activesupport (= 3.1.0.rc4)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (~> 0.14.6)

PATH
remote: .
specs:
cornerstone (0.0.1)

GEM
remote: http://rubygems.org/
specs:
arel (2.1.3)
arel (2.1.4)
bcrypt-ruby (2.1.4)
builder (3.0.0)
capybara (1.0.0)
Expand Down Expand Up @@ -104,18 +84,36 @@ GEM
multi_json (1.0.3)
nokogiri (1.5.0)
orm_adapter (0.0.5)
polyglot (0.3.1)
pickle (0.4.8)
cucumber (>= 0.8)
rake
polyglot (0.3.2)
rack (1.3.1)
rack-cache (1.0.2)
rack (>= 0.4)
rack-mount (0.8.1)
rack-mount (0.8.2)
rack (>= 1.0.0)
rack-ssl (1.3.2)
rack
rack-test (0.6.0)
rack (>= 1.0)
rails (3.1.0.rc5)
actionmailer (= 3.1.0.rc5)
actionpack (= 3.1.0.rc5)
activerecord (= 3.1.0.rc5)
activeresource (= 3.1.0.rc5)
activesupport (= 3.1.0.rc5)
bundler (~> 1.0)
railties (= 3.1.0.rc5)
railties (3.1.0.rc5)
actionpack (= 3.1.0.rc5)
activesupport (= 3.1.0.rc5)
rack-ssl (~> 1.3.2)
rake (>= 0.8.7)
rdoc (~> 3.4)
thor (~> 0.14.6)
rake (0.9.2)
rdoc (3.8)
rdoc (3.9.2)
rspec (2.6.0)
rspec-core (~> 2.6.0)
rspec-expectations (~> 2.6.0)
Expand All @@ -135,15 +133,16 @@ GEM
ffi (>= 1.0.7)
json_pure
rubyzip
sprockets (2.0.0.beta.10)
hike (~> 1.0)
sprockets (2.0.0.beta.13)
hike (~> 1.2)
rack (~> 1.0)
tilt (!= 1.3.0, ~> 1.1)
tilt (~> 1.1, != 1.3.0)
sqlite3 (1.3.3)
term-ansicolor (1.0.6)
thor (0.14.6)
tilt (1.3.2)
treetop (1.4.9)
treetop (1.4.10)
polyglot
polyglot (>= 0.3.1)
tzinfo (0.3.29)
warden (1.0.4)
Expand All @@ -162,6 +161,6 @@ DEPENDENCIES
database_cleaner
devise
factory_girl_rails
rails!
pickle
rspec-rails
sqlite3
5 changes: 4 additions & 1 deletion README.rdoc
@@ -1,3 +1,6 @@
= Cornerstone

This project rocks and uses MIT-LICENSE.
This project is not suitable for use just yet. Initial migrations will be changing.

This project rocks and uses MIT-LICENSE.

36 changes: 34 additions & 2 deletions app/controllers/cornerstone/discussions_controller.rb
Expand Up @@ -2,17 +2,49 @@ module Cornerstone
class DiscussionsController < ApplicationController
respond_to :html

# GET /cornerstone/discussions/
def index
@discussions = Discussion.all
@user = cornerstone_user
@categories = Category.discussions
end

# GET /cornerstone/discussions/new
def new
@categories = Category.discussions
@category = Category.find(params[:cat]) if params[:cat]
@discussion = Discussion.new
@discussion.category_id = @category.id if @category
@discussion.posts.build
respond_with(@discussion)
end

# GET /cornerstone/discussions/:category/:id
def category
@category = Category.includes(:discussions => :posts).find(params[:category])
@discussions = @category.discussions
respond_with(@discussions, :template => "cornerstone/discussions/categorical_index")
end

# POST /cornerstone/discussions/
def create
@discussion = Discussion.new(params[:discussion])

# assign user if he's signed in
if current_cornerstone_user
@discussion.user = current_cornerstone_user
@discussion.posts.first.user = current_cornerstone_user
end

respond_with(@discussion.category, @discussion) do |format|
if @discussion.save
flash[:notice] = 'Discussion was successfully created.'
format.html {redirect_to discussion_path(@discussion.category, @discussion)}
else
@categories = Category.discussions
format.html {render :new}
end
end
end

end
end

12 changes: 12 additions & 0 deletions app/helpers/cornerstone/discussions_helper.rb
@@ -1,4 +1,16 @@
module Cornerstone
module DiscussionsHelper

# Returns details and links about the latest discussion for a given category
def latest_discussion_details(category)
if discussion = category.latest_discussion
render :partial => 'latest_discussion', :object => discussion,
:locals => {:category => category}
else
"N/A"
end
end

end
end

16 changes: 16 additions & 0 deletions app/models/cornerstone/category.rb
Expand Up @@ -8,6 +8,8 @@ class Category < ActiveRecord::Base
has_many :discussions

# == ACCESSIBILITY == #
attr_accessible :name, :category_type, :description

# == SCOPES == #

scope :discussions, lambda { where("cornerstone_categories.category_type = 'Discussion'") }
Expand All @@ -19,10 +21,24 @@ class Category < ActiveRecord::Base

validates :category_type, :inclusion => { :in => Cornerstone::Category::TYPES }

validates :description, :presence => true,
:length => {:maximum => 500}

# == CALLBACKS == #
# == CLASS METHODS == #

# == INSTANCE METHODS == #

# Provides the latest discussion created for this category
def latest_discussion
latest_discussions(1).first
end

# Provides the last 'num' discussions created for this category
def latest_discussions(num=nil)
Discussion.latest_for_category(self, num)
end

end
end

50 changes: 39 additions & 11 deletions app/models/cornerstone/discussion.rb
Expand Up @@ -2,31 +2,59 @@ module Cornerstone
class Discussion < ActiveRecord::Base

# == CONSTANTS == #
STATUS = Cornerstone::Config.discussion_statuses

# == ASSOCIATIONS == #
belongs_to :category, :counter_cache => :item_count
# belongs_to also established by acts_as_cornerstone_user
# belongs_to 'user' also established by acts_as_cornerstone_user

has_many :posts, :dependent => :destroy
accepts_nested_attributes_for :posts

# == ACCESSIBILITY == #
# == SCOPES == #
attr_accessible :subject, :privte, :category_id, :posts_attributes

# == VALIDATIONS == #
validates :name, :presence => true,
:length => {:maximum => 50},
:if => Proc.new { |d| d.user.nil? }
# == SCOPES == #
default_scope :order => "created_at DESC",
:conditions => {:privte => false}

validates :email, :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create },
:if => Proc.new { |d| d.user.nil? }
scope :latest_for_category, lambda { |cat, num=5|
where(:category_id => cat.id)
.limit(num) }

# == VALIDATIONS == #
validates :subject, :presence => true,
:length => {:maximum => 50}
:length => { :maximum => 50 }

validates :body, :presence => true,
:length => {:maximum => 3000}
validates :status, :inclusion => { :in => Cornerstone::Discussion::STATUS }

validates :category, :presence => true

# == CALLBACKS == #
after_save :set_latest_discussion, :on => :create

# == CLASS METHODS == #

# == INSTANCE METHODS == #
def author
self.posts.first.author_name
end

#######
private
#######

# Update the category's latest discussion author/date
def set_latest_discussion
post = self.posts.first
if post
cat = self.category
cat.latest_discussion_author = post.author_name
cat.latest_discussion_date = Time.now.to_s(:db)
cat.save
end
end


end

Expand Down

0 comments on commit ed24131

Please sign in to comment.