ruby reshaping_the_command_approach.rb

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby reshaping_the_command_approach.rb相关的知识,希望对你有一定的参考价值。

# MODEL

class Case < ActiveRecord::Base
  include Eventable
  
  has_many :tasks

  concerning :Assignment do
    def assign_to(new_owner:, details:)
      transaction do
        update_assigment_attributes new_owner, details

        transfer_open_tasks_to new_owner
        create_initial_contact_task details

        record_event :assigned, details[:comments]
      end
    end
  
    private
      def update_assigment_attributes(new_owner, details)
        update! details.slice(:distribute_at, :distribute_rule_name).merge(owner: new_owner)
      end
  
      def transfer_open_tasks_to(new_owner)
        tasks.open.each { |task| task.update! owner: new_owner }
      end
    
      def create_initial_contact_task(details)
        if details[:require_initial] && owner.tasks.initials?
          tasks.create! kind: :initial, details[:comments]
        end
      end
  end
end

class Task < ActiveRecord::Base
  belongs_to :user
  belongs_to :case
  
  scope :open, -> { where.not status: :closed }
end

class User
  has_many :tasks do
    def initials?
      where(kind: :initial).exist?
    end
  end
end


class Event < ActiveRecord::Base
  store :details, accessors: [ :activity, :creator, :comments ]
  
  cattr_accessor :creator, instance_accessor: false
  before_save :set_creator

  private
    def set_creator
      self.creator ||= self.class.creator
    end
end

module Eventable
  extend ActiveSupport::Concern
  
  included do
    has_many :events
  end
  
  private
    def record_event(activity, attributes = {})
      events.create! attributes.merge(activity: activity, eventable: self)
    end
end


# CONTROLLER

class Cases::AssignmentController < ApplicationController
  before_action :set_case
  
  def create
    @case.assign_to new_owner: find_new_owner, details: assignment_params
  end
  
  private
    def set_case
      @case = @current_account.cases.find
    end
    
    def assignment_params
      params.require(:case).permit(:comments, :distribute_at, :distribute_rule_name, :require_initial)
    end

    def find_new_owner
      @current_account.users.find(params[:case][:owner_id])
    end
end

class ApplicationController < ActionController::Base
  include CurrentUser, CurrentAccount
end

module CurrentUser
  extend ActiveSupport::Concern
  
  included do
    before_action :set_current_user
  end
  
  private
    def set_current_user
      @current_user = authorize
      Event.creator = @current_user
    end
end


# VIEW

class EventSummarizer
  def initialize(event)
    @event = event
  end
  
  def summary
    summarizer.new(@event).send(summary_method)
  end
  
  private
    def summarizer
      Object.const_get("#{@event.eventable.class}EventSummarizer")
    end
    
    def summary_method
      "#{@event.eventable.class.to_s.underscore}_#{@event.activity}_summary"
    end
end


class CaseEventSummarizer < EventSummarizer
  def case_assigned_summary
    "Case assigned to #{@event.eventable.owner.full_name}".tap do |summary|
      summary << "; #{@event.comments}" if @event.comments
    end
  end
end

以上是关于ruby reshaping_the_command_approach.rb的主要内容,如果未能解决你的问题,请参考以下文章

Ruby 25 岁了!Ruby 之父说 Ruby 3 有望 3 倍提速

如何学习ruby?Ruby学习技巧分享

ruby Ruby脚本,看看是否用openssl编译了ruby

什么是ruby?

ruby和ruby ee

ruby入门知识:了解ruby历史及特性