Class: ProjectMembersController

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

Instance Method Summary collapse

Methods included from RequestType

#json_request?

Methods included from LogRecent

#log_user_recent_route

Methods included from Cookies

#digest_cookie, #digested_cookie_exists?

Methods included from Whitelist

#whitelist_constantize

Methods included from ProjectsHelper

#invalid_object, #project_link, #project_matches, #project_tag, #projects_list, #projects_search_form

Methods included from Api::Intercept

#intercept_api

Instance Method Details

#clipboardObject



87
88
89
90
91
92
93
# File 'app/controllers/project_members_controller.rb', line 87

def clipboard
  if @project_member = sessions_current_user.project_members.where(project_id: sessions_current_project_id).first
    render :show
  else
    render json: {}
  end
end

#createObject

POST /otus



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/controllers/project_members_controller.rb', line 27

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

#create_manyObject

POST /project_members/create_many



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/controllers/project_members_controller.rb', line 42

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

#destroyObject

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



97
98
99
100
101
102
103
# File 'app/controllers/project_members_controller.rb', line 97

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

#editObject

GET /project_members/1/edit



23
24
# File 'app/controllers/project_members_controller.rb', line 23

def edit
end

#indexObject

GET /project_members.json



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

def index
  @project_members = ProjectMember.joins(:user).where(project_id: sessions_current_project_id).order('users.name ASC')
end

#many_newObject



58
59
60
61
# File 'app/controllers/project_members_controller.rb', line 58

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

#newObject

GET /project_members/new



17
18
19
20
# File 'app/controllers/project_members_controller.rb', line 17

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

#project_id_paramObject (private)



131
132
133
# File 'app/controllers/project_members_controller.rb', line 131

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

#project_member_paramsObject (private)



123
124
125
# File 'app/controllers/project_members_controller.rb', line 123

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

#project_members_paramsObject (private)



127
128
129
# File 'app/controllers/project_members_controller.rb', line 127

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

#set_available_usersObject (private)



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

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

#set_form_variablesObject (private)



111
112
113
# File 'app/controllers/project_members_controller.rb', line 111

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

#set_member_projectObject (private)



119
120
121
# File 'app/controllers/project_members_controller.rb', line 119

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

#set_project_memberObject (private)



115
116
117
# File 'app/controllers/project_members_controller.rb', line 115

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

#updateObject

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



65
66
67
68
69
70
71
72
73
74
75
# File 'app/controllers/project_members_controller.rb', line 65

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

#update_clipboardObject

PATCH /project_members/1/update_clipboard.json



78
79
80
81
82
83
84
85
# File 'app/controllers/project_members_controller.rb', line 78

def update_clipboard
  @project_member = sessions_current_user.project_members.where(project_id: sessions_current_project_id).first  
  if @project_member.update( params.require(:project_member).permit(clipboard: {}) )
    render :show, status: :ok, location: @project_member 
  else
    render json: @project_member.errors, status: :unprocessable_entity 
  end
end