Skip to content

Instantly share code, notes, and snippets.

@aiwilliams
Created September 20, 2008 18:58
Show Gist options
  • Save aiwilliams/11778 to your computer and use it in GitHub Desktop.
Save aiwilliams/11778 to your computer and use it in GitHub Desktop.
# Copyright (c) 2007, Adam Williams and John W. Long.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
require 'singleton'
require 'ostruct'
require 'digest/sha1'
# A very handy place to:
#
# * store information about the application
# * generate urls without a controller/mailer
# * generate nice, short sha1 digests
# * assign unique per record generated codes
#
# == Installation
#
# 1. In config/initializers/application.rb:
#
# Application.host = "mykillerapp.com"
#
# 2. In app/controllers/application.rb:
#
# include ApplicationConfiguration::ControllerTriggeredUpdating
#
# == Configuring values for you application
#
# As this is an OpenStruct instance, you may assign anything you'd like, as
# in:
#
# Application.no_reply_address = "noreply@mykillerapp.com"
#
# Now, in your application, you can do stuff like this:
#
# Application.no_reply_address
#
# == Routing
#
# Routes are available to you everywhere. Of course, they already are in
# controllers, views, and mailers. This basically handles accurately
# configuring the existing host and port defaults for Rails
# ActionController::UrlWriter stuff. Normally, you would have to do this in
# each of your environment files.
#
# Application.signup_path # named routes are available!
#
class ApplicationConfiguration < OpenStruct
include Singleton
include ActionController::UrlWriter
# Make the named routes we have public methods. This will be invoked when
# the routes are loaded.
def self.routes_loaded!
instance_methods.each do |method|
method = method.to_sym
if ActionController::Routing::Routes.named_routes.helpers.include?(method)
public method
end
end
end
# And just in case they already have been...
routes_loaded!
# Answers a proc for use in an ActiveRecord.before_create callback to assign
# a unique-for-model.base_class code. The attribute defaults to :code, but
# if you need it to assign to something else, you can provide that.
def assign_code(attribute = :code)
lambda do |model_instance|
while model_instance.send(attribute).nil? || model_instance.class.base_class.exists?(attribute => model_instance.send(attribute))
model_instance.send :"#{attribute}=", Application.generate_code
end
end
end
# Generates shorter codes, keeping URLs that use them shorter
def generate_code
Digest::SHA1.hexdigest(Time.now.to_s.split(//).sort_by {rand}.join)[0..10]
end
def host
default_url_options[:host]
end
def host=(value)
default_url_options[:host] = value
end
def port
default_url_options[:port]
end
# Assignment is ignored if you pass 80, since ActionController::UrlWriter
# will put the port in URLs if it is provided at all.
def port=(value)
if value.nil? || value.to_s == '80'
default_url_options.delete(:port)
else
default_url_options[:port] = value
end
end
module ControllerTriggeredUpdating
def self.included(controller_class)
controller_class.module_eval do
before_filter :assign_current_host
end
end
def assign_current_host
ApplicationConfiguration.instance.host = request.host
ApplicationConfiguration.instance.port = request.port
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment