-
-
Save dwbutler/e7d6aaa64cbaa792402b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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