Class: Admin::ProjectsController

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

Constant Summary collapse

BY_SCOPES =
{
  'not approved' => :unapproved,
  'unallocated division' => :with_unallocated_budget_division,
  'unallocated manager' => :with_unallocated_manager
}

Instance Method Summary collapse

Instance Method Details

#editObject


34
35
36
37
38
39
40
41
42
# File 'app/controllers/admin/projects_controller.rb', line 34

def edit
  @request_types = RequestType.order(name: :asc)
  if params[:id] != '0'
    @project = Project.find(params[:id])
    render partial: 'edit', locals: { project: @project }
  else
    render nothing: true
  end
end

#filterObject


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/admin/projects_controller.rb', line 44

def filter
  filters = params[:filter] || {}

  by_scope = BY_SCOPES[filters[:by]]

  base_scope = Project.send(by_scope).in_state(filters[:status]).alphabetical

  # arel_table is used to generate the more complex like query
  projects = Project.arel_table
  scope = filters[:q].present? ? base_scope.where(projects[:name].matches("%#{filters[:q]}%")) : base_scope

  @projects = scope

  render partial: 'filtered_projects'
end

#indexObject


19
20
21
22
# File 'app/controllers/admin/projects_controller.rb', line 19

def index
  @projects = Project.alphabetical
  @request_types = RequestType.alphabetical
end

#managed_updateObject


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/admin/projects_controller.rb', line 60

def managed_update
  @project = Project.find(params[:id])
  redirect_if_not_owner_or_admin(@project)

  unless params[:project][:uploaded_data].blank?
    document_settings = {}
    document_settings[:uploaded_data] = params[:project][:uploaded_data]
    doc = Document.create(document_settings)
    doc.documentable = @project
    doc.save
  end
  params[:project].delete(:uploaded_data)

  pre_approved = @project.approved?

  if @project.update(params[:project])
    if pre_approved == false && @project.approved == true
      EventFactory.project_approved(@project, current_user)
    end

    flash[:notice] = 'Your project has been updated'
    redirect_to controller: 'admin/projects', action: 'update', id: @project.id
  else
    logger.warn "Failed to update attributes: #{@project.errors.map { |e| e.to_s }}"
    flash[:error] = 'Failed to update attributes for project!'
    render action: :show, id: @project.id and return
  end
end

#showObject


24
25
26
# File 'app/controllers/admin/projects_controller.rb', line 24

def show
  @project = Project.find(params[:id])
end

#sortObject


89
90
91
92
93
94
95
96
97
# File 'app/controllers/admin/projects_controller.rb', line 89

def sort
  @projects = Project.all.sort_by { |project| project.name }
  if params[:sort] == 'date'
    @projects = @projects.sort_by { |project| project.created_at }
  elsif params[:sort] == 'owner'
    @projects = @projects.sort_by { |project| project.user_id }
  end
  render partial: 'projects'
end

#updateObject


28
29
30
31
32
# File 'app/controllers/admin/projects_controller.rb', line 28

def update
  @project = Project.find(params[:id])
  flash[:notice] = 'Your project has been updated'
  render partial: 'manage_single_project'
end