Skip to content

Commit

Permalink
refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-tang committed Nov 7, 2011
1 parent babdd57 commit 9f5eeae
Show file tree
Hide file tree
Showing 41 changed files with 357 additions and 207 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ gem 'rails', '3.0.7'
# Bundle edge Rails instead:
# gem 'rails', :git => 'git://github.com/rails/rails.git'

gem 'mysql2'
#gem 'mysql2'
gem "mysql2", "~> 0.2.7"
gem 'acts-as-taggable-on', '~>2.1.0'
gem 'paperclip'
gem 'mime-types', :require => 'mime/types'
Expand Down
19 changes: 2 additions & 17 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,11 @@ GEM
activesupport (3.0.7)
acts-as-taggable-on (2.1.1)
rails
archive-tar-minitar (0.5.2)
arel (2.0.10)
builder (2.1.2)
cocaine (0.1.0)
coderay (1.0.3)
colored (1.2)
columnize (0.3.4)
daemons (1.1.4)
delayed_job (2.1.4)
activesupport (~> 3.0)
Expand All @@ -46,8 +44,6 @@ GEM
escape (0.0.4)
friendly_id (4.0.0.beta14)
i18n (0.5.0)
linecache19 (0.5.12)
ruby_core_source (>= 0.1.4)
mail (2.2.17)
activesupport (>= 2.3.6)
i18n (>= 0.4.0)
Expand Down Expand Up @@ -89,20 +85,10 @@ GEM
activesupport (= 3.0.7)
rake (>= 0.8.7)
thor (~> 0.14.4)
rake (0.8.7)
rake (0.9.2.2)
redcarpet (1.17.2)
rsolr (0.12.1)
builder (>= 2.1.2)
ruby-debug-base19 (0.11.25)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
ruby_core_source (>= 0.1.4)
ruby-debug19 (0.11.6)
columnize (>= 0.3.1)
linecache19 (>= 0.5.11)
ruby-debug-base19 (>= 0.11.19)
ruby_core_source (0.1.5)
archive-tar-minitar (>= 0.5.2)
sexp_processor (3.0.7)
sunspot (1.2.1)
escape (= 0.0.4)
Expand All @@ -128,11 +114,10 @@ DEPENDENCIES
friendly_id (~> 4.0.0.beta14)
mail (= 2.2.17)
mime-types
mysql2
mysql2 (~> 0.2.7)
paperclip
rails (= 3.0.7)
rails_best_practices
redcarpet
ruby-debug19
sunspot_rails
will_paginate (>= 3.0.pre)
27 changes: 25 additions & 2 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class ApplicationController < ActionController::Base
before_filter :set_i18n_locale_from_params
before_filter :authorize
before_filter :filter_css_curt
before_filter :filter_right_fragments, :except=>[:gallery_list, :tag_gallery_list]
before_filter :authorize
protect_from_forgery

rescue_from ActiveRecord::RecordNotFound, :with => :render_404
Expand Down Expand Up @@ -39,7 +40,6 @@ def default_url_options
{ :locale => I18n.locale }
end


def authorize
unless EydUser.find_by_id(session[:user_id])
redirect_to login_url, :notice => "Please log in"
Expand Down Expand Up @@ -104,4 +104,27 @@ def fetch_tech_curt
session[:curt_guestbk] =""
session[:curt_gallery] =""
end

def filter_right_fragments
fetch_cloud_tags
fetch_comments
fetch_hottopics
end

def fetch_cloud_tags
unless read_fragment('tag_fragment')
@cloud_tags = EydBlog.tag_counts
end
end

def fetch_comments
unless read_fragment('comment_fragment')
@comments = EydComment.find_by_sql("select comment.* from eyd_comments comment where comment.is_guestbook = false order by comment.updated_at desc limit 5")
end
end

def fetch_hottopics
@hottopics = EydBlog.find_by_sql("select blog.* from eyd_blogs blog where blog.user_id=2 and blog.is_draft=false order by blog.view_count desc limit 5")
end

end
11 changes: 11 additions & 0 deletions app/controllers/eydf_gallery_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class EydfGalleryController < ApplicationController
skip_before_filter :authorize

def gallery_list
@total_avatars = EydAvatar.find_by_sql("select ava.* from eyd_avatars ava where ava.user_id=1 order by ava.updated_at desc limit 280")
end

def tag_gallery_list
@total_avatars = EydAvatar.tagged_with(params[:id])
end
end
28 changes: 28 additions & 0 deletions app/controllers/eydf_guestbk_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class EydfGuestbkController < ApplicationController
skip_before_filter :authorize
before_filter :fetch_categories, :fetch_archivals
before_filter :build_comment, :only=> [:guest_list]

