Skip to content
This repository has been archived by the owner on Mar 6, 2018. It is now read-only.

Commit

Permalink
1.Add search function.2.improve flash notice using, there has success…
Browse files Browse the repository at this point in the history
…_notice/error_notice to save flash notice.3.add something useful method for ApplicationHelper.4.version to 0.2.0.
  • Loading branch information
Jason Lee committed May 14, 2010
1 parent f5ed1a0 commit 081f668
Show file tree
Hide file tree
Showing 56 changed files with 3,640 additions and 244 deletions.
31 changes: 28 additions & 3 deletions app/controllers/application_controller.rb
Expand Up @@ -10,9 +10,18 @@ class ApplicationController < ActionController::Base
before_filter :init_filter

def init_filter
check_login
check_login

if not /user\/register|user\/login|user\/logout/.match(request.env["HTTP_REFERER"])
session[:referer] = request.env["HTTP_REFERER"]
end
end

def redirect_back(default)
redirect_to(session[:referer] || default)
session[:referer] = nil
end

# 输出404错误
def render_404
render_optional_error_file(404)
Expand Down Expand Up @@ -45,8 +54,24 @@ def require_admin
return true
end

def save_notice(notice)
flash[:notice] = notice
# save flash notice
def save_notice(notice,error = false,mark = :default)
flash[mark] = {}
if error
flash[mark][:notice_error] = notice
else
flash[mark][:notice_success] = notice
end
end

# save error message in flash
def error_notice(notice,mark = :default)
save_notice(notice,true,mark)
end

# save success message in flash
def success_notice(notice,mark = :default)
save_notice(notice,false,mark)
end

def save_login(user)
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/home_controller.rb
Expand Up @@ -10,7 +10,7 @@ def register
passwd_unencoded = @user.passwd
@user.passwd = User.encode(@user.passwd)
if @user.save
save_notice("Register successed.")
success_notice("Register successed.")
# send regist successed mail to user Email
Mailer.regist_successed(@user,passwd_unencoded)
save_login(@user)
Expand Down Expand Up @@ -59,7 +59,7 @@ def settings
p = params[:user]
p[:admin] = @user.admin
if @user.update_attributes(p)
save_notice('User was successfully updated.')
success_notice('User was successfully updated.')
redirect_to :controller => "home", :action => "settings"
else
render :action => "settings"
Expand All @@ -70,7 +70,7 @@ def settings
def password
if request.method == :put
if @current_user.update_passwd(params[:old_passwd], params[:new_passwd], params[:confirm_passwd])
save_notice("Password changed.")
success_notice("Password changed.")
redirect_to :action => "password"
end
end
Expand Down
44 changes: 35 additions & 9 deletions app/controllers/snippets_controller.rb
Expand Up @@ -2,8 +2,19 @@ class SnippetsController < ApplicationController
# GET /snippets
# GET /snippets.xml
before_filter :require_login, :only => [:edit,:update,:destroy]
before_filter :init_sidebar, :only => [:index, :search]
validates_captcha

private
def init_sidebar
@top_languages = Language.find_top
if not @user
@top_users = User.find_top_by_snippets_count
@recent_comments = Comment.snippet_recent(5)
end
end

public
def index
@sub_title = ""
@snippets_count = 0
Expand Down Expand Up @@ -38,12 +49,6 @@ def index
@feed_title = "Recent snippets"
set_seo_meta(nil)
end

@top_languages = Language.find_top
if not @user
@top_users = User.find_top_by_snippets_count
@recent_comments = Comment.snippet_recent(10)
end

if params[:type] == "feed"
# Set the content type to the standard one for RSS
Expand Down Expand Up @@ -94,7 +99,8 @@ def show
else

if @comment.save
redirect_to :controller => :snippets, :action => :show,:id => params[:id],:anchor => "comments"
success_notice('Comment successfully created.', :comments)
redirect_to snippet_path(params[:id],:anchor => "comments")
else
render :action => "show",:archor => "comments"
end
Expand Down Expand Up @@ -147,7 +153,7 @@ def create
else
respond_to do |format|
if @snippet.save
flash[:notice] = 'Snippet was successfully created.'
success_notice('Snippet was successfully created.')
format.html { redirect_to(@snippet) }
format.xml { render :xml => @snippet, :status => :created, :location => @snippet }
else
Expand All @@ -167,7 +173,7 @@ def update

respond_to do |format|
if @snippet.update_attributes(params[:snippet])
flash[:notice] = 'Snippet was successfully updated.'
success_notice('Snippet was successfully updated.')
format.html { redirect_to(@snippet) }
format.xml { head :ok }
else
Expand All @@ -182,10 +188,30 @@ def update
def destroy
@snippet = Snippet.find(params[:id])
@snippet.destroy
success_notice("Snippet was deleted.")

