Class: Spree::Taxon

Inherits:
Base
  • Object
show all
Extended by:
FriendlyId
Defined in:
app/models/spree/taxon.rb

Defined Under Namespace

Modules: ActiveStorageAttachment, PaperclipAttachment

Instance Method Summary collapse

Methods inherited from Base

display_includes

Methods included from Core::Permalinks

#generate_permalink, #save_permalink

Instance Method Details

#active_productsActiveRecord::Relation<Spree::Product>

Returns the active products the belong to this taxon.

Returns:

  • (ActiveRecord::Relation<Spree::Product>)

    the active products the belong to this taxon



70
71
72
# File 'app/models/spree/taxon.rb', line 70

def active_products
  products.not_deleted.available
end

#all_productsActiveRecord::Relation<Spree::Product>

Returns all self and descendant products.

Returns:

  • (ActiveRecord::Relation<Spree::Product>)

    all self and descendant products



75
76
77
78
79
80
# File 'app/models/spree/taxon.rb', line 75

def all_products
  scope = Product.joins(:taxons)
  scope.where(
    spree_taxons: { id: self_and_descendants.select(:id) }
  )
end

#all_variantsActiveRecord::Relation<Spree::Variant>

Returns all self and descendant variants, including master variants.

Returns:

  • (ActiveRecord::Relation<Spree::Variant>)

    all self and descendant variants, including master variants.



83
84
85
# File 'app/models/spree/taxon.rb', line 83

def all_variants
  Variant.where(product_id: all_products.select(:id))
end

#child_index=(idx) ⇒ Object



101
102
103
104
105
106
107
108
109
# File 'app/models/spree/taxon.rb', line 101

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



131
132
133
134
135
136
137
138
139
# File 'app/models/spree/taxon.rb', line 131

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


111
112
113
# File 'app/models/spree/taxon.rb', line 111

def permalink_part
  permalink.split('/').last
end


115
116
117
118
119
120
121
# File 'app/models/spree/taxon.rb', line 115

def permalink_part=(value)
  if parent.present?
    self.permalink = "#{parent.permalink}/#{value}"
  else
    self.permalink = value
  end
end

#pretty_nameString

Returns this taxon’s ancestors names followed by its own name, separated by arrows.

Returns:

  • (String)

    this taxon’s ancestors names followed by its own name, separated by arrows



89
90
91
92
93
94
95
96
97
98
# File 'app/models/spree/taxon.rb', line 89

def pretty_name
  if parent.present?
    [
      parent.pretty_name,
      name
    ].compact.join(" -> ")
  else
    name
  end
end

#seo_titleString

Returns meta_title if set otherwise a string containing the root name and taxon name.

Returns:

  • (String)

    meta_title if set otherwise a string containing the root name and taxon name



35
36
37
38
39
40
41
# File 'app/models/spree/taxon.rb', line 35

def seo_title
  if meta_title.present?
    meta_title
  else
    root? ? name : "#{root.name} - #{name}"
  end
end

Sets this taxons permalink to a valid url encoded string based on its name and its parents permalink (if present.)



45
46
47
48
# File 'app/models/spree/taxon.rb', line 45

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

Returns:

  • (Boolean)


125
126
127
# File 'app/models/spree/taxon.rb', line 125

def should_generate_new_friendly_id?
  permalink_changed? || super
end

#to_paramString

Returns this taxon’s permalink.

Returns:

  • (String)

    this taxon’s permalink



64
65
66
# File 'app/models/spree/taxon.rb', line 64

def to_param
  permalink
end

Update the permalinks for all children



59
60
61
# File 'app/models/spree/taxon.rb', line 59

def update_child_permalinks
  children.each(&:update_permalinks)
end

Update the permalink for this taxon and all children (if necessary)



51
52
53
54
55
56
# File 'app/models/spree/taxon.rb', line 51

def update_permalinks
  set_permalink

  # This will trigger update_child_permalinks if permalink has changed
  save!
end