Permalink
Browse files

Merge branch 'master' into fullpath_argument_for_annotation_tags

  • Loading branch information...
2 parents bd7d528 + 1efb8b0 commit 6f534ad7502fe4f6e5c906e1036a934b2241dc07 @lokimeyburg lokimeyburg committed Apr 18, 2013
View
@@ -15,3 +15,4 @@ spec/reports
test/tmp
test/version_tmp
tmp
+integration/
View
@@ -1,21 +1,21 @@
-# Ress
+# RESS
A system for building mobile optimized Rails applications using semantic,
-media query-based device detection and server side progressive enhancement.
+media query-based device detection and server side component optimization.
## Background
-Ress is an extension of the [devicejs](https://github.com/borismus/device.js)
+RESS is an extension of the [devicejs](https://github.com/borismus/device.js)
library written by [Boris Smus](http://smus.com/). It adds a back end for
-adapting server responses based on client side feature detection. Ress allows
+adapting server responses based on client side feature detection. RESS allows
you to specify alternate versions of your website, along with media queries
for which devices should be redirected to which version.
## How it Works
### HTML Annotations
-When you register alternate mobile versions of your website, Ress adds annotations
+When you register alternate mobile versions of your website, RESS adds annotations
to the `<head>` of your document that describe where these pages are located and
which devices should be redirected to them.
@@ -43,7 +43,7 @@ all of the `[rel="alternate"]` links in your markup, and evalute their media que
to determine if there is an alternate version available that matches the client.
If there is, the user is redirected to the url for that version.
-### Server Side Components
+### Server Side Component Optimization
Ress allows you to customize how your Rails application responds to mobile requests in
two ways:
@@ -123,6 +123,24 @@ make sure that it includes "Touch Events" and "Media Queries", eg:
http://modernizr.com/download/#-touch-mq-teststyles-prefixes
+### Sessions and Cookies
+
+In order to share sessions and cookies between the different subdomains used by the
+alternate versions of your app, you need to configure the `:domain` option both in
+the `config/initializers/session_store.rb` and when setting cookies. For more
+information about how this works see this
+[Railscast](http://railscasts.com/episodes/221-subdomains-in-rails-3).
+
+### Development
+
+Because RESS uses subdomains, while developing alternate versions you cannot
+load your site via `localhost` or an IP address. If you want to test on
+the same machine you are running your rails app on, you can load it through
+http://lvh.me or install [pow](http://pow.cx/) and set up a `.dev` domain
+for your app. If you need to test on a mobile device you might want to try
+http://xip.io/.
+
+
## Performance considerations
The javascript included by Ress does some checks and will use client-side
@@ -5,10 +5,16 @@
# == Canonical Subdomain
#
- # If the cannonical version of your application is served under a subdomain
+ # If the canonical version of your application is served under a subdomain
# you must set it here:
#
# config.set_canonical :subdomain => "subdomain"
+ #
+ # By default, alternate version subdomains are prepended to the canonical url.
+ # If you are using a canonical subdomain and you would like it to be replaced
+ # by the alternate subdomains, set this option to true:
+ #
+ # config.replace_canonical_subdomain = false
# == Modernizr
View
@@ -1,9 +1,10 @@
require "action_view"
require "ress/version"
+require "ress/subdomain"
require "ress/alternate_version"
require "ress/canonical_version"
-require "ress/category_collection"
+require "ress/config"
require "ress/controller_additions"
require "ress/view_helpers"
@@ -14,24 +15,28 @@
module Ress
extend self
- def category_collection
- @categories ||= CategoryCollection.new
+ def config
+ @config ||= Config.new
end
def canonical_version
- category_collection.canonical_version
+ config.canonical_version
end
def alternate_versions
- category_collection.alternate_versions
+ config.alternate_versions
end
def include_modernizr?
- category_collection.include_modernizr
+ config.include_modernizr
+ end
+
+ def replace_canonical_subdomain?
+ config.replace_canonical_subdomain
end
def configure
- yield(category_collection)
+ yield(config)
end
end
@@ -2,35 +2,30 @@ module Ress
class CanonicalVersion
- attr_reader :subdomain
-
def initialize(options = {})
- @subdomain = options.fetch(:subdomain, false)
+ @subdomain = Ress::Subdomain.create(options.fetch(:subdomain, nil))
end
- def matches?(subdomain)
- if self.subdomain
- self.subdomain == subdomain
- else
- subdomain.empty?
- end
+ def matches?(req_subdomain)
+ subdomain.matches?(req_subdomain)
end
# Create a tag of this format:
# `<link rel="canonical" href="http://www.example.com/page-1" >`
- def link_tag(protocol, fullpath, subdomain, view)
- view.tag :link, :rel => 'canonical', :href => url(protocol, fullpath, subdomain)
+ def link_tag(protocol, fullpath, req_subdomain, view)
+ view.tag :link, :rel => 'canonical', :href => url(protocol, fullpath, req_subdomain)
end
- def url(protocol, fullpath, subdomain)
- fullpath = fullpath[(subdomain.length + 1)..-1] unless subdomain.empty?
- if self.subdomain
- "#{protocol}#{self.subdomain}.#{fullpath}"
- else
- "#{protocol}#{fullpath}"
- end
+ def url(protocol, fullpath, req_subdomain)
+ subdomain.url(protocol, fullpath, req_subdomain)
end
+ private
+
+ def subdomain
+ @subdomain
+ end
+
end
end
@@ -1,8 +1,8 @@
module Ress
- class CategoryCollection
+ class Config
- attr_accessor :include_modernizr
+ attr_accessor :include_modernizr, :replace_canonical_subdomain
attr_reader :canonical_version, :alternate_versions
def initialize
View
@@ -0,0 +1,81 @@
+module Ress
+
+ class Subdomain
+
+ # this class is a factory, don't instantiate it directly
+ private_class_method :new
+
+ def self.create(subdomain)
+ case subdomain
+ when String
+ StringSubdomain.new(subdomain)
+ when Regexp
+ RegexpSubdomain.new(subdomain)
+ else
+ NilSubdomain.new
+ end
+ end
+
+ class NilSubdomain
+ def matches?(subdomain)
+ subdomain.empty?
+ end
+
+ def url(protocol, fullpath, subdomain)
+ fullpath = fullpath[(subdomain.length + 1)..-1] unless subdomain.empty?
+ "#{protocol}#{fullpath}"
+ end
+ end
+
+ class StringSubdomain
+
+ attr_reader :subdomain
+
+ def initialize(subdomain)
+ @subdomain = subdomain
+ end
+
+ def matches?(subdomain)
+ self.subdomain == subdomain
+ end
+
+ def url(protocol, fullpath, subdomain)
+ fullpath = fullpath[(subdomain.length + 1)..-1] unless subdomain.empty?
+ "#{protocol}#{self.subdomain}.#{fullpath}"
+ end
+
+ end
+
+ # The main inspiration for this class is to match xip.io
+ # subdomains for many devs working in development mode with
+ # the same configuration.
+ class RegexpSubdomain
+
+ attr_reader :subdomain
+
+ def initialize(subdomain)
+ @subdomain = subdomain
+ end
+
+ def matches?(subdomain)
+ self.subdomain =~ subdomain
+ end
+
+ def url(protocol, fullpath, subdomain)
+ fullpath = fullpath[(subdomain.length + 1)..-1] unless subdomain.empty?
+ begin
+ return build_url(protocol, subdomain, fullpath) if matches?(subdomain)
+ end while( subdomain.slice!(/^[^\.]+\.?/) )
+ end
+
+ private
+
+ def build_url(protocol, subdomain, fullpath)
+ subdomain.empty? ? "#{protocol}#{fullpath}" : "#{protocol}#{subdomain}.#{fullpath}"
+ end
+
+ end
+
+ end
+
+end
View
@@ -1,3 +1,3 @@
module Ress
- VERSION = "0.0.6"
+ VERSION = "0.0.10"
end
View
@@ -9,6 +9,7 @@ def ress_annotation_tags(options={})
path = "#{request.host_with_port}#{fullpath}"
html = if canonical_request?
+ path.gsub!("#{request.subdomain}.", '') if Ress.replace_canonical_subdomain?
Ress.alternate_versions.map do |category|
category.link_tag(request.protocol, path, self)
end.join
@@ -1,21 +1,7 @@
-require_relative '../../lib/ress/canonical_version'
+require_relative '../../lib/ress'
describe Ress::CanonicalVersion do
- describe '#subdomain' do
-
- it 'defaults to false' do
- category = Ress::CanonicalVersion.new
- category.subdomain.should == false
- end
-
- it 'can be overridden by an optional parameter' do
- category = Ress::CanonicalVersion.new(:subdomain => 'foo')
- category.subdomain.should == 'foo'
- end
-
- end
-
describe '#matches?' do
context 'with no canonical subdomain' do
@@ -1,8 +1,8 @@
require_relative '../../lib/ress'
-describe Ress::CategoryCollection do
+describe Ress::Config do
- let(:collection) { Ress::CategoryCollection.new }
+ let(:collection) { Ress::Config.new }
describe '#add_alternate' do
@@ -27,12 +27,12 @@
describe 'canonical_version' do
it 'defaults to a canonical_version with no subdomain' do
- collection.canonical_version.subdomain.should be_false
+ collection.canonical_version.matches?('').should be_true
end
it 'can be altered through set_canonical' do
collection.set_canonical :subdomain => 'foo'
- collection.canonical_version.subdomain.should == 'foo'
+ collection.canonical_version.matches?('foo').should be_true
end
end
Oops, something went wrong.

0 comments on commit 6f534ad

Please sign in to comment.