OSDN Git Service

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