service_packs_controller.rb 2.42 KB
Newer Older
1
class ServicePacksController < ApplicationController
Khang Le's avatar
Khang Le committed
2
3
4
5
6
7
8
9
10
11
12
13
  # only allow admin
  before_action :require_admin

  # Specifying Layouts for Controllers, looking at OPENPROJECT_ROOT/app/views/layouts/admin
  layout 'admin'

  def index
    @service_packs = ServicePack.all
  end

  def new
    @service_pack = ServicePack.new
14
15
16
    # TimeEntryActivity.shared.count.times {@service_pack.mapping_rates.build}
    @sh = TimeEntryActivity.shared
    @c = TimeEntryActivity.shared.count
Khang Le's avatar
Khang Le committed
17
18
19
20
  end

  def show
    @service_pack = ServicePack.find(params[:id])
21
22
    # controller chooses not to get the thresholds.
    # assume the service pack exists.
23
    # TODO: make a separate action JSON only.
24
    respond_to do |format|
25
26
27
28
      format.json {
        render plain: ServicePackPresenter.new(@service_pack).json_export(:rate)
      }
      format.html {
29
        @rates = @service_pack.mapping_rates
30
31
        @assignments = @service_pack.assigns.where(assigned: true).all
      }
32
    end
Khang Le's avatar
Khang Le committed
33
34
35
  end

  def create
36
37
38
39
40
41
42
    # @service_pack = ServicePack.new(service_pack_params)
    # if @service_pack.save
    #   redirect_to @service_pack
    # else
    #   render 'new'
    # end
    mapping_rate_attribute = params['service_pack']['mapping_rates_attributes']
43
    # binding.pry
44
45
    activity_id = []
    mapping_rate_attribute.each {|_index, hash_value| activity_id.push(hash_value['activity_id'])}
46

47
    if activity_id.uniq.length == activity_id.length
48
      @service_pack = ServicePack.new(service_pack_params)
49
50
51
52
53
      # 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
Tam Le's avatar
Tam Le committed
54
        flash.now[:error] = 'Service Pack creation failed.'
55
56
57
        @service_pack = ServicePack.new
        render 'new'
      end
Khang Le's avatar
Khang Le committed
58
    else
59
      # render plain: 'duplicated'
Tam Le's avatar
Tam Le committed
60
      flash.now[:error] = 'Only one rate can be defined to one activity.'
61
62
      @service_pack = ServicePack.new
      render 'new'
Khang Le's avatar
Khang Le committed
63
64
65
    end
  end

Kiet's avatar
Kiet committed
66
67
68
69
70
  def edit
    @sp = ServicePack.find(params[:id])
    @activity = @sp.time_entry_activities.build
  end

Kiet's avatar
Kiet committed
71
72
73
74
75
76
77
  def destroy
    @sp = ServicePack.find(params[:id])
    @sp.destroy

    redirect_to service_packs_path
  end

Khang Le's avatar
Khang Le committed
78
79
80
  private

  def service_pack_params
81
    params.require(:service_pack).permit(:name, :total_units, :started_date, :expired_date, :threshold1, :threshold2, mapping_rates_attributes: [:id, :activity_id, :service_pack_id, :units_per_hour, :_destroy])
Khang Le's avatar
Khang Le committed
82
  end
83

84
end