diff --git a/app/controllers/light/users_controller.rb b/app/controllers/light/users_controller.rb index 4f4d2c9..a53e4d2 100644 --- a/app/controllers/light/users_controller.rb +++ b/app/controllers/light/users_controller.rb @@ -43,22 +43,27 @@ def sendtest end def unsubscribe - unless(@user.is_subscribed) - @message = 'You have already unsubscribed!!' - else + if @user.present? && @user.sidekiq_status == 'Subscribed' @user.update(is_subscribed: 'false', - unsubscribed_at: DateTime.now, - sidekiq_status: 'Unsubscribed') + unsubscribed_at: DateTime.now, + sidekiq_status: 'Unsubscribed') @message = 'Unsubscribed successfully!!' + else + @message = response_message('unsubscribed') end end def subscribe - @user.update(is_subscribed: 'true', - sidekiq_status: 'Subscribed', - subscribed_at: DateTime.now, - remote_ip: request.remote_ip, - user_agent: request.env['HTTP_USER_AGENT']) + if @user.present? && @user.sidekiq_status == 'Unsubscribed' + @user.update(is_subscribed: 'true', + sidekiq_status: 'Subscribed', + subscribed_at: DateTime.now, + remote_ip: request.remote_ip, + user_agent: request.env['HTTP_USER_AGENT']) + @message = 'Subscribed successfully!!' + else + @message = response_message('subscribed') + end end def sendmailer @@ -140,5 +145,21 @@ def users_params def user_with_token @user = Light::User.where(token: params[:token]).first end + + def dummy_token? + params[:token] == 'test_user_dummy_id' + end + + def response_message(status) + if dummy_token? + "#{status.capitalize} successfully!!" + elsif @user.nil? + "Hey, it seems request you are trying to access is invalid. If you have any " + + "concerns about our newsletter's subscription, kindly get in touch with " + + "hr@joshsoftware.com" + else + "You have already #{status}!!" + end + end end end diff --git a/app/helpers/light/users_helper.rb b/app/helpers/light/users_helper.rb index 72d5930..c9dd6e7 100644 --- a/app/helpers/light/users_helper.rb +++ b/app/helpers/light/users_helper.rb @@ -1,4 +1,7 @@ module Light module UsersHelper + def dummy_token? + params[:token] == 'test_user_dummy_id' + end end end diff --git a/app/views/light/users/subscribe.html.haml b/app/views/light/users/subscribe.html.haml index edc9c14..a25f2cc 100644 --- a/app/views/light/users/subscribe.html.haml +++ b/app/views/light/users/subscribe.html.haml @@ -1,7 +1,8 @@ %h2 %center - Subscribed successfully!! + = @message.html_safe %br - To unsubscribe again - = link_to "CLICK HERE !!", main_app.users_unsubscribe_path(token: @user.token) + - if @user.present? || dummy_token? + To unsubscribe again + = link_to "CLICK HERE !!", main_app.users_unsubscribe_path(token: @user.try(:token) || params[:token]) diff --git a/app/views/light/users/unsubscribe.html.haml b/app/views/light/users/unsubscribe.html.haml index 9261112..ab62f37 100644 --- a/app/views/light/users/unsubscribe.html.haml +++ b/app/views/light/users/unsubscribe.html.haml @@ -1,6 +1,7 @@ %h2 %center - = @message + = @message.html_safe %br - Would you like to subscribe again? - = link_to "CLICK HERE !!", main_app.users_subscribe_path(token: @user.token) + - if @user.present? || dummy_token? + Would you like to subscribe again? + = link_to "CLICK HERE !!", main_app.users_subscribe_path(token: @user.try(:token) || params[:token])