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

Class Method Details

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



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

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



16
17
18
19
20
# File 'app/models/comable/category.rb', line 16

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



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

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



12
13
14
# File 'app/models/comable/category.rb', line 12

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

.to_jstree(options = {}) ⇒ Object



29
30
31
# File 'app/models/comable/category.rb', line 29

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