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 / posts_controller.rb
Size: Mime:
module JPlatform
  class PostsController < BaseController
    layout "j_platform"
    before_action :ajax_only, only: [:new]

    def new
      render json: { object: render_to_string(partial: 'layouts/social_post/form', locals: { project: current_project, post: nil }) }
    end

    def edit
      render json: { object: render_to_string(partial: 'layouts/social_post/form', locals: { project: current_project, post: ScheduledItemsGroup.find(params[:id]) }) }
    end

    def create
      @post = ::JPlatform::Project::Post.new(params[:project_post])
      @post.prefix_options[:project_id] = params[:project_id]
      update_time_by_params()

      unless @post.valid?
        return render json: { status: 'failure', message: @post.errors.full_messages }, status: 422
      end

      begin
        @post.save
        if params[:post_time]
          return render json: { status: 'success' }
        end

        if @post.errors.full_messages.present?
          return render json: { status: 'failure', message: @post.errors.full_messages }, status: 400
        end
        error_social_messages = @post.social_messages.reject { |sm| sm.try(:post_errors).blank? }

        @post_stats = @post.social_messages.map{ |sm| { social_account: sm.try(:author),
                                                         error_messages: sm.try(:post_errors) } }

        if error_social_messages.present?
          render json: { status: 'failure', messages: @post_stats }, status: 422
        else
          if !!params[:renders_ticket_path]
            social_messages = @post.social_messages.select { |sm| sm.try(:post_errors).blank? }
            # NOTE: this is wrong... "social_messages" isn't what you expect here...
            # Fortunately, we aren't currently using it.
            render json: { status: 'success', object: render_to_string(partial: "j_platform/ticket/external_conversations/external_conversation", collection: social_messages) }
          else
            render json: { status: 'success' }
          end
        end
      rescue => e
        handle_schedule_post_create(e)
      end
    end

    def update
      if params[:project_post]
        @post = ScheduledItemsGroup.find(params[:id])
        update_time_by_params()

        if params[:post_time]
          saved = @post.update_attributes(params[:project_post]) #Maybe do something to catch if params[:project_post] is nil...
          render json: { status: (saved ? 'success' : 'error') }
        else
          # No scheduled time specified => destroy scheduled items, then treat as create (to immediately post)
          params[:project_id] ||= @post_item.project_id
          @post.destroy
          return create
        end
      else
        render json: { status: 'error' } # Do something to signify the params weren't passed correctly?
      end
    end

    private

    def update_time_by_params
      if params[:post_time] && @post.present?
        @post.post_time = params[:post_time]
        @post.post_timezone = params[:post_timezone]
      end
    end

    def handle_schedule_post_create(e)
      # only when schedule post
      if e.to_s.include? 'got "true"'
        render json: { status: 'success' }
      else
        # TODO: fix tests so that we can do:
        # raise e
        render json: { status: 'failure' }, status: 500
      end
    end
  end
end