Monday, September 3, 2007

कम्प्यूटर पर संदेश टंकित करें, नोकिया फोन से भेज




भारतीय भाषाओं में एस एम एस भेजना एक दुष्कर कार्य प्रतीत होता है। पर कुछ सुविधाएँ हैं जिसका प्रयोग करके आप इसका भी मजा ले सकते हैं। यदि आप नोकिया का फोन प्रयोग करते हैं तो यह काफी हद तक संभव है।ज्यादातर फोन निर्माता फोन खरीदते समय एक सी डी देते हैं जिसमे आपके कम्प्यूटर के लिए एक साफ्टवेयर होता है। यह साफ्टवेयर आपके फोन पर संगीत, फोटो आदि भेजने के लिए प्रयुक्त किया जाता है। आमतौर पर, आप इस साफ्टवेयर की मदद से फोन और कम्प्यूटर पर आपके दोस्त-परिचितों के नम्बर भी सीधे स्थानांतरित कर सकेंगे। इसके लिए फोन और कम्प्यूटर एक तार के माध्यम से, अथवा ब्लू टूथ या इन्फ्रा रेड पोर्ट के द्वारा जोड़े जाते हैं। नोकिया भी एक ऐसा ही साफ्टवेयर सी डी पर देती है जिसे पी सी सूट कहते हैं। पर एक महत्वपूर्ण सुविधा के साथ- इस साफ्टवेयर के माध्यम से आप अपना एस एम एस संदेश पी सी पर ही टंकित कर सकते है।
साथ लगे चित्र को देखिए। मैने रमण भाई के युनिनागरी टंकन पटल पर अपने संदेश तैयार करके यहाँ चिपका दिया है। फिर यही से अपने दोस्त का नाम दिया, तो साफ्टवेयर ने उनका नम्बर मेरे फोन से स्वयं ही समझ लिया। फिर वहीं से प्रेषण का बटन दबा के संदेश को रवाना कर दिया। यह काफी आसान प्रक्रिया है।कुछ बातों का ध्यान रखना जरूरी है। नोकिया के ज्यादातर अच्छे फोन पी सी सूट समर्थित हैं। (मैं एक पिटा हुआ ६६१० प्रयोग करता हूँ)। पर अच्छा होगा यदि आप पी सी सूट का नया वर्जन नोकिया के अंतरस्थल (यहाँ) से लें। यह तकरीबन २५ एम बी का है। ये भी ध्यान रखें कि कभी-कभी अक्षर बिखर जाते है। यदि पाने वाले का फोन नोकिया नहीं है, तो सब कुछ कचरा हो जाता है। फिर भी, 'कुछ नहीं से बेहतर कुछ भी' वाली बात होती है। दूसरी बात, सदेंश शायद छोटे लगें, पर अंग्रेजी से ज्यादा बाइट्स लेते प्रतीत होते हैं। यह पी सी सूट बिना आज्ञा के ही इसे दो या अधिक टुकड़ो में बाँट देता है। यदि ऐसा हुआ, तो उतने एस एम एस के पैसे भी लगेंगे।कुल मिला कर मेरा अनुभव संतोषप्रद है, पर यह और अच्छा हो सकता है। खासकर जब कि पाने वाले के फोन के निर्माता (ब्राण्ड) की जानकारी का अभाव हो तो ऐसी अवस्था का क्या निवारण है। यदि आप के कुछ दुसरे अनुभव हैं, तो आप भी बाँटे।

DRY RAILS database.yml config file

From http://blog.bleything.net/articles/2006/06/27/dry-out-your-database-yml:



login: &login
adapter: mysql

username: username
password: password
host: mysql.example.com

development:
<<: *login
database: app_dev

test:
<<: *login
database: app_test

production:

<<: *login
database: app_prod

Improved in_place_select_editor

The in_place_select_editor method assumes that you want to display the exact item being edited. That isn't always the case. You might want to display a related attribute. The following modifies in_place_select_editor_field by allowing it to take a display parameter (third parameter), which is then passed to to_content_tag_display (also modified)

module ApplicationHelper

def in_place_select_editor(field_id, options = {})

function = "new Ajax.InPlaceSelectEditor("
function << "'#{field_id}', "

function << "'#{url_for(options[:url])}'"
function << (', ' + options_for_javascript(

{
'selectOptionsHTML' =>
%('#{escape_javascript(options[:select_options].gsub(/\n/, ""))}')

}
)
) if options[:select_options]
function << ')'

javascript_tag(function)
end


def in_place_select_editor_field(object, method, display, tag_options = {},
in_place_editor_options = {})

tag = ::ActionView::Helpers::InstanceTag.new(object, method, self)

