OSDN Git Service

fix filter include
[pettanr/pettanr.git] / lib / manifest / list_group / list / filter.rb
1 module Manifest
2   module ListGroupModule
3     module ListModule
4       class FilterList < Base
5         
6         def set_default
7           super
8         end
9         
10         def init
11           super
12           @filter_item_name = @model_list_manifest.filter_item_name
13           @filter_key = @model_list_manifest.filter_key
14         end
15         
16         def where_condition filter_item_id
17           w = self.base_where_condition
18           w += ' and ' unless w.blank?
19           [w + @table_name + '.' + @filter_key + ' = ?', filter_item_id] 
20         end
21         
22         def include_hash
23           w = super
24           w.merge!({@filter_item_name => {} }) unless w[@filter_item_name]
25           w
26         end
27         
28         def items operators, options, offset, page_size
29           filter_item_id = options[:id]
30           @model.where(self.where_condition(filter_item_id)).includes(self.include_hash).order(self.order).offset(offset).limit(page_size)
31         end
32         
33         def count operators, options
34           filter_item_id = options[:id]
35           @model.where(self.where_condition(filter_item_id)).includes(self.include_hash).count
36         end
37         
38         def self._add_action item_name, action_name, list_name, list_conf
39           return
40           model = item_name.classify.constantize
41           controller_name = model.plural + 'Controller'
42           controller = controller_name.constantize
43           list_list_conf = Pettanr::Application::manifest.list(item_name).lists[list_name] || {}
44           from = list_list_conf['from']
45           filter_model = from.classify.constantize
46           filter_key = list_list_conf['filter_key']
47           return if controller.method_defined?(action_name)
48           controller.define_method(action_name) do 
49             filter_list filter_model, params
50           end
51         end
52         
53       end
54       
55     end
56   end
57 end