Class: Comable::Category

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

Constant Summary collapse

DEFAULT_PATH_NAME_DELIMITER =
' > '

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_path_name(path_name, root: nil, delimiter: DEFAULT_PATH_NAME_DELIMITER) ⇒ Object



24
25
26
27
28
29
# File 'app/models/comable/category.rb', line 24

def find_by_path_name(path_name, root: nil, delimiter: DEFAULT_PATH_NAME_DELIMITER)
  names = path_name.split(delimiter)
  names.inject(root) do |category, name|
    (category ? category.children : roots).find_by(name: name) || break
  end
end

.find_by_path_names(path_names, delimiter: DEFAULT_PATH_NAME_DELIMITER) ⇒ Object



18
19
20
21
22
# File 'app/models/comable/category.rb', line 18

def find_by_path_names(path_names, delimiter: DEFAULT_PATH_NAME_DELIMITER)
  path_names.map do |path_name|
    find_by_path_name(path_name, delimiter: delimiter)
  end.compact
end

.from_jstree!(jstree_json) ⇒ Object



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

def from_jstree!(jstree_json)
  jstree = JSON.parse(jstree_json)

  transaction do
    restore_from_jstree!(jstree)
  end
end

.path_names(delimiter: DEFAULT_PATH_NAME_DELIMITER) ⇒ Object



14
15
16
# File 'app/models/comable/category.rb', line 14

def path_names(delimiter: DEFAULT_PATH_NAME_DELIMITER)
  categoris.path(&:name).join(delimiter)
end

.to_jstree(options = {}) ⇒ Object



31
32
33
# File 'app/models/comable/category.rb', line 31

def to_jstree(options = {})
  build_to_jstree(arrange_serializable(order: :position), options).to_json
end

Instance Method Details

#touch_all_productsObject



74
75
76
# File 'app/models/comable/category.rb', line 74

def touch_all_products
  products.update_all(updated_at: Time.now)
end