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

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

Instance Method Details

- (ActiveRecord::Relation<Spree::Product>) active_products

Returns the active products the belong to this taxon

Returns:

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

    the active products the belong to this taxon



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

def active_products
  products.not_deleted.available
end

- (Array) applicable_filters

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



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

def applicable_filters
  fs = []
  # fs << ProductFilters.taxons_below(self)
  ## unless it's a root taxon? left open for demo purposes

  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

- (Object) child_index=(idx)



97
98
99
100
101
102
103
104
105
# File 'app/models/spree/taxon.rb', line 97

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


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

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


111
112
113
114
115
116
117
# File 'app/models/spree/taxon.rb', line 111

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

- (String) pretty_name

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



90
91
92
93
94
# File 'app/models/spree/taxon.rb', line 90

def pretty_name
  ancestor_chain = ancestors.map(&:name)
  ancestor_chain << name
  ancestor_chain.join(" -> ")
end

- (String) seo_title

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



48
49
50
51
52
53
54
# File 'app/models/spree/taxon.rb', line 48

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



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

def set_permalink
  permalink_tail = permalink.split('/').last if permalink.present?
  permalink_tail ||= name.to_url
  self.permalink_part = permalink_tail
end

- (String) to_param

Returns this taxon's permalink

Returns:

  • (String)

    this taxon's permalink



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

def to_param
  permalink
end

Update the permalinks for all children



73
74
75
# File 'app/models/spree/taxon.rb', line 73

def update_child_permalinks
  children.each(&:update_permalinks)
end

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



65
66
67
68
69
70
# File 'app/models/spree/taxon.rb', line 65

def update_permalinks
  set_permalink

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