Sha256: f521508ab99d31ce9c1ed0bfecf6163a3ee1979f207ca38b35465af3d9a2e661

Contents?: true

Size: 1.58 KB

Versions: 6

Compression:

Stored size: 1.58 KB

Contents

class RailsExecution::Tasks::CreateService

  def initialize(params, current_owner)
    @params = params
    @current_owner = current_owner
    @task = nil
    @error = nil
  end

  def call
    initialize_task
    create_task
  rescue => e
    @error = e.to_s
  ensure
    return self
  end

  attr_reader :error
  attr_reader :task

  private

  attr_reader :params
  attr_reader :current_owner

  def initialize_task
    @task = ::RailsExecution::Task.new({
      status: :created,
      owner_id: current_owner&.id,
      owner_type: ::RailsExecution.configuration.owner_model.to_s,
      title: params.dig(:task, :title),
      description: params.dig(:task, :description),
      script: params.dig(:task, :script),
      scheduled_at: parsed_scheduled_at,
      repeat_mode: params.dig(:task, :repeat_mode)
    })

    @task.assign_reviewers(params.dig(:task, :task_review_ids).to_a)
    @task.assign_labels(params[:task_label_ids].to_a)
    @task.syntax_status = ::RailsExecution::Services::SyntaxChecker.new(@task.script).call ? 'good' : 'bad'
  end

  def scheduled_at
    @scheduled_at ||= params.dig(:task, :scheduled_at)
  end

  def parsed_scheduled_at
    @parsed_scheduled_at ||= scheduled_at.blank? ? nil : Time.zone.parse(scheduled_at)
  rescue
    raise 'Invalid input for scheduling time'
  end

  def create_task
    if @task.save
      @task.add_files(params[:attachments]&.permit!.to_h, current_owner) if ::RailsExecution.configuration.file_upload
      ::RailsExecution.configuration.notifier.new(@task).after_create
    else
      raise @task.errors.messages.values.flatten.first
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rails_execution-0.1.12 app/services/rails_execution/tasks/create_service.rb
rails_execution-0.1.11 app/services/rails_execution/tasks/create_service.rb
rails_execution-0.1.10 app/services/rails_execution/tasks/create_service.rb
rails_execution-0.1.9 app/services/rails_execution/tasks/create_service.rb
rails_execution-0.1.8 app/services/rails_execution/tasks/create_service.rb
rails_execution-0.1.7 app/services/rails_execution/tasks/create_service.rb