Sha256: f89b7474279a7df96ab017311bc1de8f10109acd344683fc712051e8a53c3b29

Contents?: true

Size: 1.91 KB

Versions: 22

Compression:

Stored size: 1.91 KB

Contents

module Cms

  module MobileAware

    include Cms::DefaultCaches

    # Looks for a mobile template if the request is mobile, falling back to the html template if it can't be found.
    #
    # @return [String] relative path/name of the layout to be rendered by a page (i.e. 'layouts/templates/default')
    def determine_page_layout
      if respond_as_mobile?
        mobile_exists = template_exists?(@page.layout_name, "layouts/mobile")
        return @page.layout(:mobile) if mobile_exists
      end
      @page.layout(:full)
    end

    # This is changing a class attribute in order to write the page cache to a different directory based on mobile vs full page request.
    # @warning - May not be thread safe?
    def select_cache_directory
      if respond_as_mobile?
        self.class.page_cache_directory = mobile_cache_directory
      else
        self.class.page_cache_directory = cms_cache_directory
      end
    end

    # Because of caching, CMS pages should only return mobile content on a separate subdomain.
    # or if a CMS editor wants to see the mobile version of the page.
    #
    # @return [Boolean] true if this request is considered 'mobile', false otherwise
    def respond_as_mobile?
      log "For mobile optimization, checking the subdomain for '#{request.domain}' is '#{request.subdomain}'."
      if params[:template] =='mobile'
        session[:mobile_mode] = true
      elsif params[:template] =='full'
        session[:mobile_mode] = false
      end

      request.subdomain == "m" || (session[:mobile_mode] == true && current_user.able_to?(:edit_content))
    end

    private

    def print_request_info
      log "*" * 20
      log "User Agent: #{request.user_agent}"
      m = "Mobile Request?: "
      if respond_as_mobile?
        m += "Yes"
      else
        m += "No"
      end
      log m
    end

    def log(m)
      logger.debug m
    end

    def banner(m)
      log "*" * 20
      log m
    end
  end

end

Version data entries

22 entries across 22 versions & 2 rubygems

Version Path
browsercms-artirix-4.0.4 lib/cms/mobile_aware.rb
browsercms-artirix-4.0.3.3 lib/cms/mobile_aware.rb
browsercms-artirix-4.0.3.2 lib/cms/mobile_aware.rb
browsercms-artirix-4.0.3.1 lib/cms/mobile_aware.rb
browsercms-artirix-4.0.3 lib/cms/mobile_aware.rb
browsercms-artirix-4.0.2 lib/cms/mobile_aware.rb
browsercms-artirix-4.0.1.1 lib/cms/mobile_aware.rb
browsercms-artirix-4.0.0.rc1.art4 lib/cms/mobile_aware.rb
browsercms-4.0.0.rc1 lib/cms/mobile_aware.rb
browsercms-4.0.0.beta lib/cms/mobile_aware.rb
browsercms-4.0.0.alpha lib/cms/mobile_aware.rb
browsercms-3.5.7 lib/cms/mobile_aware.rb
browsercms-3.5.6 lib/cms/mobile_aware.rb
browsercms-3.5.5 lib/cms/mobile_aware.rb
browsercms-3.5.4 lib/cms/mobile_aware.rb
browsercms-3.5.3 lib/cms/mobile_aware.rb
browsercms-3.5.2 lib/cms/mobile_aware.rb
browsercms-3.5.1 lib/cms/mobile_aware.rb
browsercms-3.5.0 lib/cms/mobile_aware.rb
browsercms-3.5.0.rc3 lib/cms/mobile_aware.rb