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

Constants included from Models::Auditable

Models::Auditable::ALWAYS_IGNORED

Constants included from Schedulable

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 Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Instance Attribute Details

#nameObject (readonly)



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

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:



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

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

.exclude_admin_optionsActiveRecord::Relation<Profile>

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

Returns:

  • (ActiveRecord::Relation<Profile>)

See Also:



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

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:



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

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

.homeownerActiveRecord::Relation<Profile>

A relation of Profiles that are homeowner. Active Record Scope

Returns:

  • (ActiveRecord::Relation<Profile>)

See Also:



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

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

.homeowner_profileObject



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

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

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



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/profile.rb', line 54

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:



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

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:



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

scope :trades, -> { exclude_homeowner.exclude_admin_options }

.unknown_tradeObject



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

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

Instance Method Details

#correct_discount(number_of_completed_orders) ⇒ Object



130
131
132
133
134
135
136
# File 'app/models/profile.rb', line 130

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)


102
103
104
# File 'app/models/profile.rb', line 102

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

#direct_commercial?Boolean

Returns:

  • (Boolean)


122
123
124
# File 'app/models/profile.rb', line 122

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

#direct_pro?Boolean

def distributor?
report_grouping == 'Wholesalers'
end

Returns:

  • (Boolean)


118
119
120
# File 'app/models/profile.rb', line 118

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

#e_commerce_misc?Boolean

Returns:

  • (Boolean)


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

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

#friendly_nameObject



78
79
80
81
82
# File 'app/models/profile.rb', line 78

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

#homeowner?Boolean

Returns:

  • (Boolean)


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

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)


84
85
86
# File 'app/models/profile.rb', line 84

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



106
107
108
# File 'app/models/profile.rb', line 106

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)


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

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