ruby - how to associate a new user's email internet domain with a network on my website? (rails 3) -


for example, if user signs using email johndoe@xyz.com, want user part of "xyz" network on website. people allowed join respective networks. there numerous networks on website , separate each other. how user table looks in schema.db

create_table "activities", :force => true |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",     :null => false     t.datetime "updated_at",     :null => false   end    add_index "activities", ["owner_id", "owner_type"], :name => "index_activities_on_owner_id_and_owner_type"   add_index "activities", ["recipient_id", "recipient_type"], :name => "index_activities_on_recipient_id_and_recipient_type"   add_index "activities", ["trackable_id", "trackable_type"], :name => "index_activities_on_trackable_id_and_trackable_type"      create_table "users", :force => true |t|     t.string    "name"     t.string    "email"     t.timestamp "created_at",                         :null => false     t.timestamp "updated_at",                         :null => false     t.string    "password_digest"     t.string    "remember_token"     t.boolean   "admin",           :default => false   end 

i'm using activities track user model , display them in "feed". add in view: "username has joined xyz network." tracking user model.

create network model. it's database table contain string called domain. network have many users; user belong network. add network_id string user model well.

associations , support methods should this:

class network < activerecord::base   has_many :users    def peers     self.users   end  end  class user < activerecord::base   belongs_to :network   delegate :peers => :network    validates :network_id, :presence => true   # ... end 

now possible:

@network.users # => collection of users in network  @user.network # => network user belongs  @user.peers # => collection of users within @user's network # => note: include @user in collection 

when user signs up, create or find network record containing domain string extracted user's email address. when user updates email address, check if domain has changed, , re-create or re-find network record matching domain.

it tempting automate in model, implemented in controller, userscontroller#create , userscontroller#update.

def create   # ...isntantiate @user...   #   set_network_id_from_email(@user.email)   #   # ...save... end  def update   # ...instantiate @user...   #   if @user.email != params[:user][:email]       set_network_id_from_email(params[:user][:email])   end   #   # ...update_attributes... end  private  def set_network_id_from_email(addr)   if email = mail::address.new(addr)     @user.network_id = network.find_or_create_by_domain(email.domain)   end end 

Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -