OSDN Git Service

t#31060:add export
[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]
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 count
38     @ar = {:count => Artist.visible_count}
39     respond_to do |format|
40       format.json { render json: @ar.to_json }
41     end
42   end
43   
44   def list
45     @artists = Artist.all
46
47     respond_to do |format|
48       format.html { render layout: 'system' }
49       format.json { render json: @artists }
50     end
51   end
52
53   def browse
54     @ar = Artist.find(params[:id])
55
56     respond_to do |format|
57       format.html { render layout: 'system' }
58       format.json { render json: @ar }
59     end
60   end
61
62   # GET /artists/new
63   # GET /artists/new.json
64   def new
65     @ar = Artist.new
66     @ar.supply_default 
67
68     respond_to do |format|
69       format.html # new.html.erb
70       format.js
71       format.json { render json: @ar.to_json(Artist.show_json_opt) }
72     end
73   end
74
75   # GET /artists/1/edit
76   def edit
77     @ar = Artist.edit(params[:id], @author)
78     respond_to do |format|
79       format.html 
80       format.js
81     end
82   end
83
84   # POST /artists
85   # POST /artists.json
86   def create
87     @ar = Artist.new()
88     @ar.supply_default 
89     @ar.attributes = params[:artist]
90     @ar.overwrite @author
91     respond_to do |format|
92       if @ar.save
93         flash[:notice] = I18n.t('flash.notice.created', :model => Artist.model_name.human)
94         format.html { redirect_to root_path }
95         format.json { render json: @ar.to_json(Artist.show_json_opt), status: :created, location: @artist }
96       else
97         format.html { render action: "new" }
98         format.json { render json: @ar.errors, status: :unprocessable_entity }
99       end
100     end
101   end
102
103   # PUT /artists/1
104   # PUT /artists/1.json
105   def update
106     @ar = Artist.edit(params[:id], @author)
107     @ar.attributes = params[:artist]
108     @ar.overwrite @author
109
110     respond_to do |format|
111       if @ar.save
112         flash[:notice] = I18n.t('flash.notice.updated', :model => Artist.model_name.human)
113         format.html { redirect_to '/home/configure' }
114         format.json { head :ok }
115       else
116         format.html { render action: "edit" }
117         format.json { render json: @ar.errors, status: :unprocessable_entity }
118       end
119     end
120   end
121
122   # DELETE /artists/1
123   # DELETE /artists/1.json
124   def destroy
125     @ar = Artist.edit(params[:id], @author)
126     @ar.destroy
127
128     respond_to do |format|
129       format.html { redirect_to artists_url }
130       format.json { head :ok }
131     end
132   end
133 end