Skip to content

Instantly share code, notes, and snippets.

@slbug
Created August 10, 2014 15:50
Show Gist options
  • Save slbug/82f9042aef89c0cd19aa to your computer and use it in GitHub Desktop.
Save slbug/82f9042aef89c0cd19aa to your computer and use it in GitHub Desktop.

Revisions

  1. slbug created this gist Aug 10, 2014.
    91 changes: 91 additions & 0 deletions gistfile1.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,91 @@
    unless File.exist?('Gemfile')
    File.write('Gemfile', <<-GEMFILE)
    source 'https://rubygems.org'
    gem 'rails', github: 'rails/rails'
    gem 'arel', github: 'rails/arel'
    gem 'rack', github: 'rack/rack'
    gem 'i18n', github: 'svenfuchs/i18n'
    gem 'public_activity', github: 'pokonski/public_activity'
    gem 'sqlite3'
    GEMFILE

    system 'bundle'
    end

    require 'bundler'
    Bundler.setup(:default)

    require 'active_record'
    require 'minitest/autorun'
    require 'logger'

    require 'public_activity'

    # This connection will do for database-independent bug reports.
    ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
    ActiveRecord::Base.logger = Logger.new(STDOUT)

    ActiveRecord::Schema.define do
    create_table :trips do |t|
    end

    create_table :travelers do |t|
    t.integer :trip_id
    t.integer :user_id
    end

    create_table :users do |t|
    end

    create_table "activities", force: true do |t|
    t.integer "trackable_id"
    t.string "trackable_type"
    t.integer "owner_id"
    t.string "owner_type"
    t.string "key"
    t.text "parameters"
    t.integer "recipient_id"
    t.string "recipient_type"
    t.datetime "created_at"
    t.datetime "updated_at"
    end
    end

    class Trip < ActiveRecord::Base
    has_many :travelers
    has_many :users, through: :travelers
    end

    class Traveler < ActiveRecord::Base
    include PublicActivity::Model
    belongs_to :user
    belongs_to :post

    tracked
    end

    class User < ActiveRecord::Base
    has_many :travelers
    has_many :trips, through: :travelers
    end

    class Activity < PublicActivity::Activity
    end

    class BugTest < Minitest::Test
    def test_association_stuff
    t = Trip.create!
    u1 = User.create!
    u2 = User.create!

    t.users = [u1, u2]

    # 2 activites should be created "traveler.create"
    assert_equal 2, Activity.where(key: 'traveler.create').count

    t.users = [u1]

    # 1 activity should be created "traveler.destroy"
    assert_equal 1, Activity.where(key: 'traveler.destroy').count
    end
    end