Class: ProjectMembersController

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

Instance Attribute Summary

Attributes inherited from ApplicationController

#is_data_controller, #is_task_controller, #meta_data, #meta_description, #meta_keywords, #meta_title, #page_title, #site_name

Instance Method Summary (collapse)

Methods inherited from ApplicationController

#clear_project_and_user_variables, #digest_cookie, #digested_cookie_exists?, #disable_turbolinks, #intercept_api, #invalid_object, #is_data_controller?, #is_task_controller?, #log_user_recent_route, #notice_user, #project_matches, #record_not_found, #set_project_and_user_variables, #set_project_from_params, #token_authenticate

Methods included from ProjectsHelper

#project_link, #project_tag, #projects_list, #projects_search_form

Instance Method Details

- (Object) create

POST /otus



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/project_members_controller.rb', line 20

def create
  @project_member = ProjectMember.new(project_member_params)

  respond_to do |format|
    if @project_member.save
      format.html { redirect_to project_path(@project_member.project),
                    notice: "User #{@project_member.user.name}' was successfully added to #{@project_member.project.name}." }
    else
      set_available_users
      format.html { render action: 'new' }
    end
  end
end

- (Object) create_many

POST /project_members/create_many



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'app/controllers/project_members_controller.rb', line 35

def create_many
  begin
    ApplicationRecord.transaction do
      project_members_params.each do |user_id|
        @member_project.project_members.create!(project_member_params.merge(user_id: user_id)) 
      end 
    end

    @project_member = ProjectMember.new(project_member_params)
    redirect_to project_path(@member_project), notice: "Project members were #{project_members_params.empty? ? 'not selected.' : 'added to project.'}"

  rescue ActiveRecord::RecordInvalid
    redirect_to many_new_project_members_path, notice: "There was a problem adding project members, none were added." 
  end
end

- (Object) destroy

DELETE /project_members/1 DELETE /project_members/1.json



72
73
74
75
76
77
78
# File 'app/controllers/project_members_controller.rb', line 72

def destroy
  @project_member.destroy
  respond_to do |format|
    format.html { redirect_to project_path(@project_member.project), notice: 'Project member was successfully destroyed.' }
    format.json { head :no_content }
  end
end

- (Object) edit

GET /project_members/1/edit



16
17
# File 'app/controllers/project_members_controller.rb', line 16

def edit
end

- (Object) many_new



51
52
53
54
# File 'app/controllers/project_members_controller.rb', line 51

def many_new
  @project_member = ProjectMember.new(project_member_params)
  redirect_to project_path(@project_member.project), alert: 'There are no additional users available to add to this project.' if !@available_users.any?
end

- (Object) new

GET /project_members/new



10
11
12
13
# File 'app/controllers/project_members_controller.rb', line 10

def new
  @project_member = ProjectMember.new(project_member_params)
  redirect_to project_path(@project_member.project), alert: 'There are no additional users available to add to this project.' if !@available_users.any?
end

- (Object) project_id_param (private)



106
107
108
# File 'app/controllers/project_members_controller.rb', line 106

def project_id_param
  params.require(:project_member).permit(:project_id)[:project_id]
end

- (Object) project_member_params (private)



98
99
100
# File 'app/controllers/project_members_controller.rb', line 98

def project_member_params
  params.require(:project_member).permit(:project_id, :is_project_administrator, :user_id)
end

- (Object) project_members_params (private)



102
103
104
# File 'app/controllers/project_members_controller.rb', line 102

def project_members_params
  params.require(:project_member).permit(user_ids: [])[:user_ids] || []
end

- (Object) set_available_users (private)



82
83
84
# File 'app/controllers/project_members_controller.rb', line 82

def set_available_users
  @available_users = User.all.not_in_project(@member_project).order(:name)
end

- (Object) set_form_variables (private)



86
87
88
# File 'app/controllers/project_members_controller.rb', line 86

def set_form_variables
  @target_letters = @available_users.collect {|u| u.name[0].upcase}.uniq
end

- (Object) set_member_project (private)



94
95
96
# File 'app/controllers/project_members_controller.rb', line 94

def set_member_project
  @member_project = Project.find(project_id_param)
end

- (Object) set_project_member (private)



90
91
92
# File 'app/controllers/project_members_controller.rb', line 90

def set_project_member
  @project_member = ProjectMember.find(params[:id])
end

- (Object) update

PATCH/PUT /project_members/1 PATCH/PUT /project_members/1.json



58
59
60
61
62
63
64
65
66
67
68
# File 'app/controllers/project_members_controller.rb', line 58

def update
  respond_to do |format|
    if @project_member.update(project_member_params)
      format.html { redirect_to  project_path(@project_member.project), notice: 'Project member was successfully updated.' }
      format.json { render :show, status: :ok, location: @project_member }
    else
      format.html { render :edit }
      format.json { render json: @project_member.errors, status: :unprocessable_entity }
    end
  end
end