OSDN Git Service

fix test
[pettanr/pettanr.git] / app / controllers / application_controller.rb
index 4d8ca5f..f1da1ce 100644 (file)
@@ -23,39 +23,32 @@ class ApplicationController < ActionController::Base
       :location => {:controller => params[:controller], :action => params[:action]}
     }
     @server_result[:location][:id] = params[:id] if params[:id]
-    if Admin.count.to_i == 0 or License.count.to_i == 0
-      if params[:controller] == 'system' and params[:action] == 'start'
-      else
-        redirect_to :controller => '/system', :action => 'start'
-      end
+    user = if user_signed_in?
+      current_user
     else
-      user = if user_signed_in?
-        current_user
-      else
-        nil
-      end
-      author = if user
-        user.author
-      else
-        nil
-      end
-      artist = if user
-        user.artist
-      else
-        nil
-      end
-      admin = if admin_signed_in?
-        current_admin
-      else
-        nil
-      end
-      demand_user = if demand_user_signed_in?
-        current_demand_user
-      else
-        nil
-      end
-      @operators = Operator.new [user, author, artist, admin, demand_user]
+      nil
+    end
+    author = if user
+      user.author
+    else
+      nil
+    end
+    artist = if user
+      user.artist
+    else
+      nil
+    end
+    admin = if admin_signed_in?
+      current_admin
+    else
+      nil
+    end
+    demand_user = if demand_user_signed_in?
+      current_demand_user
+    else
+      nil
     end
+    @operators = Operator.new [user, author, artist, admin, demand_user]
   end
   
   def authenticate_reader
@@ -70,13 +63,16 @@ class ApplicationController < ActionController::Base
     authenticate_user! unless @operators.resource_reader?
   end
   
+  def authenticate_admin
+    authenticate_admin! unless @operators.admin?
+  end
+  
   def authenticate_author
     if @operators.author
       true
     else
       respond_to do |format|
         format.html { redirect_to main_app.new_author_path, :status => :found }
-        format.js { render "authors/new" }
         format.json { 
           raise ActiveRecord::Forbidden
         }
@@ -91,7 +87,6 @@ class ApplicationController < ActionController::Base
     else
       respond_to do |format|
         format.html { redirect_to main_app.new_artist_path, :status => :found }
-        format.js { render "artists/new" }
         format.json { 
           raise ActiveRecord::Forbidden
         }
@@ -111,43 +106,66 @@ class ApplicationController < ActionController::Base
   def set_model
     @my_controller = Manifest.manifest.controllers[params[:controller].to_s]
     @my_action = @my_controller.actions[params[:action].to_s]
-    @my_model = Manifest.manifest.models[@my_action.item_name]
+    @my_model = Manifest.manifest.models[@my_controller.item_name]
     @my_model_class = @my_model.classify
   end
   
   def set_list
     set_model
-    @my_model_list = @my_model.lists[@my_action.list_name]
-    @my_list = Manifest.manifest.lists[@my_action.item_name]
-    @list = @my_list[@my_action.list_name]
+    @my_list_model = Manifest.manifest.models[@my_action.item_name]
+    @my_list_model_class = @my_list_model.classify
+    @list = Locmare::ListGroup.list @my_action.item_name, @my_action.list_name
   end
   
   def filer_list
     set_list
-    list_result = @list.open(@operators, {:id => params[:id]})
+    list_result = @list.open(@operators, 
+      {:id => params[:id], :page => params[:page], :page_size => params[:page_size]}
+    )
     @items = list_result.items 
     respond_to do |format|
       format.html {
-        @filer = Manifest::View::Filer.new @list.item_name, list_result.items, list_result.paginate, @operators
+        @filer = Locmare::Filer.new @list.item_name, list_result.items, list_result, :default, @operators
         render @filer.template_name, :locals => {
           :filer => @filer
         }
       }
