OSDN Git Service

t#31537:fix flash
[pettanr/pettanr.git] / app / controllers / stories_controller.rb
1 class StoriesController < ApplicationController
2   layout 'test' if MagicNumber['test_layout']
3   if MagicNumber['run_mode'] == 0
4     before_filter :authenticate_user, :only => [:new, :create, :edit, :update, :destroy]
5     before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy]
6   else
7     before_filter :authenticate_reader, :only => [:index, :show, :comic]
8     before_filter :authenticate_user, :only => [:new, :create, :edit, :update, :destroy]
9     before_filter :authenticate_author, :only => [:new, :create, :edit, :update, :destroy]
10   end
11   before_filter :authenticate_admin!, :only => [:list, :browse]
12
13   def index
14     @page = Story.page params[:page]
15     @page_size = Story.page_size params[:page_size]
16     @stories = Story.list(@page, @page_size)
17
18     respond_to do |format|
19       format.html {
20         @paginate = Story.list_paginate(@page, @page_size)
21       }
22       format.json { render :json => @stories.to_json(Story.list_json_opt) }
23     end
24   end
25
26   def show
27     @story = Story.show(params[:id], [@user, @admin])
28
29     respond_to do |format|
30       format.html # show.html.erb
31       format.json { render json: @story.story_as_json(@author) }
32     end
33   end
34   
35   def comic
36     @comic = Comic.show(params[:id], [@user, @admin])
37     cnt = Story.count(:conditions => ['comic_id = ?', @comic.id]).to_i
38     @offset = Story.offset cnt, params[:offset]
39     @panel_count = Story.panel_count cnt, params[:count]
40     @stories = Story.play_list(@comic, @author, @offset, @panel_count)
41     respond_to do |format|
42       format.html {
43         @paginate = Story.play_list_paginate(@comic, @offset, @panel_count)
44         if @author
45           @new_panels = Panel.mylist(@author, 0, 5)
46         end
47       }
48       format.json {render text: Story.list_as_json_text(@stories, @author)}
49       format.jsonp {
50         render :json => "callback(" + @stories.to_json_list + ");"
51       }
52     end
53   end
54   
55   def list
56     @stories = Story.all
57
58     respond_to do |format|
59       format.html { render layout: 'system' }# index.html.erb
60       format.json { render json: @stories }
61     end
62   end
63
64   def browse
65     @story = Story.find(params[:id])
66
67     respond_to do |format|
68       format.html { render layout: 'system' } # show.html.erb
69       format.json { render json: @story }
70     end
71   end
72   
73   def new
74     @story = Story.new 
75     @story.supply_default
76     respond_to do |format|
77       format.html # new.html.erb
78       format.js
79       format.json { render json: @story.story_as_json(@author) }
80     end
81   end
82
83   def edit
84     @story = Story.edit(params[:id], @author)
85     respond_to do |format|
86       format.html 
87       format.js
88     end
89   end
90
91   def create
92     @story = Story.new 
93     @story.supply_default
94     @story.attributes = params[:story]
95     @story.overwrite @author
96     @comic = Comic.edit(@story.comic_id, @author) if @story.comic_id
97     @panel = Panel.show(@story.panel_id, @author) if @story.panel_id
98     
99     respond_to do |format|
100       if @story.store
101         flash[:notice] = I18n.t('flash.notice.created', :model => Story.model_name.human)
102         format.html { redirect_to action: :comic, id: @story.comic_id }
103         format.json { render json: @story.story_as_json(@author) }
104       else
105         flash[:notice] = I18n.t('flash.notice.not_created', :model => Story.model_name.human)
106         format.html { render action: "new" }
107         format.json { render json: @story.errors, status: :unprocessable_entity }
108       end
109     end
110   end
111   
112   def update
113     @story = Story.edit(params[:id], @author)
114     ot = @story.t
115     @story.attributes = params[:story]
116     @story.overwrite @author
117     respond_to do |format|
118       if @story.store ot
119         flash[:notice] = I18n.t('flash.notice.updated', :model => Story.model_name.human)
120         format.html { redirect_to action: :comic, id: @story.comic_id }
121         format.json { head :ok }
122       else
123         flash[:notice] = I18n.t('flash.notice.not_updated', :model => Story.model_name.human)
124         format.html { render action: "edit" }
125         format.json { render json: @story.errors, status: :unprocessable_entity }
126       end
127     end
128   end
129
130   def destroy
131     @story = Story.edit(params[:id], @author)
132     respond_to do |format|
133       if @story.destroy_and_shorten
134         flash[:notice] = I18n.t('flash.notice.destroyed', :model => Story.model_name.human)
135         format.html { redirect_to :controller => 'stories', :action => :comic, :id => @story.comic_id }
136         format.json { head :ok }
137       else
138         flash[:notice] = I18n.t('flash.notice.not_destroyed', :model => Story.model_name.human)
139         format.html { redirect_to story_path(@story) }
140         format.json { render json: @story.errors, status: :unprocessable_entity }
141       end
142     end
143   end
144 end