Class: RoomType

Inherits:
ApplicationRecord show all
Defined in:
app/models/room_type.rb

Overview

== Schema Information

Table name: room_types
Database name: primary

id :integer not null, primary key
environment :string(50)
friendly_name :string
name :string(32) not null
parent_room :string
seo_key :string
tire_tracks_option :boolean

Indexes

idx_environment (environment)
index_room_types_on_name (name)
index_room_types_on_seo_key (seo_key)

Constant Summary collapse

ROOM_ICON_FILE_BASENAME_BY_RT_ID =

Room icon file basename by rt id.

{ '2' => 'bathroom', '4' => 'bedroom', '10' => 'kitchen', '1' => 'basement', '22' => 'basement', '18' => 'other-room', '20' => 'driveway', '21' => 'crosswalk', '29' => 'tiles', '23' => 'patio-chair' }.freeze
MINIMUM_SQFT_FOR_IQ =

Minimum sqft for iq.

{ indoor: 2.6, outdoor: 10.0 }.freeze

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

Instance Attribute Summary collapse

Has many collapse

Has and belongs to many collapse

Class Method Summary collapse

Instance Method Summary collapse

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)

Validates the room type name is present.

Validations:



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

validates :name, presence: true

Class Method Details

.by_environmentActiveRecord::Relation<RoomType>

A relation of RoomTypes that are by environment. Active Record Scope

Returns:

See Also:



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

scope :by_environment, ->(ev) { where(environment: ev).by_name }

.by_nameActiveRecord::Relation<RoomType>

A relation of RoomTypes that are by name. Active Record Scope

Returns:

See Also:



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

scope :by_name, -> { order(:name) }

.grouped_by_environment_for_select(is_public = nil) ⇒ Hash{String => Array<Array(String, Integer)>}

Room types grouped by environment for grouped select dropdowns.

Parameters:

  • is_public (Boolean, nil) (defaults to: nil)

    when true, restricts to environments with public product line options

Returns:

  • (Hash{String => Array<Array(String, Integer)>})

    environment → [name, id] pairs



110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'app/models/room_type.rb', line 110

def self.grouped_by_environment_for_select(is_public = nil)
  if is_public
    envs = HeatingElementProductLineOption.options_scope(is_public: true).distinct.pluck(:environment)
    rts = RoomType.by_environment(envs).to_a
  else
    rts = RoomType.all
  end
  rt_by_env = rts.group_by(&:environment)
  rt_by_env.each do |k, v|
    rt_by_env[k] = v.map { |r| [r.name, r.id] }
  end
  rt_by_env
end

.indoorActiveRecord::Relation<RoomType>

A relation of RoomTypes that are indoor. Active Record Scope

Returns:

See Also:



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

scope :indoor, -> { where(environment: 'Indoor').by_name }

.indoorsActiveRecord::Relation<RoomType>

A relation of RoomTypes that are indoors. Active Record Scope

Returns:

See Also:



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

scope :indoors, -> { where(environment: 'Indoor').by_name }

.options_by_environment_cached(environment) ⇒ Array<Hash{Symbol => String}>

Cached room type options for quote builder (by seo_key and name).

Only a non-empty result is cached. A transient empty read (a cold DB
mid-branch/restore, or a request racing ahead of seeding) must not be
written, or it poisons the cache for the full hour — every quote in that
environment then renders a blank "Application Type" dropdown and freezes
room_type_options: [] into the shared RhcParamSet. (Rails.cache.fetch
with skip_nil is not honored by every store, so cache explicitly.)

Parameters:

  • environment (String)

    'Indoor' or 'Outdoor'

Returns:

  • (Array<Hash{Symbol => String}>)

    { key: seo_key, value: name } entries, sorted by name



93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/models/room_type.rb', line 93

def self.options_by_environment_cached(environment)
  cache_key = ['room_type_options', environment]
  cached = Rails.cache.read(cache_key)
  return cached if cached.present?

  options = select(:name, :seo_key)
            .where(environment:)
            .pluck(:seo_key, :name)
            .sort_by { |rt| rt[1] }
            .map { |rt| { key: rt[0], value: rt[1] } }
  Rails.cache.write(cache_key, options, expires_in: 1.hour) if options.present?
  options
end

.options_for_select(ev = nil, parameterize = false) ⇒ Array<Array(String, Object)>

[label, value] pairs of room types for select dropdowns.

Parameters:

  • ev (String, nil) (defaults to: nil)

    environment filter ('Indoor'/'Outdoor')

  • parameterize (Boolean) (defaults to: false)

    use parameterized names as values instead of ids

Returns:

  • (Array<Array(String, Object)>)


73
74
75
76
77
78
79
80
81
# File 'app/models/room_type.rb', line 73

def self.options_for_select(ev = nil, parameterize = false)
  res = order(:name)
  res = res.by_environment(ev) if ev.present?
  if parameterize
    res.map { |f| [f.name, f.name.parameterize] }
  else
    res.map { |f| [f.name, f.id] }
  end
end

.outdoorActiveRecord::Relation<RoomType>

A relation of RoomTypes that are outdoor. Active Record Scope

Returns:

See Also:



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

scope :outdoor, -> { where(environment: 'Outdoor').by_name }

.outdoorsActiveRecord::Relation<RoomType>

A relation of RoomTypes that are outdoors. Active Record Scope

Returns:

See Also:



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

scope :outdoors, -> { where(environment: 'Outdoor').by_name }

Instance Method Details

#design_tool_fixturesActiveRecord::Relation<DesignToolFixture>

Design tool fixtures available for this room type.

Returns:

See Also:



29
# File 'app/models/room_type.rb', line 29

has_and_belongs_to_many :design_tool_fixtures

#heating_element_product_line_optionsActiveRecord::Relation<HeatingElementProductLineOption>

Heating-element product line options available for this room type.

Returns:

See Also:



24
# File 'app/models/room_type.rb', line 24

has_many :heating_element_product_line_options, inverse_of: :room_type

#is_indoor?Boolean

Whether this is an indoor room type.

Returns:

  • (Boolean)


59
60
61
# File 'app/models/room_type.rb', line 59

def is_indoor?
  environment == 'Indoor'
end

#is_outdoor?Boolean

Whether this is an outdoor room type.

Returns:

  • (Boolean)


65
66
67
# File 'app/models/room_type.rb', line 65

def is_outdoor?
  environment == 'Outdoor'
end

#minimum_sqft_for_iqFloat

Minimum room square footage required for an Instant Quote, by environment.

Returns:

  • (Float)


51
52
53
54
55
# File 'app/models/room_type.rb', line 51

def minimum_sqft_for_iq
  env_sym = :indoor
  env_sym = environment.to_s.downcase.to_sym if environment.present?
  MINIMUM_SQFT_FOR_IQ[env_sym]
end

#showcasesActiveRecord::Relation<Showcase>

Showcases featuring this room type.

Returns:

See Also:



26
# File 'app/models/room_type.rb', line 26

has_many :showcases

#storesActiveRecord::Relation<Store>

Stores offering this room type.

Returns:

  • (ActiveRecord::Relation<Store>)

See Also:



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

has_and_belongs_to_many :stores