Module: Lesli::RequesterInterface

Included in:
ApplicationDeviseController, ApplicationLesliController
Defined in:
app/interfaces/lesli/requester_interface.rb

Instance Method Summary collapse

Instance Method Details

#get_user_agent(as_string = true) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/interfaces/lesli/requester_interface.rb', line 80

def get_user_agent(as_string=true)

    http_user_agent = request.env["HTTP_USER_AGENT"]

    # parse user agent
    user_agent = UserAgent.parse(http_user_agent)

    user_agent_version = user_agent.version.to_a.first(2).join(".")

    # return user agent as object
    if as_string == false
        return {
            platform: user_agent.platform,
            os: user_agent.os,
            browser: user_agent.browser,
            version: user_agent_version
        }
    end

    # return user agent info as string
    "#{user_agent.platform} #{user_agent.os} - #{user_agent.browser} #{user_agent_version}"
end

#set_localeObject

Set the user language based on user_settings, session configuration or instance default locale



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/interfaces/lesli/requester_interface.rb', line 41

def set_locale
    # get saved language in session, browser language or the default in config
    # the session param is setted in settings controller through "get :language, to: "settings#language""
    locale = session[:locale] || I18n.default_locale

    # get user's preferred language
    # IMPORTANT:
    #       Here it's not possible to use the methods provided by devise to check if user is
    #       authenticated "user_signed_in", due those methods redirects to the login controller
    #       if user is not authenticated; For some scenarios we need to have control of the behavior
    #       for not authenticated user requests, thats why here we go deeper and check if user is
    #       authenticated checking the warden storage
    # locale = current_user.locale if warden.authenticated?

    # language defined in the http header request
    locale = request.headers["Require-Language"] unless request.headers["Require-Language"].blank?

    # use default locale if requested language is not supported
    locale = I18n.default_locale unless I18n.available_locales.include?(locale.to_sym)

    # set the new locale
    I18n.locale = locale
end

#set_pathObject



36
37
38
# File 'app/interfaces/lesli/requester_interface.rb', line 36

def set_path
    @@engine_path = Lesli::Engine.routes.find_script_name({})
end

#set_requesterObject

Set default query params for:



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/interfaces/lesli/requester_interface.rb', line 66

def set_requester
    @query = {
        :search => params[:search] || nil,
        :pagination => {
            :perPage => (params[:perPage] ? params[:perPage].to_i : 12),
            :page => (params[:page] ? params[:page].to_i : 1)
        },
        :order => {
            :by => (params[:orderBy] || "id"),
            :dir => (params[:order] || "desc")
        }
    }
end