OSDN Git Service

fix leaf update logic
authoryasushiito <yas@pen-chan.jp>
Wed, 4 Jun 2014 05:16:32 +0000 (14:16 +0900)
committeryasushiito <yas@pen-chan.jp>
Wed, 4 Jun 2014 05:16:32 +0000 (14:16 +0900)
app/assets/javascripts/manifest/work/items.js.coffee.erb
app/controllers/application_controller.rb
app/controllers/comic_stories_controller.rb
app/controllers/scroll_panels_controller.rb
app/controllers/scrolls_controller.rb
app/controllers/story_sheets_controller.rb
app/models/scroll_panel.rb
lib/manifest/item/leaf.rb
lib/peta/leaf.rb
public/manifest.json

index a78d25d..63d164b 100644 (file)
@@ -11,6 +11,7 @@
     type: 'leaf',\r
     args: {\r
       parent_model_name: 'scroll',\r
+      destination_model_name: 'panel',\r
     },\r
   },\r
   comic: {\r
@@ -22,6 +23,7 @@
     type: 'leaf',\r
     args: {\r
       parent_model_name: 'comic',\r
+      destination_model_name: 'story',\r
     },\r
   },\r
   story: {\r
@@ -33,6 +35,7 @@
     type: 'leaf',\r
     args: {\r
       parent_model_name: 'story',\r
+      destination_model_name: 'sheet',\r
     },\r
   },\r
   sheet: {\r
   license_group: {\r
   },\r
   license: {\r
-    type: 'leaf',\r
+    type: 'system_resource',\r
     args: {\r
       parent_model_name: 'license_group',\r
       boost: {\r
index 7f64519..8169f6f 100644 (file)
@@ -253,6 +253,34 @@ class ApplicationController < ActionController::Base
     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
     format.html {
       flash[:notice] = I18n.t('flash.notice.updated', :model => @my_model_class.model_name.human)
@@ -293,6 +321,34 @@ class ApplicationController < ActionController::Base
     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_update_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 list_count
     set_list
     j = {:count => @list.count(@operators, {:id => params[:id]})}
index bcc8df1..88b937b 100644 (file)
@@ -63,45 +63,31 @@ class ComicStoriesController < ApplicationController
   end
   
   def create
-    @comic_story = ComicStory.new 
-    @comic_story.supply_default
-    @comic_story.attributes = params[:comic_story]
-    @comic_story.overwrite @operators
-    @comic = Comic.edit(@comic_story.comic_id, @operators) if @comic_story.comic_id
-    @story = Story.show(@comic_story.story_id, @operators) if @comic_story.story_id
+    @my_model_class = self.class.model
+    @item = @my_model_class.new
+    @item.supply_default
+    @item.attributes = params[@item.item_name]
+    @item.overwrite @operators
+    @binder = @my_model_class.binder_model.edit(@item.binder_id, @operators) if @item.binder_id
+    @panel = @my_model_class.destination_model.show(@item.destination_id, @operators) if @item.destination_id
     
-    respond_to do |format|
-      if @comic_story.store @operators
-        flash[:notice] = I18n.t('flash.notice.created', :model => ComicStory.model_name.human)
-        format.html { redirect_to play_comic_path(@comic) }
-        format.json { render json: @comic_story.comic_story_as_json(@operators.author) }
-      else
-        flash[:notice] = I18n.t('flash.notice.not_created', :model => ComicStory.model_name.human)
-        format.html { render action: "new" }
-        format.json { render json: @comic_story.errors, status: :unprocessable_entity }
-      end
-    end
+    leaf_render_create play_scroll_path(@binder)
   end
   
   def update
-    @comic_story = ComicStory.edit(params[:id], @operators)
-    ot = @comic_story.t
-    @comic_story.attributes = params[:comic_story]
-    @comic_story.overwrite @operators
-    @comic = Comic.edit(@comic_story.comic_id, @operators) if @comic_story.comic_id
-    respond_to do |format|
-      if @comic_story.store @operators, ot
-        flash[:notice] = I18n.t('flash.notice.updated', :model => ComicStory.model_name.human)
-        format.html { redirect_to play_comic_path(@comic) }
-        format.json { head :ok }
-      else
-        flash[:notice] = I18n.t('flash.notice.not_updated', :model => ComicStory.model_name.human)
-        format.html { render action: "edit" }
-        format.json { render json: @comic_story.errors, status: :unprocessable_entity }
-      end
-    end
+    @my_model_class = self.class.model
+    @item = @my_model_class.edit(params[:id], @operators)
+    ot = @item.t
+    @item.attributes = params[@item.item_name]
+    @item.overwrite @operators
+    @binder = @my_model_class.binder_model.edit(@item.binder_id, @operators) if @item.binder_id
+    # no check permission for destination
+    # swapable hidden panel
+    #@panel = @my_model_class.destination_model.show(@item.destination_id, @operators) if @item.destination_id
+    
+    leaf_render_update ot, play_scroll_path(@binder)
   end
-
+  
   def destroy
     @comic_story = ComicStory.edit(params[:id], @operators)
     @comic = Comic.edit(@comic_story.comic_id, @operators) if @comic_story.comic_id
index fd47963..c11046a 100644 (file)
@@ -63,45 +63,31 @@ class ScrollPanelsController < ApplicationController
   end
   
   def create
-    @scroll_panel = ScrollPanel.new 
-    @scroll_panel.supply_default
-    @scroll_panel.attributes = params[:scroll_panel]
-    @scroll_panel.overwrite @operators
-    @scroll = Scroll.edit(@scroll_panel.scroll_id, @operators) if @scroll_panel.scroll_id
-    @panel = Panel.show(@scroll_panel.panel_id, @operators) if @scroll_panel.panel_id
+    @my_model_class = self.class.model
+    @item = @my_model_class.new
+    @item.supply_default
+    @item.attributes = params[@item.item_name]
+    @item.overwrite @operators
+    @binder = @my_model_class.binder_model.edit(@item.binder_id, @operators) if @item.binder_id
+    @panel = @my_model_class.destination_model.show(@item.destination_id, @operators) if @item.destination_id
     
-    respond_to do |format|
-      if @scroll_panel.store @operators
-        flash[:notice] = I18n.t('flash.notice.created', :model => ScrollPanel.model_name.human)
-        format.html { redirect_to play_scroll_path(@scroll) }
-        format.json { render json: @scroll_panel.scroll_panel_as_json(@operators.author) }
-      else
-        flash[:notice] = I18n.t('flash.notice.not_created', :model => ScrollPanel.model_name.human)
-        format.html { render action: "new" }
-        format.json { render json: @scroll_panel.errors, status: :unprocessable_entity }
-      end
-    end
+    leaf_render_create play_scroll_path(@binder)
   end
   
   def update
-    @scroll_panel = ScrollPanel.edit(params[:id], @operators)
-    ot = @scroll_panel.t
-    @scroll_panel.attributes = params[:scroll_panel]
-    @scroll_panel.overwrite @operators
-    @scroll = Scroll.edit(@scroll_panel.scroll_id, @operators) if @scroll_panel.scroll_id
-    respond_to do |format|
-      if @scroll_panel.store @operators, ot
-        flash[:notice] = I18n.t('flash.notice.updated', :model => ScrollPanel.model_name.human)
-        format.html { redirect_to play_scroll_path(@scroll) }
-        format.json { head :ok }
-      else
-        flash[:notice] = I18n.t('flash.notice.not_updated', :model => ScrollPanel.model_name.human)
-        format.html { render action: "edit" }
-        format.json { render json: @scroll_panel.errors, status: :unprocessable_entity }
-      end
-    end
+    @my_model_class = self.class.model
+    @item = @my_model_class.edit(params[:id], @operators)
+    ot = @item.t
+    @item.attributes = params[@item.item_name]
+    @item.overwrite @operators
+    @binder = @my_model_class.binder_model.edit(@item.binder_id, @operators) if @item.binder_id
+    # no check permission for destination
+    # swapable hidden panel
+    #@panel = @my_model_class.destination_model.show(@item.destination_id, @operators) if @item.destination_id
+    
+    leaf_render_update ot, play_scroll_path(@binder)
   end
-
+  
   def destroy
     @scroll_panel = ScrollPanel.edit(params[:id], @operators)
     @scroll = Scroll.edit(@scroll_panel.scroll_id, @operators) if @scroll_panel.scroll_id
index 4b7d1c3..5c933c5 100644 (file)
@@ -81,7 +81,6 @@ class ScrollsController < ApplicationController
   
   def create
     set_model
-    @scroll = @item
     @item = @my_model_class.new
     @item.supply_default 
     @item.attributes = params[@my_model_class.item_name]
@@ -91,7 +90,6 @@ class ScrollsController < ApplicationController
   
   def update
     set_edit
-    @scroll = @item
     @item.attributes = params[@my_model_class.item_name]
     @item.overwrite @operators
     render_update
index 0708389..b3b076e 100644 (file)
@@ -63,45 +63,31 @@ class StorySheetsController < ApplicationController
   end
   
   def create
-    @story_sheet = StorySheet.new 
-    @story_sheet.supply_default
-    @story_sheet.attributes = params[:story_sheet]
-    @story_sheet.overwrite @operators
-    @story = Story.edit(@story_sheet.story_id, @operators) if @story_sheet.story_id
-    @sheet = Sheet.show(@story_sheet.sheet_id, @operators) if @story_sheet.sheet_id
+    @my_model_class = self.class.model
+    @item = @my_model_class.new
+    @item.supply_default
+    @item.attributes = params[@item.item_name]
+    @item.overwrite @operators
+    @binder = @my_model_class.binder_model.edit(@item.binder_id, @operators) if @item.binder_id
+    @panel = @my_model_class.destination_model.show(@item.destination_id, @operators) if @item.destination_id
     
-    respond_to do |format|
-      if @story_sheet.store @operators
-        flash[:notice] = I18n.t('flash.notice.created', :model => StorySheet.model_name.human)
-        format.html { redirect_to play_story_path(@story) }
-        format.json { render json: @story_sheet.to_json(StorySheet.show_json_opt) }
-      else
-        flash[:notice] = I18n.t('flash.notice.not_created', :model => StorySheet.model_name.human)
-        format.html { render action: "new" }
-        format.json { render json: @story_sheet.errors, status: :unprocessable_entity }
-      end
-    end
+    leaf_render_create play_scroll_path(@binder)
   end
   
   def update
-    @story_sheet = StorySheet.edit(params[:id], @operators)
-    ot = @story_sheet.t
-    @story_sheet.attributes = params[:story_sheet]
-    @story_sheet.overwrite @operators
-    @story = Story.edit(@story_sheet.story_id, @operators) if @story_sheet.story_id
-    respond_to do |format|
-      if @story_sheet.store @operators, ot
-        flash[:notice] = I18n.t('flash.notice.updated', :model => StorySheet.model_name.human)
-        format.html { redirect_to play_story_path(@story) }
-        format.json { head :ok }
-      else
-        flash[:notice] = I18n.t('flash.notice.not_updated', :model => StorySheet.model_name.human)
-        format.html { render action: "edit" }
-        format.json { render json: @story_sheet.errors, status: :unprocessable_entity }
-      end
-    end
+    @my_model_class = self.class.model
+    @item = @my_model_class.edit(params[:id], @operators)
+    ot = @item.t
+    @item.attributes = params[@item.item_name]
+    @item.overwrite @operators
+    @binder = @my_model_class.binder_model.edit(@item.binder_id, @operators) if @item.binder_id
+    # no check permission for destination
+    # swapable hidden panel
+    #@panel = @my_model_class.destination_model.show(@item.destination_id, @operators) if @item.destination_id
+    
+    leaf_render_update ot, play_scroll_path(@binder)
   end
-
+  
   def destroy
     @story_sheet = StorySheet.edit(params[:id], @operators)
     @story = Story.edit(@story_sheet.story_id, @operators) if @story_sheet.story_id
index 6cc22b6..9ce35bb 100644 (file)
@@ -28,34 +28,6 @@ class ScrollPanel < Peta::Leaf
     'scrolls.visible > 0'
   end
   
-  def self.list_opt
-    {
-      :author => {}, 
-      :scroll => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloon => {}, :speech => {}}
-      }
-    }
-  end
-  
-  def self.list_json_opt
-    {:include => {
-      :author => {}, 
-      :scroll => {
-        :author => {}
-      }, 
-      :panel => {
-        :author => {}, 
-        :panel_pictures => {:picture => {:artist => {}, :license => {}}}, 
-        :speech_balloons =>{:balloon => {}, :speech => {}}
-      }
-    }}
-  end
-  
   def self.show_opt
     {:include => {
       :author => {}, 
@@ -79,25 +51,4 @@ class ScrollPanel < Peta::Leaf
     self.scroll.own?(operators) and self.panel.usable?(operators)
   end
   
-  def store operators, old_t = nil
-    res = false
-    self.class.transaction do
-      case self.allow? operators
-      when true
-        self.rotate old_t
-      when false
-        raise ActiveRecord::Forbidden
-      else
-      end
-      res = self.save
-      raise ActiveRecord::Rollback unless res
-      res = self.class.validate_t(self.scroll_id) 
-      unless res
-        self.errors.add :t, 'unserialized'
-        raise ActiveRecord::Rollback 
-      end
-    end
-    res
-  end
-  
 end
index 519e6fd..c78e9cb 100644 (file)
@@ -1,7 +1,7 @@
 module Manifest
   module ItemModule
     class LeafPeta < BasePeta
-      attr :parent_model_name
+      attr :parent_model_name, :destination_model_name
       
       def set_default
         super
@@ -11,6 +11,7 @@ module Manifest
       def init
         super
         @parent_model_name = @args['parent_model_name']
+        @destination_model_name = @args['destination_model_name']
       end
       
     end
index 59b84a5..51ea19c 100644 (file)
@@ -12,10 +12,22 @@ module Peta
       define_singleton_method("parent_model") do 
         pm
       end
+      destm = Manifest.manifest.models[self.my_peta.destination_model_name].classify
+      define_singleton_method("destination_model") do 
+        destm
+      end
       pfk = self.my_peta.parent_model_name + '_id'
       define_singleton_method("binder_key") do 
         pfk
       end
+      dest_key = if self.my_peta.destination_model_name
+        self.my_peta.destination_model_name + '_id'
+      else
+        nil
+      end
+      define_singleton_method("destination_key") do 
+        dest_key
+      end
       # Instance Methods
     end
     
@@ -27,6 +39,14 @@ module Peta
       self.parent_model
     end
     
+    def self.binder_model
+      self.parent_model
+    end
+    
+    def self.destination_model
+      self.parent_model
+    end
+    
     def root
       self.__send__ self.class.root_model.item_name
     end
@@ -41,39 +61,10 @@ module Peta
       self.root.visible? operators
     end
     
-    def self.play_list_where cid
-      ['scroll_panels.scroll_id = ?', cid]
-    end
-    
     def self.play_list_order
       self.table_name + '.t'
     end
     
-    def self.play_list scroll, author, offset = 0, limit = ScrollPanel.default_panel_size
-      ScrollPanel.where(self.play_list_where(scroll.id)).includes(ScrollPanel.list_opt).order('scroll_panels.t').offset(offset).limit(limit)
-    end
-    
-    def self.play_sheet_where sid
-      ['story_sheets.story_id = ?', sid]
-    end
-    
-    def self.play_sheet story, operators, page = 1
-      ss = StorySheet.where(self.play_sheet_where(story.id)).includes(StorySheet.list_opt).order('story_sheets.t').offset(page -1).limit(1).first
-      if ss 
-        if ss.sheet
-          Sheet.show(ss.sheet.id, operators)
-        else
-          nil
-        end
-      else
-        nil
-      end
-    end
-    
-    def self.play_paginate story, page
-      Kaminari.paginate_array(Array.new(StorySheet.where(self.play_sheet_where(story.id)).includes(StorySheet.list_opt).count, nil)).page(page).per(1)
-    end
-    
     def self.new_t binder_id
       r = self.max_t(binder_id)
       r.blank? ? 0 : r.to_i + 1
@@ -92,10 +83,6 @@ module Peta
       r.map {|sp| sp.t}
     end
     
-    def self.top_sheet story, author
-      StorySheet.play_list(story, author).first
-    end
-    
     def self.serial? ary
       i = 0
       ary.compact.sort.each do |t|
@@ -113,10 +100,22 @@ module Peta
       self.class.binder_key
     end
     
+    def destination_key
+      self.class.destination_key
+    end
+    
     def binder_id
       self.attributes[self.binder_key]
     end
     
+    def destination_id
+      if self.destination_key
+        self.attributes[self.destination_key]
+      else
+        nil
+      end
+    end
+    
     def insert_shift
       self.class.update_all('t = t + 1', 
         [self.binder_key + ' = ? and t >= ?', self.binder_id, self.t]
@@ -162,6 +161,27 @@ module Peta
       end
     end
     
+    def store operators, old_t = nil
+      res = false
+      self.class.transaction do
+        case self.allow? operators
+        when true
+          self.rotate old_t
+        when false
+          raise ActiveRecord::Forbidden
+        else
+        end
+        res = self.save
+        raise ActiveRecord::Rollback unless res
+        res = self.class.validate_t(self.binder_id) 
+        unless res
+          self.errors.add :t, 'unserialized'
+          raise ActiveRecord::Rollback 
+        end
+      end
+      res
+    end
+    
     def destroy_and_shorten
       res = false
       self.class.transaction do
index a3a321a..32c3da9 100644 (file)
@@ -8,7 +8,8 @@
     "scroll_panel": {\r
       "type": "leaf",\r
       "args": {\r
-        "parent_model_name": "scroll"\r
+        "parent_model_name": "scroll",\r
+        "destination_model_name": "panel"\r
       }\r
     },\r
     "comic": {\r
@@ -18,7 +19,8 @@
     "comic_story": {\r
       "type": "leaf",\r
       "args": {\r
-        "parent_model_name": "comic"\r
+        "parent_model_name": "comic",\r
+        "destination_model_name": "story"\r
       }\r
     },\r
     "story": {\r
@@ -28,7 +30,8 @@
     "story_sheet": {\r
       "type": "leaf",\r
       "args": {\r
-        "parent_model_name": "story"\r
+        "parent_model_name": "story",\r
+        "destination_model_name": "sheet"\r
       }\r
     },\r
     "sheet": {\r