Skip to content

Commit

Permalink
allow plugins to add header tabs easily
Browse files Browse the repository at this point in the history
  • Loading branch information
bborn committed Nov 19, 2009
1 parent 7f7d14d commit 1f35776
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 68 deletions.
144 changes: 76 additions & 68 deletions app/controllers/base_controller.rb
Expand Up @@ -8,6 +8,7 @@ class BaseController < ApplicationController
around_filter :set_locale
skip_before_filter :verify_authenticity_token, :only => :footer_content
helper_method :commentable_url
before_filter :initialize_header_tabs

caches_action :site_index, :footer_content, :if => Proc.new{|c| c.cache_action? }
def cache_action?
Expand Down Expand Up @@ -66,95 +67,102 @@ def advertise
def css_help
end

def admin_required
current_user && current_user.admin? ? true : access_denied
end

def admin_or_moderator_required
current_user && (current_user.admin? || current_user.moderator?) ? true : access_denied
end
private
def admin_required
current_user && current_user.admin? ? true : access_denied
end

def admin_or_moderator_required
current_user && (current_user.admin? || current_user.moderator?) ? true : access_denied
end

def find_user
if @user = User.active.find(params[:user_id] || params[:id])
@is_current_user = (@user && @user.eql?(current_user))
unless logged_in? || @user.profile_public?
flash[:error] = :this_users_profile_is_not_public_youll_need_to_create_an_account_and_log_in_to_access_it.l
access_denied
def find_user
if @user = User.active.find(params[:user_id] || params[:id])
@is_current_user = (@user && @user.eql?(current_user))
unless logged_in? || @user.profile_public?
flash[:error] = :this_users_profile_is_not_public_youll_need_to_create_an_account_and_log_in_to_access_it.l
access_denied
else
return @user
end
else
return @user
flash[:error] = :please_log_in.l
access_denied
end
else
flash[:error] = :please_log_in.l
access_denied
end
end

def require_current_user
@user ||= User.find(params[:user_id] || params[:id] )
unless admin? || (@user && (@user.eql?(current_user)))
redirect_to :controller => 'sessions', :action => 'new' and return false
def require_current_user
@user ||= User.find(params[:user_id] || params[:id] )
unless admin? || (@user && (@user.eql?(current_user)))
redirect_to :controller => 'sessions', :action => 'new' and return false
end
return @user
end
return @user
end

def popular_tags(limit = nil, order = ' tags.name ASC', type = nil)
sql = "SELECT tags.id, tags.name, count(*) AS count
FROM taggings, tags
WHERE tags.id = taggings.tag_id "
sql += " AND taggings.taggable_type = '#{type}'" unless type.nil?
sql += " GROUP BY tags.id, tags.name"
sql += " ORDER BY #{order}"
sql += " LIMIT #{limit}" if limit
Tag.find_by_sql(sql).sort{ |a,b| a.name.downcase <=> b.name.downcase}
end
def popular_tags(limit = nil, order = ' tags.name ASC', type = nil)
sql = "SELECT tags.id, tags.name, count(*) AS count
FROM taggings, tags
WHERE tags.id = taggings.tag_id "
sql += " AND taggings.taggable_type = '#{type}'" unless type.nil?
sql += " GROUP BY tags.id, tags.name"
sql += " ORDER BY #{order}"
sql += " LIMIT #{limit}" if limit
Tag.find_by_sql(sql).sort{ |a,b| a.name.downcase <=> b.name.downcase}
end


def get_recent_footer_content
@recent_clippings = Clipping.find_recent(:limit => 10)
@recent_photos = Photo.find_recent(:limit => 10)
@recent_comments = Comment.find_recent(:limit => 13)
@popular_tags = popular_tags(30, ' count DESC')
@recent_activity = User.recent_activity(:size => 15, :current => 1)
def get_recent_footer_content
@recent_clippings = Clipping.find_recent(:limit => 10)
@recent_photos = Photo.find_recent(:limit => 10)
@recent_comments = Comment.find_recent(:limit => 13)
@popular_tags = popular_tags(30, ' count DESC')
@recent_activity = User.recent_activity(:size => 15, :current => 1)

