Advertisement
Guest User

Untitled

a guest
Aug 24th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. # frozen_string_literal: true
  2.  
  3. class SubscribersController < ApplicationController
  4. include Mixins::SubscriberSearch
  5. # include Mixins::ChannelSearch
  6. # include Mixins::ChannelGroupSearch
  7. # include Mixins::AdministrativeLogging
  8. # include Mixins::SubscriberActivities
  9.  
  10. before_action :load_subscriber
  11. skip_before_action :load_subscriber, :only => [:new,:create,:index,:no_longer_in_service]
  12. before_action :load_user, :only =>[:new, :create, :index, :show, :download_activity,:no_longer_in_service]
  13.  
  14. def index
  15. session[:root_page] = subscribers_path
  16. #@subscribers = @user.subscribers.load
  17. handle_subscribers_query
  18. respond_to do |format|
  19. format.html # index.html.erb
  20. format.json { render json: @subscribers }
  21. end
  22. end
  23.  
  24. def no_longer_in_service
  25. binding.pry
  26. session[:root_page] = subscribers_path
  27. ids = SubscriberResponse.where("caption like ?", "%number is no longer in service%").map{|sr| sr.subscriber_id}.uniq.compact
  28. @subscribers = @user.subscribers.where(id: ids).order(created_at: :desc)
  29. @subscribers = @subscribers.page(subscribers_page).per_page(10)
  30. @subscribers = @subscribers.search(params[:subscribers_search]) if params[:subscribers_search]
  31. respond_to do |format|
  32. format.html #no_longer_in_service
  33. format.json { render json: @subscribers }
  34. end
  35. end
  36.  
  37. def new
  38. @subscriber = @user.subscribers.new
  39.  
  40. respond_to do |format|
  41. format.html # new.html.erb
  42. format.json { render json: @subscriber }
  43. end
  44. end
  45.  
  46. # def new
  47. # subscriber = Subscriber.new
  48. # render json: subscriber
  49. # end
  50.  
  51. def create
  52. subscriber = Subscriber.new(subscriber_params.merge(user: user).except(:bot_id, :bot_code))
  53. if subscriber.save
  54. subscriber.integration_setups
  55. render json: subscriber
  56. else
  57. render json: subscriber.errors, status: :unprocessable_entity
  58. end
  59. end
  60.  
  61. def user
  62. @user ||= begin
  63. user = User.find(subscriber_params.dig(:user_id)) if subscriber_params.key?(:user_id)
  64. user = Bot.find(subscriber_params.dig(:bot_id)).user if user.nil? && subscriber_params.dig(:bot_id).present?
  65. user = Bot.find_by_code(subscriber_params.dig(:bot_code)).user if user.nil? && subscriber_params.dig(:bot_code).present?
  66. user = default_user if user.nil? && default_user
  67. user
  68. end
  69. end
  70.  
  71. def bot
  72. @bot ||= begin
  73. bot = Bot.find(subscriber_params.dig(:bot_id)) if subscriber_params.dig(:bot_id).present?
  74. bot = Bot.find_by_code(subscriber_params.dig(:bot_code)) if bot.nil? && subscriber_params.dig(:bot_code).present?
  75. bot = default_bot if bot.nil? && default_bot
  76. bot
  77. end
  78. end
  79.  
  80. private
  81.  
  82. def load_subscriber
  83. authenticate_user!
  84. @user = current_user
  85. begin
  86. @subscriber = @user.subscribers.find(subscriber_params[:id])
  87. redirect_to(root_url,alert:'Access Denied') unless @subscriber
  88. rescue
  89. redirect_to(root_url,alert:'Access Denied')
  90. end
  91. end
  92.  
  93. def subscriber_params
  94. params.require(:subscriber).permit(:email, :password, :password_confirmation, :user_id, :bot_id, :bot_code)
  95. end
  96.  
  97. def default_user
  98. @default_user ||= User.find(2)
  99. end
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement