Thursday, 9 February 2012

ruby shortcut and format specifier



NOTE: Some of these seem only to work for DateTime (e.g. %L, %N)


%a - The abbreviated weekday name (“Sun”)
%A - The full weekday name (“Sunday”)
%b - The abbreviated month name (“Jan”)
%B - The full month name (“January”)
%c - The preferred local date and time representation
%C - Century (20 in 2009)
%d - Day of the month (01..31)
%D - Date (%m/%d/%y)
%e - Day of the month, blank-padded ( 1..31)
%F - Equivalent to %Y-%m-%d (the ISO 8601 date format)
%h - Equivalent to %b
%H - Hour of the day, 24-hour clock (00..23)
%I - Hour of the day, 12-hour clock (01..12)
%j - Day of the year (001..366)
%k - hour, 24-hour clock, blank-padded ( 0..23)
%l - hour, 12-hour clock, blank-padded ( 0..12)
%L - Millisecond of the second (000..999)
%m - Month of the year (01..12)
%M - Minute of the hour (00..59)
%n - Newline (n)
%N - Fractional seconds digits, default is 9 digits (nanosecond)
  • %3N millisecond (3 digits)
  • %6N microsecond (6 digits)
  • %9N nanosecond (9 digits)
%p - Meridian indicator (“AM” or “PM”)
%P - Meridian indicator (“am” or “pm”)
%r - time, 12-hour (same as %I:%M:%S %p)
%R - time, 24-hour (%H:%M)
%s - Number of seconds since 1970-01-01 00:00:00 UTC.
%S - Second of the minute (00..60)
%t - Tab character (t)
%T - time, 24-hour (%H:%M:%S)
%u - Day of the week as a decimal, Monday being 1. (1..7)
%U - Week number of the current year, starting with the first Sunday as the first day of the first week (00..53)
%v - VMS date (%e-%b-%Y)
%V - Week number of year according to ISO 8601 (01..53)
%W - Week number of the current year, starting with the first Monday as the first day of the first week (00..53)
%w - Day of the week (Sunday is 0, 0..6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00..99)
%Y - Year with century
%z - Time zone as hour offset from UTC (e.g. +0900)
%Z - Time zone name
%% - Literal “%” character
t = Time.now                        #=> 2007-11-19 08:37:48 -0600
t.strftime("Printed on %m/%d/%Y")   #=> "Printed on 11/19/2007"
t.strftime("at %I:%M%p")            #=> "at 08:37AM"

Wednesday, 8 February 2012

WORKING WITH YIELD AND content_for METHOD


Within the context of a layout, yield identifies a section where content from the view should be inserted.


<html>
<head><title>Home Page</title></head>
<body>
  <table width="100%">
    <tr><td>
      <%= yield %>
    </td><td>
      <%= yield :sidebar %>
    </td></tr>
      </table>
  </body>
 </html>
The first yield will be replaced with HOME view when rendered.  The second yield
 is a different: we are passing it a symbol, saying he wants to yield :sidebar
 at that point in time

Now The content_for method allows you to insert content into a named yield block
 in your layout

<% content_for :sidebar do %>
  <title>sidebar</title>
<% end %>

Blocking default design present in application layout


This is the way from where you are calling your default sidebar in application.html.erb


<div id="define_according_to_your_stylesheet" class="<%= sidebar_disabled? %>">
<!-- START Sidebar -->
                 <%= yield :sidebar %>
                 <%= app_screen_tip -%>
<!-- END Sidebar -->
     </div>

Here <%=sidebar_disabled? %> is calling method present in application helper


define a method in application helper which is loaded automatically when your application loaded


def sidebar_disabled?
    current_page = "#{controller.controller_name}.#{controller.action_name}"
    current_controller = controller.controller_name
    pages = %w(
      home.index
#comment [home(its your controller name).index(its your action)]
   
)    return pages.include?(current_controller) || pages.include?(current_page)
  end



by this way you can block anything which loaded by default when application loaded



Monday, 6 February 2012

Download YouTube Videos from Ubuntu 11.10


YouTube Video Downloader in Ubuntu 11.10

You can use various ways to download a youtube video into your ubuntu 11.1o. Using a yoututbe client, a command lineutility, a web browser plugin are available for downloading videos from youtube.

