Module: Api::Affiliation

Extended by:
ActiveSupport::Concern
Included in:
ChallengesController, ProgramsController, ProjectsController, UsersController
Defined in:
app/controllers/concerns/api/affiliation.rb

Instance Method Summary collapse

Instance Method Details

#affiliation_createObject



37
38
39
40
41
42
43
44
45
# File 'app/controllers/concerns/api/affiliation.rb', line 37

def affiliation_create
  render json: { error: 'Object not set' }, status: :not_found and return if @obj.nil?

  affiliation = @obj.add_affiliation_to(@parent)

  render json: { error: 'Affiliation could not be created' }, status: :unprocessable_entity and return unless affiliation

  render json: { success: true }, status: :created
end

#affiliation_destroyObject



56
57
58
59
60
61
62
63
# File 'app/controllers/concerns/api/affiliation.rb', line 56

def affiliation_destroy
  @obj.remove_affiliation_to(@parent)
  if ::Affiliation.find_by(affiliate: @obj, parent: @parent).nil?
    render json: { success: true }, status: :ok
  else
    render json: { errors: ["Error while deleting the affiliation"] }
  end
end

#affiliation_indexObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/controllers/concerns/api/affiliation.rb', line 16

def affiliation_index
  affiliations = @obj.affiliations.preload(:parent)
  response = affiliations.map do |affiliation|
    {
      parent_type: affiliation.parent_type,
      parent_id: affiliation.parent_id,
      status: affiliation.status,
      title: affiliation.parent.title,
      short_title: affiliation.parent.short_title,
      banner_url: affiliation.parent.banner_url,
      banner_url_sm: affiliation.parent.banner_url_sm,
      logo_url_sm: affiliation.parent.logo_url_sm
    }
  end
  render json: { parents: response }, status: :ok
end

#affiliation_showObject



33
34
35
# File 'app/controllers/concerns/api/affiliation.rb', line 33

def affiliation_show
  render json: @affiliation, status: :ok
end

#affiliation_updateObject



47
48
49
50
51
52
53
54
# File 'app/controllers/concerns/api/affiliation.rb', line 47

def affiliation_update
  @affiliation.status = params[:affiliations][:status]
  @affiliation.save

  render json: { error: 'Affiliation could not be updated' }, status: :unprocessable_entity and return unless @affiliation

  render json: { success: true }, status: :ok
end