OSDN Git Service

fix:balloon edit
[pettanr/pettanr.git] / lib / manifest / manifest.rb
index a871faa..ce33eea 100644 (file)
@@ -2,43 +2,86 @@ module Manifest
   class Manifest
     cattr_accessor :manifest
     attr :system_resources, :magic_numbers,
-      :controllers, :models, :lists, :profilers, :filers, :forms, :lists, :views
+      :items, :controllers, :models, :module_names, 
+      :inflectors, :singulars, :plurals
     # call me before load routes.rb
     # routes.rb needs engine_resources manifest in system_resources
     # ex. it's adding license_groups routes
-    def initialize manifest
-      @manifest = manifest || {}
-      @system_resources = SystemResource.new(self, SystemResource.set_default(@manifest['system_resources']))
-      @magic_numbers = @manifest['magic_numbers']
+    def initialize global_json
+      @module_names = []
+      @global_json = global_json || {}
+      @system_resources = SystemResource.new(self, SystemResource.set_default(@global_json['system_resources']))
+      @magic_numbers = @global_json['magic_numbers']
     end
     
     def init
       # managers can't initialize before load  application.rb
       return unless defined? ::Pettanr
-      @controllers = Controller.manager(self, @manifest['controllers'])
-      @models = Model.manager(self, @manifest['models'])
-      @locals = @manifest['locals']
-      @filers = Filer.manager(self, @locals['filers'])
-      @profilers = Profiler.manager(self, @locals['profilers'])
-      @forms = Form.base_manager(self, @locals['forms'])
-      @forms.merge(Form.extend_manager(self, @locals['forms']))
-      @lists = List.manager(self, @locals['lists'])
+      @inflectors = @global_json['inflectors']
+      @item_names = {}
+      @table_names = {}
+      @inflectors.each do |item_name, table_name|
+        @item_names[item_name] = table_name  # monkey copy
+        @table_names[table_name] = item_name
+      end
+      @items = ManifestBase.load_type_name_args(self, @global_json, 'items', ItemFactory)
+      @controllers = Controller.load(self, @global_json, 'controllers')
+      self.replace_action_alias
+      @models = Model.load(self, @global_json, 'models')
+    end
+    
+    def init_after_load_manifest
       @system_resources.init
-      add_action
+      PettanImager.tmb_w = @magic_numbers['thumbnail_width']
+      PettanImager.tmb_h = @magic_numbers['thumbnail_height']
     end
     
-    def add_action
-        return
-      @controllers.each do |controller_name, controller|
-        model_name = controller.model_name
-        next if model_name.blank?
-        controller.actions.each do |action_name, conf|
-          next unless conf['type'] == 'list'
-          list_name = conf['list']['list_name']
-          list = Pettanr::Application::manifest.list_managers[model_name]
-          list.add_action action_name, list_name
+    def replace_action_alias
+      replaces = {}
+      @controllers.each do |controller_name, controller_manifest|
+        controller_manifest.actions.each do |action_name, action_manifest|
+          next unless action_manifest.alias
+          alias_action = action_manifest.alias_action
+          next unless alias_action.type == action_manifest.type
+          args = {}
+          action_manifest.a_arg_names.each do |name|
+            args[name] = action_manifest.__send__(name) || alias_action.__send__(name)
+          end
+          action_manifest.b_arg_names.each do |name|
+            args[name] = alias_action.__send__(name) || action_manifest.__send__(name)
+          end
+          args['original'] = action_manifest
+          json = {'type' => alias_action.type, 'args' => args}
+          new_action_manifest = ControllerModule::ActionFactory.factory(
+            controller_manifest, action_manifest.name, json, action_manifest.module_name
+          )
+          replaces[controller_name] ||= {}
+          replaces[controller_name][action_name] = new_action_manifest
         end
       end
+      replaces.each do |controller_name, controller|
+        controller.each do |action_name, action_manifest|
+          ::Manifest.manifest.controllers[controller_name].actions[action_name] = action_manifest
+        end
+      end
+    end
+    
+    def load_models_manifest
+      @models.each do |model_name, model|
+        # skip extend model.
+        next unless @items[model.name]
+        model.classify.load_manifest
+      end
+    end
+    
+    # table_name to item_name
+    def singularize table_name
+      @table_names[table_name]
+    end
+    
+    # item_name to table_name
+    def pluralize item_name
+      @item_names[item_name]
     end
     
   end
@@ -48,18 +91,14 @@ module Manifest
       Manifest.manifest
     end
     
-    def load json
-      Manifest.manifest = Manifest.new json
+    def load global_json
+      Manifest.manifest = Manifest.new global_json
     end
     
     def item_name_to_model item_name
       item_name.classify.constantize
     end
     
-    def singularize name
-      name.singularize
-    end
-    
   end
 
   extend ModuleMethods