Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

Rob Dupuis

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Rails 2.1.0 monkey patch to add the recognized or matched rails route to the request

This was based on code I found here from Chris Cruft - http://cho.hapgoods.com/wordpress/?p=151

Enhanced to support rails 2.1.0 and also allows alias method chains for recognize from other plugins still to work.

   1  
   2  module ActionController
   3    class AbstractRequest
   4      attr_reader :recognized_route
   5      
   6      def init_recognized_route_from_path_parameters
   7        recognized_route = path_parameters.delete(:recognized_route) 
   8      end
   9    end
  10  
  11    module Routing
  12      
  13      class RouteSet
  14        def recognize_with_route(request)
  15          result = recognize_without_route(request)
  16          request.init_recognized_route_from_path_parameters
  17          result
  18        end
  19        alias_method_chain :recognize, :route
  20  
  21        def write_recognize_optimized_with_route
  22          tree = segment_tree(routes)
  23          body = generate_code(tree)
  24          instance_eval %{
  25            def recognize_optimized(path, env)
  26              segments = to_plain_segments(path)
  27              index = #{body}
  28              return nil unless index
  29              while index < routes.size
  30                result = routes[index].recognize(path, env) and result[:recognized_route] = routes[index] and return result
  31                index += 1
  32              end
  33              nil
  34            end
  35          }, __FILE__, __LINE__
  36        end
  37        alias_method_chain :write_recognize_optimized, :route
  38  
  39      end
  40    end
  41  end
  42  

Code to disable YUI calendar previous and next month links icons

This solution does not require modifying the yui source and works on YUI version 2.5.0. It will disable the next and previous arrows based on the out of bounds dates you specify with mindate and maxdate when you configure the calendar. Requires YUI dom and a namespace called your_app.

1) attach an onRender event when you configure the calendar and before you call render

   1  calendar.renderEvent.subscribe(YAHOO.your_app.calendar().initArrows, calendar);


2) add the following function:

   1  
   2  YAHOO.your_app.calendar = function() {
   3  	
   4  	var addDays = function(date, days) {
   5  		return YAHOO.widget.DateMath.add(date, YAHOO.widget.DateMath.DAY, days);
   6  	};
   7  	
   8  	var showPreviousArrow = function(cal) {
   9  		return showArrow(cal, cal.toDate(cal.cellDates[0]), 1);
  10  	};
  11  
  12  	var showNextArrow = function(cal) {
  13  		return showArrow(cal, cal.toDate(cal.cellDates[cal.cellDates.length-1]), -1);
  14  	};
  15  	
  16  	var showArrow = function(cal, startingDate, step) {
  17  		if (!cal.isDateOOM(startingDate)) { //ie not overlapping
  18  			return !cal.isDateOOB(addDays(startingDate, (-1 * step)));
  19  		}
  20  		for (var i=0; (i * step) < 7; i += step) { //iterate forwards for previous month check, backwards for next month check
  21  			var date = addDays(startingDate, i);
  22  			if (!cal.isDateOOM(date)) { //shortcut exit; as soon as we find an in month date we can supress the arrow
  23  				return false;
  24  			} else if (!cal.isDateOOB(date)) {
  25  				return true;
  26  			}
  27  		}
  28  		return false;
  29  	};
  30  	
  31  	return {
  32  		initArrows: function(type, args, cal) {
  33  			if (!showPreviousArrow(cal)) {
  34  				hideArrow(cal, cal.Style.CSS_NAV_LEFT);
  35  			}
  36  
  37  			if (!showNextArrow(cal)) {
  38  				hideArrow(cal, cal.Style.CSS_NAV_RIGHT);
  39  			}
  40  		}
  41  	};
  42  };
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS