Sha256: 82fa2093ad6e01633f3cb29becdd0afd8a78628b32cb92cd13c631730bf0875b

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

module Tasuku
  module Concerns::Models::Taskables::Taskable::Submission
    extend ActiveSupport::Concern

    included do
      scope :by, ->(author) { where author: author }

      after_create do |model|
        if model.request.task.present?
          Tasks::Taskables::Taskable::Response.create do |response|
            response.author = model.author
            response.submittable = model
            response.task = model.request.task
          end
        end
      end

      before_destroy do |model|
        Tasks::Taskables::Taskable::Response.where(
          "submittable_id = ? and submittable_type = ? and author_id = ? and author_type = ?", 
          model.id, model.class.name, model.author.id, model.author.class.name
        ).delete_all
      end
    end

    module ClassMethods
      # Declare the relation the response belongs to.
      #
      # is - A String or Symbol describing the relation that instances of this model
      #      are responses for.
      #
      # This method really only aliases the relation to 'request' so as to promote a
      # common interface for any submission.
      def request is: nil
        alias_method :request, is
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tasuku-1.1.0 lib/tasuku/concerns/models/taskables/taskable/submission.rb