-      format.json { render json: @items.to_json(@my_model_class.list_json_opt) }
+      format.json { render json:  @items.map{|i| i.attributes}.to_json }
+      # rails3.2 has problem
+      # y method defined as private
+      # attribute y conflict at PanelPicture, balloon ..etc
+      # use i.attributes[name]
+      # format.json { render json:  @items.to_json }
       format.atom 
       format.rss
     end
   end
   
+  def set_play
+    set_list
+    @item = @my_model_class.show(params[:id], @operators)
+  end
+  
+  def play_list
+    @options = {:id => params[:id], :my_play => @item.own?(@operators), 
+      :offset => params[:offset], :count => params[:count], 
+      :page => params[:page], :page_size => params[:page_size]
+    }
+    list_result = @list.open(@operators, @options)
+    @items = list_result.items 
+    @count = list_result.count
+    @pager = list_result.paginate
+  end
+  
   def set_show
     set_model
     @item = @my_model_class.show(params[:id], @operators)
   end
   
   def show_prof_format format
-    format.prof { 
-      self.formats = [:html] 
-      @profiler = Manifest::View::Profiler.new @my_model.model_name, @item, @operators
+    format.prof {
+      self.formats = [:html]
+      @profiler = Locmare::Profiler.new @my_model.model_name, @item, @operators
       render @profiler.template_name, :locals => {
         :profiler => @profiler
       }
@@ -158,19 +176,218 @@ class ApplicationController < ActionController::Base
     format.json { render json: @item.to_json(@my_model_class.show_json_opt) }
   end
   
-  def form_new
+  def show_json_format_for_root format
+    format.json { render json: @item.attributes.to_json(@my_model_class.show_json_opt_for_root) }
+  end
+  
+  def set_new
     set_model
     @item = @my_model_class.new
+    @item.boosts 'post'
     @item.supply_default
+  end
+  
+  def set_edit
+    set_model
+    @item = @my_model_class.edit(params[:id], @operators)
+    @item.boosts 'post'
+  end
+  
+  def render_form
     respond_to do |format|
       format.html { 
-        @form = Manifest::View::Form.new @item.item_name, @item, true, @operators
+        @form = Locmare::Bucket.factory @item.item_name, @item, true, true, @operators
         render @form.template_name, :locals => {
           :form => @form
         }
       }
-      format.js
-      format.json { render json: @item.to_json(@my_model_class.show_json_opt) }
+      format.json { render json: @item.to_json }
+    end
+  end
+  
+  def form_new
+    set_new
+    render_form
+  end
+  
+  def form_edit
+    set_edit
+    render_form
+  end
+  
+  def created_html_format format, redirect_url = nil
+    format.html {
+      flash[:notice] = I18n.t('flash.notice.created', :model => @my_model_class.model_name.human)
+      redirect_to (redirect_url ? redirect_url : @item)
+    }
+  end
+  
+  def created_json_format format
+    format.json {
+      render json: @item.to_json(@my_model_class.show_json_opt), status: :created, location: @item
+    }
+  end
+  
+  def not_created_html_format format
+    format.html {
+      flash[:notice] = I18n.t('flash.notice.not_created', :model => @my_model_class.model_name.human)
+      render_form
+    }
+  end
+  
+  def not_created_json_format format
+    format.json {
+      render json: @item.errors, status: :unprocessable_entity
+    }
+  end
+  
+  def render_create redirect_url = nil
+    if @item.save
+      respond_to do |format|
+        created_html_format format, redirect_url
+        created_json_format format
+      end
+    else
+      respond_to do |format|
+        not_created_html_format format
+        not_created_json_format format
+      end
+    end
+  end
+  
+  def leaf_created_html_format format, redirect_url
+    format.html {
+      flash[:notice] = I18n.t('flash.notice.created', :model => @my_model_class.model_name.human)
+      redirect_to redirect_url
+    }
+  end
+  
+  def leaf_not_created_html_format format, redirect_url
+    format.html {
+      flash[:notice] = I18n.t('flash.notice.not_created', :model => @my_model_class.model_name.human)
+      redirect_to redirect_url
+    }
+  end
+  
+  def leaf_render_create redirect_url
+    if @item.store @operators
+      respond_to do |format|
+        leaf_created_html_format format, redirect_url
+        created_json_format format
+      end
+    else
+      respond_to do |format|
+        leaf_not_created_html_format format, redirect_url
+        not_created_json_format format
+      end
+    end
+  end
+  
+  def updated_html_format format, redirect_url = nil
+    format.html {
+      flash[:notice] = I18n.t('flash.notice.updated', :model => @my_model_class.model_name.human)
+      redirect_to (redirect_url ? redirect_url : @item)
+    }
+  end
+  
+  def updated_json_format format
+    format.json {
+      head :ok
+    }
+  end
+  
+  def not_updated_html_format format
+    format.html {
+      flash[:notice] = I18n.t('flash.notice.not_updated', :model => @my_model_class.model_name.human)
+      render_form
+    }
+  end
+  
+  def not_updated_json_format format
+    format.json {
+      render json: @item.errors, status: :unprocessable_entity
+    }
+  end
+  
+  def render_update redirect_url = nil
+    if @item.save
+      respond_to do |format|
+        updated_html_format format, redirect_url
+        updated_json_format format
+      end
+    else
+      respond_to do |format|
+        not_updated_html_format format
+        not_updated_json_format format
+      end
+    end
+  end
+  
+  def leaf_updated_html_format format, redirect_url
+    format.html {
+      flash[:notice] = I18n.t('flash.notice.updated', :model => @my_model_class.model_name.human)
+      redirect_to redirect_url
+    }
+  end
+  
+  def leaf_not_updated_html_format format, redirect_url
+    format.html {
+      flash[:notice] = I18n.t('flash.notice.not_updated', :model => @my_model_class.model_name.human)
+      redirect_to redirect_url
+    }
+  end
+  
+  def leaf_render_update ot, redirect_url
+    if @item.store @operators, ot
+      respond_to do |format|
+        leaf_updated_html_format format, redirect_url
+        updated_json_format format
+      end
+    else
+      respond_to do |format|
+        leaf_not_updated_html_format format, redirect_url
+        not_updated_json_format format
+      end
+    end
+  end
+  
+  def destroyed_html_format format, redirect_url
+    format.html {
+      flash[:notice] = I18n.t('flash.notice.destroyed', :model => @my_model_class.model_name.human)
+      redirect_to redirect_url
+    }
+  end
+  
+  def destroyed_json_format format
+    format.json {
+      head :ok
+    }
+  end
+  
+  def not_destroyed_html_format format
+    format.html {
+      flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => @my_model_class.model_name.human)
+      redirect_to @item
+    }
+  end
+  
+  def not_destroyed_json_format format
+    format.json {
+      render json: @item.errors, status: :unprocessable_entity
+    }
+  end
+  
+  def render_destroy redirect_url = nil
+    if @item.destroy
+      respond_to do |format|
+        destroyed_html_format format, redirect_url
+        destroyed_json_format format
+      end
+    else
+      respond_to do |format|
+        not_destroyed_html_format format
+        not_destroyed_json_format format
+      end
     end
   end
   
@@ -202,17 +419,11 @@ class ApplicationController < ActionController::Base
   end
   
   def assist_items item_name, list_name
-    list = Manifest.manifest.lists[item_name][list_name]
+    list = Locmare::ListGroup.list item_name, list_name
     list_result = list.open(@operators, {:id => @item.id, :page => 1, :page_size => 5})
     list_result.items
   end
   
-  def assist_filer item_name, items
-    @filer = Manifest::View::Filer.new @list.item_name, list_result.items, list_result.paginate, @operators
-    filer_manager = Pettanr::Application::manifest.filer_managers[item_name]
-    filer_manager.open(item_name, items, @operators, nil)
-  end
-  
   def set_image(file)
     if file.respond_to?(:read)
       file.read