Merge branch 'actioncable'

This commit is contained in:
yafeilee 2016-04-28 11:28:30 +08:00
commit db6a585467
19 changed files with 62 additions and 7 deletions

View File

@ -1,9 +1,11 @@
//= require jquery
//= require jquery_ujs
//= require turbolinks
//= require action_cable
//= require foundation
//= require js.cookie
//= require 'jquery.html5-fileupload'
//= require cable
//= require_tree .
$(document).on('turbolinks:load', function(){

View File

@ -0,0 +1 @@
@App ||= {}

View File

@ -0,0 +1,12 @@
$(document).on 'turbolinks:load', ->
$('#alert-container .close-button').click ()->
$('#alert-container').hide()
if $('#blog-show-page').length > 0
window.App.cable ||= ActionCable.createConsumer()
if window.App.comment_channel
window.App.comment_channel.unsubscribe()
window.App.comment_channel = window.App.cable.subscriptions.create { channel: "CommentChannel", post_id: $('#blog-show-page').data('post_id') },
received: (data)->
if data['not'] != Cookies.get('cable_id')
$.get( $('#blog-show-page').data('url') )

View File

@ -1,3 +0,0 @@
$(document).on 'turbolinks:load', ->
$('#alert-container .close-button').click ()->
$('#alert-container').hide()

View File

@ -0,0 +1,5 @@
# Be sure to restart your server when you modify this file. Action Cable runs in a loop that does not support auto reloading.
module ApplicationCable
class Channel < ActionCable::Channel::Base
end
end

View File

@ -0,0 +1,5 @@
# Be sure to restart your server when you modify this file. Action Cable runs in a loop that does not support auto reloading.
module ApplicationCable
class Connection < ActionCable::Connection::Base
end
end

View File

@ -0,0 +1,6 @@
# Be sure to restart your server when you modify this file. Action Cable runs in a loop that does not support auto reloading.
class CommentChannel < ApplicationCable::Channel
def subscribed
stream_from "comment_post_#{params[:post_id]}"
end
end

View File

@ -17,6 +17,7 @@ class BlogsController < ApplicationController
end
def show
cookies[:cable_id] = SecureRandom.uuid
@post = Post.find(params[:id])
@post.visited
@prev = Post.where('created_at < ?', @post.created_at).order(created_at: :desc).first

View File

@ -15,12 +15,18 @@ class CommentsController < ApplicationController
if @comment.save
@comments = @post.comments.order(created_at: :desc)
ActionCable.server.broadcast "comment_post_#{@comment.post.id}", { not: cookies[:cable_id] }
render :create_ok
else
render :create_fail
end
end
def refresh
@post = Post.find(params[:blog_id])
@comments = @post.comments.order(created_at: :desc)
end
private
def comment_params
params.require(:comment).permit(:content, :name, :email)

View File

@ -4,7 +4,7 @@
- content_for(:title) do
| #{@post.title}
.row.blog-wrapper
.row.blog-wrapper#blog-show-page data-url=refresh_blog_comments_path(@post) data-post_id=@post.id
.small-12.large-9.large-centered.columns
= render partial: "post", :locals=> { post: @post }
.comment-field

View File

@ -0,0 +1 @@
$('.comment-diag').replaceWith('<%= j render partial: 'comment_content', locals: { comments: @comments }%>');

View File

@ -10,6 +10,7 @@ html
= favicon_link_tag 'favicon.png', type: 'image/png'
= javascript_include_tag "application", 'data-turbolinks-track' => "reload"
= csrf_meta_tags
= action_cable_meta_tag
body data-whatinput="mouse"
- if content_for?(:main)
= yield(:main)

9
config/cable.yml Normal file
View File

@ -0,0 +1,9 @@
production:
adapter: redis
url: redis://localhost:6379/wblog_cable
development:
adapter: async
test:
adapter: async

View File

@ -46,7 +46,7 @@ WBlog::Application.configure do
config.log_level = :info
# Prepend all log lines with the following tags.
# config.log_tags = [ :subdomain, :uuid ]
config.log_tags = [ :request_id ]
# Use a different logger for distributed setups.
# config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new)
@ -59,7 +59,10 @@ WBlog::Application.configure do
# Precompile additional assets.
# application.js, application.css, and all non-JS/CSS in app/assets folder are already added.
config.assets.precompile += %w( vendor/modernizr.js )
# config.assets.precompile += %w( )
config.action_cable.url = 'wss://example.com/cable'
config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ]
# Ignore bad email addresses and do not raise email delivery errors.
# Set this to true and configure the email server for immediate delivery to raise delivery errors.

View File

@ -4,7 +4,11 @@ WBlog::Application.routes.draw do
collection do
get :rss
end
resources :comments, only: [:index, :create]
resources :comments, only: [:index, :create] do
collection do
get :refresh
end
end
resources :likes, only: [:index, :create, :destroy] do
member do
get :is_liked
@ -42,4 +46,6 @@ WBlog::Application.routes.draw do
get '/mobile' => 'home#mobile'
root 'blogs#index'
mount ActionCable.server => '/cable'
end