OSDN Git Service

41a320e2de5c8e2e0c98a6965281aa38dd64d473
[pettanr/pettanr.git] / app / controllers / original_licenses_controller.rb
1 class OriginalLicensesController < ApplicationController
2   layout 'test' if Pettanr::TestLayout
3   before_filter :authenticate_admin!, :only => [:list, :browse, :new, :edit, :create, :update, :destroy, :import]
4
5   private
6   
7   def set_data(file)
8     if file.respond_to?(:read)
9       file.read
10     else
11       file
12     end
13   end
14   
15   public
16   
17   # GET /original_licenses
18   # GET /original_licenses.json
19   def index
20     @page = OriginalLicense.page params[:page]
21     @page_size = OriginalLicense.page_size params[:page_size]
22     @original_licenses = OriginalLicense.list({}, @page, @page_size)
23
24     respond_to do |format|
25       format.html # index.html.erb
26       format.json { render json: @original_licenses.to_json(OriginalLicense.list_json_opt) }
27     end
28   end
29
30   # GET /original_licenses/1
31   # GET /original_licenses/1.json
32   def show
33     @original_license = OriginalLicense.show(params[:id])
34
35     respond_to do |format|
36       format.html # show.html.erb
37       format.json { render json: @original_license.to_json(OriginalLicense.show_json_include_opt) }
38     end
39   end
40
41   def list
42     @original_licenses = OriginalLicense.all
43
44     respond_to do |format|
45       format.html { render layout: 'system' }
46       format.json { render json: @original_licenses }
47     end
48   end
49
50   def browse
51     @original_license = OriginalLicense.find(params[:id])
52
53     respond_to do |format|
54       format.html { render layout: 'system' }
55       format.json { render json: @original_license }
56     end
57   end
58
59   # GET /original_licenses/new
60   # GET /original_licenses/new.json
61   def new
62     @original_license = OriginalLicense.new
63     @original_license.supply_default
64     respond_to do |format|
65       format.html # new.html.erb
66       format.js
67       format.json { render json: @original_license }
68     end
69   end
70
71   # GET /original_licenses/1/edit
72   def edit
73     @original_license = OriginalLicense.show(params[:id])
74     respond_to do |format|
75       format.html
76       format.js
77     end
78   end
79
80   # POST /original_licenses
81   # POST /original_licenses.json
82   def create
83     @original_license = OriginalLicense.new
84     @original_license.supply_default
85     @original_license.attributes = params[:original_license]
86     respond_to do |format|
87       if @original_license.store
88         format.html { redirect_to @original_license, notice: 'Original license was successfully created.' }
89         format.json { render json: @original_license, status: :created, location: @original_license }
90       else
91         format.html { render action: "new" }
92         format.json { render json: @original_license.errors, status: :unprocessable_entity }
93       end
94     end
95   end
96
97   # PUT /original_licenses/1
98   # PUT /original_licenses/1.json
99   def update
100     @original_license = OriginalLicense.show(params[:id])
101     @original_license.attributes = params[:original_license]
102
103     respond_to do |format|
104       if @original_license.store 
105         format.html { redirect_to @original_license, notice: 'Original license was successfully updated.' }
106         format.json { head :ok }
107       else
108         format.html { render action: "edit" }
109         format.json { render json: @original_license.errors, status: :unprocessable_entity }
110       end
111     end
112   end
113
114   # DELETE /original_licenses/1
115   # DELETE /original_licenses/1.json
116   def destroy
117     @original_license = OriginalLicense.find(params[:id])
118     @original_license.destroy
119
120     respond_to do |format|
121       format.html { redirect_to original_licenses_url }
122       format.json { head :ok }
123     end
124   end
125   
126   def import
127     @data = set_data params[:file]
128     respond_to do |format|
129       @errors = OriginalLicense.import @data
130       if @errors and @errors.empty?
131         format.html { redirect_to :action => :list }
132         format.json { render text: 'ok', status: :created }
133       else
134         format.html { render action: "result" }
135         format.json { render json: @errors, status: :unprocessable_entity }
136       end
137     end
138   end
139
140 end