OSDN Git Service

t#30322:create provider license import func
[pettanr/pettanr.git] / app / controllers / application_controller.rb
1 class ApplicationController < ActionController::Base
2   protect_from_forgery
3   layout :devise_layout if MagicNumber['test_layout']
4   before_filter :bf
5   
6   def devise_layout
7     if devise_controller?
8       case resource_name
9       when :admin
10         'test'
11       when :user
12         'test'
13       end
14     else
15       'application'
16     end
17   end
18   
19   def bf
20     @server_result = {
21       :location => {:controller => params[:controller], :action => params[:action]}
22     }
23     @server_result[:location][:id] = params[:id] if params[:id]
24     if Admin.count.to_i == 0 or License.count.to_i == 0
25       if params[:controller] == 'system' and params[:action] == 'start'
26       else
27         redirect_to :controller => '/system', :action => 'start'
28       end
29     else
30       if user_signed_in?
31         @user = current_user
32         @author = @user.author
33         @artist = if @author and @author.artist?
34           @author.artist
35         else
36           nil
37         end
38       end
39       @admin = if admin_signed_in?
40         current_admin
41       else
42         nil
43       end
44       @demand_user = if demand_user_signed_in?
45         current_demand_user
46       else
47         nil
48       end
49     end
50   end
51   
52   def authenticate_author
53     if @author
54       true
55     else
56       respond_to do |format|
57         format.html { redirect_to main_app.new_author_path, :status => :found }
58         format.js { render "authors/new" }
59         format.json { 
60           raise ActiveRecord::Forbidden
61         }
62       end
63       false
64     end
65   end
66   
67   def authenticate_artist
68     if @author.artist?
69       true
70     else
71       respond_to do |format|
72         format.html { redirect_to main_app.new_artist_path, :status => :found }
73         format.js { render "artists/new" }
74         format.json { 
75           raise ActiveRecord::Forbidden
76         }
77       end
78       false
79     end
80   end
81   
82   def set_image(file)
83     if file.respond_to?(:read)
84       file.read
85     else
86       Base64.decode64(file.to_s.gsub(' ', '+'))
87     end
88   end
89   
90   def ymd_to_time ymd_str
91     return nil if ymd_str.blank?
92     date = nil
93     begin
94       date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
95     rescue
96       date = nil
97     end
98     date
99   end
100   
101   def export_url demander_url, token, date
102     u = demander_url + (demander_url[-1] == '/' ? '' : '/')
103     u = URI.join(u, 'licenses_export.json?auth_token=' + token)
104     u = URI.join(u, '&date=' + date) unless date.blank?
105     u.to_s
106   end
107   
108   def export_from_provider url
109     json = RestClient.get url
110     JSON.parse json
111   end
112   
113 =begin
114   rescue_from ActiveRecord::RecordNotFound, :with => :render_404
115
116   private
117   def render_404(exception = nil)
118     if exception
119         logger.info "Rendering 404: #{exception.message}"
120     end
121     respond_to do |format|
122           format.html { 
123     render :file => "#{Rails.root}/public/404.html", :status => 404, :layout => false
124 }
125           format.json { 
126     render :text => "404 found", :status => 404
127 }
128   end
129   end
130 =end  
131 end