respond_to do |format|
format.html { redirect_to(snippets_url) }
format.xml { head :ok }
end
end

def search
if !@current_user
error_notice("Sorry, currently only allows registered users to use search function, <br />
you can register and sign in before using this feature.")
redirect_to register_path
return
end

if params[:s].blank?
redirect_to snippets_path
return
end

@snippets = Snippet.search_with_page(params[:s].split("\s"),:page => params[:page])
@snippets_count = @snippets.count
@sub_title = "Search snippets by \"#{params[:s]}\""
render :action => "index"
end
end
38 changes: 30 additions & 8 deletions app/helpers/application_helper.rb
Expand Up @@ -3,16 +3,29 @@
module ApplicationHelper

# return the formatted flash[:notice] html
def success_messages
if flash[:notice]
'
<div id="successMessage" class="successMessage">
'+flash[:notice]+'
</div>
'
def notice_message(mark = :default, scroll = false)
if !flash[mark]
return ''
end

notice = flash[mark]
flash[mark] = nil
if notice[:notice_success]
result = '<div id="success_message_'+mark.to_s+'" class="successMessage">'+notice[:notice_success]+'</div>'
if scroll == true
result += '<script type="text/javascript">
$.scrollTo("#success_message_'+mark.to_s+'",200);</script>'
end
elsif notice[:notice_error]
result = '<div id="error_explanation_'+mark.to_s+'" class="errorExplanation">'+notice[:notice_error]+'</div>'
if scroll == true
result += '<script type="text/javascript">$.scrollTo("#error_explanation_'+mark.to_s+'",200);</script>'
end
else
''
result = ''
end

return result
end

# form auth token
Expand Down Expand Up @@ -41,4 +54,13 @@ def has_permission(snippet)
def format_comment(comment)
return auto_link(simple_format(html_escape(comment)))
end

# remove all space or break in HTML
def spaceless(&block)
data = capture(&block)
data = data.gsub(/>\s+</,"><")
concat(data)
end


end
7 changes: 6 additions & 1 deletion app/models/snippet.rb
Expand Up @@ -59,5 +59,10 @@ def self.find_page(page = 1)
:include => [:user,:language]
end


# == search snippets
# keys => split by space, etc. "Ruby on Rails" => ["ruby","on","rails"]
def self.search_with_page(keys = [], options = {})
page = options[:page] || 1
Snippet.title_like_all_or_desc_like_all(keys).find_page(page)
end
end
3 changes: 2 additions & 1 deletion app/views/home/login.html.erb
@@ -1,4 +1,5 @@
<% content_for :main do %>
<%= notice_message %>
<h2>Login</h2>
<div id="signin">
<% form_for @user,:url => "login" do |f| -%>
Expand All @@ -8,7 +9,7 @@
</div>
<% end %>
<p>
<%= f.label :email, "Email:" %><br />
<%= f.label :email, "Email or Username:" %><br />
<%= f.text_field :email,:class => 'text' %>
</p>
<p>
Expand Down
2 changes: 1 addition & 1 deletion app/views/home/password.html.erb
@@ -1,7 +1,7 @@
<% content_for :main do %>
<div class="form" id="modify_password">
<% form_for(@current_user,:url => "") do |f| %>
<%= success_messages %>
<%= notice_message %>
<%= f.error_messages %>
<p>
<label for="old_passwd">Old password:</label><br />
Expand Down
1 change: 1 addition & 0 deletions app/views/home/register.html.erb
@@ -1,4 +1,5 @@
<% content_for :main do %>
<%= notice_message %>
<h2>Register<h2>
<div id="register">
<% form_for @user,:url => "register" do |f| -%>
Expand Down
2 changes: 1 addition & 1 deletion app/views/home/settings.html.erb
Expand Up @@ -3,7 +3,7 @@
<div id="settings">
<% form_for(@user,:url => "") do |f| %>
<%= f.error_messages %>
<%= success_messages %>
<%= notice_message %>
<p>
<%= f.label :login, "User name:" %><br />
<%= @user.login %>
Expand Down
2 changes: 1 addition & 1 deletion app/views/languages/index.html.erb
@@ -1,7 +1,7 @@
<% content_for :main do %>
<h2>Listing languages</h2>
<div id="languages">
<%= success_messages %>
<%= notice_message %>
<table class="grid">
<tr class="head">
<td>Name</td>
Expand Down
6 changes: 3 additions & 3 deletions app/views/layouts/application.html.erb
Expand Up @@ -6,11 +6,11 @@
<title><%= @page_title %></title>
<link rel="shortcut icon" href="/favicon.ico?v=001" />
<%= stylesheet_link_tag "src/base","src/pasite", :cache => "pasite" %>
<%= yield :styles %>
<%= yield :styles %>
<%= yield :head %>
</head>
<body>
<%= yield :scripts %>
<%= javascript_include_tag("src/jquery","src/jquery.scrollto",:cache => "jquery") %>
<div id="header">
<ul id="nav">
<li<%= ' class="hot active"' if params[:controller] == "snippets" and params[:action] == "new" %>>
Expand Down Expand Up @@ -63,7 +63,7 @@
<script type="text/javascript">
var uservoiceJsHost = ("https:" == document.location.protocol) ? "https://uservoice.com" : "http://cdn.uservoice.com";
</script>
<%= javascript_include_tag "uservoice/tab" %>
<%= javascript_include_tag "src/uservoice/tab", :cache => "uservoice/tab" %>
<script type="text/javascript">
UserVoice.Tab.show({
/* required */
Expand Down
8 changes: 8 additions & 0 deletions app/views/snippets/index.html.erb
Expand Up @@ -3,6 +3,7 @@
<% end %>
<% content_for :main do %>
<h2><%= @sub_title %> <%= link_to (image_tag("feed.gif",:alt => "Subscription code",:class => "feed")), :action => "index", :type => "feed" %></h2>
<%= notice_message %>
<div id="snippets">
<div class="pages">
<%= will_paginate @snippets %>
Expand Down Expand Up @@ -50,6 +51,13 @@
</ul>
</div>
<% else %>
<div class="box" id="search_box">
<form action="<%= search_snippets_path %>" method="get">
<label for="search_key">Search:</label><br />
<input type="text" name="s" value="<%= h params[:s] %>" id="search_key">
<input type="submit" value="Go" />
</form>
</div>
<div class="box" id="right_top_languages">
<h3>Top Languages</h3>
<ul>
Expand Down
3 changes: 2 additions & 1 deletion app/views/snippets/show.html.erb
Expand Up @@ -4,7 +4,7 @@
<% content_for :scripts do %>
<% end %>
<% content_for :main do %>
<%= success_messages %>
<%= notice_message %>
<h2>#<%= @snippet.id %> <%=h @snippet.title %> <%= image_tag("icons/private.gif",:alt => "Private") if @snippet.private %></h2>
<div id="snippet_show">
<div class="snippet_code">
Expand Down Expand Up @@ -45,6 +45,7 @@
</div>
<% end %>
</div>
<%= notice_message(:comments) %>
<div id="comment_new">
<div class="form">
<% form_for @comment,:url => "/code/#{params[:id]}/comment" do |f|%>
Expand Down
2 changes: 1 addition & 1 deletion app/views/users/index.html.erb
@@ -1,7 +1,7 @@
<% content_for :main do %>
<h2>Listing users</h2>
<div id="users">
<%= success_messages %>
<%= notice_message %>
<table class="grid">
<tr class="head">
<td>Name</td>
Expand Down
4 changes: 2 additions & 2 deletions config/environment.rb
@@ -1,7 +1,7 @@
# Be sure to restart your server when you modify this file

# Specifies gem version of Rails to use when vendor/rails is not present
RAILS_GEM_VERSION = '2.3.4' unless defined? RAILS_GEM_VERSION
RAILS_GEM_VERSION = '2.3.5' unless defined? RAILS_GEM_VERSION

# Bootstrap the Rails environment, frameworks, and default configuration
require File.join(File.dirname(__FILE__), 'boot')
Expand Down Expand Up @@ -64,7 +64,7 @@
APP_DOMAIN = "http://#{APP_HOST}"

# custom configs
APP_VERSION = '0.1.7'
APP_VERSION = '0.2.0'

APP_THEMES_NAMES = ['Active4D','Cobalt','Dawn','Mac Classic','LAZY','IDLE','BlackBoard','Twilight',
'Sunburst','SpaceCadet']
Expand Down
2 changes: 1 addition & 1 deletion config/environments/development.rb
Expand Up @@ -13,7 +13,7 @@
config.action_view.debug_rjs = true
config.action_controller.perform_caching = false

config.cache_store = :file_store,"c:/cygwin/tmp/cache/pastie"
config.cache_store = :file_store,"/tmp/cache/pastie"

# Don't care if the mailer can't send
config.action_mailer.raise_delivery_errors = false

0 comments on commit 081f668

Please sign in to comment.