Class: ProjectsController

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

Overview

rubocop:todo Metrics/ClassLength

Constant Summary

Constants included from FlashTruncation

FlashTruncation::STRING_OVERHEAD

Instance Method Summary collapse

Methods inherited from ApplicationController

#block_api_access, #evil_parameter_hack!, #extract_header_info, #set_cache_disabled!

Methods included from FlashTruncation

#max_flash_size, #truncate_flash, #truncate_flash_array

Instance Method Details

#collaboratorsObject



105
106
107
108
109
110
# File 'app/controllers/projects_controller.rb', line 105

def collaborators
  @project = Project.find(params[:id])
  @all_roles = %w[owner follower manager]
  @roles = Role.where(authorizable_id: @project.id, authorizable_type: 'Project')
  @users = User.order(:first_name)
end

#createObject

rubocop:todo Metrics/AbcSize, Metrics/MethodLength



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/projects_controller.rb', line 46

def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  # TODO[5002667]: All of this code should be in a before_create/after_create callback in the Project model ...
  @project = Project.new(params[:project])
  @project.save!

  current_user.grant_manager(@project)

  # Creates an event when a new Project is created
  EventFactory.new_project(@project, current_user)

  # TODO[5002667]: ... to here.

  flash[:notice] = 'Your project has been created'
  respond_to do |format|
    format.html { redirect_to project_path(@project) }
    format.xml { render xml: @project, status: :created, location: @project }
    format.json { render json: @project, status: :created, location: @project }
  end
rescue ActiveRecord::RecordInvalid => e
  flash.now[:error] = 'Problems creating your new project'
  respond_to do |format|
    format.html { render action: 'new' }
    format.xml { render xml: @project.errors, status: :unprocessable_entity }
    format.json { render json: @project.errors, status: :unprocessable_entity }
  end
end

#destroyObject



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

def destroy
  @project.destroy

  respond_to do |format|
    format.html { redirect_to(projects_url) }
    format.xml { head :ok }
  end
end

#editObject



41
42
43
44
# File 'app/controllers/projects_controller.rb', line 41

def edit
  @project = Project.find(params[:id])
  @users = User.all
end

#followObject

rubocop:todo Metrics/AbcSize



112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/projects_controller.rb', line 112

def follow # rubocop:todo Metrics/AbcSize
  @project = Project.find(params[:id])
  if current_user.follower_of?(@project)
    current_user.remove_role 'follower', @project
    flash[:notice] = "You have stopped following the '#{@project.name}' project."
  else
    current_user.grant_follower(@project)
    flash[:notice] = "You are now following the '#{@project.name}' project."
  end
  redirect_to project_path(@project)
end

#grant_roleObject

rubocop:todo Metrics/MethodLength



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/controllers/projects_controller.rb', line 125

def grant_role # rubocop:todo Metrics/AbcSize
  @user = User.find(params[:role][:user])
  @project = Project.find(params[:id])
  @role = Role.find_by(name: params[:role][:authorizable_type])

  if request.xhr?
    if params[:role]
      @user.grant_role(params[:role][:authorizable_type].to_s, @project)
      @roles = @project.roles
      flash[:notice] = 'Role added'
      render partial: 'roles', status: 200
    else
      @roles = @project.roles
      flash[:error] = 'A problem occurred while adding the role'
      render partial: 'roles', status: 500
    end
  else
    @roles = @project.roles
    flash[:error] = 'A problem occurred while adding the role'
    render partial: 'roles', status: 401
  end
end

#indexObject

TODO: before_action :redirect_if_not_owner_or_admin, :only => [:create, :update, :destroy, :edit, :new]



12
13
14
15
16
17
18
19
20
# File 'app/controllers/projects_controller.rb', line 12

def index
  @projects = Project.alphabetical

  respond_to do |format|
    format.html
    format.xml { render xml: Project.alphabetical }
    format.json { render json: Project.alphabetical }
  end
end

#newObject



31
32
33
34
35
36
37
38
39
# File 'app/controllers/projects_controller.rb', line 31

def new
  @project = Project.new

  respond_to do |format|
    format.html
    format.xml { render xml: @project }
    format.json { render json: @project }
  end
end


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

def related_studies
  @project = Project.find(params[:id])
  @studies = @project.studies

  respond_to do |format|
    format.html
    format.xml
  end
end

#remove_roleObject

rubocop:todo Metrics/MethodLength



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
# File 'app/controllers/projects_controller.rb', line 151

def remove_role # rubocop:todo Metrics/AbcSize
  @user = User.find(params[:role][:user])
  @project = Project.find(params[:id])
  @role = Role.find_by(name: params[:role][:authorizable_type])

  if request.xhr?
    if params[:role]
      @user.remove_role(params[:role][:authorizable_type].to_s, @project)
      @roles = @project.roles
      flash[:error] = 'Role was removed'
      render partial: 'roles', status: 200
    else
      @roles = @project.roles
      flash[:error] = 'A problem occurred while removing the role'
      render partial: 'roles', status: 500
    end
  else
    @roles = @project.roles
    flash[:error] = 'A problem occurred while removing the role'
    render partial: 'roles', status: 401
  end
end

#showObject



22
23
24
25
26
27
28
29
# File 'app/controllers/projects_controller.rb', line 22

def show
  @page_name = @project.name
  respond_to do |format|
    format.html
    format.xml
    format.json { render json: @project }
  end
end

#updateObject



73
74
75
76
77
78
79
80
81
82
83
84
# File 'app/controllers/projects_controller.rb', line 73

def update
  respond_to do |format|
    if @project.update(params[:project])
      flash[:notice] = 'Project was successfully updated.'
      format.html { redirect_to(@project) }
      format.xml { head :ok }
    else
      format.html { render action: 'edit' }
      format.xml { render xml: @project.errors, status: :unprocessable_entity }
    end
  end
end