Module: Workarea::Details
- Extended by:
- ActiveSupport::Concern
- Included in:
- Catalog::Product, Catalog::Variant
- Defined in:
- app/models/workarea/details.rb
Instance Method Summary collapse
-
#detail_names ⇒ Array<String>
Output all names of all details currently stored on this model.
-
#fetch_detail(name) ⇒ Object
Find a detail based on a name.
-
#has_detail?(name) ⇒ Boolean
Determine whether this detail applies as an option for this variant.
-
#matches_detail?(name, value) ⇒ Boolean
Determine whether this variant is a match to the passed detail.
-
#matches_details?(details) ⇒ Boolean
Determine whether this variant is a match to the all of the passed details.
-
#update_details(values) ⇒ void
Update details by replacing existing detail values with new detail values.
Instance Method Details
#detail_names ⇒ Array<String>
Output all names of all details currently stored on this model. If the model has been rehydrated from a raw #as_document
Hash of parameters, this method also dives into the current I18n.locale
. On models generated by Mongoid or within the context of the current application scope, this method will just read #keys
from the details hash stored on the model.
94 95 96 97 98 99 100 101 102 103 |
# File 'app/models/workarea/details.rb', line 94 def detail_names locale = I18n.locale.to_s hash = if details.keys.include?(locale) details[locale] else details end hash.keys end |
#fetch_detail(name) ⇒ Object
Find a detail based on a name. Uses string optionizing to determine whether the key on the details hash matches. Used when looking up options in view models.
36 37 38 39 40 41 42 43 |
# File 'app/models/workarea/details.rb', line 36 def fetch_detail(name) key = details.keys.detect do |tmp| tmp.to_s.optionize == name.to_s.optionize end return nil unless key.present? details[key] end |
#has_detail?(name) ⇒ Boolean
Determine whether this detail applies as an option for this variant
70 71 72 |
# File 'app/models/workarea/details.rb', line 70 def has_detail?(name) fetch_detail(name).present? end |
#matches_detail?(name, value) ⇒ Boolean
Determine whether this variant is a match to the passed detail. Used when selecting variants in view models.
54 55 56 57 58 59 60 61 62 63 |
# File 'app/models/workarea/details.rb', line 54 def matches_detail?(name, value) detail_value = fetch_detail(name) return false if detail_value.blank? if detail_value.respond_to?(:map) detail_value.map(&:to_s).map(&:optionize).include?(value.optionize) else detail_value.to_s.optionize == value.optionize end end |
#matches_details?(details) ⇒ Boolean
Determine whether this variant is a match to the all of the passed details.
80 81 82 83 84 |
# File 'app/models/workarea/details.rb', line 80 def matches_details?(details) details.all? do |key, value| Array.wrap(value).all? { |v| matches_detail?(key, v) } end end |
#update_details(values) ⇒ void
This method returns an undefined value.
Update details by replacing existing detail values with new detail values. If a detail has been explicitly set to blank in the new details, then that detail will be deleted.
17 18 19 20 21 22 23 24 25 26 |
# File 'app/models/workarea/details.rb', line 17 def update_details(values) values ||= {} self.details ||= {} details.merge!(values) details.each do |name, value| details.delete(name) if value.blank? end end |