Class: SpreeCmCommissioner::HomepageDataLoader

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serialization
Defined in:
app/services/spree_cm_commissioner/homepage_data_loader.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHomepageDataLoader

Returns a new instance of HomepageDataLoader.



15
16
17
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 15

def initialize
  @config = SpreeCmCommissioner::Configuration.new
end

Instance Attribute Details

#configObject

Returns the value of attribute config.



5
6
7
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 5

def config
  @config
end

#display_product_idsObject

Returns the value of attribute display_product_ids.



5
6
7
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 5

def display_product_ids
  @display_product_ids
end

#display_productsObject

Returns the value of attribute display_products.



5
6
7
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 5

def display_products
  @display_products
end

Returns the value of attribute featured_brand_ids.



5
6
7
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 5

def featured_brand_ids
  @featured_brand_ids
end

Returns the value of attribute featured_brands.



5
6
7
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 5

def featured_brands
  @featured_brands
end

Returns the value of attribute featured_vendor_ids.



5
6
7
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 5

def featured_vendor_ids
  @featured_vendor_ids
end

Returns the value of attribute featured_vendors.



5
6
7
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 5

def featured_vendors
  @featured_vendors
end

#homepage_background_idsObject

Returns the value of attribute homepage_background_ids.



5
6
7
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 5

def homepage_background_ids
  @homepage_background_ids
end

#homepage_backgroundsObject

Returns the value of attribute homepage_backgrounds.



5
6
7
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 5

def homepage_backgrounds
  @homepage_backgrounds
end

#homepage_banner_idsObject

Returns the value of attribute homepage_banner_ids.



5
6
7
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 5

def homepage_banner_ids
  @homepage_banner_ids
end

#homepage_bannersObject

Returns the value of attribute homepage_banners.



5
6
7
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 5

def homepage_banners
  @homepage_banners
end

#idObject

Returns the value of attribute id.



5
6
7
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 5

def id
  @id
end

#top_categoriesObject

Returns the value of attribute top_categories.



5
6
7
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 5

def top_categories
  @top_categories
end

#top_category_idsObject

Returns the value of attribute top_category_ids.



5
6
7
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 5

def top_category_ids
  @top_category_ids
end

Returns the value of attribute trending_categories.



5
6
7
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 5

def trending_categories
  @trending_categories
end

Returns the value of attribute trending_category_ids.



5
6
7
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 5

def trending_category_ids
  @trending_category_ids
end

Class Method Details

.cache_key(_options = {}) ⇒ Object



19
20
21
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 19

def self.cache_key(_options = {})
  'homepage_data_loader'
end

.clear_cache(_options = {}) ⇒ Object



23
24
25
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 23

def self.clear_cache(_options = {})
  Rails.cache.delete(cache_key)
end

.with_cacheObject



27
28
29
30
31
32
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 27

def self.with_cache
  Rails.cache.fetch(cache_key) do
    data_loader = new
    data_loader.call
  end
end

Instance Method Details

#callObject



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 34

def call
  set_record_id
  set_homepage_backgrounds
  set_homepage_banners
  set_featured_vendors

  # set_trending_categories
  # set_top_catgories
  # set_display_products
  # set_featured_brands

  self
end

#set_display_productsObject



102
103
104
105
106
107
108
109
110
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 102

def set_display_products
  config_taxon_ids = @config.displayed_product_taxon_ids
  taxon_ids = config_taxon_ids.present? ? config_taxon_ids.split(',') : []

  return @display_products = Spree::Taxon.none if taxon_ids.blank?

  @display_products = SpreeCmCommissioner::Feed::TaxonProduct.call(taxon_ids, limit: 6, serialize_data: true)
  @display_product_ids = @display_products.map(&:id)
end


82
83
84
85
86
87
88
89
90
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 82

def set_featured_brands
  config_taxon_ids = @config.featured_brand_taxon_ids
  taxon_ids = config_taxon_ids.present? ? config_taxon_ids.split(',') : []

  return @featured_brands = Spree::Taxon.none if taxon_ids.blank?

  @featured_brands = Spree::Taxon.where(id: taxon_ids).limit(12).order(:lft)
  @featured_brand_ids = @featured_brands.map(&:id)
end


92
93
94
95
96
97
98
99
100
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 92

def set_featured_vendors
  config_vendor_ids = @config.featured_vendor_ids
  vendor_ids = config_vendor_ids.present? ? config_vendor_ids.split(',') : []

  return @featured_vendors = Spree::Vendor.none if vendor_ids.blank?

  @featured_vendors = Spree::Vendor.where(id: vendor_ids)
  @featured_vendor_ids = @featured_vendors.pluck(:id)
end

#set_homepage_backgroundsObject



52
53
54
55
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 52

def set_homepage_backgrounds
  @homepage_backgrounds = SpreeCmCommissioner::HomepageBackground.active.includes(app_image: :attachment_blob).order(:priority)
  @homepage_background_ids = @homepage_backgrounds.pluck(:id)
end

#set_homepage_bannersObject



57
58
59
60
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 57

def set_homepage_banners
  @homepage_banners = SpreeCmCommissioner::HomepageBanner.active.includes(app_image: :attachment_blob).order(:priority)
  @homepage_banner_ids = @homepage_banners.pluck(:id)
end

#set_record_idObject



48
49
50
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 48

def set_record_id
  @id = SecureRandom.hex
end

#set_top_catgoriesObject



62
63
64
65
66
67
68
69
70
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 62

def set_top_catgories
  config_taxon_ids = @config.top_category_taxon_ids
  taxon_ids = config_taxon_ids.present? ? config_taxon_ids.split(',') : []

  return @trending_categories = Spree::Taxon.none if taxon_ids.blank?

  @top_categories = Spree::Taxon.where(id: taxon_ids).limit(4).order(:lft)
  @top_category_ids = @top_categories.map(&:id)
end


72
73
74
75
76
77
78
79
80
# File 'app/services/spree_cm_commissioner/homepage_data_loader.rb', line 72

def set_trending_categories
  config_taxon_ids = @config.trending_category_taxon_ids
  taxon_ids = config_taxon_ids.present? ? config_taxon_ids.split(',') : []

  return @trending_categories = Spree::Taxon.none if taxon_ids.blank?

  @trending_categories = Spree::Taxon.where(id: taxon_ids).limit(5).order(:lft)
  @trending_category_ids = @trending_categories.map(&:id)
end