Class: ServicePacksController

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

Instance Method Summary collapse

Instance Method Details

#createObject

The string with the minus sign in front is a shorthand for <string>.freeze reducing server processing time (and testing time) by 30%! Freezing a string literal will stop it from being created anew over and over. All literal strings will be frozen in Ruby 3 by default, which is a good idea.



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/service_packs_controller.rb', line 47

def create
  mapping_rate_attribute = params[:service_pack][:mapping_rates_attributes]
  # binding.pry
  activity_id = []
  mapping_rate_attribute.each {|_index, hash_value| activity_id.push(hash_value[:activity_id])}

  if activity_id.uniq.length == activity_id.length
    @service_pack = ServicePack.new(service_pack_params)
    # render plain: 'not duplicated'
    if @service_pack.save
      flash[:notice] = -'Service Pack creation successful.'
      redirect_to action: :show, id: @service_pack.id and return
    else
      flash.now[:error] = -'Service Pack creation failed.'
    end
  else
    # render plain: 'duplicated'
    flash.now[:error] = -'Only one rate can be defined to one activity.'
  end
  # the only successful path has returned 10 lines ago.
  @sh = TimeEntryActivity.shared
  @c = TimeEntryActivity.shared.count
  render 'new'
end

#default_breadcrumbObject



131
132
133
# File 'app/controllers/service_packs_controller.rb', line 131

def default_breadcrumb
  action_name == 'index' ? -'Service Packs' : ActionController::Base.helpers.link_to(-'Service Packs', service_packs_path)
end

#destroyObject



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

def destroy
  @sp = ServicePack.find_by(id: params[:id])
  if @sp.nil?
    flash[:error] = -"Service Pack not found"
    redirect_to action: :index and return
  end
  if @sp.assigned?
    flash.now[:error] = "Please unassign this SP from all projects before proceeding!"
    redirect_to @sp
  end
  @sp.destroy!

  redirect_to service_packs_path
end

#editObject



72
73
74
75
76
77
78
79
# File 'app/controllers/service_packs_controller.rb', line 72

def edit
  @sp = ServicePack.find_by(id: params[:id])
  if @sp.nil?
    flash[:error] = -"Service Pack not found"
    redirect_to action: :index and return
  end
  # @activity = @sp.time_entry_activities.build
end

#indexObject



8
9
10
11
12
# File 'app/controllers/service_packs_controller.rb', line 8

def index
  @service_packs = ServicePack.all
  # for demo
  #ServicePacksMailer.notify_under_threshold1(User.first,@service_packs.first).deliver_now
end

#newObject



14
15
16
17
18
19
# File 'app/controllers/service_packs_controller.rb', line 14

def new
  @service_pack = ServicePack.new
  # TimeEntryActivity.shared.count.times {@service_pack.mapping_rates.build}
  @sh = TimeEntryActivity.shared
  @c = TimeEntryActivity.shared.count
end

#showObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/controllers/service_packs_controller.rb', line 21

def show
  @service_pack = ServicePack.find(params[:id])
  # controller chooses not to get the thresholds.
  # assume the service pack exists.
  # TODO: make a separate action JSON only.
  respond_to do |format|
    format.json {
      # the function already converted this to json
      render plain: ServicePackPresenter.new(@service_pack).json_export(:rate), status: 200
    }
    format.html {
      # http://www.chrisrolle.com/en/blog/benchmark-preload-vs-eager_load
      @rates = @service_pack.mapping_rates.preload(:activity)
      @assignments = @service_pack.assignments.preload(:project)
    }
    format.csv {
      render csv: ServicePackReport.new(@service_pack).call, filename: "service_pack_#{@service_pack.name}.csv"
    }
  end
end

#show_local_breadcrumbObject

for breadcrumb code



127
128
129
# File 'app/controllers/service_packs_controller.rb', line 127

def show_local_breadcrumb
  true
end

#statisticsObject

:Docs

  • Limit: Serving JSON only. Must be Admin to access.

  • Purpose:

Return a table with consumed units for a Service Pack grouped by activities and sorted from large to small.

  • Expected Inputs:

[service_pack_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, consumed]

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

  • act_name: Name of activity

Status: 200

  • When raising error

HTTP 404: SP not found HTTP 400: Malformed request.



159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'app/controllers/service_packs_controller.rb', line 159

def statistics
  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
  if !ServicePack.find_by(id: params[:service_pack_id])
    render json: {error: 'NOT FOUND'}, status: 404 and return
  end

  # Notice: Change max(t3.name) to ANY_VALUE(t3.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 t3.pid AS act_id, 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
  SQL
  group_clause = <<-SQL
    GROUP BY t3.pid
    ORDER BY consumed DESC
  SQL
  where_clause = "WHERE t1.service_pack_id = ?"
  where_clause << (start_day.nil? ? '' : ' AND t1.created_at BETWEEN ? AND ?')
  query = body_query + where_clause + group_clause
  # binding.pry
  par = start_day.nil? ? [query, params[:service_pack_id]] : [query, params[:service_pack_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

#updateObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/controllers/service_packs_controller.rb', line 81

def update
  @sp = ServicePack.find_by(id: params[:id])
  if @sp.nil?
    flash[:error] = -"Service Pack not found"
    redirect_to action: :index and return
  end

  mapping_rate_attribute = params[:service_pack][:mapping_rates_attributes]
  activity_id = []
  mapping_rate_attribute.each {|_index, hash_value| activity_id.push(hash_value[:activity_id])}

  if activity_id.uniq.length == activity_id.length
    # No duplication
    add_units
    @sp.assign_attributes(service_pack_edit_params)
    # binding.pry
    if @sp.save
      flash[:notice] = -'Service Pack update successful.'
      redirect_to @sp
    else
      flash.now[:error] = -'Service Pack update failed.'
      render -'edit'
    end
  else
    # Duplication
    flash.now[:error] = -'Only one rate can be defined to one activity.'
    render -'edit'
  end
end