OSDN Git Service

temp
[pettanr/pettanr.git] / lib / manifest / profiler.rb
index b17e05f..1512dbb 100644 (file)
 
 module Pettanr
-  
   class ProfilerManager
-    class Ccolumn
-      def initialize item, attribute_name, attribute_conf
-        @attribute_conf = attribute_conf || {}
-      end
-      
-      def label
-        label = item.class.human_attribute_name(field_name)
-      end
-      
-      def value
-        case field_conf['type']
-        when 'datetime'
-          if item.attributes[field_name]
-            l item.attributes[field_name]
+    class Profiler
+      class Column
+        def initialize item_name, column_name, item, operators, manifest
+          @item_name = item_name
+          @column_name = column_name
+          @item = item
+          @operators = operators
+          @manifest = manifest
+          
+          @model = @item_name.classify.constantize
+          @model_attributes = @manifest.model(@item_name).attributes
+          @column_conf = @model_attributes[@column_name]
+        end
+        
+        def label view
+          @model.human_attribute_name(@column_name)
+        end
+        
+        def date?
+          case @column_conf['type']
+          when 'datetime'
+            if self.value
+              true
+            else
+              false
+            end
           else
-            item.attributes[field_name]
+            false
           end
-        else
-          item.attributes[field_name]
         end
-      end
-      
-      def note
-        if field_conf['source']
-          case field_conf['source']['type']
-          when 'magic_number'
-            t_selected_item(field_conf['source']['key'], item.attributes[field_name])
-          when 'model'
+        
+        def value
+          @item.attributes[@column_name]
+        end
+        
+        def disp_value view
+          if self.date?
+            view.l self.value
           else
+            self.value
           end
         end
-      end
-      
-    end
-    
-    class BelongsTo
-      def initialize belongs_to_conf
-        @belongs_to_conf = belongs_to_conf || {}
-      end
-      
-      def each item, operators
-        belongs_to_conf.each do |association_model_name|
-          model_belongs_to_conf = model_associations['belongs_to'][association_model_name]
-          parent_model = association_model_name.classify.constantize
-          id_column = model_belongs_to_conf['id_column']
-          parent = nil
-          begin
-            parent = parent_model.show(item.attributes[id_column], roles)
-          rescue ActiveRecord::RecordNotFound
+        
+        def note?
+          if @column_conf['source']
+            case @column_conf['source']['type']
+            when 'magic_number'
+              true
+            when 'model'
+              false
+            else
+              false
+            end
+          else
+            false
           end
-          if parent
-            yield parent
+        end
+        
+        def note view
+          if self.note?
+            '(' + view.t_selected_item(@column_conf['source']['key'], self.value) + ')'
+          else
           end
         end
+        
       end
-    end
-    
-    class HasMany
-      def initialize has_many_conf
-        @has_many_conf = has_many_conf || {}
+      
+      def initialize item_name, item, operators, conf, manifest
+        @item_name = item_name
+        @item = item
+        @operators = operators
+        @conf = conf
+        @manifest = manifest
+        
+        @lists = @manifest.profiler(@item_name).lists
+        @belongs_to_conf = @manifest.profiler(@item_name).belongs_to
+        @has_many_conf = @manifest.profiler(@item_name).has_many
+        @has_one_conf = @manifest.profiler(@item_name).has_one
       end
       
-      def each item, operators
-        @has_many_conf.each do |association_model_name|
-          model_has_many_conf = model_associations['has_many'][association_model_name]
-          child_model = association_model_name.classify.constantize
-          foreign_key = model_has_many_conf['foreign_key']
-          children = child_model.__send__(model_has_many_conf['list_method'], item.attributes['id'], operators, 1, 3)
-          yield children, child_model
-        end
+      def modelize str
+        str.classify.constantize
       end
       
-    end
-    
-    class HasOne
-      def initialize has_one_conf
-        @has_one_conf = has_one_conf || {}
+      def header_filer
+        @manifest.filer_managers[@item_name].open @item_name, [@item], @operators, nil
       end
       
-      def each item, operators
-        @has_one_conf.each do |association_model_name|
-          model_has_one_conf = model_associations['has_one'][association_model_name]
-          child_model = association_model_name.classify.constantize
-          foreign_key = model_has_one_conf['foreign_key']
-          children = child_model.__send__(model_has_one_conf['list_method'], item.attributes['id'], operators, 1, 3)
-          yield children, child_model
+      def each_column
+        @conf['columns'].each do |column_name|
+          column = Column.new @item_name, column_name, @item, @operators, @manifest
+          yield column
         end
       end
       
-    end
-    
-    class Profiler
-      def initialize item
-        @attribute_conf = attribute_conf || {}
+      def parent_item parent_model, model_belongs_to_conf
+        r = nil
+        id_column = model_belongs_to_conf['id_column']
+        begin
+          r = parent_model.show(@item.attributes[id_column], @operators)
+        rescue ActiveRecord::RecordNotFound
+        end
+        r
+      end
+      
+      def each_belongs_to
+        @belongs_to_conf.each do |association_model_name|
+          model_belongs_to_conf = @manifest.model(@item_name).belongs_to[association_model_name]
+          parent_model = self.modelize association_model_name
+          parent = self.parent_item parent_model, model_belongs_to_conf
+          if parent
+            parent_filer = @manifest.filer_managers[@item_name].open @item_name, [@item], @operators, nil
+            yield parent_filer
+          end
+        end
       end
       
-      def header
-        ender 'system/filer.html', :items => [item], :model => item.class, :roles => roles, :pager => nil 
+      def each_has_many
+        @has_many_conf.each do |profiler_list_name|
+          model_name = @lists['model_name']
+          list_name = @lists['list_name']
+          list = @manifest.list_managers[model_name].open(list_name, 1, 3, @operators)
+          items = list.items 
+          has_many_filer = @manifest.filer_managers[model_name].open model_name, items, @operators, nil
+          yield has_many_filer
+        end
       end
       
-      def each_column
-        profiler_conf['fields'].each do |field_name|
-          yield label, value, note
+      def each_has_one 
+        @has_one_conf.each do |profiler_list_name|
+          list = @manifest.list_managers[@item_name].open(profiler_list_name, 1, 1, @operators)
+          items = list.items 
+          has_one_filer = @manifest.filer_managers[@item_name].open @item_name, items, @operators, nil
+          yield has_one_filer
         end
       end
       
@@ -115,27 +145,10 @@ module Pettanr
       @item_name = @profiler_manifest.item_name
       @manifest = profiler_manifest.manifest
       @profiler_conf = @profiler_manifest.conf
-      @associations_conf = @profiler_conf['associations'] || {}
-      @belongs_to = BelongsTo.new @profiler_manifest.belongs_to
-      @bhas_many = HasMany.new @profiler_manifest.has_many
-      @has_one = HasOne.new @profiler_manifest.has_one
     end
     
-    def open
-      Profiler.new item
-    end
-    
-    def render view, operators
-      @belongs_to.each(item, operators) do |parent|
-        render 'system/filer.html', :items => [parent], :model => parent.class, :operators => operators, :pager => nil
-      end
-      @has_many.each(item, operators) do |items, child_model|
-        #child_model.filer.list operators, , 1, 3, items
-        render 'system/filer.html', :items => items, :model => child_model, :operators => operators, :pager => nil
-      end
-      @has_one.each(item, operators) do |items, child_model|
-        render 'system/filer.html', :items => items, :model => child_model, :operators => operators, :pager => nil
-      end
+    def open item, operators
+      Profiler.new @item_name, item, operators, @profiler_conf, @manifest
     end
     
   end