From 3698cc03cf999e0e443fa7e5dd60ada9132ed93d Mon Sep 17 00:00:00 2001 From: Akash Manohar J Date: Thu, 8 Mar 2012 22:15:26 +0530 Subject: [PATCH] Lists channels and activities on the index page Signed-off-by: Akash Manohar J --- .../activities_controller.js.coffee | 6 +++ .../controllers/channels_controller.js.coffee | 9 +++- .../controllers/users_controller.js.coffee | 2 + .../ember/models/activity.js.coffee | 2 + .../ember/models/channel.js.coffee | 3 +- .../activities/list_activities.handlebars | 4 ++ .../list_channel_activities.handlebars | 4 ++ .../templates/activities/show.handlebars | 1 + .../activities/list_activities.js.coffee | 4 ++ .../list_channel_activities.js.coffee | 7 +++ .../ember/views/activities/show.js.coffee | 5 ++ app/controllers/activities_controller.rb | 31 +++++++++++ app/controllers/channels_controller.rb | 53 +++++++++++++++++++ app/controllers/main_controller.rb | 2 +- app/models/activity_observer.rb | 3 +- app/views/main/index.html.erb | 12 ++++- config/routes.rb | 4 ++ lib/broadcasters/faye.rb | 2 +- spec/controllers/channels_controller_spec.rb | 9 ++++ spec/factories.rb | 5 ++ spec/models/activity_observer_spec.rb | 5 ++ 21 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 app/assets/javascripts/ember/controllers/activities_controller.js.coffee create mode 100644 app/assets/javascripts/ember/controllers/users_controller.js.coffee create mode 100644 app/assets/javascripts/ember/models/activity.js.coffee create mode 100644 app/assets/javascripts/ember/templates/activities/list_activities.handlebars create mode 100644 app/assets/javascripts/ember/templates/activities/list_channel_activities.handlebars create mode 100644 app/assets/javascripts/ember/templates/activities/show.handlebars create mode 100644 app/assets/javascripts/ember/views/activities/list_activities.js.coffee create mode 100644 app/assets/javascripts/ember/views/activities/list_channel_activities.js.coffee create mode 100644 app/assets/javascripts/ember/views/activities/show.js.coffee create mode 100644 app/controllers/activities_controller.rb create mode 100644 app/controllers/channels_controller.rb create mode 100644 spec/controllers/channels_controller_spec.rb create mode 100644 spec/factories.rb create mode 100644 spec/models/activity_observer_spec.rb diff --git a/app/assets/javascripts/ember/controllers/activities_controller.js.coffee b/app/assets/javascripts/ember/controllers/activities_controller.js.coffee new file mode 100644 index 00000000..5cde3ca8 --- /dev/null +++ b/app/assets/javascripts/ember/controllers/activities_controller.js.coffee @@ -0,0 +1,6 @@ +Kandan.ActivitiesController = Ember.ResourceController.create({ + resourceType: Kandan.Activity + + _resourceUrl: ()-> + "/channels/#{@get('channel').get('id')}/activities" +}) \ No newline at end of file diff --git a/app/assets/javascripts/ember/controllers/channels_controller.js.coffee b/app/assets/javascripts/ember/controllers/channels_controller.js.coffee index da75b14c..09d63aa8 100644 --- a/app/assets/javascripts/ember/controllers/channels_controller.js.coffee +++ b/app/assets/javascripts/ember/controllers/channels_controller.js.coffee @@ -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) +}) \ No newline at end of file diff --git a/app/assets/javascripts/ember/controllers/users_controller.js.coffee b/app/assets/javascripts/ember/controllers/users_controller.js.coffee new file mode 100644 index 00000000..51c12ad9 --- /dev/null +++ b/app/assets/javascripts/ember/controllers/users_controller.js.coffee @@ -0,0 +1,2 @@ +Kandan.UsersController = Ember.ResourceController.create + resourceType: Kandan.User \ No newline at end of file diff --git a/app/assets/javascripts/ember/models/activity.js.coffee b/app/assets/javascripts/ember/models/activity.js.coffee new file mode 100644 index 00000000..e19bdb78 --- /dev/null +++ b/app/assets/javascripts/ember/models/activity.js.coffee @@ -0,0 +1,2 @@ +Kandan.Activity = Ember.Resource.extend + resourceName: 'activity' diff --git a/app/assets/javascripts/ember/models/channel.js.coffee b/app/assets/javascripts/ember/models/channel.js.coffee index 22fde931..b6665c1a 100644 --- a/app/assets/javascripts/ember/models/channel.js.coffee +++ b/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' diff --git a/app/assets/javascripts/ember/templates/activities/list_activities.handlebars b/app/assets/javascripts/ember/templates/activities/list_activities.handlebars new file mode 100644 index 00000000..3a693c22 --- /dev/null +++ b/app/assets/javascripts/ember/templates/activities/list_activities.handlebars @@ -0,0 +1,4 @@ +List activitiy boxes for channels here +{{#each channels}} + {{view Kandan.ListChannelActivitiesView channelBinding="this"}} +{{/each}} \ No newline at end of file diff --git a/app/assets/javascripts/ember/templates/activities/list_channel_activities.handlebars b/app/assets/javascripts/ember/templates/activities/list_channel_activities.handlebars new file mode 100644 index 00000000..0e09ca2b --- /dev/null +++ b/app/assets/javascripts/ember/templates/activities/list_channel_activities.handlebars @@ -0,0 +1,4 @@ +CHANNEL: {{channel.name}} +{{#each channel.activities}} + {{view Kandan.ShowActivityView activityBinding="this"}} +{{/each}} diff --git a/app/assets/javascripts/ember/templates/activities/show.handlebars b/app/assets/javascripts/ember/templates/activities/show.handlebars new file mode 100644 index 00000000..43add763 --- /dev/null +++ b/app/assets/javascripts/ember/templates/activities/show.handlebars @@ -0,0 +1 @@ +USER:{{activity.user.id}} {{activity.id}} - {{activity.content}} diff --git a/app/assets/javascripts/ember/views/activities/list_activities.js.coffee b/app/assets/javascripts/ember/views/activities/list_activities.js.coffee new file mode 100644 index 00000000..8177f962 --- /dev/null +++ b/app/assets/javascripts/ember/views/activities/list_activities.js.coffee @@ -0,0 +1,4 @@ +Kandan.ListActivitiesView = Ember.View.extend({ + templateName: 'ember/templates/activities/list_activities' + channelsBinding: 'Kandan.ChannelsController' +}) \ No newline at end of file diff --git a/app/assets/javascripts/ember/views/activities/list_channel_activities.js.coffee b/app/assets/javascripts/ember/views/activities/list_channel_activities.js.coffee new file mode 100644 index 00000000..5216d3de --- /dev/null +++ b/app/assets/javascripts/ember/views/activities/list_channel_activities.js.coffee @@ -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' + +}) \ No newline at end of file diff --git a/app/assets/javascripts/ember/views/activities/show.js.coffee b/app/assets/javascripts/ember/views/activities/show.js.coffee new file mode 100644 index 00000000..30a35346 --- /dev/null +++ b/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' +}) \ No newline at end of file diff --git a/app/controllers/activities_controller.rb b/app/controllers/activities_controller.rb new file mode 100644 index 00000000..b7725a95 --- /dev/null +++ b/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 diff --git a/app/controllers/channels_controller.rb b/app/controllers/channels_controller.rb new file mode 100644 index 00000000..5d47a58f --- /dev/null +++ b/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 diff --git a/app/controllers/main_controller.rb b/app/controllers/main_controller.rb index f4a1a103..53973cb6 100644 --- a/app/controllers/main_controller.rb +++ b/app/controllers/main_controller.rb @@ -2,6 +2,6 @@ class MainController < ApplicationController before_filter :authenticate_user! def index - @channels = Channel.all + @channels = Channel.includes(:activities => :user).all end end diff --git a/app/models/activity_observer.rb b/app/models/activity_observer.rb index 78dc7942..b2a54873 100644 --- a/app/models/activity_observer.rb +++ b/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 diff --git a/app/views/main/index.html.erb b/app/views/main/index.html.erb index caa882ba..75c554be 100644 --- a/app/views/main/index.html.erb +++ b/app/views/main/index.html.erb @@ -4,9 +4,19 @@ {{ view Kandan.ListChannelsView }} + + + <%= 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 %> diff --git a/config/routes.rb b/config/routes.rb index df846952..72941f21 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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. diff --git a/lib/broadcasters/faye.rb b/lib/broadcasters/faye.rb index 97deb767..fafb62a3 100644 --- a/lib/broadcasters/faye.rb +++ b/lib/broadcasters/faye.rb @@ -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" diff --git a/spec/controllers/channels_controller_spec.rb b/spec/controllers/channels_controller_spec.rb new file mode 100644 index 00000000..7ca6c38d --- /dev/null +++ b/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 diff --git a/spec/factories.rb b/spec/factories.rb new file mode 100644 index 00000000..eb4e7878 --- /dev/null +++ b/spec/factories.rb @@ -0,0 +1,5 @@ +Factory.define do + factory :channel do |f| + f.name "Test Channel" + end +end diff --git a/spec/models/activity_observer_spec.rb b/spec/models/activity_observer_spec.rb new file mode 100644 index 00000000..7a607f4d --- /dev/null +++ b/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