Class CategoryFinder
In: app/models/category_finder.rb
Parent: Object

Methods

Attributes

category_id  [R] 

Public Class methods

[Source]

   # File app/models/category_finder.rb, line 3
3:   def initialize(cat)
4:     @category = cat
5:     @category_id = @category.id
6:   end

Public Instance methods

[Source]

    # File app/models/category_finder.rb, line 38
38:   def current_events(year, month, options={})
39:     options = {:page => 1}.merge(options)
40:     range = Event.date_range(year, month)
41: 
42:     Event.paginate(:all, {:include => :categories, :conditions => { 'categories.id' => category_id, :start_date => range }}.merge(options))
43:   end

[Source]

    # File app/models/category_finder.rb, line 10
10:   def find(asset, query='', options={})
11:     @region = Region.find_by_id(options.delete(:region)) if options.has_key?(:region)
12:     if @region && options[:within]
13:       options[:origin] = [@region.lat, @region.lng]
14:     else
15:       options.delete(:within)
16:     end
17: 
18:     date_range = options.delete(:date_range)
19: 
20:     options = {:page => 1, :per_page => options.delete(:limit)}.merge(options)
21:     if query.blank?
22:       asset_class(asset).paginate(:all, options_for_find(asset_class(asset), {:order => "#{asset_table(asset)}.name"}.merge(options), date_range))
23:     else
24:       ferret_options = {:page => options.delete(:page), :per_page => options.delete(:per_page)}
25:       asset_class(asset).find_by_contents(query, ferret_options, options_for_find(asset_class(asset), options, date_range))
26:     end
27:   end

[Source]

    # File app/models/category_finder.rb, line 33
33:   def most_commented_articles(limit=10, options={})
34:     options = {:page => 1, :per_page => limit, :order => 'comments_count DESC'}.merge(options)
35:     Article.paginate(:all, options_for_find(Article, options))
36:   end

[Source]

    # File app/models/category_finder.rb, line 51
51:   def product_categories_count(asset, product_categories_ids, objects_ids=nil)
52:     conditions = [ "product_categorizations.category_id in (?) and #{ProfileCategorization.table_name}.category_id = ?", product_categories_ids, category_id]
53: 
54:     if asset == :products
55:       if objects_ids
56:         conditions[0] += ' and product_categorizations.product_id in (?)'
57:         conditions << objects_ids
58:       end
59:       ProductCategory.find(
60:         :all, 
61:         :select => 'categories.id, count(*) as total', 
62:         :joins => "inner join product_categorizations on (product_categorizations.category_id = categories.id) inner join products on (products.id = product_categorizations.product_id) inner join #{ProfileCategorization.table_name} on (#{ProfileCategorization.table_name}.profile_id = products.enterprise_id)", 
63:         :group => 'categories.id', 
64:         :conditions => conditions
65:       )
66:     elsif asset == :enterprises
67:       if objects_ids
68:         conditions[0] += ' and products.enterprise_id in (?)'
69:         conditions << objects_ids
70:       end
71:       ProductCategory.find(
72:         :all,
73:         :select => 'categories.id, count(distinct products.enterprise_id) as total',
74:         :joins => "inner join product_categorizations on (product_categorizations.category_id = categories.id) inner join products on (products.id = product_categorizations.product_id) inner join #{ProfileCategorization.table_name} on (#{ProfileCategorization.table_name}.profile_id = products.enterprise_id)",
75:         :group => 'categories.id', 
76:         :conditions => conditions 
77:       )
78:     else
79:       raise ArgumentError, 'only products and enterprises supported'
80:     end.inject({}) do |results,pc| 
81:         results[pc.id]= pc.total.to_i
82:         results
83:     end
84:   end

[Source]

    # File app/models/category_finder.rb, line 29
29:   def recent(asset, limit = nil)
30:     find(asset, nil, :limit => limit, :order => 'created_at DESC, id DESC')
31:   end

[Source]

    # File app/models/category_finder.rb, line 45
45:   def upcoming_events(options = {})
46:     options = { :page => 1}.merge(options)
47: 
48:     Event.paginate(:all, {:include => :categories, :conditions => [ 'categories.id = ? and start_date >= ?', category_id, Date.today ], :order => 'start_date' }.merge(options))
49:   end

Protected Instance methods

[Source]

     # File app/models/category_finder.rb, line 125
125:   def asset_class(asset)
126:     asset.to_s.singularize.camelize.constantize
127:   end

[Source]

     # File app/models/category_finder.rb, line 129
129:   def asset_table(asset)
130:     asset_class(asset).table_name
131:   end

[Source]

     # File app/models/category_finder.rb, line 88
 88:   def options_for_find(klass, options={}, date_range = nil)
 89:     if defined? options[:product_category]
 90:       prod_cat = options.delete(:product_category)
 91:     end
 92:     
 93:     case klass.name
 94:     when 'Comment'
 95:       {:joins => 'inner join articles_categories on articles_categories.article_id = comments.article_id', :conditions => ['articles_categories.category_id = (?)', category_id]}.merge!(options)
 96:     when 'Product'
 97:       if prod_cat
 98:         {:joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id inner join product_categorizations on (product_categorizations.product_id = products.id)', :conditions => ['categories_profiles.category_id = (?) and product_categorizations.category_id = (?)', category_id, prod_cat.id]}.merge!(options)
 99:       else
100:         {:joins => 'inner join categories_profiles on products.enterprise_id = categories_profiles.profile_id', :conditions => ['categories_profiles.category_id = (?)', category_id]}.merge!(options)
101:       end
102:     when 'Article', 'TextArticle'
103:       {:joins => 'inner join articles_categories on (articles_categories.article_id = articles.id)', :conditions => ['articles_categories.category_id = (?)', category_id]}.merge!(options)
104:     when 'Event'
105:       conditions =
106:         if date_range
107:           ['articles_categories.category_id = (?) and start_date between ? and ?', category_id, date_range.first, date_range.last]
108:         else
109:           ['articles_categories.category_id = (?) ', category_id ]
110:         end
111:       {:joins => 'inner join articles_categories on (articles_categories.article_id = articles.id)', :conditions => conditions}.merge!(options)
112:     when 'Enterprise'
113:       if prod_cat
114:         {:joins => 'inner join categories_profiles on (categories_profiles.profile_id = profiles.id) inner join products on (products.enterprise_id = profiles.id) inner join product_categorizations on (product_categorizations.product_id = products.id)', :conditions => ['categories_profiles.category_id = (?) and product_categorizations.category_id = (?)', category_id, prod_cat.id]}.merge!(options)
115:       else
116:         {:joins => 'inner join categories_profiles on (categories_profiles.profile_id = profiles.id)', :conditions => ['categories_profiles.category_id = (?)', category_id]}.merge!(options)
117:       end    
118:     when 'Person', 'Community'
119:       {:joins => 'inner join categories_profiles on (categories_profiles.profile_id = profiles.id)', :conditions => ['categories_profiles.category_id = (?)', category_id]}.merge!(options)
120:     else
121:       raise "unreconized class #{klass.name}"
122:     end
123:   end

[Validate]