Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
all the code following along with my post
  • Loading branch information
rdonovan committed Oct 22, 2010
1 parent 68f1eda commit 53368ef
Show file tree
Hide file tree
Showing 69 changed files with 4,389 additions and 310 deletions.
8 changes: 8 additions & 0 deletions app/controllers/about_controller.rb
@@ -0,0 +1,8 @@
class AboutController < ApplicationController
def index
end

def team
end

end
12 changes: 12 additions & 0 deletions app/controllers/application_controller.rb
Expand Up @@ -2,9 +2,21 @@
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
include Authentication
helper :all # include all helpers, all the time
protect_from_forgery # See ActionController::RequestForgeryProtection for details

# Scrub sensitive parameters from your log
# filter_parameter_logging :password
before_filter :load_content_pieces




private

def load_content_pieces
@content_piece_main = ContentPiece.find(:first, :order => 'created_at', :conditions => ["controller = ? AND view = ? AND area = 'main'", params[:controller], params[:action]])
end

end
46 changes: 46 additions & 0 deletions app/controllers/content_pieces_controller.rb
@@ -0,0 +1,46 @@
class ContentPiecesController < ApplicationController
before_filter :login_required

def index
@content_pieces = ContentPiece.all
end

def show
@content_piece = ContentPiece.find(params[:id])
end

def new
@content_piece = ContentPiece.new
end

def create
@content_piece = ContentPiece.new(params[:content_piece])
if @content_piece.save
flash[:notice] = "Successfully created content piece."
redirect_to @content_piece
else
render :action => 'new'
end
end

def edit
@content_piece = ContentPiece.find(params[:id])
end

def update
@content_piece = ContentPiece.find(params[:id])
if @content_piece.update_attributes(params[:content_piece])
flash[:notice] = "Successfully updated content piece."
redirect_to @content_piece
else
render :action => 'edit'
end
end

def destroy
@content_piece = ContentPiece.find(params[:id])
@content_piece.destroy
flash[:notice] = "Successfully destroyed content piece."
redirect_to content_pieces_url
end
end
5 changes: 5 additions & 0 deletions app/controllers/home_controller.rb
@@ -0,0 +1,5 @@
class HomeController < ApplicationController
def index
end

end
22 changes: 22 additions & 0 deletions app/controllers/sessions_controller.rb
@@ -0,0 +1,22 @@
class SessionsController < ApplicationController
def new
end

def create
user = User.authenticate(params[:login], params[:password])
if user
session[:user_id] = user.id
flash[:notice] = "Logged in successfully."
redirect_to_target_or_default(root_url)
else
flash.now[:error] = "Invalid login or password."
render :action => 'new'
end
end

def destroy
session[:user_id] = nil
flash[:notice] = "You have been logged out."
redirect_to root_url
end
end
16 changes: 16 additions & 0 deletions app/controllers/users_controller.rb
@@ -0,0 +1,16 @@
class UsersController < ApplicationController
def new
@user = User.new
end

def create
@user = User.new(params[:user])
if @user.save
session[:user_id] = @user.id
flash[:notice] = "Thank you for signing up! You are now logged in."
redirect_to root_url
else
render :action => 'new'
end
end
end
2 changes: 2 additions & 0 deletions app/helpers/about_helper.rb
@@ -0,0 +1,2 @@
module AboutHelper
end
2 changes: 2 additions & 0 deletions app/helpers/content_pieces_helper.rb
@@ -0,0 +1,2 @@
module ContentPiecesHelper
end
2 changes: 2 additions & 0 deletions app/helpers/home_helper.rb
@@ -0,0 +1,2 @@
module HomeHelper
end
22 changes: 22 additions & 0 deletions app/helpers/layout_helper.rb
@@ -0,0 +1,22 @@
# These helper methods can be called in your template to set variables to be used in the layout
# This module should be included in all views globally,
# to do so you may need to add this line to your ApplicationController
# helper :layout
module LayoutHelper
def title(page_title, show_title = true)
@content_for_title = page_title.to_s
@show_title = show_title
end

def show_title?
@show_title
end

def stylesheet(*args)
content_for(:head) { stylesheet_link_tag(*args) }
end

def javascript(*args)
content_for(:head) { javascript_include_tag(*args) }
end
end
2 changes: 2 additions & 0 deletions app/helpers/sessions_helper.rb
@@ -0,0 +1,2 @@
module SessionsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/users_helper.rb
@@ -0,0 +1,2 @@
module UsersHelper
end
3 changes: 3 additions & 0 deletions app/models/content_piece.rb
@@ -0,0 +1,3 @@
class ContentPiece < ActiveRecord::Base
attr_accessible :controller, :view, :area, :content
end
38 changes: 38 additions & 0 deletions app/models/user.rb
@@ -0,0 +1,38 @@
class User < ActiveRecord::Base
# new columns need to be added here to be writable through mass assignment
attr_accessible :username, :email, :password, :password_confirmation

attr_accessor :password
before_save :prepare_password

validates_presence_of :username
validates_uniqueness_of :username, :email, :allow_blank => true
validates_format_of :username, :with => /^[-\w\._@]+$/i, :allow_blank => true, :message => "should only contain letters, numbers, or .-_@"
validates_format_of :email, :with => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
validates_presence_of :password, :on => :create
validates_confirmation_of :password
validates_length_of :password, :minimum => 4, :allow_blank => true

# login can be either username or email address
def self.authenticate(login, pass)
user = find_by_username(login) || find_by_email(login)
return user if user && user.matching_password?(pass)
end

def matching_password?(pass)
self.password_hash == encrypt_password(pass)
end

private

def prepare_password
unless password.blank?
self.password_salt = Digest::SHA1.hexdigest([Time.now, rand].join)
self.password_hash = encrypt_password(password)
end
end

