OSDN Git Service

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