Why Gemfury? Push, build, and install  RubyGems npm packages Python packages Maven artifacts PHP packages Go Modules Debian packages RPM packages NuGet packages

Repository URL to install this package:

Details    
j_platform / app / controllers / j_platform / tickets_controller.rb
Size: Mime:
module JPlatform
  class TicketsController < BaseController
    before_action :ajax_only, only: [:new, :edit]
    before_action :require_available_projects
    before_action :load_project_and_members, only: [:index, :show, :new]
    before_action :load_ticket_statuses, only: [:index, :show]
    before_action :load_project_ticket_lists, only: [:index, :new]
    before_action :load_ticket_resolutions, only: [:show]
    before_action :load_priorities, only: [:new, :edit]

    layout "j_platform"
    helper JPlatform::TicketHelper

    def index
      session[:show_all_tickets] ||= params[:show_all] == 'true'
      @show_all_tickets = session[:show_all_tickets]

      tickets_options = { project_id: @project.id }
      tickets_options.merge!({ created_start: 7.days.ago.to_date.iso8601 }) unless @show_all_tickets

      @tickets = Ticket.all(params: tickets_options)
      @alert_no_tickets = @tickets.count == 0 && @show_all_tickets
    end

    def show
      @ticket = Ticket.find(params[:id], params: { project_id: @project.id, include_external_conversation: true, include_feed_items: true })

      message = @ticket.default_reply_text
      @post = Project::Post.new(social_account: @ticket.default_social_account, message: message)
    rescue ActiveResource::ForbiddenAccess
      redirect_to :tickets
    end

    def new
      @ticket = Ticket.new
      render :new_edit, layout: false
    end

    def create
      ticket_params = params.require(:ticket).permit(:project_ticket_list_id, :social_message_id, :assignee_id, :due_on, :priority, :tag_names, :comment, :social_message_origin_ids, :social_account_id)
      ticket_params[:social_message_origin_ids] = case params[:ticket][:social_message_origin_ids]
        when Array then params[:ticket][:social_message_origin_ids]
        when String then params[:ticket][:social_message_origin_ids].split(',')
        else []
        end.compact
      @ticket = Ticket.new(ticket_params)

      if @ticket.save
        last_updated_at = Rails.cache.fetch("j_platform_project_#{@ticket.project_id}_last_updated_at", race_condition_ttl: 5)
        cache_params = [@ticket.project_id, params: { include_client_memberships: true, include_tickets: true, include_project_ticket_lists: true }]
        Rails.cache.delete("j_platform_project_#{cache_params}_#{last_updated_at}")
        render json: { message: t('tickets.messages.create_success'), id: @ticket.id }
      else
        @project = current_project(include_client_memberships: true, include_project_ticket_lists: true)
        @project_members = @project.members
        @project_ticket_lists = ProjectTicketList.all(params: { project_id: @project.id })
        @priorities = Ticket.priorities
        render :new_edit, status: 422, layout: false
      end
    end

    def edit
      @project = current_project(include_client_memberships: true, include_project_ticket_lists: true)
      @ticket = Ticket.find(params[:id], params: { project_id: @project.id })
      @ticket_statuses = Ticket.select_statuses
      @project_members = @project.members
      @project_ticket_lists = ProjectTicketList.all(params: { project_id: @project.id })
      render :new_edit, layout: false
    end

    def update
      ticket_params = params.require(:ticket).permit(:project_ticket_list_id, :social_message_id, :status, :status_cd, :resolution_id, :assignee_id, :due_on, :priority, :is_hot, :tag_names, :comment, :draft_message)
      @project = current_project
      @ticket = Ticket.find(params[:id], params: { project_id: @project.id })
      ticket_params.delete(:comment) if ticket_params[:comment].blank?

      if @ticket.update_attributes(ticket_params)
        last_updated_at = Rails.cache.fetch("j_platform_project_#{@ticket.project_id}_last_updated_at", race_condition_ttl: 5)
        cache_params = [@ticket.project_id, params: { include_client_memberships: true, include_tickets: true, include_project_ticket_lists: true }]
        Rails.cache.delete("j_platform_project_#{cache_params}_#{last_updated_at}")
        if ticket_params.has_key?(:draft_message) or params[:ticket].keys == ["comment"]
          render json: { object: render_to_string(@ticket.feed_items), message: t('tickets.messages.update_success') }
        else
          @ticket = Ticket.find(params[:id], params: { project_id: @project.id, include_external_conversation: true, include_feed_items: true })
          @post = Project::Post.new({ social_account: @ticket.default_social_account, message: @ticket.default_reply_text })
          render json: { object: render_to_string(partial: 'j_platform/tickets/view', locals: { ticket: @ticket, post: @post }), message: t('tickets.messages.update_success') }
        end
      else
        return render json: { status: false } if ticket_params.has_key?(:draft_message)
        render json: { message: @ticket.errors.full_messages.join(", ") }, status: 422
      end
    end

    private
      def load_ticket_statuses
        @ticket_statuses = Ticket.statuses
      end

      def load_project_ticket_lists
        raise ArgumentError, "@project is not set before calling load_project_ticket_lists" if @project.nil?

        @project_ticket_lists = Rails.cache.fetch("project_ticket_list_project_#{@project.id}", expires_in: 5.minutes) do
          ProjectTicketList.all(params: { project_id: @project.id })
        end
      end

      def load_ticket_resolutions
        raise ArgumentError, "@project is not set before calling load_resolutions" if @project.nil?

        @resolutions = Rails.cache.fetch("ticket_resolutions_client_#{@project.client_id}", expires_in: 5.minutes) do
          Ticket::Resolution.find(:all, params: { client_id: @project.client_id })
        end
      end

      def load_project_and_members
        @project = current_project(include_client_memberships: true, include_project_ticket_lists: true)
        @project_members = @project.members
      end

      def load_priorities
        @priorities = Ticket.priorities
      end
  end
end