tag:blogger.com,1999:blog-88126168654302164852024-03-14T11:14:50.252+07:00Blog TitleLorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.Maysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.comBlogger79125tag:blogger.com,1999:blog-8812616865430216485.post-21660785464764331472017-03-17T15:13:00.000+07:002017-03-17T15:18:50.799+07:00chat widget test<script>
!function(e){var t,l,n,d;t=e.createElement("iframe"),t.id="hinterlands-chat",t.style.border="none",t.style.position="fixed",t.style.bottom="0",t.style.right="0",t.style.zIndex="99999999999",t.style.width="90px",t.style.height="80px",t.allowfullscreen=!0,e.body.appendChild(t);l=t.contentWindow.document,l.open(),l.write(o),d=l.getElementsByTagName("head")[0],n=l.createElement("script"),n.src="https://gohinterlands.com/widget",d.appendChild(n),l.close()}(document);</script>Maysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-11177707621957655802015-02-24T03:30:00.001+07:002015-02-24T04:26:50.072+07:00random notes/etc/rc.local<br />
<br />
<pre>
mount -t cifs //192.168.56.1/projects /home/[user]/projects -o `cat /home/[user]/.projects_smb_opts`
iptables -t nat -A PREROUTING -p tcp --dport 80 -j REDIRECT --to-port 3000
</pre>
<br />
<br />
gnome-terminal shortcut<br />
<br />
<pre>
[Desktop Entry]
Encoding=UTF-8
Type=Application
Name=projects
Name[en_US]=projects
Exec=gnome-terminal --tab-with-profile=Default --working-directory=/home/[user]/projects
Comment[en_US]=
</pre>Maysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com1tag:blogger.com,1999:blog-8812616865430216485.post-48161502871600390132015-02-24T03:24:00.001+07:002015-02-24T03:24:59.855+07:00Sharing files from windows to lubuntu using samba<pre class="brush: bash">nano .projects_smb_opts
</pre>
<pre class="brush: plain">user=[username],password=[password],dir_mode=0777,file_mode=0777
</pre>
<pre class="brush: bash">mkdir /home/[lubuntu username]/projects
</pre>
<pre class="brush: bash">sudo nano /etc/rc.local
</pre>
<pre class="brush: plain">mount -t cifs //192.168.56.2/projects /home/[lubuntu username]/projects -o `cat /home/[lubuntu username]/.projects_smb_opts`
</pre>
Maysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-52029726849316143552015-02-24T03:24:00.000+07:002015-02-24T03:24:25.826+07:00static ip on lubuntu/etc/network/interfaces
<br />
<pre class="brush: plain">auto eth0
iface eth0 inet static
address 192.168.56.101
gateway 192.168.56.100
netmask 255.255.255.0
network 192.168.56.0
broadcast 192.168.56.255
</pre>
Maysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com1tag:blogger.com,1999:blog-8812616865430216485.post-70612398268070262352014-07-15T15:37:00.000+07:002014-07-15T15:37:07.233+07:00git push huge filesadd config:
<pre class="brush:plain">
[http]
postBuffer = 209715200
</pre>Maysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-76293133795054888172014-07-02T14:18:00.001+07:002014-07-02T14:22:55.534+07:00Creating swapfilesource: <a href="https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-12-04" target="_blank">https://www.digitalocean.com/community/tutorials/how-to-add-swap-on-ubuntu-12-04</a><br />
<ol>
<li>
listing enabled swaps
<br />
<pre class="brush:bash">sudo swapon -s
</pre>
</li>
<li>
check available space
<br />
<pre class="brush:bash"> df
</pre>
</li>
<li>
create swap file (the following will create 1GB swap, modify count if needed)
<br />
<pre class="brush:bash">sudo dd if=/dev/zero of=/swapfile bs=1024 count=1024k
</pre>
</li>
<li>
prepare the swap file by creating a linux swap area
<br />
<pre class="brush:bash">sudo mkswap /swapfile
</pre>
</li>
<li>
activate the swap file
<br />
<pre class="brush:bash">sudo swapon /swapfile
</pre>
</li>
<li>
ensure that the swap is permanent by adding it to the fstab file
<br />
<pre class="brush:bash">sudo nano /etc/fstab
</pre>
paste:
<br />
<pre class="brush:plain">/swapfile none swap sw 0 0
</pre>
</li>
<li>
set swappiness in the file to 10 so swap act as an emergency buffer
<br />
<pre class="brush:bash">echo 10 | sudo tee /proc/sys/vm/swappiness
echo vm.swappiness = 10 | sudo tee -a /etc/sysctl.conf
</pre>
</li>
<li>
prevent the file from being world-readable
<br />
<pre class="brush:bash">sudo chown root:root /swapfile
sudo chmod 0600 /swapfile
</pre>
</li>
</ol>
Maysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-88070970136814846732014-05-23T19:41:00.001+07:002014-05-23T19:41:36.659+07:00Fixing wireless connection problem in lubuntuboot into recovery root command line, type
mount -o remount,rw /
to allow write access, install wicd
apt-get install wicd
and rebootMaysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-9425558093343290092014-04-11T16:40:00.000+07:002014-04-11T16:43:45.392+07:00Rails (Rack::Utils) HTTP Status Codes and Symbols<pre class="brush:bash">
ruby -e 'require "rack/utils"; puts Rack::Utils::SYMBOL_TO_STATUS_CODE.map{|s,c| "|" + c.to_s.center(5) + "|" + s.inspect.center(60) + "|" + Rack::Utils::HTTP_STATUS_CODES[c].to_s.center(60) + "|"}.join("\n")'
</pre>
<pre class="brush:plain">
| 100 | :continue | Continue |
| 101 | :switching_protocols | Switching Protocols |
| 102 | :processing | Processing |
| 200 | :ok | OK |
| 201 | :created | Created |
| 202 | :accepted | Accepted |
| 203 | :non_authoritative_information | Non-Authoritative Information |
| 204 | :no_content | No Content |
| 205 | :reset_content | Reset Content |
| 206 | :partial_content | Partial Content |
| 207 | :multi_status | Multi-Status |
| 208 | :already_reported | Already Reported |
| 226 | :im_used | IM Used |
| 300 | :multiple_choices | Multiple Choices |
| 301 | :moved_permanently | Moved Permanently |
| 302 | :found | Found |
| 303 | :see_other | See Other |
| 304 | :not_modified | Not Modified |
| 305 | :use_proxy | Use Proxy |
| 306 | :reserved | Reserved |
| 307 | :temporary_redirect | Temporary Redirect |
| 308 | :permanent_redirect | Permanent Redirect |
| 400 | :bad_request | Bad Request |
| 401 | :unauthorized | Unauthorized |
| 402 | :payment_required | Payment Required |
| 403 | :forbidden | Forbidden |
| 404 | :not_found | Not Found |
| 405 | :method_not_allowed | Method Not Allowed |
| 406 | :not_acceptable | Not Acceptable |
| 407 | :proxy_authentication_required | Proxy Authentication Required |
| 408 | :request_timeout | Request Timeout |
| 409 | :conflict | Conflict |
| 410 | :gone | Gone |
| 411 | :length_required | Length Required |
| 412 | :precondition_failed | Precondition Failed |
| 413 | :request_entity_too_large | Request Entity Too Large |
| 414 | :request_uri_too_long | Request-URI Too Long |
| 415 | :unsupported_media_type | Unsupported Media Type |
| 416 | :requested_range_not_satisfiable | Requested Range Not Satisfiable |
| 417 | :expectation_failed | Expectation Failed |
| 422 | :unprocessable_entity | Unprocessable Entity |
| 423 | :locked | Locked |
| 424 | :failed_dependency | Failed Dependency |
| 425 | :reserved_for_webdav_advanced_collections_expired_proposal | Reserved for WebDAV advanced collections expired proposal |
| 426 | :upgrade_required | Upgrade Required |
| 509 | :unassigned | Unassigned |
| 428 | :precondition_required | Precondition Required |
| 429 | :too_many_requests | Too Many Requests |
| 431 | :request_header_fields_too_large | Request Header Fields Too Large |
| 500 | :internal_server_error | Internal Server Error |
| 501 | :not_implemented | Not Implemented |
| 502 | :bad_gateway | Bad Gateway |
| 503 | :service_unavailable | Service Unavailable |
| 504 | :gateway_timeout | Gateway Timeout |
| 505 | :http_version_not_supported | HTTP Version Not Supported |
| 506 | :"variant_also_negotiates_(experimental)" | Variant Also Negotiates (Experimental) |
| 507 | :insufficient_storage | Insufficient Storage |
| 508 | :loop_detected | Loop Detected |
| 510 | :not_extended | Not Extended |
| 511 | :network_authentication_required | Network Authentication Required |
</pre>Maysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-54229021744822643822014-04-02T15:30:00.000+07:002014-04-02T15:30:04.509+07:00uninstall all gems linuxcurl -Ls https://gist.github.com/nixpulvis/5042764/raw/7daa611e707f1884eb701551464bfd1dc9e7a953/gem-reset | rubyMaysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-3777805152518353182014-02-12T19:00:00.003+07:002014-02-12T19:00:30.273+07:00plug.dj autowoot bookmarklet<a href="javascript:(function(){var awoot = setInterval(function(){if(!$('#meh').hasClass('selected')) $('#woot:not(.selected)').click();}, 5000);$(window).resize(function(){ $('#playback').css({left: 0, width: '100%', 'max-height': 676, height: '80%'}); $('#playback-container').css({left: 0, width: '100%', height: '100%'})}).trigger('resize'); $('body').css('background', ''); $('#audience,#dj-booth,.background').hide(); })();">plug.dj autowoot</a>Maysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-44806498160912034572013-07-06T21:26:00.001+07:002013-07-08T17:56:38.079+07:00Sharing gems (or any files really) from lubuntu to windowsContinuing from my
<a href="http://tech.maysora.com/2013/06/developing-rails-in-windows-using-linux.html">previous post</a>
about developing rails using lubuntu vm as rails server, I need to have access to gem files inside lubuntu from windows for viewing its sources..<br />
<br />
At first I tried to use virtual box shared folders to store rbenv/gems files but it cause many problems in ruby or at the very least it result in a very slow performance (especially when gems are loading)<br />
<br />
After many many sleepless nights (dramatization) I finally succeed by using samba and sharing the gems via (virtual) network with the following steps..<br />
<br />
<a name='more'></a><br /><br />
<h1>
Sharing folder in lubuntu</h1>
<br />
Add symbolic link from gems folder (this is optional but recommended if you have multiple ruby installed)
assuming you're using ruby 1.9.3-p448 under rbenv in default path:
<br />
<pre class="brush: bash">mkdir $HOME/gems-link
ln -s $HOME/.rbenv/versions/1.9.3-p448/lib/ruby/gems/1.9.1/gems $HOME/gems-link/1.9.3-p448
</pre>
repeat for each installed ruby<br />
<br />
Install samba:
<br />
<pre class="brush: bash">sudo apt-get install samba
</pre>
Edit samba configuration file:
<br />
<pre class="brush: bash">sudo nano /etc/samba/smb.conf
</pre>
and add the following (you don't need "follow symlinks" and "wide links" line and need to change the path if you didn't use symbolic link earlier)
<br />
<pre class="brush: plain">[gems-link]
path = /home/[your username]/gems-link
; writeable = no
; browseable = yes
guest ok = yes
follow symlinks = yes
wide links = yes
</pre>
you can set writeable to yes if you want to modify gems file in windows (unlikely..)
and also add the following to global configuration if you use symbolic link:
<br />
<pre class="brush: plain">unix extensions = no
</pre>
reboot lubuntu<br />
<br />
<h1>
Access shared folder in windows</h1>
<br />
If you already added lubuntu ip to windows hosts file from
<a href="http://tech.maysora.com/2013/06/developing-rails-in-windows-using-linux.html">previous post</a>
then you can access shared folders in lubuntu in
<br />
<pre class="brush: plain">\\vm.local
</pre>
that's it :)Maysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-1476783262531782072013-06-28T16:39:00.000+07:002013-07-06T21:56:39.034+07:00Developing Rails in Windows using Linux Server<a href="http://3.bp.blogspot.com/-kXyBnT-9E-8/UdgstlVlhUI/AAAAAAAAAFk/TEW6Qam7YgQ/s1600/workspace.png" imageanchor="1"><img border="0" height="225" src="http://3.bp.blogspot.com/-kXyBnT-9E-8/UdgstlVlhUI/AAAAAAAAAFk/TEW6Qam7YgQ/s400/workspace.png" width="400" /></a><br />
<br />
Developing rails/ruby app in windows is painful as many gems aren't compatible or optimized for windows. But windows is OS that I'm most comfortable with.. Linux (ubuntu) lack some tools that I need and I simply hate OSX (stupid keyboard layout and hardware restriction), so I prefer dealing with gem problems rather than changing OS..<br />
<br />
But then it occurred to me, why not just use virtual machine with linux for server? And turns out it's pretty neat.. And I think some people are too use windows as main OS so I posted here the step for setting up linux vm for rails..<br />
<a name='more'></a><br />
<h1>
Required Software
</h1>
<ul>
<li><a href="https://www.virtualbox.org/wiki/Downloads" target="_blank">VirtualBox</a> (install it)</li>
<li><a href="http://archive.ubuntu.com/ubuntu/dists/raring/main/installer-amd64/current/images/netboot/mini.iso">Ubuntu mini ISO</a> (this link is for Raring 64)</li>
</ul>
<h1>
Installing minimal Lubuntu
</h1>
<ol>
<li>Create virtual machine:</li>
<ol>
<li>Open virtualbox and click new machine</li>
<li>Name it anything you want, Type: Linux, Version: Other Linux</li>
<li>Allocate 1GB ram to it (it's enough for 1 dev server) or more if you can afford it</li>
<li>Create a virtual hard drive</li>
<li>Any hard drive type, the default should be good enough</li>
<li>Dynamically allocated hard drive</li>
<li>Any amount of disk space, the default also good enough</li>
</ol>
<li>Initial setup:</li>
<ol>
<li>click File, Preferences, Input: uncheck Auto Capture Keyboard (this feature is useless to us)</li>
<li>right click your virtual machine created in step 1 and click Settings</li>
<li>Open Network tab set Adapter 1 to Host-only Adapter (this will be used so we can access rails server in virtual machine from windows)</li>
<li>set Adapter 2 to NAT (so virtual machine can access internet)</li>
<li>Open Storage tab and mount Ubuntu mini ISO to CD/DVD Drive</li>
<li>Close settings, right click virtual machine and choose Create shortcut on desktop, the if you like pin the shortcut to start menu for easier access</li>
<li>At this point you can close virtualbox manager and use shortcut</li>
</ol>
<li>Lubuntu installation</li>
<ol>
<li>Start your virtual machine</li>
<li>In boot menu chooce command-line install</li>
<li>Follow instruction <a href="https://help.ubuntu.com/community/Lubuntu/Documentation/MinimalInstall" target="_blank">here</a> starting from Method step 4 as I'm too lazy to write it and it will be better updated (pick Lubuntu core installation)</li>
</ol>
</ol>
<h1>
Installing Guest Addition
</h1>
<ol>
<li>Run sudo apt-get install build-essential dkms</li>
<li>Start your virtual machine and login if you haven't already</li>
<li>Click Devices, Install Guest Additions.. It will mount a VBOXADDITIONS</li>
<li>Open mounted directory in terminal and run sudo ./VBoxLinuxAdditions.run</li>
<li>Wait until it done and reboot</li>
</ol>
<h1>
Adding Shared Folders and others
</h1>
<br />
In virtual machine menu click Devices, Shared Folders, Choose your project folders in Folder Path, then enter Folder Name that will be used in lubuntu, and check make permanent..<br />
<br />
Next you want to mount it on start up automatically, edit rc.local file:<br />
<pre class="brush: bash">sudo nano /etc/rc.local
</pre>
Add the following line before exit 0: (assuming shared folder name is projects)<br />
<pre class="brush: bash">mount -t vboxsf projects /home/[your-username]/projects
</pre>
Then create projects folder inside your home folder:<br />
<pre class="brush: bash">mkdir $HOME/projects
</pre>
Reboot and shared folder should be added to your home.<br />
<br />
Next enable bidirectional shared clipboard from Devices menu<br />
<br />
<h1>
Installing terminal emulator
</h1>
<br />
Lubuntu by default preinstalled with XTerm terminal emulator, and IMO it's bad.. So I prefer to install gnome-terminal to replace it:<br />
<pre class="brush: bash"> sudo apt-get install gnome-terminal
</pre>
or if you like other terminal feel free to install it (although preferable the one that support full screen)<br />
<br />
If you're using gnome-terminal you might want to hide menubar by default by going editing profile and uncheck Show menubar by default<br />
<h1>
Installing required libraries
</h1>
<pre class="brush: bash">sudo apt-get install build-essential zlib1g-dev libssl-dev libreadline6-dev libxslt1-dev libcurl4-openssl-dev git-core
</pre>
also if you use postgresql:<br />
<pre class="brush: bash">
sudo apt-get install postgresql
</pre>
<h1>
Installing rbenv
</h1>
<br />
Simply follow installation instruction here:<br />
<a href="https://github.com/sstephenson/rbenv#installation" target="_blank">https://github.com/sstephenson/rbenv#installation</a><br />
<br />
<h1>
Editing windows hosts file
</h1>
<br />
Run ifconfig in lubuntu terminal, check eth0 inet IP, it should be something like 192.168.56.101..<br />
Copy it (block, ctrl+shift+c for gnome-terminal)<br />
Then in windows, open C:\Windows\System32\drivers\etc\hosts with your favorite text editor<br />
<br />
paste the IP copied earlier and add an address for it, for example:<br />
<pre class="brush: plain">192.168.56.101 vm.local
</pre>
now if you start a rails server in lubuntu you can simply enter vm.local:3000 (if server port is 3000) in windows.<br />
<br />
<h1>
Finishing
</h1>
<br />
Now everything is done and time to enjoy it.<br />
Things you can do:<br />
<ul>
<li>Press F11 (for gnome-terminal) for full screen so you got similar feel as windows command prompt except better</li>
<li>Others useful shortcuts (for gnome-terminal)</li>
<ul>
<li>ctrl+shift+t : open new tab</li>
<li>alt+1..9 : switch to tab x</li>
<li>ctrl+shift+w : close tab</li>
<li>ctrl+shift+c : copy </li>
<li>ctrl+shift+v : paste</li>
</ul>
<li>And most importantly.. Code without worry about incompatibility but with the comfort of windows!</li>
</ul>
What's next?
<br />
<ul>
<li>
<a href="http://tech.maysora.com/2013/07/sharing-gems-or-any-files-really-from.html">Access lubuntu gem files from windows</a>
</li>
<li>More soon..</li>
</ul>
Enjoy..Maysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-85567060614130268972012-11-24T02:40:00.002+07:002013-06-28T17:53:37.232+07:00Sublime Text 2: jump to definitionSince I moved from netbeans (which no longer officially support rails) to sublime text 2, the only thing that I missed is jump to definition function, often when I need to know how certain method from a gem works I'm forced to search it manually inside that gem folder while on netbeans I can just use alt+click to instantly navigate there..<br />
<br />
After some googling I found SublimeCodeIntel which most of the time isn't working and make sublime heavier instead. But at long last I found about CTags and a <a href="https://github.com/SublimeText/CTags" target="_blank">Sublime Package</a> for it.<br />
<br />
First you need to build the tags file by using Rebuild Tags from command palette, after that using it is pretty much similar to netbeans, but instead of alt+click we use ctrl+shift+click plus ctrl+shift+rightclick for returning to previous position and it's works perfectly... for methods inside the project..<br />
<br />
<a href="https://github.com/SublimeText/CTags#usage" target="_blank">Sublime CTags readme</a> show a script to generate tags for gems which is works perfectly, it will generate tags from all gems specified in Gemfile, but adding the script each time I work on a project is kinda troublesome so I created <a href="https://github.com/Maysora/ctags_bundler_rails" target="_blank">a simple gem</a> to add several rake tasks for generating tags. The gem basically add 3 rake tasks, one for generating tags for project, one for gems, and the last one for both project and gems.Maysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-8459987817968764252012-10-24T01:59:00.002+07:002013-06-28T17:53:08.560+07:00Rails setup in linode<ol>
<li><div>
<span style="font-size: small;"><span style="font-family: inherit;">Login to linode</span></span></div>
</li>
<li><div>
<span style="font-size: small;"><span style="font-family: inherit;">choose linux distribution (ubuntu)</span></span></div>
</li>
<li><span style="font-size: small;"><span style="font-family: inherit;">allocate disk space and swap</span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">boot</span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">remote access using ssh</span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">apt-get update</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">apt-get upgrade --show-upgraded</span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">set hostname:</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">echo "ayahirano" > /etc/hostname</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">hostname -F /etc/hostname</span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">nano /etc/hosts</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">127.0.0.1 localhost.localdomain localhost</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">12.34.56.78 ayahirano.invalid hirano</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">2600:3c01::a123:b456:c789:d012 ayahirano.invalid hirano</span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">set timezone to UTC:</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">dpkg-reconfigure tzdata</span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">install required libs:</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">
</span></span><span style="font-family: inherit;"><span style="font-size: small;"><code>apt-get install build-essential zlib1g-dev libssl-dev libreadline6-dev libxslt1-dev libcurl4-openssl-dev apache2-mpm-prefork apache2-prefork-dev libapr1-dev libaprutil1-dev</code>
<code>git-core</code></span></span><span style="font-size: small;"><span style="font-family: inherit;">
</span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">install require lib for postgresql (optional):</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">apt-get install postgresql postgresql-contrib libpq-dev</span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">create new user:</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">adduser example_user</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">usermod -a -G sudo example_user</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">logout and relogin user the new user</span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">disable root login:</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">sudo nano /etc/ssh/sshd_config</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">PermitRootLogin no</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">setup postgresql (optional):</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">sudo -u postgres createuser --superuser example</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">sudo -u postgres psql postgres</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">\password example</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">\q</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">sudo nano /etc/postgresql/8.4/main/postgresql.conf:</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">listen_addresses = 'localhost'</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">sudo nano /etc/postgresql/8.4/main/pg_hba.conf:</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">local all all md5</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">sudo /etc/init.d/postgresql start</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">install rvm:</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">\curl -L https://get.rvm.io | bash -s stable</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">source /home/example_user/.rvm/scripts/rvm</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">install ruby:</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">rvm install ruby</span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">disable gem documentation installation:</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">
</span></span><code><span style="font-size: small;"><span style="font-family: inherit;">nano ~/.gemrc</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span></code><span style="font-size: small;"><span style="font-family: inherit;">
</span></span><code><span style="font-size: small;"><span style="font-family: inherit;">gem: --no-ri --no-rdoc</span></span></code><span style="font-size: small;"><span style="font-family: inherit;">
</span></span><code><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span></code></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">install passenger:</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">gem install passenger</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">rvmsudo passenger-install-apache2-module</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">get your app to /var/www/example</span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">disable default virtual host:</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">
</span></span><b><span style="font-weight: normal;"><code><span style="font-size: small;"><span style="font-family: inherit;">sudo a2dissite default</span></span></code></span></b></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">create new virtual host config:</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">
</span></span><code><span style="font-size: small;"><span style="font-family: inherit;">sudo nano /etc/apache2/sites-available/example</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;"><VirtualHost *:80></span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;"> ServerName ayahirano.invalid</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;"> ServerAlias *.ayahirano.invalid</span></span></code><code><span style="font-size: small;"><span style="font-family: inherit;"> RailsEnv production</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /> DocumentRoot /var/www/example/public</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;"> <Directory /var/www/</span></span></code><code><span style="font-size: small;"><span style="font-family: inherit;">example</span></span></code><code><span style="font-size: small;"><span style="font-family: inherit;">/public></span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;"> AllowOverride all</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;"> # MultiViews must be turned off.</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;"> Options -MultiViews</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;"> </Directory></span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;"></VirtualHost></span></span></code></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">enable it:</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">sudo a2ensite example</span></span><code><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span></code></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">
</span></span><span style="font-size: small;"><span style="font-family: inherit;">edit apache configuration file:</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">sudo nano /etc/apache2/mods-available/passenger.load</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">LoadModule passenger_module /home/rails/.rvm/gems/ruby-1.9.3-p286/gems/passenger-3.0.17/ext/apache2/mod_passenger.so</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">
</span></span><span style="font-size: small;"><span style="font-family: inherit;">sudo nano /etc/apache2/mods-available/passenger.conf</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">PassengerRoot /home/rails/.rvm/gems/ruby-1.9.3-p286/gems/passenger-3.0.17</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;"> PassengerRuby /home/rails/.rvm/wrappers/ruby-1.9.3-p286/ruby</span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">enable passenger module:</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">sudo a2enmod passenger</span></span></li>
<li><span style="font-size: small;"><span style="font-family: inherit;">restart apache:</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span><span style="font-size: small;"><span style="font-family: inherit;">sudo service apache2 restart</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span></li>
</ol>
<h3>
<span style="font-size: small;"><span style="font-family: inherit;">Extras</span></span></h3>
<h4>
<span style="font-size: small;"><span style="font-family: inherit;">Installing nodejs for js runtime (needed by assets precompile):</span></span><span style="font-size: small;"><span style="font-family: inherit;"><br /></span></span></h4>
<br />
<div>
<span style="font-size: small;"><span style="font-family: inherit;">https://github.com/joyent/node/wiki/Installing-Node.js-via-package-manager</span></span></div>
<br />
<h4>
<span style="font-size: small;"><span style="font-family: inherit;">Creating deploy script</span></span></h4>
<div>
<ol>
<li>cd $HOME</li>
<li>nano deploy.sh</li>
<li>#!/bin/sh<br />sudo echo -n "precompile assets? (y or n) : "<br />read precompile<br />echo -n "bundle install? (y or n) : "<br />read bundler<br />cd /var/www/example<br />git pull<br />if [ $bundler = y -o $bundler = Y -o $bundler = yes -o $bundler = Yes -o $bundler = YES ]; then<br /> bundle install --without development test<br />fi<br />if [ $precompile = y -o $precompile = Y -o $precompile = yes -o $precompile = Yes -o $precompile = YES ]; then<br /> bundle exec rake assets:precompile RAILS_ENV=production<br />fi<br />rake db:migrate RAILS_ENV=production<br /># any other thing you do when deploying...<br />sudo service apache2 restart</li>
<li>chmod 700 deploy.sh</li>
<li>Done.. To run use: ./deploy.sh</li>
</ol>
</div>
<br />Maysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-36201003417179025112012-09-18T09:14:00.000+07:002012-09-27T08:53:00.289+07:00tcs-ruby: faster ruby for windowsAs a rails developer using windows as my development machine I often getting annoyed on how much time needed to start things especially tests.<br />
In case you're wondering why did I use windows instead of other OS-es better suited for ruby like MacOSX/linux it's because I'm comfortable with it and if you're disagree then feel free to stop reading at this point :)<br />
<br />
Several days ago I found about this fork of ruby by TheCodeShop which will speed up things quiet significantly, I decided to try it and one of my project unit tests which needed around 45 sec to complete reduced to less than 20 sec.<br />
<br />
<h1>
Installation</h1>
<br />
This step assume you're using Win7 with pik to manage multiple ruby installation, if you don't have pik yet get it from https://github.com/vertiginous/pik<br />
<br />
<ol>
<li>Download one of ruby build from:<br />https://github.com/thecodeshop/ruby/wiki/Downloads<br />(I'm using winio one)</li>
<li>Extract it, for example to C:\Ruby-tcs193</li>
<li>Modify Path to use ruby-tcs: go to Control Panel, System, Advanced System Settings, Environment Variables</li>
<li>Edit Path in User variables, modify existing Ruby bin path to ruby-tcs bin (C:\Ruby-tcs193)</li>
<li>Add new variable RUBYOPT with value: "-rfenix/replace"</li>
<li>Next, add it to pik (you can stop here if you didn't use pik, but I recommend it in case you need to switch to standard ruby)</li>
<li>open cmd, then enter "pik add [path to ruby-tcs bin]" for example "pik add C:\Ruby-tcs193\bin"</li>
<li>Lastly install devkit for it, run "ruby c:\devkit\dk.rb init" then "ruby c:\devkit\dk.rb install"</li>
<li>Done :)</li>
</ol>
<br />
<br />
<h1>
Known problems</h1>
<br />
<h3>
Heroku commands</h3>
heroku commands will raise error because of non existing fenix/replace, to fix simply remove RUBYOPT temporarily by entering "set RUBYOPT="<br />
<br />
<h3>
Standard ruby</h3>
same as above because standard ruby didn't use fenix so remove RUBYOPT temporarilyMaysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-35009756731628726032012-08-09T04:38:00.000+07:002012-08-09T04:44:29.824+07:00Web page Al Bhed translator javascript (bookmarklet)well long time no post :D<br />
<br />
recently I don't have much to do and rather than dying from boredom I created a simple javascript to translate a web page to Al Bhed language (from Final Fantasy X if you're wondering)<br />
<br />
The Base translation mapping come from http://nysa.cx/albhed/ and I just converted it to javascript.<br />
<br />
Anyway here are the script:<br />
<blockquote>javascript: (function(){var f=document.createElement('script'); f.src = 'http://al-bhed-page-translator-js.googlecode.com/files/albhed.js'; document.head.appendChild(f);})();
</blockquote>just copy and paste it into the browser address bar in the page you want to translate. (keep in mind that newer firefox block this kind of script, and you need to use bookmarklet for that)<br />
<br />
To translate the page back to original, simply use this script:<br />
<blockquote>javascript: (function(){alTranslate(true);})();
</blockquote><br />
That's it :)<br />
<br />
Known bugs:<br />
- html encoded character such as &nbsp; didn't translate properly, it's fixable but I'm too lazy to fix it :P<br />
- huge page might causing the browser to freeze, also should be fixable by adding some timeout but again, I'm lazy :DMaysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-21140672040090551832012-01-01T03:55:00.000+07:002012-01-01T03:55:33.995+07:00using compass in rails 3.1.3<pre class="brush:ruby">group :assets do
gem 'sass-rails', '~> 3.1.5'
gem 'coffee-rails', '~> 3.1.1'
gem 'uglifier', '>= 1.0.3'
gem 'compass', '>= 0.12.alpha'
end
</pre><br />
<pre class="brush:ruby">config.assets.precompile << /(^[^_]|\/[^_])[^\/]*$/
</pre>Maysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-39488635737395127732011-12-31T17:14:00.001+07:002011-12-31T17:14:14.603+07:00ubuntu 11.10 rails 3 passengerhttp://ubuntuandrails.blogspot.com/2011/11/installing-rails-311-ruby-193-apache2.htmlMaysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-50676251068130415822011-10-03T03:06:00.002+07:002011-10-03T03:17:03.551+07:00install mysql2 > 0.3.6 for mysql 64bit in windowsgem install mysql2 --platform=ruby -- '--with-mysql-lib="C:\Program Files\MySQL\MySQL Server 5.5\lib" --with-mysql-include="C:\Program Files\MySQL\MySQL Server 5.5\include"'<br />
<br />
http://dev.mysql.com/downloads/connector/c/<br />
<br />
copy C:\mysql-connector-c-noinstall-6.0.2-win32\lib\libmysql.dll to ruby\bin<br />
<br />
original source: http://blog.mmediasys.com/2011/07/07/installing-mysql-on-windows-7-x64-and-using-ruby-with-it/Maysorahttp://www.blogger.com/profile/02227669348435589170noreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-92035138692972309862011-07-04T22:51:00.000+07:002011-07-04T22:51:13.991+07:00fixing option tag display none bug in webkit<h3>The Problem</h3>option tag using style display:none isn't hidden in browser using webkit engine (chrome, safari)<br />
https://bugs.webkit.org/show_bug.cgi?id=8351<br />
<br />
<h3>The workaround</h3>using jQuery js framework with one simple function taken from jquery.extended_helper_2.6:<br />
<pre class="brush:js">(function($) {
$.fn.extend({
/* hide select's specified option by removing the option and storing it in variable
* this function is a workaround to fix webkit bug when hiding select's option:
* https://bugs.webkit.org/show_bug.cgi?id=8351
* hideSelectOptions(selector, options)
* selector:
* - string selector of options to hide
* - null or undefined will not hide any options
* - empty string will hide all options
* - function will be ran for all options, return true to hide the options
* options:
* - toggleHide: true/false, activate/deactive toggle hide/show select if all options is hidden
* - placeholder: string, add placeholder option if all options is hidden
* usage:
* - $('select.certain_class').hideSelectOptions('.another_class'); // -> show previously hidden options and hide all options with 'another_class' class
* - $('select.certain_class').hideSelectOptions(''); // -> hide all options
* - $('select.certain_class').hideSelectOptions(); // -> show previously hidden options without hiding any options
*/
hideSelectOptions: function(selector, options){
if(!options) options = {};
this.filter('select').each(function(){
var $this = $(this);
// restore previously hidden options and remove any placeholder option
$this.append($this.data('hiddenOptions')).find('.placeholder_option').remove();
// return if no selector specified
if(!selector) return;
// find, detach and store specified hidden options
var hidden_options = $this.find('option');
if($.isFunction(selector))
hidden_options = hidden_options.deleteIf(function(){return !selector.call(this);});
else
hidden_options = hidden_options.filter(selector);
$this.data('hiddenOptions',
hidden_options
.removeAttr('selected')
.detach());
// find visible options
var visible_options = $this.find('option');
// toggle hide/show select depend on visible option found
if(options.toggleHide)
$this.toggle(visible_options.length);
if(options.placeholder && !visible_options.length)
$this.append('<option class="placeholder_option" value="">'+options.placeholder+'</option>');
// select first option if no other visible option selected and trigger select change
if(!visible_options.filter(':selected').length){
visible_options.filter(':first').attr('selected', 'selected');
$this.trigger('change');
}
});
return this;
}
});
})(jQuery);
</pre><br />
<h3>The Demonstration</h3><iframe style="width: 100%; height: 300px" src="http://jsfiddle.net/maysora/4FFm2/embedded/result,js,html,resources/"></iframe>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-26656816036345055892011-06-30T11:47:00.002+07:002011-06-30T11:47:59.550+07:00Additional Range method<pre class="brush:ruby">class Range
# return true if 2 range overlapped
def overlap? other_range
include?(other_range.begin) || other_range.include?(self.begin)
end
# return a new intersection range between two ranges
def & other_range
raise TypeError, "exclusive range can't be intersected with non-exclusive one" unless self.exclude_end? == other_range.exclude_end?
new_begin = [self.begin, other_range.begin].max
new_end = [self.end, other_range.end].min
new_begin <= new_end ? Range.new(new_begin, new_end, self.exclude_end?) : nil
end
# return true if other_range is a subset in self
def contains? other_range
if !exclude_end?
include?(other_range.begin) && include?(other_range.end)
elsif self.begin.respond_to?('>=')
self.begin <= other_range.begin && self.end.send(other_range.exclude_end? ? '>=' : '>', other_range.end)
else
raise TypeError, "can't determine inclusion of range with this type of members"
end
end
# return true if self is a subset of other_range
def within? other_range
other_range.contains? self
end
# return length of the range (only for subtract-able range)
def length
self.end.respond_to?('-') ? self.end - self.begin : nil
end
# return next range with same span (only for subtract-able and add-able range)
def succ allow_touch=false
return nil unless self.end.respond_to?('-') && self.end.respond_to?('+')
new_begin = allow_touch || exclude_end? || !self.end.respond_to?(:succ) ? self.end : self.end.succ
new_end = new_begin + length
Range.new(new_begin, new_end, exclude_end?)
end
# return previous range with same span (only for subtract-able and add-able range)
def pred allow_touch=false
return nil unless self.end.respond_to?('-') && self.begin.respond_to?('+')
new_end = allow_touch || exclude_end? || !self.begin.respond_to?(:pred) ? self.begin : self.begin.pred
new_begin = new_end - length
Range.new(new_begin, new_end, exclude_end?)
end
end
</pre>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-68359998895766183562011-06-28T06:10:00.000+07:002011-06-28T06:10:25.222+07:00array flatten with level for ruby 1.8.7straight to the code:<br />
<pre class="brush:ruby">class Array
alias_method :orig_flatten, :flatten
def flatten level=nil
if level.is_a?(Integer)
temp_arr = clone
level.times{ |i| temp_arr = temp_arr.inject([]){|s,v| v.is_a?(Array) ? s.concat(v) : s << v} }
temp_arr
else
orig_flatten
end
end
end
</pre>
<br />
of course although untested i'm sure it will have worse performance than the original flatten in ruby 1.9.2, but better than nothing right? ;)Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-83468972569477848842011-06-28T05:53:00.000+07:002012-12-21T21:23:59.001+07:00ruby array to hash methodupdate 2012-12-21:<br />
you should use flat_map instead of flatten, i'm too lazy to update the code though.. :P<br />
<br />
we can convert hash to array easily using .to_a method for example:<br />
<pre class="brush:ruby">hsh = {:a => 1, :b => 2, :c => 3}
arr = hsh.to_a # -> [[:a, 1], [:b, 2], [:c, 3]]
</pre>
<br />
but there's no built in method to reverse that, there's no .to_hash instance method for array.<br />
<br />
to solve that i added a simple instance method for array:<br />
<pre class="brush:ruby">class Array
def to_hash values=nil
Hash[*(values.is_a?(Array) ? self.zip(values) : self).flatten(1)]
end
end
hsh = {:a => 1, :b => 2, :c => 3}
arr = hsh.to_a # -> [[:a, 1], [:b, 2], [:c, 3]]
arr.to_hash == hsh # -> true
[:a,:b,:c].to_hash [1,2,3] # -> {:a => 1, :b => 2, :c => 3}
</pre>
<br />
a little precaution, flatten(1) will not work properly in ruby 1.8.7 or older, therefore it won't be possible to create hash with array as key or value. but there's a simple workaround for that by overriding array flatten method to behave like ruby 1.9.2 in my next post :)Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-8812616865430216485.post-80825173061640726292011-06-28T05:18:00.002+07:002011-07-04T23:02:15.508+07:00useful online tools for web programming<ul><li>Regex for ruby: http://www.rubular.com/<br />
<li>strftime: http://strfti.me/<br />
<li>css gradient generator: http://www.colorzilla.com/gradient-editor/<br />
<li>css validator: http://jigsaw.w3.org/css-validator/<br />
<li>css rounder corner generator: http://a.deveria.com/roundgen/<br />
<li>7 great js resources (not really a tool but useful): http://mashable.com/2011/07/03/7-great-javascript-resources<br />
</ul><br />
will be updated as i found another toolsUnknownnoreply@blogger.com0tag:blogger.com,1999:blog-8812616865430216485.post-50467444252994123112011-06-11T05:26:00.000+07:002011-06-11T05:26:38.241+07:00Rails general coding style<h4>Code:</h4><pre class="brush:ruby"># somethings_controller.rb
def create
@something = Something.new params[:something]
@something.save!
if @something.some_state?
@something.initialize_for_some_state
end
redirect_to success_url
rescue
render :action => "failed"
end
</pre><h4>Hint:</h4><ol><li>should avoid rescue without specifying the exception, in above code, use<br /><i>rescue ActiveRecord::RecordInvalid</i><br />
<li>better yet, don't use any rescue at all, use save instead of save! with some if<br />
<li>post processing should be done in callback.<br /><i>after_create {|something| something.initialize_for_some_state if something.some_state}</i><br />
</ol><h4>Better Code:</h4><pre class="brush:ruby"># somethings_controller.rb
def create
@something = Something.new params[:something]
if @something.save
redirect_to success_url
else
render :action => "failed"
end
end
</pre>
<hr />
<h4>Code:</h4><pre class="brush:ruby"># somethings_controller.rb
def show
@something = Something.find(:first, :conditions => "user_id = #{current_user.id} AND id = #{params[:id]} AND name = #{params[:name]}")
respond_to do |format|
format.html
format.js do
render :js => "$('#container').html('#{escape_javascript(render :partial => "something", :object => @something)}');"
end
format.json do
render :json => @something.to_json
end
end
end
</pre><h4>Hint:</h4><ol><li>SQL injection warning! <u>never</u> use data from params or from user input directly in sql code. use something like this instead:<br /><i>:conditions => ["user_id = #{current_user.id} AND id = ? AND name = ?", params[:id], params[:name]]</i><br />
<li>using model relation and dynamic finder you can avoid the conditions entirely<br /><i>@something = current_user.somethings.find_by_id_and_name params[:id], params[:name]</i><br />
<li>you should check if nothing found (@something is nil)<br />
<li>this is just my opinion, but in most case i'll avoid using respond_to, instead i'll use 3 different view files: show.html.erb, show.js.erb, show.json.erb. rails will automatically use the correct view.<br />
</ol><h4>Better Code:</h4><pre class="brush:ruby"># somethings_controller.rb
def show
@something = current_user.somethings.find_by_id_and_name params[:id], params[:name]
render_invalid "not found" if @something.nil? # read about render_invalid in http://tech.maysora.com/2010/12/rails-renderinvalid-method.html
end
</pre>
<hr /><br />
to be continued about modelUnknownnoreply@blogger.com0