MiniTube

Minitube is the best youtube client in ubuntu 11.1o. You can watch Youtube Videos right from your desktop. You can easly download youtube videos into ubuntu 11.10 using minitube. It has a very simple and easy GUI. You can configure the video size, related videos etc. You can easily search using keywords or a youtube channel name. The project is hosted in gitorius and is released as GPL.

Install Minitube, The Youtube Downloader in Ubuntu 11.10

Minitube is available in official package repository. To install minitube just type the command in command line.
sudo apt-get install minitube
you can install using Ubuntu Software Centre also. Just search for minitube in the software centre and click install.

Download videos using MiniTube in Ubuntu 11.1o

To download a video just play the video in minitube. The go Video > Download in the menu. The videos will be saved to /home/username/Videos.
 Download Youtube Videos Using Terminal Utility
You can download youtube videos by using the command line. To download videos from command line you can use the command youtube-dl.  Install install a utility  youtube-dl. Open terminal and type
sudo apt-get install youtube-dl
Now, open the youtube video in the browser and copy the URL from the address bar.
Then execute the following command -
youtube-dl "http://youtube.com/?v=VideoID"

IE9 DELETES stuff

THANKS TO:- 
            http://techno-weenie.net/2011/8/19/ie9-deletes-stuff   (FROM WHERE I GOT THIS INFORMATION)



Using more of the HTTP methods lets us keep the URLs cleaner. Web browsers don't understand PUT/PATCH/DELETE in forms, so a workaround was needed. Rails looks at a _method GET parameter on POST requests to determine what HTTP verb it should be recognized as. The GData API supports this behavior through the X-HTTP-Method-Override header.
A typical Rails controller might look like this:

class WidgetsController < ApplicationController
  # DELETE /widgets/1
  def destroy
    @widget.destroy
    redirect_to '/widgets'
  end
end

If you don't like Rails, just close your eyes and think of your favorite web framework...
This action works great for a simple form in a browser. You click "Submit", it POSTs to the server, and then you end up back at the root page. Then, you can add some jQuery to spice things up for newer browsers. Progressive enhancement and all that.

$('.remove-widget').click(function() {
  $.del(this.href, function() {
    // celebrate, disable a spinner, etc
  })
  return false
})

This works great in all modern browsers, except IE9. We discovered that not only does IE9 send a real DELETE request, it also follows the redirect with another DELETE. If that redirect points to another resource, you can get a dangerous cascading effect.
RFC 2616 is not clear about what to do in this case, but strongly suggests that redirects are not automatically followed unless coming from a safe method.
If the 302 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
Standard practice for browsers over the years is that redirects from POST requests are followed with a GET request. GET/HEAD requests are usually safe, so this seems like reasonable behavior. It's expected by web developers, and consistent across browsers.

I can't imagine that this behavior in IE9 was on purpose. It feels like an edge case that slipped through an if statement because "DELETE" != "POST". I've submitted feedback to the IE9 team about this issue. I'm curious to see what they say.

So, if your application might be responding to ajax requests with redirects, you should probably start sending back 200 OK...


Update: Eric Law on the IEInternals blog responded to one of Kyle's tweets. Apparently the behavior is correct according to HTTP 1.0, and IE has been following DELETE redirects since at least IE6.
Here's the breakdown of browser behavior when receiving a 302 redirect from a DELETE request:

IE 6-10 DELETE method is preserved
Chrome 13 Converts to GET
Firefox 6 Converts to GET
Safari 5.1 Converts to GET
Opera 11.5 Converts to GET

We didn't see the behavior in IE8, so we assumed it was new to IE9. At least, no one was sending in crazy bug reports from other browsers. This is another example why developers hate dealing with IE. Kudos to the standards compliance, though!



Installing a gem fork from Github source

THANKS TO:-
                    http://raflabs.com/blogs/silence-is-foo/2010/07/19/installing-a-gem-fork-from-github-source/




Usually, installing a gem is as easy as issuing:

1.gem install gem_name

but, what if you want to install a fork of a gem?
As you may know Github no longer hosts gems, so doing the following won't work:

$ sudo gem install technoweenie-grit --source http://gems.github.com

NOTE: Actually it'd work if the gem you're trying to install is in this list: http://gems.github.com/list.html

