Class: Gemgento::Category

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/gemgento/category.rb

Overview

Author:

  • Gemgento LLC

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#sync_neededObject

Returns the value of attribute sync_needed.



34
35
36
# File 'app/models/gemgento/category.rb', line 34

def sync_needed
  @sync_needed
end

Class Method Details

.grouped_option(category) ⇒ Array(String, Integer)

Get the option value and any subsequent child values as a path.

Parameters:

Returns:

  • (Array(String, Integer))


105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/gemgento/category.rb', line 105

def self.grouped_option(category)
  options = []

  category.children.where(is_active: true, parent: category).each do |child|
    options << [child.name, child.id]

    child.children.where(is_active: true).each do |nested_child|
      path = nested_child.tree_path
      path = path.slice((path.index("> #{child.name} >") + 2)..-1)
      options << [path, nested_child.id]
    end
  end

  return options
end

.grouped_optionsArray(String, Array(Array(String, Integer)))

Create an options array for use in an HTML select input. Categories are grouped under top level scoped categories.

Returns:

  • (Array(String, Array(Array(String, Integer))))


60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/models/gemgento/category.rb', line 60

def self.grouped_options
  options = []

  Gemgento::Category.top_level.each do |parent|

    child_options = [["#{parent.name} (parent)", parent.id]]

    parent.children.where(is_active: true, parent: parent).each do |child|
      child_options << [child.name, child.id]
      child_options << grouped_option(child) if child.children.where(is_active: true).any?
    end

    options << [parent.name, child_options]
  end

  return options
end

Instance Method Details

#after_touchObject



183
184
185
# File 'app/models/gemgento/category.rb', line 183

def after_touch
  # do nothing, plcaeholder
end

#ancestorsArray(Category)

Returns list of ancestors, starting from parent until root.

subchild1.ancestors # => [child1, root]

Returns:



163
164
165
166
167
# File 'app/models/gemgento/category.rb', line 163

def ancestors
  node, nodes = self, []
  nodes << node = node.parent while node.parent
  nodes
end

#descendentsObject



78
79
80
81
82
83
84
# File 'app/models/gemgento/category.rb', line 78

def descendents
  @descendents ||= begin
    children.includes(:children).map do |child|
      [child] + child.descendents
    end.flatten
  end
end

#enforce_positioningvoid

This method returns an undefined value.

Increment the position on all categories that come after. Used to ensure correct positioning after changing the position of a single category.



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/models/gemgento/category.rb', line 141

def enforce_positioning
  Category.skip_callback(:save, :after, :enforce_positioning)

  last_position = self.position
  categories = Category.where('parent_id = ? AND position >= ? AND id != ?', self.parent_id, self.position, self.id)
  categories.each do |category|
    break if category.position != last_position

    category.position = category.position + 1
    category.save

    last_position = category.position
  end

  Category.set_callback(:save, :after, :enforce_positioning)
end

#hierarchyArray(Gemgento::Category)

Ordered list from root to self, excluding self.

Returns:



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

def hierarchy
  parents = []
  parent = self.parent

  while parent && !parent.parent_id.nil?
    parents << parent
    parent = parent.parent
  end

  return parents.reverse
end

#mark_deletedvoid

This method returns an undefined value.

Sets the deleted_at to the current timestamp.



124
125
126
127
# File 'app/models/gemgento/category.rb', line 124

def mark_deleted
  self.deleted_at = Time.now
  self.shopify_adapter.destroy if self.shopify_adapter
end

#mark_deleted!void

This method returns an undefined value.

Marks the category as deleted and saves.



132
133
134
135
# File 'app/models/gemgento/category.rb', line 132

def mark_deleted!
  mark_deleted
  self.save
end

#products(store = nil) ⇒ ActiveRecord::Associations::CollectionProxy(Product)

Get products associated with the category. Optional scope of store.

Parameters:

  • store (Store, nil) (defaults to: nil)

Returns:

  • (ActiveRecord::Associations::CollectionProxy(Product))


173
174
175
176
177
178
179
180
181
# File 'app/models/gemgento/category.rb', line 173

def products(store = nil)
  return super if store.nil?

  Product
      .joins(:product_categories)
      .where('gemgento_product_categories.store_id = ? AND gemgento_product_categories.category_id = ?', store.id, self.id)
      .order('gemgento_product_categories.position ASC')
      .uniq
end

#sync_needed?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'app/models/gemgento/category.rb', line 52

def sync_needed?
  self.sync_needed.to_bool
end

#tree_pathString

Create a string representation of the path from the root to the Category.

Returns:

  • (String)


39
40
41
42
43
44
45
46
47
48
49
50
# File 'app/models/gemgento/category.rb', line 39

def tree_path
  root = Category.root
  parent = self.parent
  path = self.name

  while !parent.nil? && parent != root do
    path = "#{parent.name} > #{path}"
    parent = parent.parent
  end

  return path
end