OSDN Git Service

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