Class: Spree::Taxon

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

Instance Method Summary collapse

Methods inherited from Base

display_includes, #initialize_preference_defaults, page, preference

Methods included from Preferences::Preferable

#admin_form_preference_names, #default_preferences, #defined_preferences, #get_preference, #has_preference!, #has_preference?, #preference_default, #preference_type, #set_preference

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



87
88
89
# File 'app/models/spree/taxon.rb', line 87

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



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

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.



100
101
102
# File 'app/models/spree/taxon.rb', line 100

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

#applicable_filtersArray

Note:

This method is meant to be overridden on a store by store basis.

Returns filters that should be used for a taxon.

Returns:

  • (Array)

    filters that should be used for a taxon



40
41
42
43
44
45
46
47
# File 'app/models/spree/taxon.rb', line 40

def applicable_filters
  Spree::Deprecation.warn "Spree::Taxon#applicable_filters is deprecated, if you are using this functionality please move it into your own application."

  fs = []
  fs << Spree::Core::ProductFilters.price_filter if Spree::Core::ProductFilters.respond_to?(:price_filter)
  fs << Spree::Core::ProductFilters.brand_filter if Spree::Core::ProductFilters.respond_to?(:brand_filter)
  fs
end

#child_index=(idx) ⇒ Object



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

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


123
124
125
# File 'app/models/spree/taxon.rb', line 123

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


127
128
129
130
131
132
133
# File 'app/models/spree/taxon.rb', line 127

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



106
107
108
109
110
# File 'app/models/spree/taxon.rb', line 106

def pretty_name
  ancestor_chain = ancestors.map(&:name)
  ancestor_chain << name
  ancestor_chain.join(" -> ")
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



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

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.)



61
62
63
64
65
# File 'app/models/spree/taxon.rb', line 61

def set_permalink
  permalink_tail = permalink.split('/').last if permalink.present?
  permalink_tail ||= Spree::Config.taxon_url_parametizer_class.parameterize(name)
  self.permalink_part = permalink_tail
end

#to_paramString

Returns this taxon’s permalink.

Returns:

  • (String)

    this taxon’s permalink



81
82
83
# File 'app/models/spree/taxon.rb', line 81

def to_param
  permalink
end

Update the permalinks for all children



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

def update_child_permalinks
  children.each(&:update_permalinks)
end

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



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

def update_permalinks
  set_permalink

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