Class: ArCategory

Inherits:
ApplicationRecord
  • Object
show all
Defined in:
app/models/ar_category.rb

Overview

Schema information

Table name: ar_category : Categories

id Integer id created_at Time created_at updated_at Time Last update name String Category name description String Additional description of category ctype Integer Category type. Could be used for grouping categories. parent Integer Parent category. Leave blank if this is top level category. active Boolean Category is active. order Integer Additional order, which can be used for sorting. created_by Integer created_by updated_by Integer updated_by

Categories are used on ArPage documents for grouping documents. Categorization is most useful for grouping news, blog entries ...

Class Method Summary collapse

Class Method Details

.choices_for_categories(site_id = nil) ⇒ Object

Returns choices for all categories, prepared for tree_select input field



99
100
101
102
103
104
105
106
107
# File 'app/models/ar_category.rb', line 99

def self.choices_for_categories(site_id = nil)
  qry = where(active: true)
  ar = [nil]
  if site_id
    ar << (site_id.instance_of?(Integer) ? site_id : site_id.id)
  end
  qry = qry.in(ar_site_id: ar)
  qry.map { |category| [category.name, category.id, category.parent, category.order] }
end

.choices_for_ctype(site_id = nil) ⇒ Object

Returns values for category type. Values should be defined in BigTable on the site level all overall.

Parameters:

  • site_id (Integer) (defaults to: nil)

    : Return only values for specified site



91
92
93
94
# File 'app/models/ar_category.rb', line 91

def self.choices_for_ctype(site_id = nil)
  site_id = site_id.id if site_id&.class != Integer
  ArBigTable.choices_for('ar_category_type', site_id)
end

.values_for_parent(site_id = nil) ⇒ Object

Returns all values for use as parent select field.



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'app/models/ar_category.rb', line 65

def self.values_for_parent(site_id = nil) #:nodoc:
  qry = where(active: true)
  qry = qry.and(ar_site_id: site_id.id) if site_id
  parents = {} # cache parent names to minimize database usage
  qry.inject([]) do |r, v|
    if parents[v.parent].nil?
      name   = ''
      parent = v.parent
      until parent.nil?
        doc    = find(parent)
        name   = "#{doc.name} / #{name}"
        parent = doc.parent
      end
      parents[v.parent] = name
    end
    name = v.parent ? parents[v.parent] + v.name : v.name
    r << [name, v.id]
  end.sort { |a, b| a.first <=> b.first }
end