rake任务可以使用控制器"创建"方法?

时间:2017-02-16 05:51:55

标签: ruby-on-rails

我有一个rake任务,如果Purchase有一个特定的日期属性,它会从Purchases创建事件。

rake任务代码是:

namespace :events do

  desc "Create events from purchases."
  task purchases: :environment do

    purchases = Purchase.all

    purchases.each do |purchase|
      begin
        ## SUMMARY VARIABLE
        vendor = purchase.vendor.name
        order_number = purchase.order_number
        est_ship_date = purchase.estimated_ship_date
        purchase_summary = "Est. ship date: #{est_ship_date} #{vendor} PO ##{order_number}"

        ## DESCRIPTION VARIABLE
        tradegecko_url = purchase.tradegecko_url
        description = "#{vendor} PO ##{order_number} Estimated ship date: #{est_ship_date}. #{tradegecko_url}"

        ## START_DATE VARIABLE
        start_date = purchase.estimated_ship_date

        ##END_DATE VARIABLE
        end_date = start_date

        unless purchase.estimated_ship_date == nil
          event = Event.find_or_initialize_by(summary: purchase_summary)
          event.update_attributes(
            summary: purchase_summary,
            description: description,
            start_date: start_date,
            end_date: end_date
            )
          pp purchase
        end
      rescue NoMethodError => e
        puts "#{e.inspect} #{purchase.inspect}"
      end
    end
  end

当我运行rake时,事件在我的事件表中创建,它似乎工作正常,除了在我的事件控制器"创建"方法我有一个" if @ event.save"将事件推送到我的Google日历的调用,然后从新创建的Google日历事件中获取event_id并将其保存在我的数据库中。如果我手动创建一个新事件,这种方法非常有效,但是当从rake创建新事件时,这种方法无效。

events_controller.rb"创建"方法:

  def create
    @event = Event.new(event_params)

    respond_to do |format|
      if @event.save
        format.html { redirect_to @event, notice: 'Event successfully created.' }
        format.js   {}
        format.json { render json: @event, status: :created, location: @event }
      else
        format.html { render 'new' }
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end

    if @event.save

      gcal_event = Google::Apis::CalendarV3::Event.new({
        summary: @event.summary,
        description: @event.description,
        start: {
          date: @event.start_date
        },
        end: {
          date: @event.end_date
        }
      })

      @cal_service = GoogleCalendarService.new
      @cal = @cal_service.calendar

      result = @cal.insert_event(GoogleCalendarService::CALENDAR_ID, gcal_event)

      @event.calendar_event_id = result.id
      @event.save
    else
      render :nothing => true
    end
  end

有没有办法确保控制器"创建"当我运行rake任务时调用方法?

1 个答案:

答案 0 :(得分:0)

您可以使用after_create中的 Modal rails回调创建google_calendar逻辑,将其从控制器中删除。

这也可以帮助您减少代码,因为rails遵循DRY概念。无论您的事件何时创建,模型代码都会运行。

class Event < ActiveRecord::Base

after_create :google_calendar
    .
    .
    .
    .

  private
    def google_calendar
        gcal_event = Google::Apis::CalendarV3::Event.new({
            summary: self.summary,
            description: self.description,
            start: {
                date: self.start_date
            },
            end: {
                date: self.end_date
            }
        })

        @cal_service = GoogleCalendarService.new
        @cal = @cal_service.calendar

        result = @cal.insert_event(GoogleCalendarService::CALENDAR_ID, gcal_event)

        self.calendar_event_id = result.id
        self.save
    end
end

只要您的对象被保存,此方法就会运行。 (无论是来自控制器还是佣金任务)

所以,你的控制器动作将是,

def create
    @event = Event.new(event_params)

    respond_to do |format|
      if @event.save
        format.html { redirect_to @event, notice: 'Event successfully created.' }
        format.js   {}
        format.json { render json: @event, status: :created, location: @event }
      else
        format.html { render 'new' }
        format.json { render json: @event.errors, status: :unprocessable_entity }
      end
    end

  end

Here is a documentation of the various CallBacks