diff --git a/app/channels/application_cable/channel.rb b/app/channels/application_cable/channel.rb new file mode 100644 index 0000000..d672697 --- /dev/null +++ b/app/channels/application_cable/channel.rb @@ -0,0 +1,4 @@ +module ApplicationCable + class Channel < ActionCable::Channel::Base + end +end diff --git a/app/channels/application_cable/connection.rb b/app/channels/application_cable/connection.rb new file mode 100644 index 0000000..0ff5442 --- /dev/null +++ b/app/channels/application_cable/connection.rb @@ -0,0 +1,4 @@ +module ApplicationCable + class Connection < ActionCable::Connection::Base + end +end diff --git a/app/controllers/urls_controller.rb b/app/controllers/urls_controller.rb index c5ced1d..9b302d9 100644 --- a/app/controllers/urls_controller.rb +++ b/app/controllers/urls_controller.rb @@ -22,6 +22,7 @@ def create @url.user = current_user if user_signed_in? if @url.save + broadcast_url_to_user(@url) if user_signed_in? redirect_to root_path, notice: "Short URL created successfully!" else @@ -60,8 +61,8 @@ def lengthen @url.short_code = generate_long_code if @url.save - long_url = short_url_for(@url) - render json: { long_url: long_url } + broadcast_url_to_user(@url) + render json: { long_url: short_url_for(@url) } else render json: { error: @url.errors.full_messages.first }, status: :unprocessable_entity end @@ -163,6 +164,16 @@ def throttle_expand! render json: { error: "Too many requests. Please slow down." }, status: :too_many_requests end + def broadcast_url_to_user(url) + return unless url.user_id.present? + Turbo::StreamsChannel.broadcast_prepend_to( + "urls_#{url.user_id}", + target: "urls_list", + partial: "urls/url", + locals: { url: url } + ) + end + def permitted_url_params allowed = [:original_url] allowed << :short_code if user_signed_in? diff --git a/app/views/urls/_url.html.erb b/app/views/urls/_url.html.erb new file mode 100644 index 0000000..26b2f74 --- /dev/null +++ b/app/views/urls/_url.html.erb @@ -0,0 +1,27 @@ +<% kind = url.short_code.length > 20 ? "long" : "short" %> + diff --git a/app/views/urls/index.html.erb b/app/views/urls/index.html.erb index 953c028..f7ae7a5 100644 --- a/app/views/urls/index.html.erb +++ b/app/views/urls/index.html.erb @@ -103,42 +103,17 @@ -