Module: Rea::ActiveRecordExt::ClassMethods

Defined in:
lib/rea/active_record_ext.rb

Instance Method Summary collapse

Instance Method Details

#category(classification_name, name) ⇒ Object



77
78
79
80
81
82
83
84
85
# File 'lib/rea/active_record_ext.rb', line 77

def category classification_name, name
	classification_config = members[classification_name]
	category_class = classification_config[:category_class_name].constantize
	category_class.joins(:classification).
	  where("rea_classifications.name"=>classification_name,
		"rea_classifications.type_name" => self.name,
		category_class.table_name+".name" => name
  ).first
end

#default_groupObject



24
25
26
# File 'lib/rea/active_record_ext.rb', line 24

def default_group
	grouping_options[:group_class_name].constantize.find_by_type_name_and_name(self.name,self.name.underscore)
end

#grouping(*args) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/rea/active_record_ext.rb', line 10

def grouping *args
	((::Rea.[:grouping_class_names] ||= []) << self.name).uniq!
	@grouping_options = args.extract_options!
	@grouping_options = {:group_class_name=>::Rea::Group.name, 
	  :entities_group_class_name=>::Rea::EntitiesGroup.name}.merge @grouping_options
	has_many :entities_groups, :as=>:entity, :class_name=>@grouping_options[:entities_group_class_name]
	has_many :groups, :through=>:entities_groups, :class_name=>@grouping_options[:group_class_name], :uniq=>true
	attr_accessible :group_ids
	after_initialize do |entity|
	  entity.groups << entity.class.default_group if entity.new_record? && entity.group_ids.try(:empty?)
	end

end

#grouping_optionsObject



28
29
30
# File 'lib/rea/active_record_ext.rb', line 28

def grouping_options
	@grouping_options ||={}
end

#member(classification, *args) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/rea/active_record_ext.rb', line 54

def member classification, *args
	((::Rea.[:member_class_names] ||= []) << self.name).uniq!
	grouping
	$options = {:multiple=>false,
	  :category_class_name=>::Rea::Category.name,
	  :categories_entity_class_name=>::Rea::CategoriesEntity.name
	}.merge(args.extract_options!)
	members[classification] = $options.dup
	if (args.size > 0) && (categories = args[0]).is_a?(Array)
	  members[classification][:categories] = categories
	end
	if $options[:multiple]
	  has_many :categories_entities, :as=>:entity, :class_name=>$options[:categories_entity_class_name]
	  has_many classification, :through=>:categories_entities,
	    :include=>:classification, 
	    :class_name=>$options[:category_class_name],
	    :source=>:category,
	    :conditions=>"rea_classifications.type_name = '#{self.name}' and rea_classifications.name = '#{classification.to_s.singularize}'"
	else
	  belongs_to classification, :class_name=>$options[:category_class_name]
	end
end

#membersObject



32
33
34
# File 'lib/rea/active_record_ext.rb', line 32

def members
	@members ||= {}
end

#namingObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/rea/active_record_ext.rb', line 36

def naming
	define_method :display_name do
	  if self.respond_to? :name
	    @display_name ||= proc do
 default = name.humanize
 subkey = case
   when self.respond_to?(:classification)
			 "#{self.classification.try(:name)}.#{self.name}"
   else
			 "#{self.name}"
   end
 key = "#{self.class.name.underscore}.names.#{subkey}"	   
 I18n.t(key,:default=>default)
	    end.call
	  end
	end
end