Module: TmlRails::ActionControllerExtension::InstanceMethods
- Defined in:
- lib/tml_rails/extensions/action_controller_extension.rb
Instance Method Summary collapse
- #tml_access_token ⇒ Object
-
#tml_cookie ⇒ Object
Returns data from cookie set by the agent.
- #tml_filter_init ⇒ Object
- #tml_filter_reset ⇒ Object
- #tml_init ⇒ Object
-
#tml_locale ⇒ Object
Locale is retrieved from method => params => cookie => subdomain => browser accepted locales Alternatively, this method can be overwritten.
- #tml_reset ⇒ Object
-
#tml_source ⇒ Object
Overwrite this method in a controller to assign a custom source for all views.
- #tml_translator ⇒ Object
- #tml_viewing_user ⇒ Object
Instance Method Details
#tml_access_token ⇒ Object
124 125 126 |
# File 'lib/tml_rails/extensions/action_controller_extension.rb', line 124 def tml_access_token [:oauth] ? [:oauth][:token] : nil end |
#tml_cookie ⇒ Object
Returns data from cookie set by the agent
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/tml_rails/extensions/action_controller_extension.rb', line 56 def @tml_cookie ||= begin = [Tml::Utils.(Tml.config.application[:key])] if .blank? {} else HashWithIndifferentAccess.new(Tml::Utils.decode(, Tml.config.application[:token])) end end rescue Exception => ex Tml.logger.error("Failed to parse tml cookie: #{ex.}") {} end |
#tml_filter_init ⇒ Object
236 237 238 |
# File 'lib/tml_rails/extensions/action_controller_extension.rb', line 236 def tml_filter_init tml_init if Tml.config.auto_init end |
#tml_filter_reset ⇒ Object
250 251 252 |
# File 'lib/tml_rails/extensions/action_controller_extension.rb', line 250 def tml_filter_reset tml_reset if Tml.config.auto_init end |
#tml_init ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 |
# File 'lib/tml_rails/extensions/action_controller_extension.rb', line 128 def tml_init return if Tml.config.disabled? # Tml.logger.info(tml_cookie.inspect) @tml_started_at = Time.now requested_locale = desired_locale = tml_locale # if user has a custom method for providing the locale, use it if Tml.config.current_locale_method begin desired_locale = self.send(Tml.config.current_locale_method) rescue desired_locale = requested_locale end end # check if locale was previously stored in a cookie desired_locale ||= Tml.config. ? [:locale] : nil # fallback onto the browser locale desired_locale ||= Tml.config.locale_browser_enabled? ? Tml::Utils.browser_accepted_locales( request.env['HTTP_ACCEPT_LANGUAGE'] ).join(',') : nil # pp requested_locale: requested_locale, desired_locale: desired_locale # pp cookie: tml_cookie # init SDK with desired locale and get the actual locale supported in the app Tml.session.init( :source => tml_source, :locale => desired_locale, :user => tml_viewing_user, :translator => tml_translator, :access_token => tml_access_token ) if defined? I18n.enforce_available_locales I18n.enforce_available_locales = false end I18n.locale = tml_current_locale # pp current_locale: tml_current_locale # check if we want to store the last selected locale in the cookie if requested_locale == tml_current_locale and Tml.config. [:locale] = tml_current_locale [Tml::Utils.(Tml.config.application[:key])] = { :value => Tml::Utils.encode(), :expires => 1.year.from_now, :domain => Tml.config.locale[:domain] } end # pp cookie: tml_cookie # pp redirect: Tml.config.locale_redirect_enabled? if Tml.config.locale_redirect_enabled? if Tml.config.locale[:skip_default] and tml_current_locale == tml_default_locale # first lets see if we are in default locale and user doesn't want to show locale in url if Tml.config.locale_strategy == 'pre-path' and not requested_locale.nil? return redirect_to(Tml.config.locale_param => nil) end if Tml.config.locale_strategy == 'pre-domain' and request.subdomains.any? fragments = request.host.split('.') if fragments.first.match(Tml.config.locale_expression) if Tml.config.locale[:default_subdomain] fragments[0] = Tml.config.locale[:default_subdomain] else fragments.shift end end return redirect_to(host: fragments.join('.')) end if Tml.config.locale_strategy == 'custom-domain' host = Tml.config.locale[:mapping][tml_default_locale] return redirect_to(host: host) end elsif requested_locale != tml_current_locale # otherwise, the locale is not the same as what was requested, deal with it if Tml.config.locale_strategy == 'pre-path' return redirect_to(Tml.config.locale_param => tml_current_locale) end if Tml.config.locale_strategy == 'pre-domain' fragments = request.host.split('.') if request.subdomains.any? fragments[0] = tml_current_locale else fragments.unshift(tml_current_locale) end return redirect_to(host: fragments.join('.')) end if Tml.config.locale_strategy == 'custom-domain' host = Tml.config.locale[:mapping][tml_current_locale] host ||= Tml.config.locale[:mapping][tml_default_locale] return redirect_to(host: host) end end end if tml_current_translator and tml_current_translator.inline? I18n.reload! end end |
#tml_locale ⇒ Object
Locale is retrieved from method => params => cookie => subdomain => browser accepted locales Alternatively, this method can be overwritten
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/tml_rails/extensions/action_controller_extension.rb', line 79 def tml_locale # if locale has been passed by a param, it will be in the params hash if Tml.config.locale_strategy == 'param' return params[Tml.config.locale_param] # will be nil without ?locale=:locale end if Tml.config.locale_strategy == 'pre-path' return params[Tml.config.locale_param] # will be nil without /:locale end if Tml.config.locale_strategy == 'pre-domain' locale = request.subdomains.first if locale.nil? or not locale.match(Tml.config.locale_expression) locale = Tml.config.locale[:default] end return locale end if Tml.config.locale_strategy == 'custom-domain' host = "#{request.host}#{[80, 443].include?(request.port) ? '' : ":#{request.port}"}" locale = Tml.config.locale[:mapping].invert[host] # will be nil if host is wrong return locale end nil end |
#tml_reset ⇒ Object
240 241 242 243 244 245 246 247 248 |
# File 'lib/tml_rails/extensions/action_controller_extension.rb', line 240 def tml_reset return if Tml.config.disabled? @tml_finished_at = Time.now Tml.session.application.submit_missing_keys if Tml.session.application Tml.session.reset Tml.cache.reset_version Tml.logger.info("Request took #{@tml_finished_at - @tml_started_at} mls") if @tml_started_at Tml.logger.info('-----------------------------------------------------------') end |
#tml_source ⇒ Object
Overwrite this method in a controller to assign a custom source for all views
71 72 73 74 75 |
# File 'lib/tml_rails/extensions/action_controller_extension.rb', line 71 def tml_source "/#{controller_name}/#{action_name}" rescue "/classes/#{self.class.name}" end |
#tml_translator ⇒ Object
116 117 118 119 120 121 122 |
# File 'lib/tml_rails/extensions/action_controller_extension.rb', line 116 def tml_translator if [:translator] Tml::Translator.new([:translator]) else nil end end |
#tml_viewing_user ⇒ Object
106 107 108 109 110 111 112 113 114 |
# File 'lib/tml_rails/extensions/action_controller_extension.rb', line 106 def tml_viewing_user unless Tml.config.current_user_method.blank? begin self.send(Tml.config.current_user_method) rescue {} end end end |