tag:blogger.com,1999:blog-42232282163859196032024-03-13T20:18:05.612+05:30Beena ShettyBeena Shettyhttp://www.blogger.com/profile/14938317029101867051noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-4223228216385919603.post-7571081665654419052013-10-17T11:11:00.000+05:302013-10-17T11:11:01.433+05:30Check if jquery plugin is loaded <div dir="ltr" style="text-align: left;" trbidi="on">
We might always want jquery plugin which would be used in specific page instead of getting loaded in all pages. We call that jquery plugin file in that page only and also define the function in particular html page only. But writing this code in particular page is not a good practice instead it should be in written in main javascript file which contains all the jquery functions. <br />
<br />
If we write that function in main javascript file then we end with getting error as that plugin file is been defined in that particluar page only.<br />
<br />
<h3 style="text-align: left;">
<b>How to check if jquery plugin is loaded or not ?</b></h3>
<b> </b><br />
<b> Checking if method exist</b><br />
<pre class="prettyprint "><span class="text_white">
if($.fn.pluginname != 'undefined'){}
</span>
</pre>
<b><br /></b>
<b> Shorthand</b><br />
<pre class="prettyprint "><span class="text_white">
if($.fn.pluginname){}
</span>
</pre>
<br />
Replace pluginname with whatever you are calling.<br />
<br />
<b>Example:</b><br />
<br />
<pre class="prettyprint "><span class="text_white">
if($.fn.accordion){
$("#accordion").accordion({
heightStyle: "content"
});
}
</span>
</pre>
<div>
<br /></div>
<br /></div>
Beena Shettyhttp://www.blogger.com/profile/14938317029101867051noreply@blogger.com1tag:blogger.com,1999:blog-4223228216385919603.post-70552537755527778892013-01-18T13:52:00.000+05:302013-01-18T13:52:09.251+05:30Using find_or_create_by in Rails<div dir="ltr" style="text-align: left;" trbidi="on">
<h2>Find by one attribute</h2>
<pre class="prettyprint "><span class="text_white">
User.find_or_create_by_username('beena')
</span>
</pre>
It will find the record with username 'beena' in table user and if record is not found then it will create a new record with username 'beena'
<br /><br />
<h2> Find by one attribute or create by multiple attributes </h2>
<pre class="prettyprint "><span class="text_white">
User.find_or_create_by_username('beenas',
:first_name => 'beena',
:last_name => 'shetty',
:role_id => 1)
</span>
</pre>
<br/> <br/>
<h2>Find by multiple attributes</h2>
<pre class="prettyprint "><span class="text_white">
User.find_or_create_by_username_and_role_id('beenas', 2)
</span>
</pre>
It will find the record with username 'beena' and role_id '2' in table user and if record is not found then it will create a new record with username 'beena' and role_id 2
<br/> <br/>
<h2> Find by multiple attributes and pass multiple attribute to a new row to be created </h2>
<pre class="prettyprint "><span class="text_white">
User.find_or_create_by_username_and_role_id('beenas', 2) do |u|
u.first_name => 'beena',
u.last_name => 'shetty',
u.role_id => 1
end
</span>
</pre>
It will find the record with username 'beena' and role_id '2' in table user and if record is not found then it will create a new record with username 'beena', role_id 2 and along with other attribute passed.
<br/>
</div>
Beena Shettyhttp://www.blogger.com/profile/14938317029101867051noreply@blogger.com0tag:blogger.com,1999:blog-4223228216385919603.post-21441763665818201072012-11-30T10:54:00.000+05:302012-11-30T10:54:55.308+05:30Use Cpanel and WHM API's In Rails<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<b> WHM </b><br />
WebHost Manager (WHM) is a web-based tool used by server administrators and resellers to manage hosting accounts on a web server.<br />
WHM listens on ports 2086 and 2087 by default.<br />
<br />
<b> cPanel</b><br />
cPanel is a Unix based web hosting control panel that provides a graphical interface and automation tools designed to simplify the process of hosting a web site. cPanel utilizes a 3 tier structure that provides functionality for administrators, resellers, and end-user website owners to control the various aspects of website and server administration through a standard web browser.<br />
cPanel is commonly accessed on port 2082, with an SSL-secured server operating on port 2083.
<br />
<br />
<b> Use cpanel and Whm API's in Rails</b><br />
<b> Panelbeater </b>gem is used for communicating with the cPanel and WHM API's.<br />
Ruby library for the WHM & cPanel API.<br />
<br />
<b> Installation</b><br />
Github path <a href="https://github.com/BeenaShetty/panelbeater">https://github.com/BeenaShetty/panelbeater</a><br />
Specify gem 'panelbeater' in gemfile <br />
<br />
<b>Usage</b><br />
Create a server object and connect to WHM using host and secure hash:<br />
<br />
<pre class="prettyprint">
server = Panelbeater::Whm::Commands.new(
:url => url,
:api_key => secure_hash,
:port => port,
:user => username
)
</pre>
<br />
Now,you can access the modules of WHM by calling server.module<br />
<br />
<br />
<b>Example</b><br />
<i><b>Creating a new account</b></i><br />
Requires only a username and domain<br />
<br />
<pre class="prettyprint">
result = server.createacct(
:username => 'newuser',
:domain => 'newuser.com'
)
</pre>
<br />
<b>Output</b><br /><br />
createaact => {result => { :status => boolean, :statusmsg => "" }}<br />
<br />
status (boolean) — Status of the operation.<br />
1 — success.<br />
0 — failure.<br />
<br />
statusmsg (string) — Status or error message.<br />
<br />
<b> Reference </b>: <a href="http://docs.cpanel.net/twiki/bin/view/SoftwareDevelopmentKit/CreateAccount">CreateAccount</a><br />
<br />
<i> <b>Delete account</b></i><br />
Requires only username<br />
<br />
<pre class="prettyprint">
result = server.removeacct(:user => 'newuser')
</pre>
<br />
<b>Output</b><br /><br />
removeacct => {result => { :status => boolean, :statusmsg => "" }}<br />
<br />
status (boolean) — Status of the operation.<br />
1 — success.<br />
0 — failure.<br />
<br />
statusmsg (string) — Message about the status of the operation.<br />
<br />
<b>Reference</b>: <a href="http://docs.cpanel.net/twiki/bin/view/SoftwareDevelopmentKit/TerminateAccoun">RemoveAccount</a><br />
<br />
<br />
<b> Using Cpanel methods</b><br />
Connect to cpanel using server.cpanel
<br />
<pre class="prettyprint">
domain = Domain.find(5)
server.cpanel(domain.username, {
:cpanel_jsonapi_module => api_module,
:cpanel_jsonapi_func => api_function,
:cpanel_jsonapi_apiversion => api_version
})
</pre><br />
<br />
<i><b>Note:</b></i> If you use a cPanel port (2082 or 2083) to access the XML or JSON API, you can only use it to run API1 and API2 functions. Native XML and JSON API calls will not work. The ports are assigned as follows:<br />
<br />
<b>2082 — cPanel (unsecured).<br />
2083 — cPanel (secured).<br />
2086 — WHM (unsecured).<br />
2087 — WHM (secured).</b><br />
<br />
<br />
<b>Fast Mode</b><br />
<br />
Fast Mode is a newer way to use the cpanel function call in the XML or JSON API. This method is preferred, since it simplifies building the request, no longer requiring you to enter XML tags as input. The server-side processing of Fast Mode requests is also much quicker than the processing of XML requests.<br />
<br />
<b>XML Input</b><br />
<br />
The cpanel XML function call in Fast Mode takes the following variables as input.<br />
<br />
The following variables are required:<br />
<br />
<b> cpanel_xmlapi_user</b> (string) — The user as whom you wish to call the function.<br />
<b>cpanel_xmlapi_module</b> (string) — Name of the module to access.<br />
Important: API1 and API2 variables are case-sensitive. For example, specifying Email is different from specifying email.<br />
<b>cpanel_xmlapi_func</b> (string) — Name of the function to access.<br />
<br />
The following variables are optional, depending on the call:<br />
<br />
<b> cpanel_xmlapi_apiversion</b> (integer) — Version of the API to access.<br />
1 — API1.<br />
2 — API2.<br />
No input — Defaults to API2.<br />
arg (string) — List of arguments. These variables are numbered such that the first is arg-0, the second is arg-1, and so on.<br />
Example: arg-0=username&arg-1=somepass<br />
<br />
<b>JSON Input</b><br />
The cpanel JSON function call in Fast Mode takes the following variables as input.<br />
<br />
The following variables are required:<br />
<br />
<b>cpanel_jsonapi_user</b> (string) — The user as whom you wish to call the function.<br />
<b>cpanel_jsonapi_module</b> (string) — Name of the module to access.<br />
Important: API1 and API2 variables are case-sensitive. For example, specifying Email is different from specifying email.<br />
<b>cpanel_jsonapi_func</b> (string) — Name of the function to access.<br />
<br />
The following variables are optional, depending on the call:<br />
<br />
<b>cpanel_jsonapi_apiversion</b> (integer) — Version of the API to access.<br />
1 — API1.<br />
2 — API2.<br />
No input — Defaults to API2.<br />
API1: arg (string) — List of arguments. These variables are numbered such that the first is arg-0, the second is arg-1, and so on.<br />
Example: arg-0=username&arg-1=somepass<br />
API2: arg (string) — List of arguments. These variables are given by name.<br />
Example: email=bob&domain=fred.com<br />
<br />
<b>Output</b><br />
<br />
cpanelresult — Container for the result of the cPanel operation.<br />
apiversion (string) — Version of API accessed (1 or 2).<br />
module (string) — Name of the module accessed.<br />
func (string) — Name of the function accessed.<br />
type (string) — Type of function.<br />
source (string) — Source of the call (internal or external).<br />
data — Data provided by the function.<br />
result (string) — Result of the function<br />
<br />
<b>Reference: </b><a href="http://docs.cpanel.net/twiki/bin/view/SoftwareDevelopmentKit/CallingAPIFunctions">Cpanel</a><br />
<br />
<b>Example</b><br />
1. <b>Email::listpopswithdisk</b><br />
<b> </b> List email accounts associated with a particular domain.<br /><br />
<pre class="prettyprint">
server.cpanel(domain.username, {
:cpanel_jsonapi_module => "Email",
:cpanel_jsonapi_func => "listpopswithdisk",
:cpanel_jsonapi_apiversion => 2
})
</pre><br />
<br />
2.<b> Email::addpop</b><br />
Add(create) an e-mail account.<br />
<br />
<pre class="prettyprint">
server.cpanel(domain.username, {
:cpanel_jsonapi_module => "Email",
:cpanel_jsonapi_func => "addpop",
:cpanel_jsonapi_apiversion => ,
:domain => "user.com",
:email => "test",
:password => "12345",
:quota => 10
})
</pre><br />
<br />
<b>Output</b></div>
<br />
<data><br />
<reason> A string value that contains the email address if the function completes successfully. This value will contain a reason for failure if the function encounters an error.<br />
</reason><br />
<result> A boolean value that indicates success or failure. '1' indicates that the function completed successfully.<br />
</result><br />
</data><br />
<br />
<br />
<b>Reference: </b><a href="http://docs.cpanel.net/twiki/bin/view/ApiDocs/Api1/WebHome">API1 Version</a> , <a href="http://docs.cpanel.net/twiki/bin/view/ApiDocs/Api2/WebHome">API2 Version</a><br />
<br />
<br />
<br />Beena Shettyhttp://www.blogger.com/profile/14938317029101867051noreply@blogger.com5tag:blogger.com,1999:blog-4223228216385919603.post-21885303031063762482012-10-18T12:17:00.000+05:302012-10-18T12:18:00.072+05:30Calender in Javascript<div dir="ltr" style="text-align: left;" trbidi="on">
<script src="https://github.com/BeenaShetty/jquery.ui.datetimepicker/blob/master/jquery.ui.datetimepicker.js" type="text/javascript">
</script>
<br />
<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
Recently i was searching for a calender to implement in my Rails app. I came across a jquery plugin named <b>jquery.ui.datetimepicker </b>. I forked that repository from github and implemented it. Added time functionality in it and also made changes according to my requirement.<br />
But their was no document related to it. So i thought to write blog about it and also i have created a demo app.<br />
<br />
<b> Download</b><br />
Download all the js file, css file and images related to it from <a href="https://github.com/BeenaShetty/jquery.ui.datetimepicker">"https://github.com/BeenaShetty/jquery.ui.datetimepicker" </a> this link.<br />
<br />
<b> Requirement</b><br />
Include this files in your application.<br />
<br />
<b>Usage</b><br />
<pre class="whitecolor"> jQuery(document).ready(function(){
jQuery(input-target-selector).datetimepicker();
});
</pre>
where the input-target-selector naturally is class or id of a selector (eg. .datepickers or #picker). The datetimepicker will be effective for all selected input element .<br />
<br />
<h2> <a href="http://beenashetty-datetimepicker.herokuapp.com/">DEMO APP</a></h2>
</div>
</div>
Beena Shettyhttp://www.blogger.com/profile/14938317029101867051noreply@blogger.com0tag:blogger.com,1999:blog-4223228216385919603.post-27891355966561951332012-10-03T19:04:00.000+05:302012-10-03T19:04:28.829+05:30Rails Console Tips<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<br />
<h2>
1.To call Applicationcontroller methods from the Console</h2>
<br />
<pre class="prettyprint "><span class="text_white">
ApplicationController.new.methods
=> :hello, :current_user, :create_support_user
</span>
</pre>
It will display all the methods of application controller
<br />
<br />
<pre class="prettyprint "><span class="text_white">
ApplicationController.new.hello
=> "Hello World"
</span>
</pre>
Here :hello is the method defined inside application_controller.
<br />
<br />
<b><i> Note:</i></b> Instead of using ApplicationController.new you can also use <b>controller.methods </b><br />
<b> Check <a href="http://api.rubyonrails.org/classes/Rails/ConsoleMethods.html"><i>this link</i></a> for more details related to controller </b><br />
<br />
<h2>
2. To use Helper methods from the Console</h2>
<br />
<pre class="prettyprint"> <span class="text_white">
ApplicationController.helpers.methods
=> :admin?, :client?
OR
helper.methods
=> :admin?, :client?
</span>
</pre>
It will display all the helper methods<br />
<pre class="prettyprint"><span class="text_white">
helper.number_to_currency(100)
=> "$100.00"
helper.truncate("Testing User", length: 2)
=> "Testing Use..."
helper.pluralize(2, "Person")
=> "2 People"
</span>
</pre>
<br />
<h2>
3. Who defined helper?</h2>
<pre class="prettyprint"><span class="text_white">
helper.method(:truncate)
=> #<Method: ActionView::Base(ActionView::Helpers::TextHelper)#truncate>
helper.method(:link_to)
=> #<Method: ActionView::Base(ActionView::Helpers::UrlHelper)#link_to>
helper.method(:number_to_currency)
=> #<Method: ActionView::Base(ActionView::Helpers::NumberHelper)#number_to_currency
</span>
</pre>
<br />
<h2>
4. app</h2>
<pre class="prettyprint"><span class="text_white">
app.class
=> ActionDispatch::Integration::Session
</span>
</pre>
<b> i. Routes</b><br />
<pre class="prettyprint"><span class="text_white">
app.overview_admin_index_path()
=> "/admin/overview"
app.edit_user_registration_path(User.first)
=>/users/edit.12"
</span>
</pre>
<b> ii. Url </b>
<br />
<pre class="prettyprint"><span class="text_white">
app.url_for(:controller => :admin)
=> "http://www.example.com/admin"
app.url_for(:controller => :admin, :action => :overview)
=> "http://www.example.com/admin/overview"
</span>
</pre>
<b> iii. Submit request </b><br />
To make request from console
<br />
<pre class="prettyprint"><span class="text_white">
app.get "/users/sign_in"
=> 200
app.session
=> {"session_id"=>"786731e9880c620cf638b57a4db88a1e", "user_return_to"=>"/admin/overview", "flash"=>#<actiondispatch::flash::flashhash:0xbbfff80 alert="alert" used="#<Set:">, @closed=false, @flashes={:alert=>"You need to sign in or sign up before continuing."}, @now=nil>, "_csrf_token"=>"kvMbabanAsi07gEfsuzHdR7xnCEz0hMIvRBU2LiaqFs="}
app.get "/users/sign_in",{:username => 'new_user', :password => '123456789'}
=> 200
app.cookies
=> </actiondispatch::flash::flashhash:0xbbfff80>#<Rack::Test::CookieJar:0xbf005a4 @default_host="www.example.com", @cookies=[#<Rack::Test::Cookie:0xba68e24 @default_host="www.example.com", @name_value_raw="_snipper_host_india_session=BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTc4NjczMWU5ODgwYzYyMGNmNjM4YjU3YTRkYjg4YTFlBjsAVEkiE3VzZXJfcmV0dXJuX3RvBjsARkkiFC9hZG1pbi9vdmVydmlldwY7AEZJIhBfY3NyZl90b2tlbgY7AEZJIjFrdk1iYWJhbkFzaTA3Z0Vmc3V6SGRSN3huQ0V6MGhNSXZSQlUyTGlhcUZzPQY7AEY%3D--152f06b94ca610d662cfd7148b7a4a49ceac23eb", @name="_snipper_host_india_session", @value="BAh7CEkiD3Nlc3Npb25faWQGOgZFRkkiJTc4NjczMWU5ODgwYzYyMGNmNjM4YjU3YTRkYjg4YTFlBjsAVEkiE3VzZXJfcmV0dXJuX3RvBjsARkkiFC9hZG1pbi9vdmVydmlldwY7AEZJIhBfY3NyZl90b2tlbgY7AEZJIjFrdk1iYWJhbkFzaTA3Z0Vmc3V6SGRSN3huQ0V6MGhNSXZSQlUyTGlhcUZzPQY7AEY=--152f06b94ca610d662cfd7148b7a4a49ceac23eb", @options={"path"=>"/", "HttpOnly"=>nil, "domain"=>"www.example.com"}>]>
</span>
</pre>
<br />
<b> iv. Response </b>
<br />
<pre class="prettyprint"><span class="text_white">
app.response
It will display full html code
app.response.response_code
=> 200
app.response.body[0..100]
=> "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml"
app.path
=> "/users/sign_in"
</span>
</pre>
<br />
<h2>
4. Method location</h2>
To get exact source loaction of particular method using Method#source_location
<br />
<pre class="prettyprint"><span class="text_white">
User.instance_method(:role?).source_location
=>["/home/Desktop/Workspace/host_india/app/models/user.rb", 32]
</span>
</pre>
Let's try for app
<br />
<pre class="prettyprint"><span class="text_white">
app.method(:post).source_location
=> ["/home/gvo/.rvm/gems/ruby-1.9.2-p318/gems/actionpack-3.2.6/lib/action_dispatch/testing/integration.rb", 38]
</span>
</pre>
<br />
<h2>
5. Accessing the last return value </h2>
<pre class="prettyprint"><span class="text_white">
"Hello"
=> "Hello"
str = _
=> "Hello"
str
=> "Hello"
Commission.first
=> #<commission 166="166" 4947.3="4947.3" 6="6" 989.4="989.4" amount:="amount:" commission:="commission:" id:="id:" package:="package:" plan="plan" referral_id:="referral_id:">
_.amount
=> 4947.3
</commission></span>
</pre>
</div>
Beena Shettyhttp://www.blogger.com/profile/14938317029101867051noreply@blogger.com1tag:blogger.com,1999:blog-4223228216385919603.post-50910633175781393342012-08-09T18:33:00.001+05:302012-08-10T12:30:50.384+05:30Difference between find() and filter() in jquery<div dir="ltr" style="text-align: left;" trbidi="on">
<h2 style="text-align: left;">
<b>find() </b> </h2>
searches through all the child elements in the matched set.
<i> </i><br />
<br />
<i> <b> Description</b></i><i>:</i><br />
Get the descendants of each element in the current set of matched elements, filtered by a selector.<br />
<br />
<b><i> Syntax </i> </b><br />
.find( selector )<br />
selector: A string containing a selector expression to match elements against.<br />
<br />
.find( jQuery object )<br />
jQuery object: A jQuery object to match elements against.<br />
<br />
.find( element )<br />
element: An element to match elements against.<br />
<br />
<h2 style="text-align: left;">
<b>filter()</b></h2>
<b> </b>
searches through all elements in the matched set.
<i> </i><br />
<br />
<i><b> Description</b>:</i><br />
Reduce the set of matched elements to those that match the selector or pass the function's test.
<i> </i><br />
<br />
<i> <b>Syntax </b></i><br />
<i> </i>
.filter( selector )<br />
selector: A string containing a selector expression to match the current set of elements against.<br />
<br />
.filter( function(index) )<br />
function(index): A function used as a test for each element in the set. this is the current DOM element.<br />
<br />
.filter( element )<br />
element: An element to match the current set of elements against.<br />
<br />
.filter( jQuery object )<br />
jQuery object: An existing jQuery object to match the current set of elements against.
<br />
<br />
<br />
<b> Example </b><br />
<br />
<pre class="prettyprint">
<html>
<head>
<script src="jquery_1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
function filter_month(obj){
jQuery('div').css('background','white');
jQuery('div').filter(obj).css('background', '#83AFF8');
}
function find_month(obj){
jQuery('div').css('background','white');
jQuery('div').find(obj).css('background', '#83AFF8');
}
</script>
</head>
<body>
<div id="months">
<table>
<tr>
<td> January </td>
<td> February </td>
<td> March </td>
<td> April </td>
</tr>
<tr>
<td> May </td>
<td> June </td>
<td> July </td>
<td> August </td>
</tr>
<tr>
<td> September </td>
<td> October </td>
<td> November </td>
<td> December </td>
</tr>
</table>
</div>
<div>
<b> Category</b>
<div id="months"> Month </div>
<div id="weeks"> Weeks </div>
</div>
<input onclick = "filter_month('#months');" type = "button" value ="Filter" />
<input onclick = "find_month('#months');" type = "button" value ="Find" />
</body>
</html>
</pre>
<br />
<br />
<style = "text/css">
.white_color_div{
background-color:white;}
</style>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function clear(){
jQuery('div#months').css('background','white');
}
function filter_month(obj){
clear();
jQuery('div').filter(obj).css('background', '#83AFF8');
}
function find_week(obj){
clear();
jQuery('div.div_month').find(obj).css('background', '#83AFF8');
}
</script>
<div class= "div_month white_color_div" id="months" ><br/>
<table width="100%" border=1 cellpadding= "10px" cellspacing = "5px">
<tr>
<td> January </td>
<td> February </td>
<td> March </td>
<td> April </td>
</tr>
<tr>
<td> May </td>
<td> June </td>
<td> July </td>
<td> August </td>
</tr>
<tr>
<td> September </td>
<td> October </td>
<td> November </td>
<td> December </td>
</tr>
</table><br/><br/>
</div>
<div class= "div_month white_color_div" style="padding:5px;"> <b> Category</b>
<div id="months" style= "padding:5px;"> Month </div>
<div id="weeks" style= "padding:5px;"> Weeks </div>
</div>
<div class="white_color_div">
<input onclick = "filter_month('#months');" type = "button" value ="Filter" />
<input onclick = "find_week('#months');" type = "button" value ="Find" />
<input onclick = "jQuery('div#months').css('background','white');" type = "button" value ="Clear" />
<br/><br/>
</div>
</div>Beena Shettyhttp://www.blogger.com/profile/14938317029101867051noreply@blogger.com4tag:blogger.com,1999:blog-4223228216385919603.post-49125055237168705972012-05-18T11:18:00.001+05:302012-05-18T11:18:56.558+05:30Get models and controllers list inside rails application<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<br /></div>
<H2>Get collection of models inside your application.<br /></h2>
<br />
<b> 1. Get table names inside database <br /></b>
<pre class="prettyprint "><span class="text_white">
ActiveRecord::Base.connection.tables.map do |model|
model.capitalize.singularize.camelize
end
=>["Blog", "User", "UserDetail"]
</span></pre>
<br />
<b>2. Load models dir<br /></b>
<pre class="prettyprint "><span class="text_white">
@models = Dir['app/models/*.rb'].map {|f| File.basename(f, '.*').camelize.constantize.name }
</span></pre> <br />
<b>3. ActiveRecord subclasses</b><br/>
It will only get base class models not inherited models.<br/>
<pre class="prettyprint "><span class="text_white">
# make sure that relevant models are loaded otherwise
# require them prior
# Dir.glob('#{RAILS_ROOT}/app/models/*.rb').each { |file| require file }
class A < ActiveRecord::Base
end
class B < A
end
class C < B
end
ActiveRecord::Base.subclasses.each{|x| x.name }
=>["A"]
</span></pre>
<br/>
<b>4. Inherited model </b><br/>
It will get both base class models and inherited models using descendants.
<br/>
<pre class="prettyprint "><span class="text_white">
# make sure that relevant models are loaded otherwise
# require them prior
# Dir.glob('#{RAILS_ROOT}/app/models/*.rb').each { |file| require file }
class A < ActiveRecord::Base
end
class B < A
end
class C < B
end
ActiveRecord::Base.descendants.each{|x| x.name }
=>["A", "B", "C"]
</span></pre> <br />
<h2>Get collection of controllers inside your application.<br/> </h2>
<br/>
<b>1. ApplicationController subclasses</b><br/>
It will get all controller classes.<br/>
<pre class="prettyprint "><span class="text_white">
ApplicationController.subclasses
=>["AccountsController", "BlogsController", "UsersController"]
</span></pre>
<br/>
<b>2. Inherited Controller</b><br/>
It is get all controller classes along with inherited controller classes.
<br/>
<pre class="prettyprint "><span class="text_white">
class AccountsController < ApplicationController
end
class ArticlesController < ::AccountsController
 end
ApplicationController.descendants
=>["AccountsController", "BlogsController", "UsersController", "AccountsController", "ArticlesController"]
</span></pre>
<br/>
<b>Note:</b><br/>
require all files in "#{RAILS_ROOT}/app/controllers" to populate your list in development mode.
<br/>
It'll get you started, but keep in mind that in development mode you won't see much, because it will only show you what's actually been loaded. Fire it up in production mode, and you should see a list of all of your controllers.
<br/><br/><br/><br/>
<h2>To get all the actions in a controller, use action_methods </h2><br/>
<pre class="prettyprint "><span class="text_white">
PostsController.action_methods
</span></pre>
</div>Beena Shettyhttp://www.blogger.com/profile/14938317029101867051noreply@blogger.com11tag:blogger.com,1999:blog-4223228216385919603.post-88586173773186899902012-04-30T16:09:00.000+05:302012-04-30T16:12:44.249+05:30Wrapping pre tag text using css<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<b>What Is Pre-Formatted Text?</b><br />
<br />
Pre-formatted text is added to your web pages using the PRE tag.<br />
<br />
It is used to be very common to see web pages with blocks of pre-formatted text. Using the PRE tag to define sections of the page as formatted by the typing itself was a quick and easy way to get the text to display as you expected it to. This is because pre-formatted text is defined as text in which the structure is defined by typographic conventions rather than by the HTML.<br />
<br />
Try the PRE tag on your website with various different spacings and carriage returns. For example, try pasting the following into your web page HTML (leave the spaces exactly as they are written):<br />
<br />
<br />
<pre class="prettyprint ">
<span class= "text_white"><pre>
Read each statement
carefully
and
understand
the meanings....
Then share them with someone special.
</pre></span>
</pre>
<br />
Basically HTML collapses the white space in the document. This means that carriage returns, spaces, and tab characters are all collapsed to one space. If you typed the above quote into a typical HTML tag like the P tag, you would end up with one line of text:<br />
<br />
<pre class="prettyprint ">
<span class= "text_white">
Read each statement carefully and understand the meanings....Then share them with someone special.
</span>
</pre>
<br />
The PRE tag leaves the whitespace characters alone. So line breaks, spaces, and tabs are all maintained in the browser. Putting the quote inside a PRE tag results in:<br />
<br />
<pre class="prettyprint ">
<span class= "text_white">
Read each statement
carefully
and
understand
the meanings....
Then share them with someone special.
</span>
</pre>
<br />
But the PRE tag does more than just maintain the spaces. In most browseres, it is written in a monospace font. This makes the characters in the text all equal in width. In other words, the letter i takes up as much space as the letter w. But you can change this with style sheets.<br />
<br />
<h3>
<b><u>Wrapping pre tag text in a div using css</u></b></h3>
<br />
When using pre tags in html to display code blocks it will ignore the div boundaries and not wrap the text by default. By adding some css for pre tags this can be accomplished as follows.<br />
<br />
Hint: Another option is to use overflow:auto which will add scroll bars for the content.<br />
<br />
<pre class="prettyprint ">
<span class= "text_white">
pre, code{
<span class="Apple-tab-span" style="white-space: pre;"> </span>/*Wrap pre tag text for all popular browsers*/
<span class="Apple-tab-span" style="white-space: pre;"> </span>white-space: pre-wrap; /* css-3 */
<span class="Apple-tab-span" style="white-space: pre;"> </span>white-space: -moz-pre-wrap !important; /* Mozilla, since 1999 */
<span class="Apple-tab-span" style="white-space: pre;"> </span>white-space: -pre-wrap; /* Opera 4-6 */
<span class="Apple-tab-span" style="white-space: pre;"> </span>white-space: -o-pre-wrap; /* Opera 7 */
<span class="Apple-tab-span" style="white-space: pre;"> </span>word-wrap: break-word; /* Internet Explorer 5.5+ */
<span class="Apple-tab-span" style="white-space: pre;"> </span>/*Or use overflow:auto ----------- */
<span class="Apple-tab-span" style="white-space: pre;"> </span>/*overflow:auto;*/
}
</span>
</pre>
This will allow your long code to wrap properly and look like this.<br />
<br />
<pre class="prettyprint ">
<span class= "text_white">
Eg:-
ABCDEFGHIJKLMNOPQRSTUVWXYZ;ABCDEFGHIJKLMNOPQRSTUVWXYZ;ABCDEFGHIJKLMNOPQRSTUVWXYZ;ABCDEFGHIJKLMNOPQRSTUVWXYZ;ABCDEFGHIJKLMNOPQRSTUVWXYZ;
</span>
</pre>
</div>Beena Shettyhttp://www.blogger.com/profile/14938317029101867051noreply@blogger.com2tag:blogger.com,1999:blog-4223228216385919603.post-15166812137961489662012-04-13T12:29:00.000+05:302012-07-10T16:50:34.036+05:30Creating plugin as mountable engine in rails 3.1 and higher version<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div class="pContent">
<h3>
What is a Rails Engine?</h3>
Engine are miniature Rails applications that you embed into your main application.
You can share an Engine across different applications.
Since Rails 3.0, every Rails application is nothing more than an Engine, allowing you to share it very easily.<br />
<br />
In Rails 3.1 and higher version provides the ability to create a plugin as engine using a single command.<br />
<br />
<b>1. Creating engine</b><br />
<pre class="prettyprint "><code class="ruby"><span class="no text_white"> rails plugin new billing --mountable</span></code></pre>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzU8nPCCTzt7jfBJ3JgVPTMnpT8_yYgpGHcqso3YdZa_2tgxFSPVPNK_D6y0kTxjgQ1PDTQcxXdoSkgh_k1eguxIgBCb719IwXigk16lZiPIn94LwJabuD9zTmSjLp85s7j9snbwaMAHo/s1600/step1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="260" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjzU8nPCCTzt7jfBJ3JgVPTMnpT8_yYgpGHcqso3YdZa_2tgxFSPVPNK_D6y0kTxjgQ1PDTQcxXdoSkgh_k1eguxIgBCb719IwXigk16lZiPIn94LwJabuD9zTmSjLp85s7j9snbwaMAHo/s400/step1.png" width="400" /></a></div>
<span style="background-color: black;"><br /></span><br />
It creates a billing folder in the main app.<br />
I will move this folder into lib folder or you can keep it there only.<br />
<br />
2. <b>Gem file</b><br />
This billing module will function as gem in application. In order to function as gem we need to define this gem in gem file as<br />
<pre class="prettyprint "><code class="ruby"><span class="no text_white"> gem 'billing', :path => 'lib/billing'</span></code></pre>
Write this code in gem file in path option specify the path while this gem is stored in your application.<br />
After writing this run bundle install command<br />
<br />
3. <b>Mounting engine</b><br />
Next important thing is billing/config/routes.rb file:<br />
<div class="highlight">
<pre class="prettyprint "><code class="ruby"><span class="no text_white"> Billing</span><span class="o text_white">::</span><span class="no text_white">Engine</span><span class="o text_white">.</span><span class="n text_white">routes</span><span class="o text_white">.</span><span class="n text_white">draw</span> <span class="k text_white">do</span>
<span class="k text_white"> end</span>
</code></pre>
</div>
These are empty routes, but as you can see, they belong to the engine, not to the host application.<br />
<br />
Since engine is now a rack app, you can simply mount it in your application’s routes:<br />
<div class="highlight">
<pre class="prettyprint "><code class="ruby"><span class="no text_white"> Rails</span><span class="o text_white">.</span><span class="n text_white">application</span><span class="o text_white">.</span><span class="n text_white">routes</span><span class="o text_white">.</span><span class="n text_white">draw</span> <span class="k text_white">do</span>
<span class="n text_white"> mount</span> <span class="no text_white">Billing</span><span class="o text_white">::</span><span class="no text_white">Engine</span> <span class="o text_white">=></span> <span class="s2 text_white">"/billing"</span>
<span class="k text_white"> end</span>
</code></pre>
</div>
This will mount <code>Billing::Engine</code> at <code>/billing</code> path.<br />
<br />
4. <b>Isolated Engine</b><br />
Normally when you create controllers, helpers and models inside an engine,
they are treated as if they were created inside the application itself.
This means that all helpers and named routes from the application will be
available to your engine’s controllers as well.<br />
However, sometimes you want to isolate your engine from the application,
especially if your engine has its own router. To do that, you simply need
to call <code>isolate_namespace</code>. This method requires you to pass a
module where all your controllers, helpers and models should be nested to:<br />
<br />
<pre class="ruby prettyprint" data-result="[object Object]"><span class="ruby-keyword text_white"> module</span><span class="class text_white"> <span class="ruby-constant"><span class="title">Billing</span></span></span><span class="ruby-constant"></span>
<span class="ruby-keyword text_white"> class</span><span class="class"> <span class="ruby-constant"><span class="title text_white">Engine</span></span> <span class="ruby-operator"><span class="inheritance text_white"><</span></span><span class="inheritance"> <span class="ruby-constant"><span class="parent text_white">Rails</span></span><span class="parent"><span class="ruby-operator text_white">::</span><span class="ruby-constant text_white">Engine</span></span><span class="ruby-constant"></span></span><span class="ruby-constant"></span></span><span class="ruby-constant"></span>
<span class="ruby-identifier text_white"> isolate_namespace</span> <span class="ruby-constant text_white">Billing</span>
<span class="ruby-keyword text_white"> end</span>
<span class="ruby-keyword text_white"> end</span>
</pre>
With such an engine, everything that is inside the <code>MyEngine</code>
module will be isolated from the application.<br />
<br />
Write this code inside billing/lib/billing/engine.rb file<br />
Using isolated engines the behavior of routes changes.
Normally, when you namespace your controllers, you also need to do
namespace all your routes. With an isolated engine, the namespace is
applied by default, so you can ignore it in routes:<br />
<br />
<pre class="ruby prettyprint" data-result="[object Object]" data-second_best="[object Object]"><span class="constant text_white"> Billing::Engine.</span><span class="identifier text_white">routes.</span><span class="identifier text_white">draw</span> <span class="identifier text_white">do</span>
<span class="identifier text_white"> resources</span> <span class="symbol text_white">:<span class="identifier text_white">invoices</span></span>
<span class="identifier text_white"> end</span></pre>
The routes above will automatically point Biliing::InvoicesController. Furthermore, you don’t need to
use longer url helpers like <code>billing_invoices_path</code>. Instead,
you should simply use <code>invoices_path</code> as you would do with your
application.<br />
<br />
5. <b>Migrations</b><br />
Engines can have their own migrations. The default path for migrations is
exactly the same as in application: <code>db/migrate.</code><br />
<code><br /></code>
To create a migration for engine you have go to that path where engine is located <br />
eg:- cd /lib/billing<br />
Now generate the migration for invoices.<br />
Notice that migration for that is called <code>create_billing_invoices</code> instead of <code>create_invoices</code>. Also almost all of the files are places in <code>billing/</code> subdirectory. When you open <code>app/models/billing/invoice.rb</code>, you will see:<br />
<div class="highlight">
<pre class="prettyprint"><code class="ruby"><span class="k text_white"> module</span> <span class="nn text_white">Billing</span>
<span class="k text_white"> class Invoice </span><span class="o text_white"><</span> <span class="no text_white">ActiveRecord</span><span class="o text_white">::</span><span class="no text_white">Base</span>
<span class="k text_white"> end</span>
<span class="k text_white"> end</span>
</code></pre>
</div>
Everything is namespaced for a good reason.We want to avoid conflicts between engine and host application.<br />
When you run rake db:migrate this migration will not be created. In order to create this migration you have to copy this migration into main app using command<br />
<pre class="css prettyprint" data-result="[object Object]"><span class="tag text_white"> rake</span> <span class="tag text_white">billing</span><span class="pseudo text_white">:install</span><span class="pseudo text_white">:migrations</span></pre>
<pre class="css prettyprint" data-result="[object Object]"><span class="pseudo text_white"> rake db:migrate</span></pre>
<pre class="css" data-result="[object Object]"></pre>
6. <b> Cross application routes</b> <br />
In order to use engines routes in main app
<br />
<pre class="css" data-result="[object Object]"></pre>
<pre class="css prettyprint" data-result="[object Object]"> <span class="text_white">billing.invoices_path</span></pre>
<pre class="css" data-result="[object Object]"></pre>
In order to use application routes in engine
<br />
<pre class="css prettyprint" data-result="[object Object]"><span class="text_white"> main_app.logout_path</span></pre>
<br />
<br />
<span style="background-color: black;"><br /></span><br />
<div class="separator" style="clear: both; text-align: center;">
</div>
<div><b> I have created a simple app related to engine. You can check it here <a href="https://github.com/BeenaShetty/Engine">https://github.com/BeenaShetty/Engine</a></b></div>
</div>
</div>Beena Shettyhttp://www.blogger.com/profile/14938317029101867051noreply@blogger.com4