Module: TooShort::ControllerMethods

Defined in:
lib/controller_methods.rb

Overview

To be included in the controller that should handle short URLs, i.e class ShortUrlsController < ApplicationController

include TooShort::ControllerMethods

end

response handling can be customized by overwriting these methods respond_to_valid_short_url respond_to_invalid_short_url

the object that was looked up can be accessed via @object_from_short_url

Instance Method Summary collapse

Instance Method Details

#expandObject



14
15
16
17
18
19
20
21
# File 'lib/controller_methods.rb', line 14

def expand
  require_all_model_classes
  if @object_from_short_url = TooShort.expand_to_object(params[:scope], params[:hash])
    respond_to_valid_short_url
  else
    respond_to_invalid_short_url
  end
end

#respond_to_invalid_short_urlObject



27
28
29
30
31
32
33
34
# File 'lib/controller_methods.rb', line 27

def respond_to_invalid_short_url
  respond_to do |wants|
    error_hash = {:error => 'Invalid short url'}
    wants.html  { render :text  => error_hash[:error]}
    wants.xml   { render :xml   => error_hash}
    wants.json  { render :json  => error_hash}
  end
end

#respond_to_valid_short_urlObject



23
24
25
# File 'lib/controller_methods.rb', line 23

def respond_to_valid_short_url
  redirect_to @object_from_short_url
end