aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/referentials_controller.rb
blob: fe661651e5231e67197b9b97a032916e736f8eb0 (plain)
1
2
3
4
5
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
33
34
35
36
37
38
39
40
41
42
43
44
45
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
76
77
78
79
80
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
110
111
112
113
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
157
158
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
class ReferentialsController < ChouetteController
  defaults :resource_class => Referential
  before_action :load_workbench
  include PolicyChecker

  respond_to :html
  respond_to :json, :only => :show
  respond_to :js, :only => :show

  before_action :check_cloning_source_is_accessible, only: %i(new create)

  def new
    new! do
      build_referential
    end
  end

  def create
    create! do |success, failure|
      success.html do
        if @referential.created_from_id.present?
          flash[:notice] = t('notice.referentials.duplicate')
        end
        redirect_to workbench_path(@referential.workbench)
      end
      failure.html do
        Rails.logger.info "Can't create Referential : #{@referential.errors.inspect}"
        render :new
      end
    end
  end

  def show
    resource.switch
    show! do |format|
      @referential = @referential.decorate()
      @reflines = lines_collection.paginate(page: params[:page], per_page: 10)
      @reflines = ReferentialLineDecorator.decorate(
        @reflines,
        context: {
          referential: referential,
          current_organisation: current_organisation
        }
      )

      format.json {
        render :json => { :lines_count => resource.lines.count,
               :networks_count => resource.networks.count,
               :vehicle_journeys_count => resource.vehicle_journeys.count + resource.vehicle_journey_frequencies.count,
               :time_tables_count => resource.time_tables.count,
               :referential_id => resource.id}
      }
    end
  end

  def edit
    edit! do
      if @referential.in_workbench?
        @referential.init_metadatas default_date_range: Range.new(Date.today, Date.today.advance(months: 1))
      end
    end
  end

  def select_compliance_control_set
    @compliance_control_sets = ComplianceControlSet.where(organisation: current_organisation)
  end

  def validate
    ComplianceControlSetCopyWorker.perform_async(params[:compliance_control_set], params[:id])
    flash[:notice] = t('notice.referentials.validate')
    redirect_to workbench_compliance_check_sets_path(referential.workbench_id)
  end

  def destroy
    workbench = referential.workbench_id

    referential.destroy!
    redirect_to workbench_path(workbench), notice: t('notice.referential.deleted')
  end

  def archive
    referential.archive!
    redirect_to workbench_path(referential.workbench_id), notice: t('notice.referential.archived')
  end

  def unarchive
    if referential.unarchive!
      flash[:notice] = t('notice.referential.unarchived')
    else
      flash[:alert] = t('notice.referential.unarchived_failed')
    end

    redirect_to workbench_path(referential.workbench_id)
  end

  protected

  alias_method :referential, :resource
  alias_method :current_referential, :referential
  helper_method :current_referential

  def resource
    @referential ||= current_organisation.find_referential(params[:id]).decorate
  end

  def collection
    @referentials ||= current_organisation.referentials.order(:name)
  end

  def lines_collection
    @q = resource.lines.includes(:company, :network).search(params[:q])

    if sort_column && sort_direction
      @reflines ||=
        begin
          reflines = @q.result(distinct: true).order(sort_column + ' ' + sort_direction)
          reflines = reflines.paginate(page: params[:page], per_page: 10)
          reflines
        end
    else
      @reflines ||=
        begin
          reflines = @q.result(distinct: true).order(:name)
          reflines = reflines.paginate(page: params[:page], per_page: 10)
          reflines
        end
    end
  end

  def build_resource
    super.tap do |referential|
      referential.user_id = current_user.id
      referential.user_name = current_user.name
    end
  end

  def create_resource(referential)
    referential.organisation = current_organisation
    referential.ready = true
    super
  end

  def build_referential
    if params[:from]
      source_referential = Referential.find(params[:from])
      @referential = Referential.new_from(source_referential, current_organisation)
    end

    @referential.data_format = current_organisation.data_format
    @referential.workbench_id ||= params[:workbench_id]

    if @referential.in_workbench?
      @referential.init_metadatas default_date_range: Range.new(Date.today, Date.today.advance(months: 1))
    end
  end

  private
  def sort_column
    sortable_columns = Chouette::Line.column_names + ['networks.name', 'companies.name']
    params[:sort] if sortable_columns.include?(params[:sort])
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ?  params[:direction] : 'asc'
  end

  def referential_params
    params.require(:referential).permit(
      :id,
      :name,
      :organisation_id,
      :data_format,
      :archived_at,
      :created_from_id,
      :workbench_id,
      metadatas_attributes: [:id, :first_period_begin, :first_period_end, periods_attributes: [:begin, :end, :id, :_destroy], :lines => []]
    )
  end

  def check_cloning_source_is_accessible
    return unless params[:from]
    source = Referential.find params[:from]
    return user_not_authorized unless current_user.organisation.workgroups.include?(source.workbench.workgroup)
  end

  def load_workbench
    @workbench ||= Workbench.find(params[:workbench_id]) if params[:workbench_id]
    @workbench ||= resource&.workbench if params[:id]
    @workbench
  end

  alias_method :current_workbench, :load_workbench
  helper_method :current_workbench
end