Class: CategoriesController

Inherits:
BaseController show all
Defined in:
app/controllers/categories_controller.rb

Instance Method Summary collapse

Methods inherited from BaseController

#advertise, #cache_action?, #css_help, #footer_content, #homepage_features, #plaxo, #rss_site_index, #site_index

Methods included from LocalizedApplication

#get_matching_ui_locale, #get_sorted_langs_from_accept_header, #get_valid_lang_from_accept_header, #set_locale

Methods included from AuthenticatedSystem

#login_by_token, #update_last_seen_at

Instance Method Details

#createObject

POST /categories POST /categories.xml



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/categories_controller.rb', line 65

def create
  @category = Category.new(params[:category])
  
  respond_to do |format|
    if @category.save
      flash[:notice] = :category_was_successfully_created.l
      
      format.html { redirect_to category_url(@category) }
      format.xml do
        headers["Location"] = category_url(@category)
        render :nothing => true, :status => "201 Created"
      end
    else
      format.html { render :action => "new" }
      format.xml  { render :xml => @category.errors.to_xml }
    end
  end
end

#destroyObject

DELETE /categories/1 DELETE /categories/1.xml



102
103
104
105
106
107
108
109
110
# File 'app/controllers/categories_controller.rb', line 102

def destroy
  @category = Category.find(params[:id])
  @category.destroy
  
  respond_to do |format|
    format.html { redirect_to categories_url   }
    format.xml  { render :nothing => true }
  end
end

#editObject

GET /categories/1;edit



59
60
61
# File 'app/controllers/categories_controller.rb', line 59

def edit
  @category = Category.find(params[:id])
end

#indexObject

GET /categories GET /categories.xml



9
10
11
12
13
14
15
16
# File 'app/controllers/categories_controller.rb', line 9

def index
  @categories = Category.find(:all)

  respond_to do |format|
    format.html # index.rhtml
    format.xml  { render :xml => @categories.to_xml }
  end
end

#newObject

GET /categories/new



54
55
56
# File 'app/controllers/categories_controller.rb', line 54

def new
  @category = Category.new
end

#showObject

GET /categories/1 GET /categories/1.xml



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'app/controllers/categories_controller.rb', line 20

def show
  @category = Category.find(params[:id])
  @sidebar_right = true
    
  order = (params[:popular] ? "view_count #{params[:popular].eql?('DESC') ? 'DESC' : 'ASC'}": "published_at DESC")

  @posts = Post.includes(:tags).where('category_id = ?', @category.id).order(order).page(params[:page])
  
  @popular_posts = @category.posts.order("view_count DESC").find(:all, :limit => 10)
  @popular_polls = Poll.find_popular_in_category(@category)

  @rss_title = "#{configatron.community_name}: #{@category.name} "+:posts.l
  @rss_url = category_path(@category, :format => :rss)

  @active_users = User.find(:all,
    :include => :posts,
    :limit => 5,
    :conditions => ["posts.category_id = ? AND posts.published_at > ?", @category.id, 14.days.ago],
    :order => "users.view_count DESC"
    )
  
  respond_to do |format|
    format.html # show.rhtml
    format.rss {
      render_rss_feed_for(@posts, {:feed => {:title => "#{configatron.community_name}: #{@category.name} "+:posts.l, :link => category_url(@category)},
        :item => {:title => :title,
                  :link =>  Proc.new {|post| user_post_url(post.user, post)},
                  :description => :post,
                  :pub_date => :published_at} })
    }
  end
end

#show_tipsObject



112
113
114
115
116
117
# File 'app/controllers/categories_controller.rb', line 112

def show_tips
  @category = Category.find(params[:id] )
  render :partial => "/categories/tips", :locals => {:category => @category}
rescue ActiveRecord::RecordNotFound
  render :partial => "/categories/tips", :locals => {:category => nil}
end

#updateObject

PUT /categories/1 PUT /categories/1.xml



86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/controllers/categories_controller.rb', line 86

def update
  @category = Category.find(params[:id])
  
  respond_to do |format|
    if @category.update_attributes(params[:category])
      format.html { redirect_to category_url(@category) }
      format.xml  { render :nothing => true }
    else
      format.html { render :action => "edit" }
      format.xml  { render :xml => @category.errors.to_xml }        
    end
  end
end