OSDN Git Service

Merge branch 'v05' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v05
[pettanr/pettanr.git] / app / controllers / artists_controller.rb
1 class ArtistsController < 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_resource_reader, :only => [:index, :show, :resource_pictures]
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   # GET /artists
14   # GET /artists.json
15   def index
16     @page = Artist.page params[:page]
17     @page_size = Artist.page_size params[:page_size]
18     @artists = Artist.list(@page, @page_size)
19
20     respond_to do |format|
21       format.html # index.html.erb
22       format.json { render :json => @artists.to_json(Artist.list_json_opt) }
23     end
24   end
25
26   # GET /artists/1
27   # GET /artists/1.json
28   def show
29     @ar = Artist.show(params[:id], [@user, @admin, @demand_user])
30
31     respond_to do |format|
32       format.html # show.html.erb
33       format.json { render :json => @ar.to_json(Artist.show_json_opt) }
34     end
35   end
36
37   def resource_pictures
38     @ar = Artist.show(params[:id], [@user, @admin, @demand_user])
39     
40     @page = Author.page params[:page]
41     @page_size = Author.resource_picture_page_size params[:page_size]
42     @resource_pictures = ResourcePicture.mylist(@ar, @page, @page_size)
43
44     respond_to do |format|
45       format.html # index.html.erb
46       format.json { render json: @resource_pictures.to_json(ResourcePicture.list_json_opt) }
47     end
48   end
49   
50   def count
51     @ar = {:count => Artist.visible_count}
52     respond_to do |format|
53       format.json { render json: @ar.to_json }
54     end
55   end
56   
57   def list
58     @artists = Artist.all
59
60     respond_to do |format|
61       format.html { render layout: 'system' }
62       format.json { render json: @artists }
63     end
64   end
65
66   def browse
67     @ar = Artist.find(params[:id])
68
69     respond_to do |format|
70       format.html { render layout: 'system' }
71       format.json { render json: @ar }
72     end
73   end
74
75   # GET /artists/new
76   # GET /artists/new.json
77   def new
78     @ar = Artist.new
79     @ar.supply_default 
80
81     respond_to do |format|
82       format.html # new.html.erb
83       format.js
84       format.json { render json: @ar.to_json(Artist.show_json_opt) }
85     end
86   end
87
88   # GET /artists/1/edit
89   def edit
90     @ar = Artist.edit(params[:id], @author)
91     respond_to do |format|
92       format.html 
93       format.js
94     end
95   end
96
97   # POST /artists
98   # POST /artists.json
99   def create
100     @ar = Artist.new()
101     @ar.supply_default 
102     @ar.attributes = params[:artist]
103     @ar.overwrite @author
104     respond_to do |format|
105       if @ar.save
106         flash[:notice] = I18n.t('flash.notice.created', :model => Artist.model_name.human)
107         format.html { redirect_to root_path }
108         format.json { render json: @ar.to_json(Artist.show_json_opt), status: :created, location: @artist }
109       else
110         format.html { render action: "new" }
111         format.json { render json: @ar.errors, status: :unprocessable_entity }
112       end
113     end
114   end
115
116   # PUT /artists/1
117   # PUT /artists/1.json
118   def update
119     @ar = Artist.edit(params[:id], @author)
120     @ar.attributes = params[:artist]
121     @ar.overwrite @author
122
123     respond_to do |format|
124       if @ar.save
125         flash[:notice] = I18n.t('flash.notice.updated', :model => Artist.model_name.human)
126         format.html { redirect_to '/home/configure' }
127         format.json { head :ok }
128       else
129         format.html { render action: "edit" }
130         format.json { render json: @ar.errors, status: :unprocessable_entity }
131       end
132     end
133   end
134
135   # DELETE /artists/1
136   # DELETE /artists/1.json
137   def destroy
138     @ar = Artist.edit(params[:id], @author)
139     @ar.destroy
140
141     respond_to do |format|
142       format.html { redirect_to artists_url }
143       format.json { head :ok }
144     end
145   end
146 end