Class: SpudPostCategory

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.groupedObject



23
24
25
# File 'app/models/spud_post_category.rb', line 23

def self.grouped
	return all.group_by(&:parent_id)
end

.options_for_categories(config = {}) ⇒ Object

Returns an array of categories in order of heirarchy :fitler Filters out a category by ID, and all of its children

:value Pick an attribute to be used in the value field, defaults to ID


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/spud_post_category.rb', line 30

def self.options_for_categories(config={})
	collection = config[:collection] || self.grouped
	level 		 = config[:level] 		 || 0
	parent_id  = config[:parent_id]  || nil
	filter 		 = config[:filter] 		 || nil
	value      = config[:value]			 || :id
	list 			 = []
	if collection[parent_id]
		collection[parent_id].each do |c|
			if c.id != filter
				list << [level.times.collect{ '- ' }.join('') + c.name, c[value]]
				list += self.options_for_categories({:collection => collection, :parent_id => c.id, :level => level+1, :filter => filter, :value => value})
			end
		end
	end
	return list
end

Instance Method Details

#posts_with_childrenObject



48
49
50
51
52
# File 'app/models/spud_post_category.rb', line 48

def posts_with_children
	category_ids = self.self_and_ancestors.collect{ |category| category.id }
	post_ids = SpudPostCategoriesPost.where(:spud_post_category_id => category_ids).collect{ |it| it.spud_post_id  }
	return SpudPost.where(:id => post_ids)
end

#skip_before_destroyObject

tell awesome_nested_set not to destroy descendants



19
20
21
# File 'app/models/spud_post_category.rb', line 19

def skip_before_destroy
	return true
end