Class: Profile

Inherits:
ApplicationRecord show all
Includes:
Models::Auditable
Defined in:
app/models/profile.rb

Overview

== Schema Information

Table name: profiles
Database name: primary

id :integer not null, primary key
active :boolean default(TRUE), not null
description :text
jde_code_1 :string(255)
jde_code_1_bg :string(255)
jde_code_2 :string(255)
locator_eligible :boolean default(FALSE), not null
name :string(255)
report_grouping :string(255)
created_at :datetime
updated_at :datetime
legacy_tier2_program_pricing_id :integer
tier2_program_pricing_id :integer

Indexes

profiles_tier2_program_pricing_id_idx (tier2_program_pricing_id)

Foreign Keys

profiles_tier2_program_pricing_id_fk (tier2_program_pricing_id => coupons.id)

Constant Summary collapse

ETAILER_REPORT_GROUPING =

Reporting bucket that singles out e-tailer dealers (e.g. "DL - E-Tailer").
Marketing audiences exclude it — see Profile.excluding_etailers.

'E-Tailers'

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Belongs to collapse

Methods included from Models::Auditable

#creator, #updater

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Models::Auditable

#all_skipped_columns, #audit_reference_data, #should_not_save_version, #stamp_record

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#nameObject (readonly)



54
# File 'app/models/profile.rb', line 54

validates :name, presence: true, uniqueness: { case_sensitive: false }

Class Method Details

.activeActiveRecord::Relation<Profile>

A relation of Profiles that are active. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Profile>)

See Also:



48
# File 'app/models/profile.rb', line 48

scope :active, -> { where(active: true) }

.dealerActiveRecord::Relation<Profile>

A relation of Profiles that are dealer. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Profile>)

See Also:



43
# File 'app/models/profile.rb', line 43

scope :dealer, -> { where("name LIKE 'DL %'") }

.exclude_admin_optionsActiveRecord::Relation<Profile>

A relation of Profiles that are exclude admin options. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Profile>)

See Also:



45
# File 'app/models/profile.rb', line 45

scope :exclude_admin_options, -> { where.not("name ILIKE 'Buying Group'") }

.exclude_homeownerActiveRecord::Relation<Profile>

A relation of Profiles that are exclude homeowner. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Profile>)

See Also:



46
# File 'app/models/profile.rb', line 46

scope :exclude_homeowner, -> { where.not("name LIKE 'HO %'") }

.excluding_etailersActiveRecord::Relation<Profile>

A relation of Profiles that are excluding etailers. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Profile>)

See Also:



52
# File 'app/models/profile.rb', line 52

scope :excluding_etailers, -> { where('profiles.report_grouping IS DISTINCT FROM ?', ETAILER_REPORT_GROUPING) }

.homeownerActiveRecord::Relation<Profile>

A relation of Profiles that are homeowner. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Profile>)

See Also:



47
# File 'app/models/profile.rb', line 47

scope :homeowner, -> { where(id: ProfileConstants::HOMEOWNER) }

.homeowner_profileObject



56
57
58
# File 'app/models/profile.rb', line 56

def self.homeowner_profile
  Profile.find(ProfileConstants::HOMEOWNER)
end

.options_for_select(admin = false, friendly_name = false, exclude_homeowner = false, include_description = false) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'app/models/profile.rb', line 64

def self.options_for_select(admin = false, friendly_name = false, exclude_homeowner = false, include_description = false)
  profiles = Profile.active.order(:name)
  profiles = profiles.exclude_admin_options unless admin
  profiles = profiles.exclude_homeowner if exclude_homeowner
  profiles.map do |p|
    dn = if friendly_name
           if p.name == 'TP - Architect'
             'Architect Professional'
           elsif p.name == 'DL - Architect'
             'Architect Dealer'
           else
             p.friendly_name
           end
         else
           p.name
         end
    if include_description
      [dn, p.id, { description: p.name }]
    else
      [dn, p.id]
    end
  end.sort_by { |pe| pe[0] }
