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

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::EventPublishable

#publish_event

Instance Attribute Details

#nameObject (readonly)



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

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:



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

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:



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

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:



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

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:



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

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

.homeowner_profileObject



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

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

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



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

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:



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

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:



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

scope :trades, -> { exclude_homeowner.exclude_admin_options }

.unknown_tradeObject



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

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

Instance Method Details

#correct_discount(number_of_completed_orders) ⇒ Object



123
124
125
126
127
128
129
# File 'app/models/profile.rb', line 123

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:



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

has_many :customers

#direct_commercial?Boolean

Returns:

  • (Boolean)


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

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

#direct_pro?Boolean

def distributor?
report_grouping == 'Wholesalers'
end

Returns:

  • (Boolean)


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

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

#e_commerce_misc?Boolean

Returns:

  • (Boolean)


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

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

#friendly_nameObject



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

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

#homeowner?Boolean

Returns:

  • (Boolean)


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

def homeowner?
  id == ProfileConstants::HOMEOWNER
end

#invoicesActiveRecord::Relation<Invoice>

Returns:

  • (ActiveRecord::Relation<Invoice>)

See Also:



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

has_many :invoices

#organization?Boolean

Returns:

  • (Boolean)


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

def organization?
  !homeowner?
end

#tier2_program_pricingCoupon::Tier2ProgramPricing



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

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

#to_sObject



91
92
93
# File 'app/models/profile.rb', line 91

def to_s
  name
end