tag_options = { :tag => "span",
:id => "#{object}_#{method}_#{tag.object.id}_in_place_editor",
:class => "in_place_editor_field"}.merge!(tag_options)

in_place_editor_options[:url] =
in_place_editor_options[:url] ||
url_for({ :action => "set_#{object}_#{method}", :id => tag.object.id })

tag.to_content_tag_display(tag_options.delete(:tag), display, tag_options) +

in_place_select_editor(tag_options[:id], in_place_editor_options)
end

end

module ActionView
module Helpers

class InstanceTag #:nodoc:
include Helpers::TagHelper

def to_content_tag_display(tag_name, display, options = {})

content_tag(tag_name, display, options)
end

end


end
end



This permits (example), where Job.vendor_name(@job) (a custom method) is displayed.

            <%= in_place_select_editor_field(
:job,
:vendor_id,
Job.vendor_name(@job),
{},
:select_options
=> options_from_collection_for_select(@vendors, 'id', 'name', @job.vendor_id)) %>



you could also use this to display an icon to click on to display the drop-down box by passing an image_tag as the third parameter.

Time based expiration of cache fragments in Ruby on Rails (MemCacheStore and FileStore)

This allows to expire cached fragments by ttl. Example usage (in views):

<% cache("name", :ttl => 7.days) do %>

... some database-heavy stuff ...
<% end %
>


Put the following in environment.rb or in lib/.
class ActionController::Caching::Fragments::MemCacheStore
def write(name, value, options=nil)

if options.is_a?(Hash) && options.has_key?(:ttl)

ttl = options[:ttl]
else
ttl = 0

end
@data.set(name, value, ttl)

end
end

module ActionView::Helpers::CacheHelper
def cache(name = {}, options = nil, &block)

@controller.cache_erb_fragment(block, name, options)
end

end

class ActionController::Caching::Fragments::UnthreadedFileStore
def read(name, options = nil)
if options.is_a?(Hash) && options.has_key?(:ttl)

ttl = options[:ttl]
else
ttl = 0

end

fn = real_file_path(name)

# if cache expired act as if file doesn't exist

return if ttl > 0 && File.exists?(fn) && (File.mtime(fn) < (Time.now - ttl))

File.open(fn, 'rb') { |f| f.read } rescue nil

end
end

RJS with toggle_slide if not visible only

RJS is very powerful. here is a small code snippet that shows how to do a toggle on a div if NOT visible. If the div is visible, it just exchange it. Here is my rhtml code:

    <div style="height: 30px">
<div style="float: left; padding: 0 0 0 180px">

<%= link_to_remote("test_visibility" ,
:with
=> "'is_visible=' + Element.visible('list_div')",
:loading => "Element.show('getting_results')",

:complete => "Element.hide('getting_results')",
:failure => "alert('An error occured, please email us directly!')",

:url => { :action => 'one_action' }) %>

</div
>
<div id="getting_results" style="float: left; padding: 3px 0 0 30px; display: none;">
<%= image_tag "ajax-loader.gif", :class => "image", :alt => "loading..." %>

</div
>
</div>

<div id="list_div" style="display: none;" >
<
/div>



And in my controller, I have the following code:

def one_action

render :update do | page |
page.replace_html 'list_div', :partial => 'one_partial'

if params['is_visible'] == 'false'
page.visual_effect :toggle_slide, 'list_div', :duration => 2

end
end
end


How it works?
Element.visible('list_div')
is a prototype function that returns false if the element is not visible and true if it is. In the above case, we have the orginial visibility of the div to be "display: none". Thus the first time the action is called, the Ajax call will send the parameter false to the action. This will make the div appear. For subsequent requests, the value of visibility will be true, thus only replacing the div.

.htaccess That Fixes The Trailing Slash Error

// This .htaccess file is for use with a Rails application that is accessed by a symbolic link. This fixes an error in which the directory URL must have a trailing slash. Otherwise, the user receives a 400 Bad Request error.

Options +FollowSymLinks +ExecCGI

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} -d
RewriteCond %{SCRIPT_FILENAME} ^.*[^\/]$
RewriteRule ^(.*)$ $1
/ [N]

#Put the directory your Rails app is in here.
RewriteBase /directory

RewriteRule ^$ index.html [QSA]

RewriteRule ^([^.]+)$ $1.html [QSA]


RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.*)$ dispatch.cgi?$1 [QSA,L]

Passing parameters into before filter

application.rb
class ApplicationController < ActionController::Base

def hello(name)

"Hello #{name}"
end

end


users_controllers.rb
class UsersController < ApplicationController


before_filter :only => :index do |u|
u.hello('Master')

end

end