Class: FReCon::TeamsController

Inherits:
Controller show all
Defined in:
lib/frecon/controllers/teams_controller.rb

Class Method Summary collapse

Methods inherited from Controller

create, delete, index, match_number_and_competition_to_match_id, model, model_name, process_json_object_request, show, show_attribute, team_number_to_team_id, update

Class Method Details

.competitions(params) ⇒ Object



68
69
70
71
72
73
74
75
76
# File 'lib/frecon/controllers/teams_controller.rb', line 68

def self.competitions(params)
  @team = find_model params

  if @team
    @team.competitions.to_json
  else
    raise RequestError.new(404, could_not_find(params[:id], "id or number"))
  end
end

.could_not_find(value, attribute = "id", model = model_name.downcase) ⇒ Object

Since Team has a special way of finding itself, we can make the error message reflect this.



23
24
25
26
27
28
29
# File 'lib/frecon/controllers/teams_controller.rb', line 23

def self.could_not_find(value, attribute = "id", model = model_name.downcase)
  if attribute == "id" && model == "team"
    "Could not find team of id or number #{value}!"
  else
    "Could not find #{model} of #{attribute} #{value}!"
  end
end

.find_model(params) ⇒ Object

The id param will be a number or id.



17
18
19
# File 'lib/frecon/controllers/teams_controller.rb', line 17

def self.find_model(params)
  (Team.find_by id: params[:id]) || (Team.find_by number: params[:id])
end

.matches(params) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/frecon/controllers/teams_controller.rb', line 51

def self.matches(params)
  @team = find_model params

  if @team
    # Ensure that the competition ID is valid.
    if params[:competition_id]
      @competition = Competition.find params[:competition_id]

      raise RequestError.new(404, could_not_find(params[:competition_id], "id", "competition")) if @competition.nil?
    end

    @team.matches(params[:competition_id]).to_json
  else
    raise RequestError.new(404, could_not_find(params[:id], "id or number"))
  end
end

.records(params) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/frecon/controllers/teams_controller.rb', line 31

def self.records(params)
  @team = find_model params

  if @team
    if params[:competition_id]
      @competition = Competition.find params[:competition_id]

      if @competition
        @team.records.in(match_id: @competition.matches.map { |match| match.id }).to_json
      else
        raise RequestError.new(404, could_not_find(params[:competition_id], "id", "competition"))
      end
    else
      @team.records.to_json
    end
  else
    raise RequestError.new(404, could_not_find(params[:id], "id or number"))
  end
end