Sha256: bf226c94d33dc8c2dad14e7ed775cd02cf8ca3f2c9a31d03a1fbb28529be93df

Contents?: true

Size: 1.94 KB

Versions: 10

Compression:

Stored size: 1.94 KB

Contents

# -*- coding: utf-8 -*-

require 'smalruby_editor'

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  helper_method :standalone?, :raspberrypi?, :current_user,
                :current_preferences

  before_filter :set_locale
  before_filter :reload_preference if Rails.env.development?

  private

  # # rubocop:disable LineLength
  # http://www.xyzpub.com/en/ruby-on-rails/3.2/i18n_mehrsprachige_rails_applikation.html
  # # rubocop:enable LineLength
  def extract_locale_from_accept_language_header
    request.env['HTTP_ACCEPT_LANGUAGE'].scan(/^[a-z]{2}/).first rescue nil
  end

  # ロケールの設定
  def set_locale
    I18n.locale = params[:locale] || session[:locale] ||
      extract_locale_from_accept_language_header || I18n.default_locale
    session[:locale] = I18n.locale
    Rails.application.routes.default_url_options[:locale] = I18n.locale
  end

  # スタンドアローンモードかどうかを返す
  def standalone?
    return false if Rails.env == 'production'
    return true if Rails.env == 'standalone'

    if Rails.env != 'test' &&
        (ENV['SMALRUBY_EDITOR_STANDALONE_MODE'] ||
         File.exist?(Rails.root.join('tmp', 'standalone')))
      true
    else
      false
    end
  end

  # Raspberry Piかどうかを返す
  def raspberrypi?
    SmalrubyEditor.raspberrypi?
  end

  def check_whether_standalone
    unless standalone?
      fail "#{self.class.name}##{action_name}はstandaloneモードのみの機能です"
    end
  end

  def reload_preference
    Preference.reload!
  end

  def require_auth
    if session[:username].blank?
      head :unauthorized
      return false
    end
  end

  def current_user
    @current_user ||= User.where(name: session[:username]).first
  end

  def current_preferences
    current_user.try(:preferences) || Preference.to_hash
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
smalruby-editor-0.3.5-x86-mingw32 app/controllers/application_controller.rb
smalruby-editor-0.3.5 app/controllers/application_controller.rb
smalruby-editor-0.3.4-x86-mingw32 app/controllers/application_controller.rb
smalruby-editor-0.3.4 app/controllers/application_controller.rb
smalruby-editor-0.3.3-x86-mingw32 app/controllers/application_controller.rb
smalruby-editor-0.3.3 app/controllers/application_controller.rb
smalruby-editor-0.3.2-x86-mingw32 app/controllers/application_controller.rb
smalruby-editor-0.3.2 app/controllers/application_controller.rb
smalruby-editor-0.3.1-x86-mingw32 app/controllers/application_controller.rb
smalruby-editor-0.3.1 app/controllers/application_controller.rb