neumachen
11/25/2014 - 3:15 PM

custom matcher to determine of a public activity was made

custom matcher to determine of a public activity was made

RSpec::Matchers.define :log_activity do |options|

  match do |actual|
    trackable = []
    if options[:trackable_id] || [:trackable_type]
      trackable << "trackable_id = #{options[:trackable_id]}" if options[:trackable_id]
      trackable << "trackable_type = #{options[:trackable_type]}" if options[:trackable_type]
      trackable.join(" AND ") if options[:trackable_id] && options[:trackable_type]
    end

    owner = []
    if options[:owner_id] || [:owner_type]
      owner << "owner_id = #{options[:owner_id]}" if options[:owner_id]
      owner << "owner_type = #{options[:owner_type]}" if options[:owner_type]
      owner.join(" AND ") if options[:owner_id] && options[:owner_type]
    end

    recipient = []
    if options[:recipient_id] || [:recipient_type]
      recipient << "owner_id = #{options[:recipient_id]}" if options[:recipient_id]
      recipient << "owner_type = #{options[:recipient_type]}" if options[:recipient_type]
      recipient.join(" AND ") if options[:recipient_id] && options[:recipient_type]
    end

    query = []
    query << trackable unless trackable.empty?
    query << owner unless owner.empty?
    query << recipient unless recipient.empty?
    query.flatten.join(" OR ")

    pa = PublicActivity::Activity.where(query)
    pa.count >= 1
  end

  failure_message do |actual|
    "expected to log an activity but did not"
  end

  failure_message_when_negated do |actual|
    "expected to not log an activity"
  end

  description do
    "logs an activity"
  end
end