end

.trade_proActiveRecord::Relation<Profile>

A relation of Profiles that are trade pro. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Profile>)

See Also:



42
# File 'app/models/profile.rb', line 42

scope :trade_pro, -> { where("name LIKE 'TP %'") }

.tradesActiveRecord::Relation<Profile>

A relation of Profiles that are trades. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Profile>)

See Also:



41
# File 'app/models/profile.rb', line 41

scope :trades, -> { exclude_homeowner.exclude_admin_options }

.unknown_tradeObject



60
61
62
# File 'app/models/profile.rb', line 60

def self.unknown_trade
  Profile.find(ProfileConstants::UNKNOWN_TRADE)
end

.wholesale_distributorActiveRecord::Relation<Profile>

A relation of Profiles that are wholesale distributor. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Profile>)

See Also:



44
# File 'app/models/profile.rb', line 44

scope :wholesale_distributor, -> { where("name LIKE 'WD %'") }

Instance Method Details

#correct_discount(number_of_completed_orders) ⇒ Object



139
140
141
142
143
144
145
# File 'app/models/profile.rb', line 139

def correct_discount(number_of_completed_orders)
  if number_of_completed_orders >= pricing_programs.length
    pricing_programs.to_a.last
  else
    pricing_programs.to_a[number_of_completed_orders]
  end
end

#customersActiveRecord::Relation<Customer>

Returns:

See Also:



34
# File 'app/models/profile.rb', line 34

has_many :customers

#dealer?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'app/models/profile.rb', line 112

def dealer?
  name.to_s.start_with?('DL ')
end

#direct_commercial?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'app/models/profile.rb', line 131

def direct_commercial?
  report_grouping == 'Direct - Commercial Sales'
end

#direct_pro?Boolean

def distributor?
report_grouping == 'Wholesalers'
end

Returns:

  • (Boolean)


127
128
129
# File 'app/models/profile.rb', line 127

def direct_pro?
  report_grouping == 'Direct - PRO'
end

#e_commerce_misc?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'app/models/profile.rb', line 135

def e_commerce_misc?
  report_grouping == 'E-commerce - Misc'
end

#friendly_nameObject



88
89
90
91
92
# File 'app/models/profile.rb', line 88

def friendly_name
  fn = name[/.{1,3} - (.*)/, 1]
  fn = name if fn.nil?
  fn
end

#homeowner?Boolean

Returns:

  • (Boolean)


98
99
100
# File 'app/models/profile.rb', line 98

def homeowner?
  id == ProfileConstants::HOMEOWNER
end

#invoicesActiveRecord::Relation<Invoice>

Returns:

  • (ActiveRecord::Relation<Invoice>)

See Also:



35
# File 'app/models/profile.rb', line 35

has_many :invoices

#organization?Boolean

Returns:

  • (Boolean)


94
95
96
# File 'app/models/profile.rb', line 94

def organization?
  !homeowner?
end

#tier2_program_pricingCoupon::Tier2ProgramPricing



32
# File 'app/models/profile.rb', line 32

belongs_to :tier2_program_pricing, class_name: 'Coupon::Tier2ProgramPricing', optional: true

#to_sObject



116
117
118
# File 'app/models/profile.rb', line 116

def to_s
  name
end

#trade_pro?Boolean

Trade-Pro and Dealer membership are encoded in the profile name prefix
("TP - …", "DL - …", "HO - …" — same convention used by Profile.trade_pro
scope and Profile#homeowner?). The previous canonical check at
Customer#dealer_or_trade_pro_for_locator? read profile.jde_code_1,
which is a legacy attribute being phased out — these predicates
replace that path.

Returns:

  • (Boolean)


108
109
110
# File 'app/models/profile.rb', line 108

def trade_pro?
  name.to_s.start_with?('TP ')
end