/
channels_controller.rb
69 lines (61 loc) · 1.93 KB
/
channels_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
class ChannelsController < ApplicationController
before_filter :authenticate_user!
def index
# NOTE Eager loading doesn't respect limit
@channels = Channel.find(:all)
nested_channel_data = []
# TODO this can be shortened
@channels.each do |channel|
activities = []
more_activities = (channel.activities.count > Kandan::Config.options[:per_page])
channel.activities.order('id DESC').includes(:user).page.each do |activity|
activities.push activity.attributes.merge({
:user => activity.user.as_json(:only => [:id, :ido_id, :first_name, :last_name, :gravatar_hash, :active, :locale])
})
end
nested_channel_data.push channel.attributes.merge({:activities => activities.reverse, :more_activities => more_activities})
end
respond_to do |format|
format.json do
render :text => nested_channel_data.to_json
end
end
end
def create
@channel = Channel.new(params[:channel])
if @channel.save
respond_to do |format|
format.json { render :json => @channel, :status => :created }
end
else
respond_to do |format|
format.json { render :json => @channel.errors, :status => :unprocessable_entity }
end
end
end
def show
@channel = Channel.find params[:id]
respond_to do |format|
format.json { render :json => @channel }
end
end
def update
@channel = Channel.find(params[:id])
if @channel.update_attributes(params[:channel])
respond_to do |format|
format.json { render :json => @channel, :status => :ok }
end
else
respond_to do |format|
format.json { render :json => @channel.errors, :status => :unprocessable_entity }
end
end
end
def destroy
@channel = Channel.find params[:id]
@channel.destroy if not @channel.id == 1
respond_to do |format|
format.json { render :json => nil, :status => :ok}
end
end
end