aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers/referentials_controller.rb
blob: 937fd0b76628a220059ffb47d738ec943ab7d3a8 (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
class ReferentialsController < BreadcrumbController
  defaults :resource_class => Referential
  before_action :check_policy, :only => [:edit, :update]

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

  def new
    @referential = Referential.new_from(Referential.find(params[:from])) if params[:from]

    new! do
      @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
  end

  def show
     resource.switch
     show! do |format|
       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}
       }
       format.html { build_breadcrumb :show}
     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 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

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

  def collection
    @referentials ||= current_organisation.referentials.order(:name)
  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 unless referential.created_from
    super
  end

  private
  def check_policy
    authorize resource
  end

  def referential_params
    params.require(:referential).permit(
      :id,
      :name,
      :slug,
      :prefix,
      :time_zone,
      :upper_corner,
      :lower_corner,
      :organisation_id,
      :projection_type,
      :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

end