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,
  'all' => :all
}.freeze

Instance Method Summary collapse

Instance Method Details

#editObject



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

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], :all)

  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



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

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

#managed_updateObject

rubocop:todo Metrics/MethodLength



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
# File 'app/controllers/admin/projects_controller.rb', line 60

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



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

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

#sortObject

rubocop:enable Metrics/MethodLength



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

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



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

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