Class: CollavrePlan::PlansController

Inherits:
ApplicationController show all
Defined in:
app/controllers/collavre_plan/plans_controller.rb

Instance Method Summary collapse

Instance Method Details

#createObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/controllers/collavre_plan/plans_controller.rb', line 35

def create
  @plan = Collavre::Plan.new(plan_params)
  @plan.owner = Current.user
  if @plan.save
    respond_to do |format|
      format.html do
        redirect_back fallback_location: main_app.root_path, notice: t("collavre.plans.created")
      end
      format.json do
        render json: plan_json(@plan), status: :created
      end
    end
  else
    respond_to do |format|
      format.html do
        flash[:alert] = @plan.errors.full_messages.join(", ")
        redirect_back fallback_location: main_app.root_path
      end
      format.json do
        render json: { errors: @plan.errors.full_messages }, status: :unprocessable_entity
      end
    end
  end
end

#destroyObject

Raises:

  • (ActiveRecord::RecordNotFound)


60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/controllers/collavre_plan/plans_controller.rb', line 60

def destroy
  @plan = Collavre::Plan.find_by(id: params[:id])
  raise ActiveRecord::RecordNotFound unless @plan && plan_editable_by_current_user?

  @plan.destroy
  respond_to do |format|
    format.html do
      redirect_back fallback_location: main_app.root_path,
                    notice: t("collavre.plans.deleted", default: "Plan deleted.")
    end
    format.json { head :no_content }
  end
end

#indexObject



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
# File 'app/controllers/collavre_plan/plans_controller.rb', line 3

def index
  center = if params[:date].present?
              Date.parse(params[:date]) rescue Date.current
  else
              Date.current
  end
  start_date = center - 30
  end_date = center + 30
  @plans = Collavre::Plan.joins(:creative)
               .where("target_date >= ? AND DATE(creatives.created_at) <= ?", start_date, end_date)
               .order(Arel.sql("DATE(creatives.created_at) ASC"))
               .select { |plan| plan.readable_by?(Current.user) }
  calendar_scope = Collavre::CalendarEvent.includes(:creative)
                                .where("DATE(start_time) <= ? AND DATE(end_time) >= ?", end_date, start_date)
                                .order(:start_time)
  events_in_scope = calendar_scope.to_a
  own_events = events_in_scope.select { |event| event.user_id == Current.user.id }
  shared_events = events_in_scope.reject { |event| event.user_id == Current.user.id }
                                 .select { |event| event.creative&.has_permission?(Current.user, :write) }
  @calendar_events = (own_events + shared_events).uniq.sort_by(&:start_time)
  respond_to do |format|
    format.html do
      render html: render_to_string(Collavre::PlansTimelineComponent.new(plans: @plans, calendar_events: @calendar_events), layout: false)
    end
    format.json do
      plan_jsons = @plans.map { |p| plan_json(p) }
      event_jsons = @calendar_events.map { |e| calendar_json(e) }
      render json: plan_jsons + event_jsons
    end
  end
end

#updateObject

Raises:

  • (ActiveRecord::RecordNotFound)


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
# File 'app/controllers/collavre_plan/plans_controller.rb', line 74

def update
  @plan = Collavre::Plan.find_by(id: params[:id])
  raise ActiveRecord::RecordNotFound unless @plan && plan_editable_by_current_user?

  if @plan.update(plan_update_params)
    respond_to do |format|
      format.html do
        redirect_back fallback_location: main_app.root_path,
                      notice: t("collavre.plans.updated", default: "Plan updated.")
      end
      format.json do
        render json: plan_json(@plan, creative_id: params[:creative_id] || @plan.creative_id), status: :ok
      end
    end
  else
    respond_to do |format|
      format.html do
        flash[:alert] = @plan.errors.full_messages.join(", ")
        redirect_back fallback_location: main_app.root_path
      end
      format.json do
        render json: { errors: @plan.errors.full_messages }, status: :unprocessable_entity
      end
    end
  end
end