Skip to content

Instantly share code, notes, and snippets.

@dwbutler
Forked from fourfour/gist:0cf77b1ad8344f40106d
Last active August 29, 2015 14:05
Show Gist options
  • Save dwbutler/e7d6aaa64cbaa792402b to your computer and use it in GitHub Desktop.
Save dwbutler/e7d6aaa64cbaa792402b to your computer and use it in GitHub Desktop.
# Tested with ActiveRecord 4.0.5
# Run using:
# BUNDLE_GEMFILE=gemfiles/rails_4.0.gemfile bundle console
require 'sqlite3'
require 'active_record'
require 'groupify/adapter/active_record'
ActiveSupport::VERSION::STRING
# => "4.0.5"
ActiveRecord::Base.establish_connection( :adapter => 'sqlite3', :database => ":memory:" )
# Migrations
ActiveRecord::Schema.define(:version => 1) do
create_table :groups do |t|
t.string :name
t.string :type
t.timestamps
end
create_table :group_memberships do |t|
t.string :member_type
t.integer :member_id
t.integer :group_id
t.string :group_name
t.string :membership_type
end
create_table :users do |t|
t.string :name
t.string :type
t.timestamps
end
end
class User < ActiveRecord::Base
acts_as_group_member
acts_as_named_group_member
end
class Manager < User
end
class GroupMembership < ActiveRecord::Base
acts_as_group_membership
end
class Group < ActiveRecord::Base
acts_as_group :members => [:users], :default_members => :users
end
user = Manager.create!
# => #<Manager id: 1, name: nil, type: "Manager", created_at: "2014-08-21 09:03:58", updated_at: "2014-08-21 09:03:58">
group = Group.create!
# => #<Group id: 1, name: nil, type: nil, created_at: "2014-08-21 09:03:58", updated_at: "2014-08-21 09:03:58">
group.add(user, as: 'manager')
# => [#<Manager id: 1, name: nil, type: "Manager", created_at: "2014-08-21 09:03:58", updated_at: "2014-08-21 09:03:58">]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment