Class: SpreeCmCommissioner::HomepageSectionRelatable

Inherits:
Base
  • Object
show all
Defined in:
app/models/spree_cm_commissioner/homepage_section_relatable.rb

Constant Summary collapse

RELATABLE_CLASS_BY_PREVIEWABLE_TYPE =

This constant help preview sections to get the relatable class from the previewable_type of the preview role.

{
  'Spree::Taxon' => 'Spree::Taxon',
  'SpreeCmCommissioner::Show' => 'Spree::Taxon',
  'Spree::Product' => 'Spree::Product'
}.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.section_ids_previewable_by(user) ⇒ Object

homepage_section ids reachable through a user's preview roles



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/spree_cm_commissioner/homepage_section_relatable.rb', line 29

def self.section_ids_previewable_by(user)
  # get previewable_id/previewable_type from the user's preview roles,
  # eg: [[798, "SpreeCmCommissioner::Show"], [245, "Spree::Product"], [449, "Spree::Taxon"]
  # Then batch the ids by relatable_type so we run one query per type instead of one per row
  # eg. {"Spree::Taxon" => [798, 449], "Spree::Product" => [245]}
  ids_by_relatable_type = user.preview_roles.pluck(:previewable_id, :previewable_type)
                              .each_with_object(Hash.new { |h, k| h[k] = [] }) do |(id, previewable_type), acc|
    relatable_type = RELATABLE_CLASS_BY_PREVIEWABLE_TYPE[previewable_type]
    acc[relatable_type] << id if relatable_type
  end

  # find homepage_section_relatables matching those ids/types, then return the unique homepage_section ids
  ids_by_relatable_type.flat_map do |relatable_type, ids|
    where(relatable_type: relatable_type, relatable_id: ids).pluck(:homepage_section_id)
  end.uniq
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
# File 'app/models/spree_cm_commissioner/homepage_section_relatable.rb', line 15

def active?
  current_time = Time.zone.now
  (available_on.nil? || available_on <= current_time) &&
    (discontinue_on.nil? || discontinue_on >= current_time)
end