Skip to content

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Eddie Kao committed Jul 19, 2010
0 parents commit a5a336e
Show file tree
Hide file tree
Showing 149 changed files with 20,247 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gems
@@ -0,0 +1 @@
will_paginate
3 changes: 3 additions & 0 deletions README
@@ -0,0 +1,3 @@
This is a simple Registration and Guestbook system, with a easy admin system after login, just built for the 3rd PTT-Flash Workshop in Taiwan. :)

Eddie Kao (eddie@adcube.com.tw)
10 changes: 10 additions & 0 deletions Rakefile
@@ -0,0 +1,10 @@
# Add your own tasks in files placed in lib/tasks ending in .rake,
# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.

require(File.join(File.dirname(__FILE__), 'config', 'boot'))

require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

require 'tasks/rails'
27 changes: 27 additions & 0 deletions app/controllers/application_controller.rb
@@ -0,0 +1,27 @@
class ApplicationController < ActionController::Base
helper :all
protect_from_forgery

filter_parameter_logging :login_pw
helper_method :is_admin?
before_filter :get_current_registers

protected
def is_admin?
not session[:pttflashmanager].blank?
end

def render_404
render :file => "#{RAILS_ROOT}/public/404.html", :status => 404
end

def admin_required
render_404 unless is_admin?
end

private
def get_current_registers
@current_registers = Register.current_registers
end

end
31 changes: 31 additions & 0 deletions app/controllers/guestbook_controller.rb
@@ -0,0 +1,31 @@
class GuestbookController < ApplicationController

before_filter :admin_required, :only => [:destroy]

def index
@guestbooks = Guestbook.active.paginate(:order => 'id DESC', :page => params[:page], :per_page => 10)
end

def new
@guestbook = Guestbook.new
end

def create
params[:guestbook][:ip_address] = request.remote_ip
@guestbook = Guestbook.new params[:guestbook]

if @guestbook.save
flash[:notice] = '留言完成!'
redirect_to guestbooks_path
else
render :action => 'new'
end
end

def destroy
Guestbook.disable_guestbook params[:id]
flash[:notice] = "留言刪除"
redirect_to guestbooks_path
end

end
15 changes: 15 additions & 0 deletions app/controllers/main_controller.rb
@@ -0,0 +1,15 @@
class MainController < ApplicationController

def index
end

def downloads
end

def about
end

def source
end

end
49 changes: 49 additions & 0 deletions app/controllers/register_controller.rb
@@ -0,0 +1,49 @@
class RegisterController < ApplicationController

before_filter :admin_required, :only => [:index, :destroy]

def index
@registers = Register.active
end

def new
if still_available?
@register = Register.new
else
render :template => 'register/finish'
end
end

def create

# check available slot first
if Register.is_avaliable?

params[:register][:ip_address] = request.remote_ip
@register = Register.new(params[:register])

if @register.save
flash[:notice] = '報名成功,感謝您的參與!'
redirect_to root_path
else
render :action => 'new'
end
else
flash[:notice] = '抱歉! 報名已額滿!'
redirect_to new_registers_path
end

end

def destroy
Register.disable_register(params[:id])
flash[:notice] = "報名刪除成功!"
redirect_to registers_path
end

private
def still_available?
@current_registers < MaxRegister
end

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

def new
@administrator = Administrator.new
end

def create
if request.post?

login_id = params[:administrator][:login_id].strip
login_pw = params[:administrator][:login_pw].strip

admin = Administrator.get_valid_account(login_id, login_pw)

if admin
flash[:notice] = "登入成功!"
session[:pttflashmanager] = { :login_id => admin.login_id }
redirect_to registers_path
else
flash[:notice] = "登入失敗"
redirect_to root_path
end

end
end

def destroy
reset_session
flash[:notice] = "登出成功!"
redirect_to root_path
end

end
3 changes: 3 additions & 0 deletions app/helpers/application_helper.rb
@@ -0,0 +1,3 @@
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
end
2 changes: 2 additions & 0 deletions app/helpers/guestbook_helper.rb
@@ -0,0 +1,2 @@
module GuestbookHelper
end
5 changes: 5 additions & 0 deletions app/helpers/main_helper.rb
@@ -0,0 +1,5 @@
module MainHelper
def site_name
"PTT Flash 閃光俱樂部"
end
end
2 changes: 2 additions & 0 deletions app/helpers/register_helper.rb
@@ -0,0 +1,2 @@
module RegisterHelper
end
2 changes: 2 additions & 0 deletions app/helpers/sessions_helper.rb
@@ -0,0 +1,2 @@
module SessionsHelper
end
7 changes: 7 additions & 0 deletions app/models/administrator.rb
@@ -0,0 +1,7 @@
class Administrator < ActiveRecord::Base

