Module: SpreeCmCommissioner::TaxonDecorator

Defined in:
app/models/spree_cm_commissioner/taxon_decorator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object

rubocop:disable Metrics/AbcSize,Metrics/MethodLength



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/models/spree_cm_commissioner/taxon_decorator.rb', line 3

def self.prepended(base) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
  base.include SpreeCmCommissioner::TaxonKind
  base.include SpreeCmCommissioner::Transit::TaxonBitwise

  base.preference :background_color, :string
  base.preference :foreground_color, :string

  base.has_many :taxon_vendors, class_name: 'SpreeCmCommissioner::TaxonVendor'
  base.has_many :vendors, through: :taxon_vendors

  base.has_many :guest_card_classes, class_name: 'SpreeCmCommissioner::GuestCardClass'
  base.has_many :homepage_section_relatables,
                class_name: 'SpreeCmCommissioner::HomepageSectionRelatable',
                dependent: :destroy, as: :relatable

  base.has_many :user_events, class_name: 'SpreeCmCommissioner::UserEvent'
  base.has_many :users, through: :user_events, class_name: Spree.user_class.to_s
  base.has_many :products, through: :classifications, class_name: 'Spree::Product'
  base.has_many :guests, foreign_key: :event_id, class_name: 'SpreeCmCommissioner::Guest', dependent: :nullify
  base.has_many :check_ins, as: :checkinable, class_name: 'SpreeCmCommissioner::CheckIn', dependent: :nullify
  base.has_many :customer_taxons, class_name: 'SpreeCmCommissioner::CustomerTaxon'

  base.has_one :category_icon, as: :viewable, dependent: :destroy, class_name: 'SpreeCmCommissioner::TaxonCategoryIcon'

  base.has_one :web_banner, as: :viewable, dependent: :destroy, class_name: 'SpreeCmCommissioner::TaxonWebBanner'
  base.has_one :app_banner, as: :viewable, dependent: :destroy, class_name: 'SpreeCmCommissioner::TaxonAppBanner'
  base.has_one :home_banner, as: :viewable, dependent: :destroy, class_name: 'SpreeCmCommissioner::TaxonHomeBanner'
  base.has_one :video_banner, as: :viewable, dependent: :destroy, class_name: 'SpreeCmCommissioner::TaxonVideoBanner'

  # Update children association to work with nested set (lft, rgt)
  base.has_many :children, -> { order(:lft) }, class_name: 'Spree::Taxon', foreign_key: :parent_id, dependent: :destroy
  base.has_many :children_classifications, through: :children, source: :classifications, class_name: 'Spree::Classification'
  base.has_many :children_products, through: :children_classifications, class_name: 'Spree::Product', source: :product

  base.has_many :notification_taxons, class_name: 'SpreeCmCommissioner::NotificationTaxon'
  base.has_many :customer_notifications, through: :notification_taxons, class_name: 'SpreeCmCommissioner::CustomerNotification'

  base.has_many :visible_classifications, -> { where(visible: true).order(:position) }, class_name: 'Spree::Classification'
  base.has_many :visible_products, through: :visible_classifications, class_name: 'Spree::Product', source: :product

  base.belongs_to :vendor, class_name: 'Spree::Vendor'

  base.validates_associated :category_icon
  base.before_save :set_kind
  base.before_save :set_slug

  base.after_update :sync_event_dates_to_line_items, if: -> { saved_change_to_from_date? || saved_change_to_to_date? }

  base.whitelisted_ransackable_associations |= %w[vendor]
  base.whitelisted_ransackable_attributes |= %w[kind from_date to_date]

  base.enum purchasable_on: { both: 0, web: 1, app: 2 }
  base.has_many :crew_invites, class_name: 'SpreeCmCommissioner::CrewInvite', dependent: :destroy
  base.has_many :invite_user_events, through: :user_events, class_name: 'SpreeCmCommissioner::InviteUserEvent'

  base.has_many :line_items, through: :products
  base.has_many :event_line_items, class_name: 'Spree::LineItem', foreign_key: :event_id

  base.has_many :event_blazer_queries, class_name: 'SpreeCmCommissioner::TaxonBlazerQuery'
  base.has_many :blazer_queries, through: :event_blazer_queries, class_name: 'Blazer::Query'

  base.has_many :taxon_option_types, class_name: 'SpreeCmCommissioner::TaxonOptionType'
  base.has_many :taxon_option_values, class_name: 'SpreeCmCommissioner::TaxonOptionValue'

  base.after_commit :send_transaction_email_to_organizers, on: :create

  def base.active_homepage_events
    joins(:homepage_section_relatables)
      .joins("INNER JOIN spree_taxons taxon ON taxon.id = cm_homepage_section_relatables.relatable_id
              AND cm_homepage_section_relatables.relatable_type = 'Spree::Taxon'"
            )
      .joins('INNER JOIN cm_homepage_sections ON cm_homepage_section_relatables.homepage_section_id = cm_homepage_sections.id')
      .where(cm_homepage_sections: { tenant_id: nil, active: true })
      .where(kind: :event)
  end

  def base.find_event(id)
    find_by(slug: "events-#{id}")
  end
end

Instance Method Details

#background_colorObject



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

def background_color
  preferred_background_color
end

#event_slugObject



100
101
102
# File 'app/models/spree_cm_commissioner/taxon_decorator.rb', line 100

def event_slug
  slug.sub(/^events-/, '')
end

#event_urlObject



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

def event_url
  "https://#{Spree::Store.default.url}/t/#{permalink}"
end

#foreground_colorObject



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

def foreground_color
  preferred_foreground_color
end

#products_option_type_namesObject



104
105
106
107
108
109
# File 'app/models/spree_cm_commissioner/taxon_decorator.rb', line 104

def products_option_type_names
  Spree::OptionType.joins(products: :taxons)
                   .where(spree_taxons: { id: child_ids })
                   .pluck('spree_option_types.name')
                   .uniq
end

#selected_option_typesObject



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

def selected_option_types
  taxon_option_types.pluck(:option_type_id)
end

#selected_option_valuesObject



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

def selected_option_values
  taxon_option_values.pluck(:option_value_id)
end

#send_transaction_email_to_organizersObject



123
124
125
126
127
# File 'app/models/spree_cm_commissioner/taxon_decorator.rb', line 123

def send_transaction_email_to_organizers
  return unless kind == 'event' && level == 1

  SpreeCmCommissioner::OrganizersTransactionalEmailNotifier.call(event_id: id)
end

#set_kindObject



92
93
94
# File 'app/models/spree_cm_commissioner/taxon_decorator.rb', line 92

def set_kind
  self.kind = taxonomy.kind
end

#set_slugObject



96
97
98
# File 'app/models/spree_cm_commissioner/taxon_decorator.rb', line 96

def set_slug
  self.slug = permalink&.parameterize
end

#sync_event_dates_to_line_itemsObject



129
130
131
132
133
# File 'app/models/spree_cm_commissioner/taxon_decorator.rb', line 129

def sync_event_dates_to_line_items
  return unless event? && depth == 1

  ::SpreeCmCommissioner::EventLineItemsDateSyncerJob.perform_later(id)
end