OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / lib / manifest / model / list / has_many_through.rb
1 module Manifest
2   module ListModule
3     class HasManyThroughList < BaseList
4       attr :list, :list_name, :list_manifest, 
5         :belongs_to, :has_many, :has_one
6       
7       def set_default
8         super
9       end
10       
11       def init
12         super
13         return
14         @list_list_conf = @manifest.list(@item_name).lists[list_name] || {}
15         @association_name = @list_list_conf['association_name']
16         @has_many_conf = @manifest.model(@item_name).associations['has_many'] || {}
17         @association_conf = @has_many_conf[@association_name] || {}
18         @association_through_model_name = @association_conf['through']
19         @association_through_model = @association_through_model_name.classify.constantize
20         @association_model_name = @association_conf['model']
21         @association_model = @association_model_name.classify.constantize
22         @foreign_key = @association_conf['foreign_key']
23        if @association_model.content?
24           @owner_model = @association_model.owner_model
25           @owner_table_name = @owner_model.table_name if @owner_model
26         end
27       end
28       
29       def where root_item
30         w = @association_model.list_where
31         w += ' and ' unless w.blank?
32         [w + @association_through_model.table_name + '.' + @foreign_key + ' = ?', root_item.id] 
33       end
34       
35       def includes
36         {@association_through_model.table_name => {@item_name => {}}}
37       end
38       
39       def items root_item
40          @association_model.where(self.where(root_item)).includes(self.includes).order(self.order).offset(@offset).limit(@page_size)
41       end
42       
43       def paginate root_item
44         Kaminari.paginate_array(Array.new(@association_model.where(self.where(root_item)).includes(self.includes).count, nil)).page(@offset).per(@page_size)
45       end
46       
47       def self.add_action item_name, action_name, list_name, list_conf
48         return
49       end
50     end
51     
52   end
53 end