app/models/entities/account.rb in fat_free_crm-0.18.2 vs app/models/entities/account.rb in fat_free_crm-0.19.0
- old
+ new
@@ -26,12 +26,12 @@
# rating :integer default(0), not null
# category :string(32)
#
class Account < ActiveRecord::Base
- belongs_to :user
- belongs_to :assignee, class_name: "User", foreign_key: :assigned_to
+ belongs_to :user, optional: true # TODO: Is this really optional?
+ belongs_to :assignee, class_name: "User", foreign_key: :assigned_to, optional: true
has_many :account_contacts, dependent: :destroy
has_many :contacts, -> { distinct }, through: :account_contacts
has_many :account_opportunities, dependent: :destroy
has_many :opportunities, -> { order("opportunities.id DESC").distinct }, through: :account_opportunities
has_many :pipeline_opportunities, -> { order("opportunities.id DESC").distinct.pipeline }, through: :account_opportunities, source: :opportunity
@@ -44,19 +44,19 @@
serialize :subscribed_users, Set
accepts_nested_attributes_for :billing_address, allow_destroy: true, reject_if: proc { |attributes| Address.reject_address(attributes) }
accepts_nested_attributes_for :shipping_address, allow_destroy: true, reject_if: proc { |attributes| Address.reject_address(attributes) }
- scope :state, ->(filters) {
+ scope :state, lambda { |filters|
where('category IN (?)' + (filters.delete('other') ? ' OR category IS NULL' : ''), filters)
}
scope :created_by, ->(user) { where(user_id: user.id) }
scope :assigned_to, ->(user) { where(assigned_to: user.id) }
scope :text_search, ->(query) { ransack('name_or_email_cont' => query).result }
- scope :visible_on_dashboard, ->(user) {
+ scope :visible_on_dashboard, lambda { |user|
# Show accounts which either belong to the user and are unassigned, or are assigned to the user
where('(user_id = :user_id AND assigned_to IS NULL) OR assigned_to = :user_id', user_id: user.id)
}
scope :by_name, -> { order(:name) }
@@ -89,20 +89,19 @@
# Extract last line of billing address and get rid of numeric zipcode.
#----------------------------------------------------------------------------
def location
return "" unless self[:billing_address]
+
location = self[:billing_address].strip.split("\n").last
location&.gsub(/(^|\s+)\d+(:?\s+|$)/, " ")&.strip
end
# Attach given attachment to the account 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 account.
#----------------------------------------------------------------------------
def discard!(attachment)
@@ -115,17 +114,18 @@
# Class methods.
#----------------------------------------------------------------------------
def self.create_or_select_for(model, params)
# Attempt to find existing account
- if params[:id].present?
- return Account.find(params[:id])
- elsif params[:name].present?
+ return Account.find(params[:id]) if params[:id].present?
+
+ if params[:name].present?
account = Account.find_by(name: params[:name])
return account if account
end
# Fallback to create new account
+ params[:user] = model.user if model
account = Account.new(params)
if account.access != "Lead" || model.nil?
account.save
else
account.save_with_model_permissions(model)