app/models/entities/contact.rb in fat_free_crm-0.18.2 vs app/models/entities/contact.rb in fat_free_crm-0.19.0

- old
+ new

@@ -38,13 +38,13 @@ # skype :string(128) # class Contact < ActiveRecord::Base belongs_to :user - belongs_to :lead - belongs_to :assignee, class_name: "User", foreign_key: :assigned_to - belongs_to :reporting_user, class_name: "User", foreign_key: :reports_to + belongs_to :lead, optional: true # TODO: Is this really optional? + belongs_to :assignee, class_name: "User", foreign_key: :assigned_to, optional: true # TODO: Is this really optional? + belongs_to :reporting_user, class_name: "User", foreign_key: :reports_to, optional: true # TODO: Is this really optional? has_one :account_contact, dependent: :destroy has_one :account, through: :account_contact has_many :contact_opportunities, dependent: :destroy has_many :opportunities, -> { order("opportunities.id DESC").distinct }, through: :contact_opportunities has_many :tasks, as: :asset, dependent: :destroy # , :order => 'created_at DESC' @@ -62,11 +62,11 @@ accepts_nested_attributes_for :business_address, allow_destroy: true, reject_if: proc { |attributes| Address.reject_address(attributes) } scope :created_by, ->(user) { where(user_id: user.id) } scope :assigned_to, ->(user) { where(assigned_to: user.id) } - scope :text_search, ->(query) { + scope :text_search, lambda { |query| t = Contact.arel_table # We can't always be sure that names are entered in the right order, so we must # split the query into all possible first/last name permutations. name_query = if query.include?(" ") scope, *rest = query.name_permutations.map do |first, last| @@ -96,10 +96,25 @@ validates_presence_of :first_name, message: :missing_first_name, if: -> { Setting.require_first_names } validates_presence_of :last_name, message: :missing_last_name, if: -> { Setting.require_last_names } validate :users_for_shared_access + validates_length_of :first_name, maximum: 64 + validates_length_of :last_name, maximum: 64 + validates_length_of :title, maximum: 64 + validates_length_of :department, maximum: 64 + validates_length_of :email, maximum: 254 + validates_length_of :alt_email, maximum: 254 + validates_length_of :phone, maximum: 32 + validates_length_of :mobile, maximum: 32 + validates_length_of :fax, maximum: 32 + validates_length_of :blog, maximum: 128 + validates_length_of :linkedin, maximum: 128 + validates_length_of :facebook, maximum: 128 + validates_length_of :twitter, maximum: 128 + validates_length_of :skype, maximum: 128 + # Default values provided through class methods. #---------------------------------------------------------------------------- def self.per_page 20 end @@ -138,13 +153,11 @@ end # Attach given attachment to the contact if it hasn't been attached already. #---------------------------------------------------------------------------- def attach!(attachment) - unless send("#{attachment.class.name.downcase}_ids").include?(attachment.id) - send(attachment.class.name.tableize) << attachment - end + send(attachment.class.name.tableize) << attachment unless send("#{attachment.class.name.downcase}_ids").include?(attachment.id) end # Discard given attachment from the contact. #---------------------------------------------------------------------------- def discard!(attachment) @@ -158,11 +171,11 @@ # Class methods. #---------------------------------------------------------------------------- def self.create_for(model, account, opportunity, params) attributes = { lead_id: model.id, - user_id: params[:account][:user_id], + user_id: params[:account][:user_id] || account.user_id, assigned_to: params[:account][:assigned_to], access: params[:access] } %w[first_name last_name title source email alt_email phone mobile blog linkedin facebook twitter skype do_not_call background_info].each do |name| attributes[name] = model.send(name.intern) @@ -171,13 +184,11 @@ contact = Contact.new(attributes) # Set custom fields. if model.class.respond_to?(:fields) model.class.fields.each do |field| - if contact.respond_to?(field.name) - contact.send "#{field.name}=", model.send(field.name) - end + contact.send "#{field.name}=", model.send(field.name) if contact.respond_to?(field.name) end end contact.business_address = Address.new(street1: model.business_address.street1, street2: model.business_address.street2, city: model.business_address.city, state: model.business_address.state, zipcode: model.business_address.zipcode, country: model.business_address.country, full_address: model.business_address.full_address, address_type: "Business") unless model.business_address.nil? @@ -205,10 +216,11 @@ # Handles the saving of related accounts #---------------------------------------------------------------------------- def save_account(params) account_params = params[:account] - self.account = if account_params && account_params[:id] != "" && account_params[:name] != "" + valid_account = account_params && (account_params[:id].present? || account_params[:name].present?) + self.account = if valid_account Account.create_or_select_for(self, account_params) else nil end end