Merge pull request #24918 from prathamesh-sonpatki/cable-assets
Cable: Generate .js or .coffee files while generating channel as per the javascript engine of the application
This commit is contained in:
commit
db312a9fb0
@ -16,7 +16,8 @@ def create_channel_file
|
||||
if self.behavior == :invoke
|
||||
template "assets/cable.js", "app/assets/javascripts/cable.js"
|
||||
end
|
||||
template "assets/channel.coffee", File.join('app/assets/javascripts/channels', class_path, "#{file_name}.coffee")
|
||||
|
||||
js_template "assets/channel", File.join('app/assets/javascripts/channels', class_path, "#{file_name}")
|
||||
end
|
||||
|
||||
generate_application_cable_files
|
||||
|
@ -0,0 +1,18 @@
|
||||
App.<%= class_name.underscore %> = App.cable.subscriptions.create("<%= class_name %>Channel", {
|
||||
connected: function() {
|
||||
// Called when the subscription is ready for use on the server
|
||||
},
|
||||
|
||||
disconnected: function() {
|
||||
// Called when the subscription has been terminated by the server
|
||||
},
|
||||
|
||||
received: function(data) {
|
||||
// Called when there's incoming data on the websocket for this channel
|
||||
}<%= actions.any? ? ",\n" : '' %>
|
||||
<% actions.each do |action| -%>
|
||||
<%=action %>: function() {
|
||||
return this.perform('<%= action %>');
|
||||
}<%= action == actions[-1] ? '' : ",\n" %>
|
||||
<% end -%>
|
||||
});
|
@ -26,6 +26,10 @@ def template(source, *args, &block)
|
||||
super
|
||||
end
|
||||
end
|
||||
|
||||
def js_template(source, destination)
|
||||
template(source + '.js', destination + '.js')
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
@ -24,8 +24,24 @@ def test_channel_is_created
|
||||
assert_match(/class ChatChannel < ApplicationCable::Channel/, channel)
|
||||
end
|
||||
|
||||
assert_file "app/assets/javascripts/channels/chat.coffee" do |channel|
|
||||
assert_match(/App.cable.subscriptions.create "ChatChannel"/, channel)
|
||||
assert_file "app/assets/javascripts/channels/chat.js" do |channel|
|
||||
assert_match(/App.chat = App.cable.subscriptions.create\("ChatChannel/, channel)
|
||||
end
|
||||
end
|
||||
|
||||
def test_channel_with_multiple_actions_is_created
|
||||
run_generator ['chat', 'speak', 'mute']
|
||||
|
||||
assert_file "app/channels/chat_channel.rb" do |channel|
|
||||
assert_match(/class ChatChannel < ApplicationCable::Channel/, channel)
|
||||
assert_match(/def speak/, channel)
|
||||
assert_match(/def mute/, channel)
|
||||
end
|
||||
|
||||
assert_file "app/assets/javascripts/channels/chat.js" do |channel|
|
||||
assert_match(/App.chat = App.cable.subscriptions.create\("ChatChannel/, channel)
|
||||
assert_match(/,\n\n speak/, channel)
|
||||
assert_match(/,\n\n mute: function\(\) \{\n return this\.perform\('mute'\);\n \}\n\}\);/, channel)
|
||||
end
|
||||
end
|
||||
|
||||
@ -36,7 +52,7 @@ def test_channel_asset_is_not_created_when_skip_assets_is_passed
|
||||
assert_match(/class ChatChannel < ApplicationCable::Channel/, channel)
|
||||
end
|
||||
|
||||
assert_no_file "app/assets/javascripts/channels/chat.coffee"
|
||||
assert_no_file "app/assets/javascripts/channels/chat.js"
|
||||
end
|
||||
|
||||
def test_cable_js_is_created_if_not_present_already
|
||||
@ -52,7 +68,7 @@ def test_channel_on_revoke
|
||||
run_generator ['chat'], behavior: :revoke
|
||||
|
||||
assert_no_file "app/channels/chat_channel.rb"
|
||||
assert_no_file "app/assets/javascripts/channels/chat.coffee"
|
||||
assert_no_file "app/assets/javascripts/channels/chat.js"
|
||||
|
||||
assert_file "app/channels/application_cable/channel.rb"
|
||||
assert_file "app/channels/application_cable/connection.rb"
|
||||
|
Loading…
Reference in New Issue
Block a user