Disecting the "Download Source" button

Let's suppose we want to install the Techno Weenie's fork of Grit from Github:
Main repo => http://github.com/mojombo/grit
Techno Weenie's fork => http://github.com/technoweenie/grit
Github has a "Download Source" button, if you click it you'll be presented with two big images (say buttons): "ZIP" and "TAR", clicking these buttons will start downloading the code of the master branch in the selected format; below those buttons you'll find the list of tags of the repository, you'll be able to click one of the tags and again you'll be presented with "ZIP" and "TAR" to download the source of such tag.

How does this works?

This is how the URL is built:

http://github.com/USER/REPOSITORY/FORMAT/BRANCH_OR_TAG

"ZIP" => http://github.com/technoweenie/grit/zipball/master
"TAR" => http://github.com/technoweenie/grit/tarball/master
said this:


1.$ wget http://github.com/technoweenie/grit/zipball/master
2. 
3.$ unzip technoweenie-grit-v2.0.0-12-g0bd0c5f.zip
4. 
5.$ cd technoweenie-grit-0bd0c5f


Installing the gem

Gems source code has a special file called gem_name.gemspec, this is like the DNA of the gem and it's used to build it:

1.$ gem build grit.gemspec

Issuing this command will generate a gem_name-version.gem file which is the gem itself, let's install it:

1.$ sudo gem install grit-2.0.0.gem

Some projects doesn't have a .gemspec file but rake tasks, in that case you just need to do:

1.$ rake build
2.$ rake install

If any of these methods work, you'd have to look at the Rakefile for tasks to generate the .gemspec file.
...and we're done!

NOTE: 'gem build' will not take in consideration special and external dependencies the gem might require, so you must take care of the dependencies manually. Check in the code all the 'require' lines to see dependencies.
Hope it helps.



Gems On Rails Get Version 0.7.2 → ‘gemsonrails’


THANKS TO:-
                             http://gemsonrails.rubyforge.org and Dr Nic Williams from where i got this information 


BASIC WORK

Link or freeze RubyGems into your rails apps, instead of just plugins. This allows you to ‘vendor everything’ – pushing all dependent gems into your rails app thus ensuring your application will be guaranteed to work when deployed. Your application is no longer dependent on the gems that are/aren’t available on your target deployment environment.


Installation and The Basics

First you install the gem:

$ sudo gem install gemsonrails
Then you deploy the gem into your target Rails application:

$ cd rails_app_folder
$ gemsonrails
This installs some rake tasks into your app to help you freeze or link gems:

$ rake -T | grep "gems:" 
rake gems:freeze    # Freeze a RubyGem into this Rails application; init.rb will be loaded on startup.
rake gems:link      # Link a RubyGem into this Rails application; init.rb will be loaded on startup.
rake gems:unfreeze  # Unfreeze/unlink a RubyGem from this Rails application
Finally, for each gem your application uses you can either freeze a  version into the app (only for gems that do not require native  compilation), or link a gem with your app (useful for gems that require  native compilation)

$ rake gems:freeze GEM=map_by_method
Unpacked map_by_method 0.6.0 to 'map_by_method-0.6.0'

$ ls vendor/gems/map_by_method-0.6.0/
CHANGELOG    README    Rakefile     init.rb      lib     test

$ cat vendor/gems/map_by_method-0.6.0/init.rb 
require File.join(File.dirname(__FILE__), 'lib', 'map_by_method')


Restricting which environments a gem is loaded into

You can restrict which RAILS_ENVs the gem will be loaded for, using ONLY; e.g.


$ rake gems:freeze GEM=<gemname> ONLY=development
$ rake gems:link GEM=<gemname> ONLY=production,staging
In the former, only in development mode will the gem be loaded. In the latter, only in production or staging mode will the gem be loaded from the deployment gem server

Upgrading from old GemsOnRails versions

All the freezing and linking functionality is in the plugin installed by the gemsonrails command. To upgrade, simple install the new gem version, and re-run the gemsonrails command. You application will now have the newest rake tasks and functionality.


Forum

http://groups.google.com/group/gemsonrails

How to submit patches

Read the 8 steps for fixing other people’s code and for section 8b: Submit patch to Google Groups, use the Google Group above.

Licence

This code is free to use under the terms of the MIT licence.