Class: CollavrePlan::PlansController
- Inherits:
-
ApplicationController
- Object
- Collavre::ApplicationController
- ApplicationController
- CollavrePlan::PlansController
- Defined in:
- app/controllers/collavre_plan/plans_controller.rb
Constant Summary collapse
- REGISTRATION_LIMIT =
Cap how many registration/modification markers we draw so a busy window never floods the timeline (or the JSON payload). Most recent within the window win.
300- MODIFICATION_LIMIT =
300
Instance Method Summary collapse
Instance Method Details
#create ⇒ Object
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'app/controllers/collavre_plan/plans_controller.rb', line 51 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..join(", ") redirect_back fallback_location: main_app.root_path end format.json do render json: { errors: @plan.errors. }, status: :unprocessable_entity end end end end |
#destroy ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/controllers/collavre_plan/plans_controller.rb', line 76 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 |
#index ⇒ Object
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 |
# File 'app/controllers/collavre_plan/plans_controller.rb', line 9 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&.(Current.user, :write) } @calendar_events = (own_events + shared_events).uniq.sort_by(&:start_time) # "Registered"/"Modified" chips are default-off, so their creatives are # only gathered when the client explicitly asks (lazy, zero cost normally). @show_registrations = ActiveModel::Type::Boolean.new.cast(params[:registrations]) == true @registrations = @show_registrations ? registration_creatives(start_date, end_date) : [] @show_modifications = ActiveModel::Type::Boolean.new.cast(params[:modifications]) == true @modifications = @show_modifications ? modification_creatives(start_date, end_date) : [] respond_to do |format| format.html do render html: render_to_string(Collavre::PlansTimelineComponent.new(plans: @plans, calendar_events: @calendar_events, registrations: @registrations, show_registrations: @show_registrations, modifications: @modifications, show_modifications: @show_modifications), layout: false) end format.json do plan_jsons = @plans.map { |p| plan_json(p) } event_jsons = @calendar_events.map { |e| calendar_json(e) } registration_jsons = @registrations.map { |c| registration_json(c) } modification_jsons = @modifications.map { |c| modification_json(c) } render json: plan_jsons + event_jsons + registration_jsons + modification_jsons end end end |
#update ⇒ Object
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 |
# File 'app/controllers/collavre_plan/plans_controller.rb', line 90 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..join(", ") redirect_back fallback_location: main_app.root_path end format.json do render json: { errors: @plan.errors. }, status: :unprocessable_entity end end end end |