Sha256: 9701c882525ff199c342d9f430e638b9c82304fbe7d1906f5451ba24b65dcbfa
Contents?: true
Size: 1.58 KB
Versions: 1
Compression:
Stored size: 1.58 KB
Contents
require "forwardable" module MobileEnhancements class RequestHelper extend Forwardable def self.delegated_methods instance_methods.select do |name| name =~ /^(desktop|mobile)\_/ end << :determine_layout end def_delegators :configuration, :mobile_path_prefix, :mobile_layout, :desktop_layout attr_reader :request, :configuration def initialize(request, configuration) @request = request @configuration = configuration end # returns a string defining which layout file to use def determine_layout mobile_request? ? mobile_layout : desktop_layout end # strips any mobile prefix from the url def desktop_url(url = request.url) desktop_path(url) end # strips any mobile prefix from the path def desktop_path(path = request.path) path.gsub(/^(.*?\/\/.*?)?\/(#{mobile_path_prefix}\/?)?(.*)$/) do "#{$1}/#{$3}" end.freeze end # ensures a mobile prefix exists in the url def mobile_url(url = request.url) mobile_path(url) end # ensures a mobile prefix exists in the path def mobile_path(path = request.path) path.gsub(/^(.*?\/\/.*?)?\/(#{mobile_path_prefix}\/?)?(.*)$/) do "#{$1}/#{mobile_path_prefix}/#{$3}" end.freeze end # returns whether or not this is a mobile request def mobile_request? @mobile_request ||= !!(request.path =~ /^\/#{mobile_path_prefix}(\/.*)?$/) end # returns whether or not this is a desktop request def desktop_request? !mobile_request? end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
mobile-enhancements-0.0.1 | lib/mobile_enhancements/request_helper.rb |