end
end

def get_additional_homepage_data
@sidebar_right = true
@homepage_features = HomepageFeature.find_features
@homepage_features_data = @homepage_features.collect {|f| [f.id, f.public_filename(:large) ] }
def get_additional_homepage_data
@sidebar_right = true
@homepage_features = HomepageFeature.find_features
@homepage_features_data = @homepage_features.collect {|f| [f.id, f.public_filename(:large) ] }

@active_users = User.active.find_by_activity({:limit => 5, :require_avatar => false})
@featured_writers = User.find_featured
@active_users = User.active.find_by_activity({:limit => 5, :require_avatar => false})
@featured_writers = User.find_featured

@featured_posts = Post.find_featured
@featured_posts = Post.find_featured

@topics = Topic.find(:all, :limit => 5, :order => "replied_at DESC")
@topics = Topic.find(:all, :limit => 5, :order => "replied_at DESC")

@active_contest = Contest.get_active
@popular_posts = Post.find_popular({:limit => 10})
@popular_polls = Poll.find_popular(:limit => 8)
end
@active_contest = Contest.get_active
@popular_posts = Post.find_popular({:limit => 10})
@popular_polls = Poll.find_popular(:limit => 8)
end


def commentable_url(comment)
if comment.recipient && comment.commentable
if comment.commentable_type != "User"
polymorphic_url([comment.recipient, comment.commentable])+"#comment_#{comment.id}"
elsif comment
user_url(comment.recipient)+"#comment_#{comment.id}"
def commentable_url(comment)
if comment.recipient && comment.commentable
if comment.commentable_type != "User"
polymorphic_url([comment.recipient, comment.commentable])+"#comment_#{comment.id}"
elsif comment
user_url(comment.recipient)+"#comment_#{comment.id}"
end
elsif comment.commentable
polymorphic_url(comment.commentable)+"#comment_#{comment.id}"
end
elsif comment.commentable
polymorphic_url(comment.commentable)+"#comment_#{comment.id}"
end
end

def commentable_comments_url(commentable)
if commentable.owner && commentable.owner != commentable
"#{polymorphic_path([commentable.owner, commentable])}#comments"
else
"#{polymorphic_path(commentable)}#comments"
end
end
def commentable_comments_url(commentable)
if commentable.owner && commentable.owner != commentable
"#{polymorphic_path([commentable.owner, commentable])}#comments"
else
"#{polymorphic_path(commentable)}#comments"
end
end

def initialize_header_tabs
# This hook allows plugins or host apps to easily add tabs to the header by adding to the @header_tabs array
# Usage: @header_tabs << {:name => "My tab", :url => my_tab_path, :section => 'my_tab_section' }
@header_tabs = []
end

end
1 change: 1 addition & 0 deletions app/controllers/sessions_controller.rb
Expand Up @@ -18,6 +18,7 @@ def create
@user_session = UserSession.new(:login => params[:login], :password => params[:password], :remember_me => params[:remember_me])
if @user_session.save
flash[:notice] = :thanks_youre_now_logged_in.l
raise current_user.inspect
redirect_back_or_default(dashboard_user_path(current_user))
else
flash[:notice] = :uh_oh_we_couldnt_log_you_in_with_the_username_and_password_you_entered_try_again.l
Expand Down
3 changes: 3 additions & 0 deletions app/views/shared/_header.html.haml
Expand Up @@ -19,6 +19,9 @@
= topnav_tab :people.l, { :url => users_path, :section => 'users' }
- if current_user
= topnav_tab :my_profile.l, { :url => user_path(current_user), :section => 'my_profile'}
- if @header_tabs.any?
- @header_tabs.each do |tab|
= topnav_tab tab[:name], {:url => tab[:url], :section => tab[:section] }

/ SiteSearch Google
%form{:method=>"get", :action=>"http://www.google.com/custom", :target=>"_top"}
Expand Down

0 comments on commit 1f35776

Please sign in to comment.