Class: Admin::ProjectsController

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

Overview

rubocop:todo Style/Documentation

Constant Summary collapse

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

Instance Method Summary collapse

Instance Method Details

#editObject


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

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

#filterObject

rubocop:todo Metrics/AbcSize


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/controllers/admin/projects_controller.rb', line 39

def filter # rubocop:todo Metrics/AbcSize
  filters = params[:filter] || {}

  by_scope = BY_SCOPES.fetch(filters[:by], :scoped)

  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


15
16
17
18
# File 'app/controllers/admin/projects_controller.rb', line 15

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

#managed_updateObject

rubocop:todo Metrics/MethodLength


56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'app/controllers/admin/projects_controller.rb', line 56

def managed_update # rubocop:todo Metrics/AbcSize
  @project = Project.find(params[:id])
  authorize! :managed_update, @project

  if params[:project][:uploaded_data].present?
    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])
    EventFactory.project_approved(@project, current_user) if pre_approved == false && @project.approved == true

    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(&:to_s)}"
    flash[:error] = 'Failed to update attributes for project!'
    render action: :show, id: @project.id and return
  end
end

#showObject


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

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

#sortObject

rubocop:enable Metrics/MethodLength


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

def sort
  @projects = Project.all.sort_by(&:name)
  case params[:sort]
  when 'date'
    @projects = @projects.sort_by(&:created_at)
  when 'owner'
    @projects = @projects.sort_by(&:user_id)
  end
  render partial: 'projects'
end

#updateObject


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

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