Class: Spree::Taxon
- Extended by:
- FriendlyId
- Defined in:
- app/models/spree/taxon.rb
Defined Under Namespace
Modules: ActiveStorageAttachment, PaperclipAttachment
Instance Method Summary collapse
-
#active_products ⇒ ActiveRecord::Relation<Spree::Product>
The active products the belong to this taxon.
-
#all_products ⇒ ActiveRecord::Relation<Spree::Product>
All self and descendant products.
-
#all_variants ⇒ ActiveRecord::Relation<Spree::Variant>
All self and descendant variants, including master variants.
- #child_index=(idx) ⇒ Object
-
#normalize_friendly_id(value) ⇒ Object
override for FriendlyId::Slugged#normalize_friendly_id method, to control over the slug format.
- #permalink_part ⇒ Object
- #permalink_part=(value) ⇒ Object
-
#pretty_name ⇒ String
This taxon’s ancestors names followed by its own name, separated by arrows.
-
#seo_title ⇒ String
Meta_title if set otherwise a string containing the root name and taxon name.
-
#set_permalink ⇒ Object
Sets this taxons permalink to a valid url encoded string based on its name and its parents permalink (if present.).
-
#should_generate_new_friendly_id? ⇒ Boolean
override for FriendlyId::Slugged#should_generate_new_friendly_id? method, to control exactly when new friendly ids are set or updated.
-
#to_param ⇒ String
This taxon’s permalink.
-
#update_child_permalinks ⇒ Object
Update the permalinks for all children.
-
#update_permalinks ⇒ Object
Update the permalink for this taxon and all children (if necessary).
Methods inherited from Base
Methods included from Core::Permalinks
#generate_permalink, #save_permalink
Instance Method Details
#active_products ⇒ ActiveRecord::Relation<Spree::Product>
Returns the active products the belong to this taxon.
73 74 75 |
# File 'app/models/spree/taxon.rb', line 73 def active_products products.not_deleted.available end |
#all_products ⇒ ActiveRecord::Relation<Spree::Product>
Returns all self and descendant products.
78 79 80 81 82 83 |
# File 'app/models/spree/taxon.rb', line 78 def all_products scope = Product.joins(:taxons) scope.where( spree_taxons: { id: self_and_descendants.select(:id) } ) end |
#all_variants ⇒ ActiveRecord::Relation<Spree::Variant>
Returns all self and descendant variants, including master variants.
86 87 88 |
# File 'app/models/spree/taxon.rb', line 86 def all_variants Variant.where(product_id: all_products.select(:id)) end |
#child_index=(idx) ⇒ Object
104 105 106 107 108 109 110 111 112 |
# File 'app/models/spree/taxon.rb', line 104 def child_index=(idx) # awesome_nested_set sorts by :lft and :rgt. This call re-inserts the # child node so that its resulting position matches the observable # 0-indexed position. # # NOTE: no :position column needed - awesom_nested_set doesn't handle the # reordering if you bring your own :order_column. move_to_child_with_index(parent, idx.to_i) unless new_record? end |
#normalize_friendly_id(value) ⇒ Object
override for FriendlyId::Slugged#normalize_friendly_id method, to control over the slug format
134 135 136 137 138 139 140 141 142 |
# File 'app/models/spree/taxon.rb', line 134 def normalize_friendly_id(value) return '' if value.blank? parts = value.to_s.split('/') last_word = parts.pop corrected_last_word = Spree::Config.taxon_url_parametizer_class.parameterize(last_word) (parts << corrected_last_word).join('/') end |
#permalink_part ⇒ Object
114 115 116 |
# File 'app/models/spree/taxon.rb', line 114 def permalink_part permalink.split('/').last end |
#permalink_part=(value) ⇒ Object
118 119 120 121 122 123 124 |
# File 'app/models/spree/taxon.rb', line 118 def permalink_part=(value) if parent.present? self.permalink = "#{parent.permalink}/#{value}" else self.permalink = value end end |
#pretty_name ⇒ String
Returns this taxon’s ancestors names followed by its own name, separated by arrows.
92 93 94 95 96 97 98 99 100 101 |
# File 'app/models/spree/taxon.rb', line 92 def pretty_name if parent.present? [ parent.pretty_name, name ].compact.join(" -> ") else name end end |
#seo_title ⇒ String
Returns meta_title if set otherwise a string containing the root name and taxon name.
38 39 40 41 42 43 44 |
# File 'app/models/spree/taxon.rb', line 38 def seo_title if .present? else root? ? name : "#{root.name} - #{name}" end end |
#set_permalink ⇒ Object
Sets this taxons permalink to a valid url encoded string based on its name and its parents permalink (if present.)
48 49 50 51 |
# File 'app/models/spree/taxon.rb', line 48 def set_permalink permalink_tail = permalink.present? ? permalink.split('/').last : name self.permalink_part = Spree::Config.taxon_url_parametizer_class.parameterize(permalink_tail) end |
#should_generate_new_friendly_id? ⇒ Boolean
override for FriendlyId::Slugged#should_generate_new_friendly_id? method, to control exactly when new friendly ids are set or updated
128 129 130 |
# File 'app/models/spree/taxon.rb', line 128 def should_generate_new_friendly_id? permalink_changed? || super end |
#to_param ⇒ String
Returns this taxon’s permalink.
67 68 69 |
# File 'app/models/spree/taxon.rb', line 67 def to_param permalink end |
#update_child_permalinks ⇒ Object
Update the permalinks for all children
62 63 64 |
# File 'app/models/spree/taxon.rb', line 62 def update_child_permalinks children.each(&:update_permalinks) end |
#update_permalinks ⇒ Object
Update the permalink for this taxon and all children (if necessary)
54 55 56 57 58 59 |
# File 'app/models/spree/taxon.rb', line 54 def update_permalinks set_permalink # This will trigger update_child_permalinks if permalink has changed save! end |