def fetch_categories
session[:userId]=1
unless read_fragment('category_fragment'+session[:userId].to_s)
@constants = EydConstant.find_by_sql("select constant.* from eyd_constants constant where constant.user_id=1 order by constant.updated_at desc")
end
end

def fetch_archivals
unless read_fragment('archival_fragment'+session[:userId].to_s)
@archivals = EydBlog.find_by_sql("select year(created_at) as year, month(created_at) as month, count(id)as count from eyd_blogs where user_id=1 group by year(created_at),month(created_at) order by year(created_at) desc, month(created_at) desc")
end
end

def guest_list
@total_comments = EydComment.paginate_by_sql ["select comment.* from eyd_comments comment where comment.is_guestbook=true order by comment.updated_at desc"], :page => params[:page], :per_page=>10
end

protected
def build_comment
@eyd_comment = EydComment.new
end

end
142 changes: 5 additions & 137 deletions app/controllers/eydf_home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,51 +1,19 @@
class EydfHomeController < ApplicationController
skip_before_filter :authorize
before_filter :filter_right_fragments, :except=>[:gallery_list, :tag_gallery_list]
before_filter :build_comment, :only=> [:show_blog, :guest_list]
before_filter :fetch_categories, :fetch_archivals, :except=>[:gallery_list, :tag_gallery_list]
before_filter :build_comment, :only=> [:show_blog]
#caches_page :gallery_list,:tag_gallery_list

def filter_right_fragments
if session[:curt_tech] =="current" || session[:curt_ibook] =="current"
session[:userId]=2
else
#if(params[:action].to_s=='show_blog')
# session[:userId]=2
#else
session[:userId]=1
# end
end
fetch_categories
fetch_cloud_tags
fetch_comments
fetch_hottopics
fetch_archivals
end

def fetch_categories
session[:userId]=1
unless read_fragment('category_fragment'+session[:userId].to_s)
@constants = EydConstant.find_by_sql("select constant.* from eyd_constants constant where constant.user_id=#{session[:userId]} order by constant.updated_at desc")
end
end

def fetch_cloud_tags
unless read_fragment('tag_fragment')
@cloud_tags = EydBlog.tag_counts
end
end

def fetch_comments
unless read_fragment('comment_fragment')
@comments = EydComment.find_by_sql("select comment.* from eyd_comments comment where comment.is_guestbook = false order by comment.updated_at desc limit 5")
@constants = EydConstant.find_by_sql("select constant.* from eyd_constants constant where constant.user_id=1 order by constant.updated_at desc")
end
end

def fetch_hottopics
@hottopics = EydBlog.find_by_sql("select blog.* from eyd_blogs blog where blog.user_id=2 and blog.is_draft=false order by blog.view_count desc limit 5")
end

def fetch_archivals
unless read_fragment('archival_fragment'+session[:userId].to_s)
@archivals = EydBlog.find_by_sql("select year(created_at) as year, month(created_at) as month, count(id)as count from eyd_blogs where user_id=#{session[:userId]} group by year(created_at),month(created_at) order by year(created_at) desc, month(created_at) desc")
@archivals = EydBlog.find_by_sql("select year(created_at) as year, month(created_at) as month, count(id)as count from eyd_blogs where user_id=1 group by year(created_at),month(created_at) order by year(created_at) desc, month(created_at) desc")
end
end

Expand All @@ -54,104 +22,4 @@ def index
#EydMailer.mail_new_posts.deliver
@total_blogs = EydBlog.paginate_by_sql ["select blog.* from eyd_blogs blog where blog.user_id=1 and blog.is_draft=false order by blog.created_at desc"], :page => params[:page], :per_page=>20
end

def tech_list
@total_blogs = EydBlog.paginate_by_sql ["select blog.* from eyd_blogs blog where blog.user_id=2 and blog.is_draft=false order by blog.created_at desc"], :page => params[:page], :per_page=>20
end

def show_blog
#debugger
@blog = EydBlog.find(params[:id])
@total_comments = EydComment.paginate_by_sql ["select comment.* from eyd_comments comment where comment.blog_id=#{@blog.id} order by comment.updated_at asc"], :page => params[:page], :per_page=>10
@prev_next_blogs = EydComment.find_by_sql("SELECT * FROM eyd_blogs WHERE user_id = #{session[:userId]} and id IN (SELECT CASE WHEN SIGN(id - #{@blog.id}) > 0 THEN MIN(id) WHEN SIGN(id - #{@blog.id}) < 0 THEN MAX(id) END AS id FROM eyd_blogs WHERE id <> #{@blog.id} GROUP BY SIGN(id - #{@blog.id}) ORDER BY SIGN(id - #{@blog.id})) ORDER BY id ASC")
if @prev_next_blogs.size >1
@prev_blog = @prev_next_blogs[1]
@next_blog = @prev_next_blogs[0]
else
if @prev_next_blogs.size ==1
if @prev_next_blogs[0].id < @blog.id
@next_blog = @prev_next_blogs[0]
else
@prev_blog = @prev_next_blogs[0]
end
end
end
sync_blog_view_count(@blog)
end

