Module: SpreeCmCommissioner::VariantOptionsConcern
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/models/concerns/spree_cm_commissioner/variant_options_concern.rb
Instance Method Summary collapse
- #end_date ⇒ Object
-
#end_date_time ⇒ Object
No need to fallback to event end date to avoid n+1.
- #end_time ⇒ Object
- #find_option_value_name_for(option_type_name: nil) ⇒ Object
- #option_value_name_for(option_type_name: nil) ⇒ Object
- #options ⇒ Object
- #options_in_hash ⇒ Object
-
#options_text ⇒ Object
Override variant.rb to return cached formatted options text, avoiding repeated database queries.
- #post_paid? ⇒ Boolean
- #set_options_to_public_metadata ⇒ Object
- #set_options_to_public_metadata! ⇒ Object
-
#start_date_time ⇒ Object
No need to fallback to event start date to avoid n+1.
Instance Method Details
#end_date ⇒ Object
82 83 84 85 86 |
# File 'app/models/concerns/spree_cm_commissioner/variant_options_concern.rb', line 82 def end_date return start_date + .total_duration_in_seconds.seconds if start_date.present? && .total_duration_in_seconds.positive? .end_date end |
#end_date_time ⇒ Object
No need to fallback to event end date to avoid n+1. To get end_time duration of event, include event when needed instead.
74 75 76 77 78 79 80 |
# File 'app/models/concerns/spree_cm_commissioner/variant_options_concern.rb', line 74 def end_date_time return nil if end_date.blank? && end_time.blank? return end_date if end_time.blank? return end_time if end_date.blank? end_date.change(hour: end_time.hour, min: end_time.min, sec: end_time.sec) end |
#end_time ⇒ Object
88 89 90 91 92 |
# File 'app/models/concerns/spree_cm_commissioner/variant_options_concern.rb', line 88 def end_time return start_time + .total_duration_in_seconds.seconds if start_time.present? && .total_duration_in_seconds.positive? .end_time end |
#find_option_value_name_for(option_type_name: nil) ⇒ Object
58 59 60 |
# File 'app/models/concerns/spree_cm_commissioner/variant_options_concern.rb', line 58 def find_option_value_name_for(option_type_name: nil) option_values.detect { |o| o.option_type.name.downcase.strip == option_type_name.downcase.strip }.try(:name) end |
#option_value_name_for(option_type_name: nil) ⇒ Object
52 53 54 55 56 |
# File 'app/models/concerns/spree_cm_commissioner/variant_options_concern.rb', line 52 def option_value_name_for(option_type_name: nil) return [option_type_name] if .present? # empty is not considered present? find_option_value_name_for(option_type_name: option_type_name) end |
#options ⇒ Object
44 45 46 |
# File 'app/models/concerns/spree_cm_commissioner/variant_options_concern.rb', line 44 def @options ||= VariantOptions.new(self) end |
#options_in_hash ⇒ Object
48 49 50 |
# File 'app/models/concerns/spree_cm_commissioner/variant_options_concern.rb', line 48 def [:cm_options] end |
#options_text ⇒ Object
Override variant.rb to return cached formatted options text, avoiding repeated database queries. Falls back to database queries & computing the format if preload data is unavailable.
40 41 42 |
# File 'app/models/concerns/spree_cm_commissioner/variant_options_concern.rb', line 40 def @options_text ||= [:preload_options_text].presence || Spree::Variants::VisableOptionsPresenter.new(self).to_sentence end |
#post_paid? ⇒ Boolean
94 95 96 |
# File 'app/models/concerns/spree_cm_commissioner/variant_options_concern.rb', line 94 def post_paid? .payment_option == 'post-paid' end |
#set_options_to_public_metadata ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 |
# File 'app/models/concerns/spree_cm_commissioner/variant_options_concern.rb', line 98 def self. ||= {} # Cache option values as a hash to avoid N+1 queries when accessing options. # Stores {option_type_name => option_value_name} pairs that can be retrieved # without loading option_types and option_values associations. # Example: "Red, 256GB" - formatted options for quick display self.[:cm_options] = option_values.each_with_object({}) do |option_value, hash| option_type_name = option_value.option_type.name hash[option_type_name] = find_option_value_name_for(option_type_name: option_type_name) end # Cache formatted options text for quick retrieval via #options_text. # Precomputes the human-readable format (e.g., "Red, 256GB") to avoid # repeated formatting and association queries. # Example: {"color" => "red", "storage" => "256GB"} - option_type_name => option_value_name pairs self.[:preload_options_text] = Spree::Variants::VisableOptionsPresenter.new(self).to_sentence end |
#set_options_to_public_metadata! ⇒ Object
117 118 119 120 |
# File 'app/models/concerns/spree_cm_commissioner/variant_options_concern.rb', line 117 def save! end |
#start_date_time ⇒ Object
No need to fallback to event start date to avoid n+1. To get end_time duration of event, include event when needed instead.
64 65 66 67 68 69 70 |
# File 'app/models/concerns/spree_cm_commissioner/variant_options_concern.rb', line 64 def start_date_time return nil if start_date.blank? && start_time.blank? return start_date if start_time.blank? return start_time if start_date.blank? start_date.change(hour: start_time.hour, min: start_time.min, sec: start_time.sec) end |