Class: ProjectsController

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

Instance Method Summary collapse

Methods inherited from ApplicationController

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

Instance Method Details

#collaboratorsObject


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

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


43
44
45
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
# File 'app/controllers/projects_controller.rb', line 43

def create
  # 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.has_role('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


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

def destroy
  @project.destroy

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

#editObject


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

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

#followObject


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

def follow
  @project = Project.find(params[:id])
  if current_user.has_role? 'follower', @project
    current_user.has_no_role 'follower', @project
    flash[:notice] = "You have stopped following the '#{@project.name}' project."
  else
    current_user.has_role 'follower', @project
    flash[:notice] = "You are now following the '#{@project.name}' project."
  end
  redirect_to project_path(@project)
end

#grant_roleObject


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

def grant_role
  @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.has_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]


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

def index
  @projects = Project.alphabetical.page(params[:page])

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

#newObject


28
29
30
31
32
33
34
35
36
# File 'app/controllers/projects_controller.rb', line 28

def new
  @project = Project.new

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

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

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

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

#remove_roleObject


146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/projects_controller.rb', line 146

def remove_role
  @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.has_no_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


20
21
22
23
24
25
26
# File 'app/controllers/projects_controller.rb', line 20

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

#updateObject


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

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