class ServiceVersion < ApplicationRecord
  belongs_to :service
  belongs_to :user
  validates :spec, swagger_spec: true
  before_create :set_version_number
  before_validation :read_spec

  # proposed: 0, current: 1, rejected: 2, retracted:3 , outdated:4 , retired:5
  # Always add new states at the end.
  enum status: [:proposed, :current, :rejected, :retracted, :outdated, :retired]

  attr_accessor :spec_file

  def to_param
    version_number.to_s
  end

  def set_version_number
    self.version_number = service.last_version_number + 1
  end

  def read_spec
    self.spec = JSON.parse(self.spec_file.read)
  end
end

class ServiceVersion < ApplicationRecord
  belongs_to :service
  belongs_to :user
  validates :spec, swagger_spec: true
  before_create :set_version_number

  # proposed: 0, current: 1, rejected: 2, retracted:3 , outdated:4 , retired:5
  # Always add new states at the end.
  enum status: [:proposed, :current, :rejected, :retracted, :outdated, :retired]

  def spec_file
    @spec_file
  end

  def spec_file=(spec_file)
    @spec_file = spec_file
    self.spec = JSON.parse(self.spec_file.read)
  end

  def to_param
    version_number.to_s
  end

  def set_version_number
    self.version_number = service.last_version_number + 1
  end

end

Refactor JSON parsing of spec file to attr setter
Refactor JSON parsing of spec file to attr setter

This way we avoid trying to parse a file on other flows (such as service version
updates when its state change)
