dwbutler / groupify

Add group and membership functionality to your Rails models
MIT License
194 stars 42 forks source link

How to use Groupify for my app ? #28

Closed moussasarr closed 9 years ago

moussasarr commented 9 years ago

I am trying to add group functionality for my users. I have never used the Groupify gem before .

I would like to start with Groups such as : Admin, Moderator, Artist, User. The members of these groups are all users but have certain privileges . And I would also like to give members of these basic groups the ability to create new groups for mostly discussion purposes and information sharing. I am not even sure How to create the form for the group and how how to create the group, with its attributes, and membership.

I tried using Groupify on the console:

user=User.new

group=Group.new

group.add user

user.in_group?(group)

=> false

It returns false instead of true. I am stuck and though I read the documentation, I do not seem to understand the implementation:

Here is my just created group controller :

class GroupsController < ApplicationController
def new
@group = Group.new
end
def create
if @group.save
  flash.now[:success]= "Group #{@group.group_name} successfully created"
  redirect_to @group
else
  render 'new'
end
end
def show
@group = Group.find(params[:id])
end
def edit
@group = Group.find(params[:id])
end
def update
@group = Group.find(params[:id])
if @group.update_attributes(group_params)
  flash.now[:success] = "Group updated !"
  redirect_to @group
 else
  render 'edit'
  end
 end

def destroy
end
private 
def group_params
  params.require(:type, :group_name)
end
end

My group model:

class Group < ActiveRecord::Base
groupify :group, members: [:users, :assignments], default_members: :users
end

My membership model :

class GroupMembership < ActiveRecord::Base
groupify :group_membership
end
Some relevant part of my user model:

class User < ActiveRecord::Base
attr_accessor :remember_member
groupify :group_member
groupify :named_group_member

before_save { self.email = email.downcase }
has_many :posts, dependent: :destroy
has_many :comments, dependent: :destroy

has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy

has_many :passive_relationships, class_name:  "Relationship",
                               foreign_key: "followed_id",
                               dependent:   :destroy
end

And finally my group migration:

 class CreateGroups < ActiveRecord::Migration
 def change
 create_table :groups do |t|
  t.string   :type      # Only needed if using single table inheritance
  t.text     :description
 end

create_table :group_memberships do |t|
  t.string     :member_type     # Necessary to make polymorphic members work
  t.integer    :member_id       # The id of the member that belongs to this group
  t.integer    :group_id        # The group to which the member belongs
  t.string     :group_name      # The named group to which a member belongs (if using)
  t.string     :membership_type # The type of membership the member belongs with
 end

add_index :group_memberships, [:member_id, :member_type]
add_index :group_memberships, :group_id
add_index :group_memberships, :group_name
end
end

Thanks for your help. Also do you have an example application/tutorial where groupify is implemented >? PS: I am not an advanced Rails developer

dwbutler commented 9 years ago

Gotta call create or save, otherwise nothing will get saved in the database.

There isn't currently an example or tutorial, sorry. This is on my to-do list.

moussasarr commented 9 years ago

Also can you also please look at my controllers and model and let me know if I am using your gem correctly according to what I am trying to do if you are able to ? I do not mean to bother you too much with questions . I am just stuck and I really want to know how to use this gem