websocket - Using current_user to customize ActionCable channel submittion -


i'm making instant commenting rails 5. using rails *.js.erb, comfortable size of project. use actioncable deliver new comments users. thing want actioncable send rendered comments/_comment.html.haml directly client evaluate code in browser. in common case it's totally achivable. in erb template of comment have deal current_user add delete link if current_user.admin? problem when invoke commentschannel.broadcast_to , render _comment.html.haml renderer not current_user.

question how can render _comment.html.haml , submit each separate subscription defined current user, using current user subscription?

devise used authentication.

comments/_comment.html.haml

%div[comment]   .details     %span.author= comment.user.email     %span.time= l comment.created_at, format: :short   = comment.body   = link_to t('common.reply'), comment, class: 'reply-link'   = link_to t('common.delete'), comment, method: :delete, remote: true if current_user.admin?   = render 'comments/form', comment: comment.commentable.comment_threads.build, parent: comment    - if comment.children.any?     .child-comments       = render comment.children.sort_by { |c| c.created_at }.reverse 

application_cable/connection.rb

module applicationcable   class connection < actioncable::connection::base     identified_by :current_user      def connect       self.current_user = find_verified_user     end      def find_verified_user       if verified_user = env['warden'].user         verified_user       else         reject_unauthorized_connection       end     end   end end 

broadcast_comment_job.rb

class broadcastcommentjob < applicationjob   queue_as :default    def perform(comment)     commentschannel.broadcast_to \       comment.commentable,        comment: render_comment(comment)   end    private    def render_comment(comment)     commentscontroller.render(partial: 'comment', locals: { comment: comment })   end  end 

comment.rb

class comment < activerecord::base   ...   after_create_commit { broadcastcommentjob.perform_now(self) }   ... end 


Comments

Popular posts from this blog

PHP and MySQL WP -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

go - golang pprof for c library code -