OSDN Git Service

674e161275f60b88fe17c510ffb42e9990bf5fd9
[pettanr/pettanr.git] / app / controllers / common_licenses_controller.rb
1 class CommonLicensesController < ApplicationController
2   layout 'test' if Pettanr::TestLayout
3   before_filter :authenticate_user!, :only => [:index, :show]
4   before_filter :authenticate_admin!, :only => [:list, :browse, :import, :destroy]
5
6   private
7   
8   def set_data(file)
9     if file.respond_to?(:read)
10       file.read
11     else
12       file
13     end
14   end
15   
16   public
17   
18   # GET /common_licenses
19   # GET /common_licenses.json
20   def index
21     @page = CommonLicense.page params[:page]
22     @page_size = CommonLicense.page_size params[:page_size]
23     @common_licenses = CommonLicense.list({}, @page, @page_size)
24
25     respond_to do |format|
26       format.html # index.html.erb
27       format.json { render json: @common_licenses.to_json(CommonLicense.list_json_opt) }
28     end
29   end
30
31   # GET /common_licenses/1
32   # GET /common_licenses/1.json
33   def show
34     @common_license = CommonLicense.show(params[:id])
35
36     respond_to do |format|
37       format.html # show.html.erb
38       format.json { render json: @common_license.to_json(CommonLicense.show_json_include_opt) }
39     end
40   end
41
42   def list
43     @common_licenses = CommonLicense.all
44
45     respond_to do |format|
46       format.html { render layout: 'system' }
47       format.json { render json: @common_licenses }
48     end
49   end
50
51   def browse
52     @common_license = CommonLicense.find(params[:id])
53
54     respond_to do |format|
55       format.html { render layout: 'system' }
56       format.json { render json: @common_license }
57     end
58   end
59
60   # POST /common_licenses
61   # POST /common_licenses.json
62   def import
63     @data = set_data params[:file]
64     respond_to do |format|
65       @errors = CommonLicense.import @data
66       if @errors and @errors.empty?
67         format.html { redirect_to :action => :list }
68         format.json { render text: 'ok', status: :created }
69       else
70         format.html { render action: "result" }
71         format.json { render json: @errors, status: :unprocessable_entity }
72       end
73     end
74   end
75
76   # DELETE /common_licenses/1
77   # DELETE /common_licenses/1.json
78   def destroy
79     @common_license = CommonLicense.find(params[:id])
80     @common_license.destroy
81
82     respond_to do |format|
83       format.html { redirect_to common_licenses_url }
84       format.json { head :ok }
85     end
86   end
87 end