1
0
Fork 0
oldhaven/app/controllers/application_controller.rb

95 lines
2.2 KiB
Ruby
Raw Normal View History

2011-06-22 00:55:09 +00:00
require 'digest'
2011-09-17 11:38:49 +00:00
require 'vkontakte'
2011-06-22 00:55:09 +00:00
2011-04-03 16:21:05 +00:00
class ApplicationController < ActionController::Base
protect_from_forgery
2011-09-17 10:20:07 +00:00
before_filter :set_locale
2011-06-22 00:55:09 +00:00
2011-11-26 20:56:09 +00:00
def cache_for time
response.headers['Cache-Control'] = 'public, max-age=' + time.seconds.to_s
end
2011-11-27 08:41:28 +00:00
def compile_page params
2011-11-28 12:24:47 +00:00
compiler = lambda do |params|
2011-11-28 23:56:02 +00:00
@data = {}
unless params[:data].nil?
if params[:data].is_a?(Proc)
@data = params[:data].call
elsif params[:data].is_a?(Hash)
@data = params[:data]
else
@data = params[:data].serialize
end
end
2011-11-28 20:44:51 +00:00
@status = params[:status]
2011-11-28 12:24:47 +00:00
{
renderer: "unified",
data: @data,
html: render_compact_partial(params[:partial]),
title: params[:title],
status: (params[:status] unless params[:status].nil?),
callback: (params[:callback] unless params[:callback].nil?)
}.to_json.to_s
end
2011-11-28 20:44:51 +00:00
unless params[:cache_for].nil?
data = Rails.cache.fetch(params[:cache_key] || cache_key_for(params[:data]), expires_in: params[:cache_for]) do
2011-11-28 12:24:47 +00:00
compiler.call(params)
end
else
data = compiler.call(params)
end
render text: data, content_type: 'application/json'
2011-11-27 08:41:28 +00:00
end
def get_artist_name_from_query
params[:artist].gsub('%20', ' ').gsub('+', ' ').gsub('.html', '')
end
2011-10-20 11:01:57 +00:00
protected
2011-11-28 12:24:47 +00:00
2011-11-30 07:52:16 +00:00
def user_session
2011-11-29 19:42:21 +00:00
Session.find_by_key(session_key)
2011-11-28 12:24:47 +00:00
end
2011-09-17 11:38:49 +00:00
2011-11-29 19:42:21 +00:00
def current_user
2011-11-30 07:52:16 +00:00
user_session.user if user_present?
2011-11-29 19:42:21 +00:00
end
2011-11-30 07:52:16 +00:00
def user_present?
!user_session.nil? and !user_session.user.nil?
2011-06-22 00:55:09 +00:00
end
2011-09-17 10:20:07 +00:00
def set_locale
2011-11-30 07:52:16 +00:00
unless user_session.nil?
I18n.locale = user_session.user.lang
2011-09-17 10:20:07 +00:00
end
end
2011-11-29 19:42:21 +00:00
def authorize
unless Vkontakte.check(params)
render json: { status: 'login failed' }, status: 403
end
end
2011-09-17 11:38:49 +00:00
def session_key
2011-10-05 21:54:35 +00:00
if cookies[:beathaven_sid].nil?
cookies[:beathaven_sid] = {
2011-10-24 07:33:32 +00:00
:value => Session.generate_key,
2011-10-05 21:54:35 +00:00
:expire => 42.years.from_now.utc
}
2011-09-17 11:38:49 +00:00
end
2011-10-05 21:54:35 +00:00
cookies[:beathaven_sid]
2011-09-17 10:20:07 +00:00
end
2011-10-20 11:01:57 +00:00
2011-11-29 19:42:21 +00:00
def cache_key_for object
"#{object.class.to_s.underscore}_#{object.id}"
end
2011-10-20 11:01:57 +00:00
def render_compact_partial partial_name
(render_to_string :partial => partial_name.to_s).gsub(/\n(\s+)?/, '')
end
2011-04-03 16:21:05 +00:00
end