def encrypt_password(pass)
Digest::SHA1.hexdigest([pass, password_salt].join)
end
end
4 changes: 4 additions & 0 deletions app/views/about/index.html.haml
@@ -0,0 +1,4 @@
%h1 about us

:markdown
#{@content_piece_main.content}
1 change: 1 addition & 0 deletions app/views/about/team.html.haml
@@ -0,0 +1 @@
%h1 about our team
20 changes: 20 additions & 0 deletions app/views/content_pieces/_form.html.haml
@@ -0,0 +1,20 @@
- form_for @content_piece do |f|
= f.error_messages
%p
= f.label :controller
%br
= f.text_field :controller
%p
= f.label :view
%br
= f.text_field :view
%p
= f.label :area
%br
= f.text_field :area
%p
= f.label :content
%br
= f.text_area :content
%p
= f.submit
8 changes: 8 additions & 0 deletions app/views/content_pieces/edit.html.haml
@@ -0,0 +1,8 @@
- title "Edit Content Piece"

= render :partial => 'form'

%p
= link_to "Show", content_piece_path(@content_piece)
|
= link_to "View All", content_pieces_path
17 changes: 17 additions & 0 deletions app/views/content_pieces/index.html.haml
@@ -0,0 +1,17 @@
- title "Content Pieces"

%table
%tr
%th Controller
%th View
%th Area
- for content_piece in @content_pieces
%tr
%td= h content_piece.controller
%td= h content_piece.view
%td= h content_piece.area
%td= link_to 'Show', content_piece
%td= link_to 'Edit', edit_content_piece_path(content_piece)
%td= link_to 'Destroy', content_piece, :confirm => 'Are you sure?', :method => :delete

%p= link_to "New Content Piece", new_content_piece_path
5 changes: 5 additions & 0 deletions app/views/content_pieces/new.html.haml
@@ -0,0 +1,5 @@
- title "New Content Piece"

= render :partial => 'form'

%p= link_to "Back to List", content_pieces_path
21 changes: 21 additions & 0 deletions app/views/content_pieces/show.html.haml
@@ -0,0 +1,21 @@
- title "Content Piece"

%p
%strong Controller:
=h @content_piece.controller
%p
%strong View:
=h @content_piece.view
%p
%strong Area:
=h @content_piece.area
%p
%strong Content:
=h @content_piece.content

%p
= link_to "Edit", edit_content_piece_path(@content_piece)
|
= link_to "Destroy", @content_piece, :confirm => 'Are you sure?', :method => :delete
|
= link_to "View All", content_pieces_path
4 changes: 4 additions & 0 deletions app/views/home/index.html.haml
@@ -0,0 +1,4 @@
= content_for :title do
homepage

%p this is the homepage text. his is the homepage text. his is the homepage text. his is the homepage text. his is the homepage text. his is the homepage text. his is the homepage text. his is the homepage text. his is the homepage text. his is the homepage text. his is the homepage text. his is the homepage text. his is the homepage text. his is the homepage text.
35 changes: 35 additions & 0 deletions app/views/layouts/application.html.haml
@@ -0,0 +1,35 @@
!!! Strict
%html{html_attrs}

%head
%title= h(yield(:title) || "Untitled")
%meta{"http-equiv"=>"Content-Type", :content=>"text/html; charset=utf-8"}
= stylesheet_link_tag 'application'
= yield(:head)
= javascript_include_tag 'application'
%body
#container
#nav
= link_to 'home', root_url
|
= link_to 'about us', '/about'
|
= link_to 'about our team', '/about/team'
|
- if logged_in?
= link_to 'manage content', content_pieces_path
|
= link_to 'logout', '/logout'
- else
= link_to 'login', login_path
- flash.each do |name, msg|
= content_tag :div, msg, :id => "flash_#{name}"

- if show_title?
%h1=h yield(:title)

= yield

= render :partial => '/shared/scripts'
18 changes: 18 additions & 0 deletions app/views/sessions/new.html.haml
@@ -0,0 +1,18 @@


%p
Don't have an account?
= link_to "Sign up!", signup_path

- form_tag sessions_path do
%p
=label_tag :login, "Username or Email Address"
%br
= text_field_tag :login, params[:login]
%p
= label_tag :password
%br
= password_field_tag :password
%p
= submit_tag "Log in"

1 change: 1 addition & 0 deletions app/views/shared/_scripts.html.erb
@@ -0,0 +1 @@
<script type="text/javascript" src="/javascripts/wmd/wmd.js"></script>
28 changes: 28 additions & 0 deletions app/views/users/new.html.haml
@@ -0,0 +1,28 @@
= content_for :title do
Sign up

%p
Already have an account?
= link_to "Log in", login_path

- form_for @user do |f|
= f.error_messages
%p
= f.label :username
%br
= f.text_field :username
%p
= f.label :email, "Email Address"
%br
= f.text_field :email
%p
= f.label :password
%br
= f.password_field :password
%p
= f.label :password_confirmation, "Confirm Password"
%br
= f.password_field :password_confirmation
%p
= f.submit "Sign up"

3 changes: 3 additions & 0 deletions config/environment.rb
Expand Up @@ -19,6 +19,9 @@
# config.gem "hpricot", :version => '0.6', :source => "http://code.whytheluckystiff.net"
# config.gem "sqlite3-ruby", :lib => "sqlite3"
# config.gem "aws-s3", :lib => "aws/s3"
config.gem 'haml', :lib => 'haml', :version => '>=3.0.18'
config.gem "authlogic"
config.gem 'rdiscount'

# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named
Expand Down

0 comments on commit 53368ef

Please sign in to comment.