← Back to Upcase

Json display class for STI model


(Nicolo) #1

I have a table for athletes in my system called players that I converted into STI recently. Via STI there are MlbPlayers, NflPlayer, etc.

I had been using a display class which inherited from players. The class has a few methods and overrides the as_json. I want this to be different than the standard players as_json.

I’m trying to figure out what is the best approach to refactoring this. Should I have a display class that doesn’t inherit from player and just takes a player as parameter? I would have to do an active record query of about 1000 players then create instances of the new object.

Below is basic gist of the file.

class DisplayPlayer < Player
  self.inheritance_column = :_type_disabled
  def as_json(options = nil)
    super({
      root: false,
      only: [:id, :injury_status, :position, :has_story, :player_team, :game_id],
      methods: [:points_per_game, :full_name, :matchup, :is_hidden, :in_lineup, :game_time, :slot_position, :is_flexable, :cost, :probable_starter]}.merge(options || {}))
  end

  def cost

  end

  def points_per_game

  end

  def matchup
   
  end

  def in_lineup
    self.starter
  end

  def probable_starter

  end


  def is_hidden

  end

  def game_time

  end
end

(Alex Bush) #2

you should use ActiveModel::Serializers