Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # frozen_string_literal: true
- class SubscribersController < ApplicationController
- include Mixins::SubscriberSearch
- # include Mixins::ChannelSearch
- # include Mixins::ChannelGroupSearch
- # include Mixins::AdministrativeLogging
- # include Mixins::SubscriberActivities
- before_action :load_subscriber
- skip_before_action :load_subscriber, :only => [:new,:create,:index,:no_longer_in_service]
- before_action :load_user, :only =>[:new, :create, :index, :show, :download_activity,:no_longer_in_service]
- def index
- session[:root_page] = subscribers_path
- #@subscribers = @user.subscribers.load
- handle_subscribers_query
- respond_to do |format|
- format.html # index.html.erb
- format.json { render json: @subscribers }
- end
- end
- def no_longer_in_service
- binding.pry
- session[:root_page] = subscribers_path
- ids = SubscriberResponse.where("caption like ?", "%number is no longer in service%").map{|sr| sr.subscriber_id}.uniq.compact
- @subscribers = @user.subscribers.where(id: ids).order(created_at: :desc)
- @subscribers = @subscribers.page(subscribers_page).per_page(10)
- @subscribers = @subscribers.search(params[:subscribers_search]) if params[:subscribers_search]
- respond_to do |format|
- format.html #no_longer_in_service
- format.json { render json: @subscribers }
- end
- end
- def new
- @subscriber = @user.subscribers.new
- respond_to do |format|
- format.html # new.html.erb
- format.json { render json: @subscriber }
- end
- end
- # def new
- # subscriber = Subscriber.new
- # render json: subscriber
- # end
- def create
- subscriber = Subscriber.new(subscriber_params.merge(user: user).except(:bot_id, :bot_code))
- if subscriber.save
- subscriber.integration_setups
- render json: subscriber
- else
- render json: subscriber.errors, status: :unprocessable_entity
- end
- end
- def user
- @user ||= begin
- user = User.find(subscriber_params.dig(:user_id)) if subscriber_params.key?(:user_id)
- user = Bot.find(subscriber_params.dig(:bot_id)).user if user.nil? && subscriber_params.dig(:bot_id).present?
- user = Bot.find_by_code(subscriber_params.dig(:bot_code)).user if user.nil? && subscriber_params.dig(:bot_code).present?
- user = default_user if user.nil? && default_user
- user
- end
- end
- def bot
- @bot ||= begin
- bot = Bot.find(subscriber_params.dig(:bot_id)) if subscriber_params.dig(:bot_id).present?
- bot = Bot.find_by_code(subscriber_params.dig(:bot_code)) if bot.nil? && subscriber_params.dig(:bot_code).present?
- bot = default_bot if bot.nil? && default_bot
- bot
- end
- end
- private
- def load_subscriber
- authenticate_user!
- @user = current_user
- begin
- @subscriber = @user.subscribers.find(subscriber_params[:id])
- redirect_to(root_url,alert:'Access Denied') unless @subscriber
- rescue
- redirect_to(root_url,alert:'Access Denied')
- end
- end
- def subscriber_params
- params.require(:subscriber).permit(:email, :password, :password_confirmation, :user_id, :bot_id, :bot_code)
- end
- def default_user
- @default_user ||= User.find(2)
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement