Sha256: 4e97383a7b23e5e780362ba365e36164d08d888e88345eb321ae77b8d1f68c95

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require 'activity_engine/exceptions'
module ActivityEngine
  class Activity < ActiveRecord::Base
    def self.call(data)
      create! do |activity|
        activity.subject = data.subject
        activity.user = data.current_user
        activity.message = data.message
        activity.activity_type = data.activity_type
      end
    end
    default_scope { order("created_at DESC") }

    scope :for_user, lambda {|user| where(user: user)}
    scope :for_subject, lambda {|subject|
      where(subject_id: subject.to_param, subject_type: subject.class.to_s)
    }
    scope :for_activity_type, lambda {|activity_type|
      where(activity_type: activity_type)
    }
    scope :ascending_date, lambda { order("created_at ASC") }
    scope :descending_date, lambda { order("created_at DESC") }


    belongs_to :user

    def subject
      subject_type.constantize.find(subject_id)
    end

    def subject=(object)
      if object.persisted?
        write_attribute(:subject_type, object.class.to_s)
        write_attribute(:subject_id, object.to_param)
      else
        raise UnpersistedSubjectError.new(object)
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activity_engine-0.0.3 app/models/activity_engine/activity.rb