Class: AffiliationsController

Inherits:
ApplicationController show all
Defined in:
app/controllers/affiliations_controller.rb

Instance Method Summary (collapse)

Methods inherited from ApplicationController

#authorize_customer!, #authorize_provider!

Instance Method Details

- (Object) create



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/affiliations_controller.rb', line 16

def create

  if current_user.is_a? Provider
    @affiliation = Affiliation.new(
      provider_id: current_user.id,
      customer_id: params[:affiliation][:customer_id]
    )
  else
    @affiliation = Affiliation.new(
      provider_id: params[:affiliation][:provider_id],
      customer_id: current_user.id
    )
  end

  if @affiliation.save
    redirect_to affiliations_path
  else
    # Provider tried to save an invalid Affiliation (perhaps a duplicate)
    redirect_to affiliations_path, status: 403   # Forbidden action occurred
  end
end

- (Object) destroy



38
39
40
41
# File 'app/controllers/affiliations_controller.rb', line 38

def destroy
  current_user.affiliations.find(params[:id]).destroy!
  redirect_to affiliations_path, success: 'Affiliation deleted successfully.'
end

- (Object) index



5
6
7
# File 'app/controllers/affiliations_controller.rb', line 5

def index
  @affiliations = current_user.affiliations
end

- (Object) new



9
10
11
12
13
14
# File 'app/controllers/affiliations_controller.rb', line 9

def new
  @affiliation = Affiliation.new
  if current_user.is_a? Customer and params[:provider_id]
   @providers = Provider.where(id: params[:provider_id])
  end
end

- (Object) update



43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/affiliations_controller.rb', line 43

def update
  @affiliation = Affiliation.find(params[:id])
  # Only the AffiliationState can be changed, which in turn can only be set by a Provider
  if current_user.id == @affiliation.provider_id
    @affiliation.affiliation_state_id = params[:affiliation_state_id]
    @affiliation.save
    redirect_to affiliations_path
  else
    render file: '/public/404.html', status: 404
  end
end