Installation du plugin : Sous netbeans : ajouter le repo : http://actsasnetwork.rubyforge.org/svn/plugins/acts_as_network/
en mode console : sudo apt-get install git git-core (pour linux) script/plugin install git://github.com/sjlombardo/acts_as_network.git
générer la doc : rake doc:plugins
Version simple
Création d'une table des users script/generate model user
class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.string :email t.string :hashed_password t.string :salt t.datetime :dateLastLogin t.timestamps end end
def self.down drop_table :users end end
script/generate model friend
class CreateFriends < ActiveRecord::Migration def self.up create_table :friends, {:id => false} do |t| t.column :user_id, :integer, :null => false t.column :user_id_target, :integer, :null => false # target of the relationship t.timestamps end end
def self.down drop_table :friends end end
-> effectuer la migration
Modification des models
class User < ActiveRecord::Base acts_as_network :friends, :join_table => :friends end
Controleur
def test_acts_as_network jane = User.create(:email => 'Jane') jack = User.create(:email => 'Jack')
jane.friends_out << jack # Jane adds Jack as a friend puts jane.friends.include?(jack) # true Jack is Janes friend puts jack.friends.include?(jane) # true Jane is also Jack's friend! end
Version avec invitations :
Création d'une table des users script/generate model user
class CreateUsers < ActiveRecord::Migration def self.up create_table :users do |t| t.string :email t.string :hashed_password t.string :salt t.datetime :dateLastLogin t.timestamps end end
def self.down drop_table :users end end
script/generate model invite
class CreateInvites < ActiveRecord::Migration def self.up create_table :invites do |t| t.column :user_id, :integer, :null => false # source of the relationship t.column :user_id_target, :integer, :null => false # target of the relationship t.column :code, :string # random invitation code t.column :message, :text # invitation message t.column :is_accepted, :boolean t.column :accepted_at, :timestamp # when did they accept? t.timestamps end end
def self.down drop_table :invites end end
Modification des models
class User < ActiveRecord::Base acts_as_network :friends, :through => :invites, :conditions => "is_accepted = 1" end
class Invite < ActiveRecord::Base belongs_to :user belongs_to :user_target, :class_name => 'User', :foreign_key => 'user_id_target' # the target of the friend relationship validates_presence_of :user, :user_target end
Controleur :
def test_acts_as_network jane = User.create(:email => 'Jane') jack = User.create(:email => 'Jack')
invite = Invite.create(:user => jane, :user_target => jack, :message => "let's be friends!")
puts jane.friends.include?(jack) puts jack.friends.include?(jane)
invite.is_accepted = true # Now Jack accepts the invite invite.accepted_at = Time.now invite.save and jane.reload and jack.reload
puts jane.friends.include?(jack) puts jack.friends.include?(jane)
end
|