def self.get_valid_account(id, pw)
first(:conditions => { :login_id => id, :login_pw => pw, :is_active => true })
end

end
14 changes: 14 additions & 0 deletions app/models/guestbook.rb
@@ -0,0 +1,14 @@
class Guestbook < ActiveRecord::Base
attr_protected :is_active

# validations
validates_presence_of :nickname, :on => :create, :message => "can't be blank"
validates_presence_of :content, :on => :create, :message => "can't be blank"

# named scope
named_scope :active, :conditions => { :is_active => true }

def self.disable_guestbook(id)
find(id).update_attribute('is_active', false)
end
end
26 changes: 26 additions & 0 deletions app/models/register.rb
@@ -0,0 +1,26 @@
class Register < ActiveRecord::Base
attr_protected :is_active

# validations
validates_presence_of :nickname, :on => :create, :message => '為必填欄位'
validates_presence_of :email, :on => :create, :message => '為必填欄位'
validates_presence_of :tel, :on => :create, :message => '為必填欄位'
validates_format_of :email, :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\Z/i, :on => :create, :message => "格式有誤"
validates_uniqueness_of :email, :on => :create, :message => '已經報名過了'

# named scope
named_scope :active, :conditions => { :is_active => true }

def self.is_avaliable?
current_registers < MaxRegister
end

def self.current_registers
count(:conditions => { :is_active => true })
end

def self.disable_register(id)
find(id).update_attribute('is_active', false)
end

end
22 changes: 22 additions & 0 deletions app/views/guestbook/index.html.erb
@@ -0,0 +1,22 @@
<% content_for :body do -%>
<div id="add_guestbook"><%= link_to '我要留言', new_guestbook_path %></div>
<h1>留言版</h1>
<hr />

<% if !@guestbooks.blank? %>
<% for guestbook in @guestbooks %>
<div class="guestbook_content">
<% if is_admin? -%>
[<%= link_to '刪除', guestbook_path(guestbook), :method => :delete, :confirm => '確認刪除?!' %>]
<% end -%>
<%=h guestbook.nickname -%>說:<br />
<%=h simple_format(guestbook.content) %> <br />
</div>
<% end %>
<% else %>
目前尚無資料
<% end %>
<%= will_paginate @guestbooks %>
<% end -%>
7 changes: 7 additions & 0 deletions app/views/guestbook/new.html.erb
@@ -0,0 +1,7 @@
<% content_for :body do -%>
<% form_for @guestbook do |f| -%>
<%= f.label :nickname, "暱稱:" %><%= f.text_field :nickname, :size => '20' %><br />
<%= f.label :content, "標題:" %><%= f.text_area :content, :size => '45x5' %><br />
<br /><%= submit_tag "送出", :confirm => '確認送出?!' %>
<% end -%>
<% end -%>
25 changes: 25 additions & 0 deletions app/views/layouts/application.html.erb
@@ -0,0 +1,25 @@
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="Keywords" content="第三屆PTT-Flash閃光俱樂部" />
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" />
<meta name="description" content="對於平常在版上出沒的神人大大們很好奇嗎? 想知道他們還有什麼特別的大絕招嗎?.."/>
<link rel="image_src" type="image/jpeg" href="/images/facebook.png" />
<title><%= yield :page_title or site_name %></title>
<%= stylesheet_link_tag 'reset', 'style', :cache => true %>
</head>
<body>
<center>
<div id="main">
<%= render :partial => "shared/top", :locals => { :registers => @current_registers } %>
<% if flash[:notice] -%><div class="flash_notice"><%=h flash[:notice] %></div><% end -%>
<div class="container">
<%= yield :body %>
</div>
<%= render :partial => "shared/footer" -%>
</div>
</center>
</body>
</html>
6 changes: 6 additions & 0 deletions app/views/main/about.html.erb
@@ -0,0 +1,6 @@
<% content_for :body do -%>
關於這裡
<hr />
臨時用RoR刻的一個簡單報名系統,基本上沒用到太複雜的技術,應該丟上<a href="http://heroku.com/" target="_blank">Heroku</a>就會動了,對<a href="http://github.com/kaochenlong/PTT-Flash" target="_blank">原始碼</a>有興趣的歡迎拿去用 :)<br />
若有任何問題,亦歡迎來信討論 <%= SiteAdminEmail %>
<% end -%>
3 changes: 3 additions & 0 deletions app/views/main/downloads.html.erb
@@ -0,0 +1,3 @@
<% content_for :body do -%>
敬請期待
<% end -%>

0 comments on commit a5a336e

Please sign in to comment.