Hi, this blog is no longer maintained, my new blog is here

Ruby On Rails and a Conning Israeli entrepreneur

Model Reflection Overview

We all use ActiveRecord Reflection on the fly while programming, since we declare all the relations in the model and we are considering it as obvious.
i tried to search and see if there is a way to find those reflections and to alter it in runtime.

Model.reflections


i found this collection which contains all the information about the current model reflections.
for example.

  • .macro: this will return the type of the current reflection (:has_many, etc..)

  • .primary_key_name: the reflection primary key

  • .options: the reflection SQL join options

  • etc. etc.



for example, the following code will show me all the reflections for a model named Article:

  1. >> y Article.reflections.values.collect {|e| e.macro.to_s  + " => " + e.primary_key_name.to_s }  
  2. ---   
  3. - has_many => taggee_id  
  4. - belongs_to => layout_id  
  5. - has_many => article_id  
  6. - has_one => subject_id  
  7. - belongs_to => user_id  
  8. - has_many => taggee_id  
  9. => nil  

iphone on rails

A simple way to build iPhone specific interface with Rails

  1. class ApplicationController < ActionController::Base  
  2.   exempt_from_layout('iphone_html.erb')  
  3.     
  4.   before_filter :check_iphone  
  5.     
  6.   protected  
  7.   def iphone?  
  8.     request.user_agent.include?('iPhone')  
  9.   end  
  10.     
  11.   def check_iphone  
  12.     if iphone?  
  13.       request.parameters[:format] = 'iphone_html'  
  14.     end  
  15.   end  
  16. end  
  17.   
  18. class DashboardController < ApplicationController  
  19.   def index  
  20.     @top_movies = Movie.top_movies  
  21.     @movie = @top_movies.first  
  22.       
  23.     respond_to do |format|  
  24.       format.html # index.html.erb  
  25.       format.iphone_html #index.iphone_html.erb  
  26.     end  
  27.   end  
  28. end  

The running program

sometimes it can come handy to be able to know in which program you are running know.
it can be useful when displaying error information or such.

  1. caller(0)[0].split(".rb")[0]  >>  "(irb):74:in `irb_binding'"  

WYMeditor - Standards driven WYSIWYG editor

no words are needed, just a small thanks.
thanks for my man Dor Kalev for finding this edgy top of the line XHTML WYSIWYG editor

among the amazing features of this new editor:
1. XHTML strict + CSS compliant

2. No font or text formatting, sizes or colors - WYMeditor is CSS-based Designed

3. to be easy to integrate into your application

4. No installation needed - this is 100% Javascript code - no plugin, no extension

5. Simple Javascript code, you don't need to be a 'Javascript Guru' to understand it

6. Will remain as simple as possible

7. focus on well-tested code, stability and usability before adding new features

8. Image, link, table support

9. Skins supportvia CSS

10. Free and Open Source, fully adaptable to your needs



UPDATED: 13/11/07

The dude released it as a plugin, i am a bit late with this update, but
there it is.

Setting up logger options


i was called the other day about one of my past projects server going down because it's production log exceeded 2GB.
regularly, there is a cron job to cycle the log file and prevent such joyous occasions, but for some reason it didn't happen. so i decided to read a little bit about the Logger class in Rails and see if it has some magic for me yet.



in order to replace the log file path (and/or name) use the following syntax in environment.rb:

  1. Rails::Initializer.run do |config|  
  2.   config.logger = Logger.new(File.dirname(__FILE__) + "/../log/#{RAILS_ENV}.log")  
  3. end  


The actual cure for my problem came when i found the option to prevent the logger file from exceeding a pre-defined limit.
  1. config.logger = Logger.new(File.dirname(__FILE__) + "/../log/#{RAILS_ENV}.log", 1, 2*1024*1024)  


and a little extra for creating a brand new log file for each day.
  1. config.logger = Logger.new(File.dirname(__FILE__) + "/../log/#{RAILS_ENV}.log""daily")  


i suggest reading even more on the rails development browser

Add custom operators in Ruby

Jay Phillips developed the 'superators' gem, which enables the use of custom operators in Ruby. for example.

  1. require 'rubygems'  
  2. require 'superators'  
  3.   
  4. class Array  
  5.   superator "<---" do |operand|  
  6.     self << operand.reverse  
  7.   end  
  8. end  
  9.   
  10. ["jay"] <--- "spillihp"  

relative_time_helper plugin

most of us are using rails's built in time_ago_in_words, Rick Olsan wrote a much better plugin to extend that use and gives much better results.

  1. <%= relative_time(Time.now) %>  
  2. # today  
  3. <%= relative_time(1.day.ago) %>  
  4. # yesterday  
  5. <%= relative_time(1.day.from_now) %>  
  6. # tomorrow  
  7. <%= relative_time_span([Time.now, 5.days.from_now]) %>  
  8. # May 17th - 22nd  


To install it just snap it out of:

  1. script/plugin install http://ar-code.svn.engineyard.com/plugins/relative_time_helpers  

Unique Insertion into Array


  1. [1,2,3] | [3,4,5]       >> [1, 2, 3, 4, 5]  


The Web Ask eizesus.com

Subscribe

    follow me on Twitter

    Twiters Around

    About Me

    My photo
    I am a web developer for more than 9 years, managed, cried, coded, designed and made money in this industry. now trying to do it again.

    Labels