Module: ErpTechSvcs::Utils::DefaultNestedSetMethods

Included in:
AuditLogType, SecurityRole
Defined in:
lib/erp_tech_svcs/utils/default_nested_set_methods.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
# File 'lib/erp_tech_svcs/utils/default_nested_set_methods.rb', line 4

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#children_to_tree_hash(options = {}) ⇒ Object



201
202
203
# File 'lib/erp_tech_svcs/utils/default_nested_set_methods.rb', line 201

def children_to_tree_hash(options={})
  self.children.collect { |child| child.to_tree_hash(options) }
end

#is_descendant_of?(type) ⇒ Boolean

Returns:

  • (Boolean)


228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/erp_tech_svcs/utils/default_nested_set_methods.rb', line 228

def is_descendant_of?(type)
  type = self.class.iid(type) if (type.is_a? String)
  parent = self.parent

  if type.id == self.id
    result = true
  elsif parent.nil?
    result = false
  elsif parent.id == type.id
    result = true
  else
    result = parent.is_descendant_of? type
  end
  result
end

#leafObject



180
181
182
# File 'lib/erp_tech_svcs/utils/default_nested_set_methods.rb', line 180

def leaf
  children.size == 0
end

#to_json_with_leaf(options = {}) ⇒ Object



184
185
186
# File 'lib/erp_tech_svcs/utils/default_nested_set_methods.rb', line 184

def to_json_with_leaf(options = {})
  self.to_json_without_leaf(options.merge(:methods => :leaf))
end

#to_labelObject



176
177
178
# File 'lib/erp_tech_svcs/utils/default_nested_set_methods.rb', line 176

def to_label
  description
end

#to_record_representation(root = self.class.root) ⇒ Object



218
219
220
221
222
223
224
225
226
# File 'lib/erp_tech_svcs/utils/default_nested_set_methods.rb', line 218

def to_record_representation(root = self.class.root)
  # returns a string of category descriptions like
  # 'main_category > sub_category n > ... > this category instance'
  if root?
    description
  else
    crawl_up_from(self, root).split('///').compact.reverse.join(' > ')
  end
end

#to_representation(level) ⇒ Object



205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/erp_tech_svcs/utils/default_nested_set_methods.rb', line 205

def to_representation(level)
  # returns a string that consists of 1) a number of dashes equal to
  # the category's level and 2) the category's description attr
  rep = ''

  if level > 0
    level.times { rep << '-' }
    rep += ' '
  end

  rep << description
end

#to_tree_hash(options = {}) ⇒ Object



190
191
192
193
194
195
196
197
198
199
# File 'lib/erp_tech_svcs/utils/default_nested_set_methods.rb', line 190

def to_tree_hash(options={})
  options = {
    only: [:parent_id, :internal_identifier],
    leaf: self.leaf?,
    text: self.to_label,
    children: self.children.collect { |child| child.to_tree_hash(options) }
  }.merge(options)

  self.to_hash(options)
end