OSDN Git Service

t#30322:create provider license import func
[pettanr/pettanr.git] / app / controllers / application_controller.rb
index fc35d3f..d802fbf 100644 (file)
@@ -1,7 +1,21 @@
 class ApplicationController < ActionController::Base
   protect_from_forgery
+  layout :devise_layout if MagicNumber['test_layout']
   before_filter :bf
   
+  def devise_layout
+    if devise_controller?
+      case resource_name
+      when :admin
+        'test'
+      when :user
+        'test'
+      end
+    else
+      'application'
+    end
+  end
+  
   def bf
     @server_result = {
       :location => {:controller => params[:controller], :action => params[:action]}
@@ -27,6 +41,26 @@ class ApplicationController < ActionController::Base
       else
         nil
       end
+      @demand_user = if demand_user_signed_in?
+        current_demand_user
+      else
+        nil
+      end
+    end
+  end
+  
+  def authenticate_author
+    if @author
+      true
+    else
+      respond_to do |format|
+        format.html { redirect_to main_app.new_author_path, :status => :found }
+        format.js { render "authors/new" }
+        format.json { 
+          raise ActiveRecord::Forbidden
+        }
+      end
+      false
     end
   end
   
@@ -49,10 +83,33 @@ class ApplicationController < ActionController::Base
     if file.respond_to?(:read)
       file.read
     else
-      Base64.decode64(file.to_s.gsub(' ', '+')) #rubyのバグ?+でデコードされるべきキャラがスペースになる
+      Base64.decode64(file.to_s.gsub(' ', '+'))
     end
   end
   
+  def ymd_to_time ymd_str
+    return nil if ymd_str.blank?
+    date = nil
+    begin
+      date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
+    rescue
+      date = nil
+    end
+    date
+  end
+  
+  def export_url demander_url, token, date
+    u = demander_url + (demander_url[-1] == '/' ? '' : '/')
+    u = URI.join(u, 'licenses_export.json?auth_token=' + token)
+    u = URI.join(u, '&date=' + date) unless date.blank?
+    u.to_s
+  end
+  
+  def export_from_provider url
+    json = RestClient.get url
+    JSON.parse json
+  end
+  
 =begin
   rescue_from ActiveRecord::RecordNotFound, :with => :render_404