def sync_blog_view_count(blog)
#update blog view count
blog.view_count+=1
blog.update_attribute("view_count",blog.view_count);
end

def tag_list
@total_blogs = EydBlog.tagged_with(params[:id]).paginate :page => params[:page], :per_page => 50
@tags = EydBlog.tag_counts_on(:tags)
end

def tag_ibook_list
@total_ibooks = EydIbook.tagged_with(params[:id]).paginate :page => params[:page], :per_page => 50
end

def tag_gallery_list
@total_avatars = EydAvatar.tagged_with(params[:id])
end

def archival_list
@start = "'"+params[:id]+"-01'"
@end = "'"+params[:id]+"-31'"
@total_blogs = EydBlog.paginate_by_sql ["select blog.* from eyd_blogs blog where blog.user_id=#{session[:userId]} and blog.is_draft=false and blog.created_at between #{@start} and #{@end} order by blog.created_at desc"], :page => params[:page], :per_page=>50
end

def search_list
@search = EydBlog.search do
fulltext params[:search]
end
@total_blogs = @search.results
end

def category_list
@total_blogs = EydBlog.paginate_by_sql ["select blog.* from eyd_blogs blog where blog.user_id=#{session[:userId]} and blog.is_draft=false and blog.constant_id = #{params[:id]} order by blog.updated_at desc"], :page => params[:page], :per_page=>50
end

def rss_feed
@total_blogs = EydBlog.order("created_at desc")
@updated = @total_blogs.first.created_at unless @total_blogs.empty?
respond_to do |format|
format.atom {render :layout => false}
format.rss { redirect_to rss_feed_path(:format => :atom), :status => :moved_permanently }
end
end

def ibook_list
@total_ibooks = EydIbook.paginate_by_sql ["select ibook.* from eyd_ibooks ibook where ibook.user_id=2 order by ibook.updated_at desc"], :page => params[:page], :per_page=>20
end

def download
@ibook = EydIbook.find(params[:id])
sync_download_count(@ibook)
if @ibook.ibook.url!=nil
url = @ibook.ibook.url.split("?")
send_file "#{RAILS_ROOT}/public"+url[0] unless @ibook.ibook.url.nil?
end
end

def sync_download_count(ibook)
ibook.download_count+=1
ibook.update_attribute("download_count",ibook.download_count)
end

def guest_list
@total_comments = EydComment.paginate_by_sql ["select comment.* from eyd_comments comment where comment.is_guestbook=true order by comment.updated_at desc"], :page => params[:page], :per_page=>10
end

def gallery_list
@total_avatars = EydAvatar.find_by_sql("select ava.* from eyd_avatars ava where ava.user_id=1 order by ava.updated_at desc limit 280")
end

protected
def build_comment
@eyd_comment = EydComment.new
end
end
40 changes: 40 additions & 0 deletions app/controllers/eydf_ibook_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class EydfIbookController < ApplicationController
skip_before_filter :authorize
before_filter :fetch_categories, :fetch_archivals

def fetch_categories
session[:userId]=2
unless read_fragment('category_fragment'+session[:userId].to_s)
@constants = EydConstant.find_by_sql("select constant.* from eyd_constants constant where constant.user_id=2 order by constant.updated_at desc")
end
end

def fetch_archivals
unless read_fragment('archival_fragment_tech'+session[:userId].to_s)
@archivals = EydBlog.find_by_sql("select year(created_at) as year, month(created_at) as month, count(id)as count from eyd_blogs where user_id=2 group by year(created_at),month(created_at) order by year(created_at) desc, month(created_at) desc")
end
end

def ibook_list
@total_ibooks = EydIbook.paginate_by_sql ["select ibook.* from eyd_ibooks ibook where ibook.user_id=2 order by ibook.updated_at desc"], :page => params[:page], :per_page=>20
end

def tag_ibook_list
@total_ibooks = EydIbook.tagged_with(params[:id]).paginate :page => params[:page], :per_page => 50
end

def download
@ibook = EydIbook.find(params[:id])
sync_download_count(@ibook)
if @ibook.ibook.url!=nil
url = @ibook.ibook.url.split("?")
send_file "#{RAILS_ROOT}/public"+url[0] unless @ibook.ibook.url.nil?
end
end

def sync_download_count(ibook)
ibook.download_count+=1
ibook.update_attribute("download_count",ibook.download_count)
end

end
Loading

0 comments on commit 9f5eeae

Please sign in to comment.