Module: Rea::AspectType::Classification

Defined in:
lib/rea/aspect_type/classification.rb

Defined Under Namespace

Modules: Category, CategoryType Classes: Aspect, MemberType

Class Method Summary collapse

Class Method Details

.included(base) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rea/aspect_type/classification.rb', line 4

def self.included base
  base.scope :by_category, lambda { |*categories|
    $options = categories.extract_options!
    $aspect = base.classification_aspect
    $condition = {}
    categories.each do |category|
      $category_type_name = category.category_type.name
      $condition["#{$aspect.member_type_for($category_type_name)}_id"] = category.id
    end
    $condition.merge! $options
    base.where($condition)
  }

  base.send :has_many, :category_members, :class_name=>::Rea::CategoryMember.name, :as=>:entity
  base.send :has_many, :categories, :class_name=>::Rea::Category.name,
    :through=>:category_members, :source=>:category

  base.class_eval do

    def self.categories_for member_type
      self.classification_aspect.categories_for member_type
    end

    def is? category
      $field_name = self.class.classification_aspect.member_type_for(category.category_type.name)
      self.send($field_name) == category
    end
    def is_in? category
      $field_name = self.class.classification_aspect.member_type_for(category.category_type.name)
      $current = category.class.find(self.send($field_name))
      while($current) do
        return true if $current == category
        $current = $current.parent_category
      end
      return false
    end
  end
end