<commit_before>class Supplier < ActiveRecord::Base

  #==========================================
  # Associations
  
  belongs_to :user
  belongs_to :address
  has_many   :supplier_products, :dependent => :destroy
  has_many   :products, :through => :supplier_products
  has_many   :orders, :class_name => "DropShipOrder", :dependent => :nullify
  
  #==========================================
  # Validations
  
  validates_associated :address
  validates :address_id, :name, :phone, :presence => true
  validates :email, :presence => true, :email => true
  validates :url, :url => true

  #==========================================
  # Callbacks
    
  before_validation :save_address, :on => :create
  after_create :create_user_and_send_welcome
  
  #==========================================
  # Instance Methods

  # Returns the supplier's email address and name in mail format
  def email_with_name
    "#{name} <#{email}>"
  end
  
  def url=(value)
    value = "http://#{value}" unless value =~ /https?:\/\//
    write_attribute :url, value
  end
  
  
  #==========================================
  # Protected Methods
    
  protected
    
    def create_user_and_send_welcome
      password = <PASSWORD>(email.to_s)[0..16]
      self.create_user(:email => email, :password => password, :password_confirmation => password)
      user.generate_reset_password_token!      
      SupplierMailer.welcome(self).deliver!
    end
    
    def save_address # :nodoc:
      unless address.nil?
        address.phone = phone
        address.save<commit_msg>fix supplier`s user attachment after create<commit_after>class Supplier < ActiveRecord::Base

  #==========================================
  # Associations
  
  belongs_to :user
  belongs_to :address
  has_many   :supplier_products, :dependent => :destroy
  has_many   :products, :through => :supplier_products
  has_many   :orders, :class_name => "DropShipOrder", :dependent => :nullify
  
  #==========================================
  # Validations
  
  validates_associated :address
  validates :address_id, :name, :phone, :presence => true
  validates :email, :presence => true, :email => true
  validates :url, :url => true

  #==========================================
  # Callbacks
    
  before_validation :save_address, :on => :create
  after_create :create_user_and_send_welcome
  
  #==========================================
  # Instance Methods

  # Returns the supplier's email address and name in mail format
  def email_with_name
    "#{name} <#{email}>"
  end
  
  def url=(value)
    value = "http://#{value}" unless value =~ /https?:\/\//
    write_attribute :url, value
  end
  
  
  #==========================================
  # Protected Methods
    
  protected
    
    def create_user_and_send_welcome
      password = Digest::SHA1.hexdigest(email.to_s)[0..16]
      user = self.create_user(:email => email, :password => password, :password_confirmation => password)
      user.send(:generate_reset_password_token!)
      SupplierMailer.welcome(self).deliver!
      self.save
    end
    
    def save_address # :nodoc:
      unless address.nil?
        address.phone = phone
        address.save
{'fraction_non_alphanumeric': 0.22664576802507838, 'fraction_numerical': 0.00219435736677116, 'mean_word_length': 3.8129713423831073, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2720841', 'n_tokens_mistral': 946, 'n_tokens_neox': 922, 'n_words': 206}