class SearchKeywordsController < ApplicationController #FIXME The user interface for adding and editing search queries is almost unusable, fix for the user access_control do allow logged_in end def index @search_keyword = SearchKeyword.new if current_user.has_role?(:admin) watched_user = params[:user_id] ? User.find_by_username(params[:user_id]) : current_user else watched_user = current_user flash[:notice] = t :access_denied_flash \ if params[:user_id] and (current_user.username != params[:user_id]) end @search_keyword.user = watched_user @search_keywords = watched_user.search_keywords end def newsupdate page = (params[:page] || 1).to_i @articles = current_user.collect_recent_articles.paginate(:page => page, :per_page => Article.per_page) end def postnews flash[:notice] = current_user.post_recent_news ? \ t(:news_were_posted_flash) : t(:no_news_were_posted_flash) redirect_to :action => 'newsupdate' end def show @search_keyword = get_keyword_for_current_user(params[:id]) if @search_keyword.nil? then flash[:notice] = t :keyword_does_not_exist_flash redirect_to :controller => 'site', :action => 'index' end end def new @search_keyword = SearchKeyword.new @search_keyword.user = current_user end def create @search_keyword = SearchKeyword.new(params[:search_keyword]) @search_keyword.user = current_user if @search_keyword.save flash[:notice] = t :keyword_creation_success_flash redirect_to @search_keyword else render :action => 'new' end end def edit @search_keyword = get_keyword_for_current_user(params[:id]) if @search_keyword.nil? flash[:notice] = t :keyword_update_denied_flash redirect_to :controller => 'site', :action => 'index' end end def update @search_keyword = get_keyword_for_current_user(params[:id]) if @search_keyword.nil? then flash[:notice] = t :keyword_update_denied_flash redirect_to :controller => 'site', :action => 'index' return end if @search_keyword.update_attributes(params[:search_keyword]) flash[:notice] = t :keyword_update_success_flash redirect_to @search_keyword else render :action => 'edit' end end def destroy @search_keyword = get_keyword_for_current_user(params[:id]) if @search_keyword.nil? then flash[:notice] = t :access_denied_flash redirect_to :controller => 'site', :action => 'index' else @search_keyword.destroy flash[:notice] = t :keyword_destroy_success_flash redirect_to search_keywords_url end end private def get_keyword_for_current_user(keyword_id) keyword_to_show = SearchKeyword.find(keyword_id) keyword_to_show.user === current_user ? SearchKeyword.find(params[:id]) : nil end end