Class: AssignsController

Inherits:
ApplicationController
  • Object
show all
Includes:
SPAssignmentManager
Defined in:
app/controllers/assigns_controller.rb

Instance Method Summary collapse

Methods included from SPAssignmentManager

#_unassign, #assign_to, #assigned?, #unassigned?

Instance Method Details

#assignObject



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/controllers/assigns_controller.rb', line 6

def assign
  # binding.pry
  return head 403 unless @can_assign = User.current.allowed_to?(:assign_service_packs, @project)

  if assigned?(@project)
    flash.now[:alert] = "You must unassign first!"
    render_400 and return
  end
  @service_pack = ServicePack.find_by(id: params[:assign][:service_pack_id])
  if @service_pack.nil?
    flash.now[:alert] = "Service Pack not found"
    render_404 and return
  end
  if @service_pack.available?
      # binding.pry
      assign_to(@service_pack, @project)
      flash.now[:notice] = "Service Pack '#{@service_pack.name}' successfully assigned to project '#{@project.name}'"
      render 'already_assigned' and return
  else
    # already assigned for another project
    # constraint need
    flash.now[:alert] = "Service Pack '#{@service_pack.name}' has been already assigned"
    render_400 and return
  end
  flash.now[:alert] = 'Service Pack cannot be assigned'
  redirect_to action: :show
end

#reportObject



81
82
83
84
85
86
87
88
# File 'app/controllers/assigns_controller.rb', line 81

def report
  return head 403 unless User.current.allowed_to?(:see_assigned_service_packs, @project)
  if assignment = assigned?(@project)
    render csv: ServicePackReport.new(assignment.service_pack).call(@project), filename: "ServicePackReport_#{@project.name.gsub(/\s+/, -'_')}.csv"
  else
    render_404
  end
end

#select_to_transferObject



77
78
79
# File 'app/controllers/assigns_controller.rb', line 77

def select_to_transfer
  return head 403 unless @can_assign = User.current.allowed_to?(:assign_service_packs, @project)
end

#showObject



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
72
73
74
75
# File 'app/controllers/assigns_controller.rb', line 46

def show
  # This will lock even admins out if the module is not activated.
  return head 403 unless 
  User.current.allowed_to?(:see_assigned_service_packs, @project) ||
  (@can_assign = User.current.allowed_to?(:assign_service_packs, @project)) ||
  (@can_unassign = User.current.allowed_to?(:unassign_service_packs, @project))
  # binding.pry
  if @assignment = @project.assigns.find_by(assigned: true)
    if @assignment.service_pack.unavailable?
      @assignment.terminate
      @assignment = nil # signifying no assignments are in effect
      # as the single one is terminated.
    end
  end
  # binding.pry
  if @assignment.nil?
    if @can_assign ||= User.current.allowed_to?(:assign_service_packs, @project)
      @assignables = ServicePack.availables
      if @assignables.exists?
        @assignment = Assign.new
        render -'not_assigned_yet' and return
      end
    end
    render -'unassignable'
    # binding.pry
  else
    @service_pack = @assignment.service_pack
    render -'already_assigned'
  end
end

#statisticsObject

:Docs

  • Limit: Serving JSON only. Intended to restrict to :show.

  • Purpose:

Return a table with consumed units by a Project grouped by service pack, then activities and sorted from large to small.

  • Expected Inputs:

[project_id]: Sharing the same route with the resourceful default. Put in the link. Mandatory. [start_period]: Beginning of the counting period. As a date. Optional. [end_period]: Ending of the counting period. As a date. Optional. start_period MUST NOT be later than end_period. Both or none of [start_period, end_period] can be present.

  • Expected Outputs

Top class: None Content: Array of object having [name, act_name, consumed]

  • Name: Name of Service Pack

  • act_name: Name of activity

  • consumed: How many units are consumed (in given period)

Status: 200

  • When raising error

HTTP 400: Malformed request.



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/controllers/assigns_controller.rb', line 114

def statistics
  return head 403 unless 
  User.current.allowed_to?(:see_assigned_service_packs, @project) ||
  User.current.allowed_to?(:assign_service_packs, @project) ||
  User.current.allowed_to?(:unassign_service_packs, @project)
  
  start_day = params[:start_period]&.to_date # ruby >= 2.3.0
  end_day = params[:end_period]&.to_date
  if start_day.nil? ^ end_day.nil?
    render json: { error: 'GET OUT!'}, status: 400 and return
  end

  # Notice: Change max(t4.name) to ANY_VALUE(t4.name) on production builds.
  # MySQL specific >= 5.7.5
  # https://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html

  get_parent_id = <<-SQL
    SELECT id, name, COALESCE(parent_id, id) AS pid
    FROM #{TimeEntryActivity.table_name}
    WHERE type = 'TimeEntryActivity'
    SQL
  body_query = <<-SQL
    SELECT t1.service_pack_id AS spid, max(t4.name) AS name, t3.pid AS pid,
    max(t3.name) AS act_name, sum(t1.units) AS consumed
    FROM #{ServicePackEntry.table_name} t1
    INNER JOIN #{TimeEntry.table_name} t2
    ON t1.time_entry_id = t2.id
    INNER JOIN (#{get_parent_id}) t3
    ON t2.activity_id = t3.id
    INNER JOIN #{ServicePack.table_name} t4
    ON t1.service_pack_id = t4.id
    SQL
  group_clause = <<-SQL
    GROUP BY t1.service_pack_id, t3.pid
    ORDER BY consumed DESC
    SQL
  where_clause = "WHERE t2.project_id = ?"
  where_clause << (start_day.nil? ? '' : ' AND t1.created_at BETWEEN ? AND ?')
  query = body_query + where_clause + group_clause
  par = start_day.nil? ? [query, params[@project.id]] : [query, params[@project.id], start_day, end_day]
  sql = ActiveRecord::Base.send(:sanitize_sql_array, par)
  render json: ActiveRecord::Base.connection.exec_query(sql).to_hash, status: 200
end

#unassignObject



34
35
36
37
38
39
40
41
42
43
44
# File 'app/controllers/assigns_controller.rb', line 34

def unassign
  return head 403 unless @can_unassign = User.current.allowed_to?(:unassign_service_packs, @project)

  if unassigned?(@project)
    flash[:alert] = 'No Service Pack is assigned to this project'
    render_404 and return
  end
  _unassign(@project)
  flash[:notice] = 'Unassigned a Service Pack from this project'
  redirect_to action: :show and return
end