Skip to content

Commit

Permalink
Lists channels and activities on the index page
Browse files Browse the repository at this point in the history
Signed-off-by: Akash Manohar J <akash@akash.im>
  • Loading branch information
HashNuke committed Mar 8, 2012
1 parent 4125ff0 commit 3698cc0
Show file tree
Hide file tree
Showing 21 changed files with 167 additions and 6 deletions.
@@ -0,0 +1,6 @@
Kandan.ActivitiesController = Ember.ResourceController.create({
resourceType: Kandan.Activity

_resourceUrl: ()->
"/channels/#{@get('channel').get('id')}/activities"
})
@@ -1,2 +1,9 @@
Kandan.ChannelsController = Ember.ResourceController.create
Kandan.ChannelsController = Ember.ResourceController.create({
resourceType: Kandan.Channel


# load: (json)->
# console.log "activity loads..."
# console.log json
# @_super(json)
})
@@ -0,0 +1,2 @@
Kandan.UsersController = Ember.ResourceController.create
resourceType: Kandan.User
2 changes: 2 additions & 0 deletions app/assets/javascripts/ember/models/activity.js.coffee
@@ -0,0 +1,2 @@
Kandan.Activity = Ember.Resource.extend
resourceName: 'activity'
3 changes: 2 additions & 1 deletion app/assets/javascripts/ember/models/channel.js.coffee
@@ -1,2 +1,3 @@
class Kandan.Channel extends Ember.Resource
Kandan.Channel = Ember.Resource.extend
resourceUrl: '/channels'
resourceName: 'channel'
@@ -0,0 +1,4 @@
List activitiy boxes for channels here
{{#each channels}}
{{view Kandan.ListChannelActivitiesView channelBinding="this"}}
{{/each}}
@@ -0,0 +1,4 @@
CHANNEL: {{channel.name}}
{{#each channel.activities}}
{{view Kandan.ShowActivityView activityBinding="this"}}
{{/each}}
@@ -0,0 +1 @@
USER:{{activity.user.id}} {{activity.id}} - {{activity.content}}
@@ -0,0 +1,4 @@
Kandan.ListActivitiesView = Ember.View.extend({
templateName: 'ember/templates/activities/list_activities'
channelsBinding: 'Kandan.ChannelsController'
})
@@ -0,0 +1,7 @@
Kandan.ListChannelActivitiesView = Ember.View.extend({
templateName: 'ember/templates/activities/list_channel_activities'
channelBinding: 'Kandan.ChannelsController'
activitiesBinding: 'Kandan.ActivitiesController'
# userBinding: 'Kandan.UsersController'

})
5 changes: 5 additions & 0 deletions app/assets/javascripts/ember/views/activities/show.js.coffee
@@ -0,0 +1,5 @@
Kandan.ShowActivityView = Ember.View.extend({
templateName: 'ember/templates/activities/show'
className: ['activity']
tagName: 'div'
})
31 changes: 31 additions & 0 deletions app/controllers/activities_controller.rb
@@ -0,0 +1,31 @@
class ActivitiesController < ApplicationController
before_filter :authenticate_user!

def index
@activities = Channel.find(params[:channel_id]).activities.includes(:user).all
respond_to do |format|
format.json { render :json => @activities, :include => :user }
end
end

def create
@activity = Channel.find(params[:channel_id]).activities.build(params[:activity])
if @activity.save
respond_to do |format|
format.json { render :json => @activity, :status => :created }
end
else
respond_to do |format|
format.json { render :json => @activity.errors, :status => :unprocessable_entity }
end
end
end

def show
@activity = Activity.find params[:id]
respond_to do |format|
format.json { render :json => @activity }
end
end

end
53 changes: 53 additions & 0 deletions app/controllers/channels_controller.rb
@@ -0,0 +1,53 @@
class ChannelsController < ApplicationController
before_filter :authenticate_user!

def index
@channels = Channel.includes(:activities => :user).all
respond_to do |format|
format.json do
render :json => @channels, :include => {:activities => {:include=>:user}}
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
2 changes: 1 addition & 1 deletion app/controllers/main_controller.rb
Expand Up @@ -2,6 +2,6 @@ class MainController < ApplicationController
before_filter :authenticate_user!

def index
@channels = Channel.all
@channels = Channel.includes(:activities => :user).all
end
end
3 changes: 2 additions & 1 deletion app/models/activity_observer.rb
@@ -1,6 +1,7 @@
class ActivityObserver < ActiveRecord::Observer

def after_save(activity)
Kandan::Config.broadcaster.broadcast(activity.attributes)
faye_channel = "/channels/#{activity.channel.to_param}/messages"
Kandan::Config.broadcaster.broadcast(faye_channel, activity.attributes)
end
end
12 changes: 11 additions & 1 deletion app/views/main/index.html.erb
Expand Up @@ -4,9 +4,19 @@
{{ view Kandan.ListChannelsView }}
</script>


<script type="text/x-handlebars">
{{ view Kandan.ListActivitiesView }}
</script>

<%= javascript_tag do %>
$(function() {
Kandan.ChannelsController.loadAll(<%= @channels.to_json.html_safe %>);
var channels_json = <%= @channels.to_json(
:include => {
:activities => {
:include=>:user
}}).html_safe %>
Kandan.ChannelsController.loadAll(channels_json);
});
<%- end %>

Expand Down
4 changes: 4 additions & 0 deletions config/routes.rb
Expand Up @@ -3,6 +3,10 @@
root :to => "main#index"
devise_for :users

resources :channels do
resources :activities
end

# The priority is based upon order of creation:
# first created -> highest priority.

Expand Down
2 changes: 1 addition & 1 deletion lib/broadcasters/faye.rb
Expand Up @@ -3,7 +3,7 @@ class Faye
class << self
def broadcast(channel, message)
# NOTE FAYE_CLIENT is set in the config.ru file due to the faye bug
if defined?(FAYE_CLIENT)
if defined?(FAYE_CLIENT) && (not FAYE_CLIENT.nil?)
FAYE_CLIENT.publish channel, message
else
puts "OOPS! FAYE_CLIENT is not defined"
Expand Down
9 changes: 9 additions & 0 deletions spec/controllers/channels_controller_spec.rb
@@ -0,0 +1,9 @@
require "spec_helper"

describe ChannelsController do
describe "GET index" do
it "should return list of channels in JSON" do
get :index
end
end
end
5 changes: 5 additions & 0 deletions spec/factories.rb
@@ -0,0 +1,5 @@
Factory.define do
factory :channel do |f|
f.name "Test Channel"
end
end
5 changes: 5 additions & 0 deletions spec/models/activity_observer_spec.rb
@@ -0,0 +1,5 @@
require 'spec_helper'

describe ActivityObserver do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit 3698cc0

Please sign in to comment.