• Skip to content
  • Skip to navigation
  • Log in

Code & Config – sharing the smart bits

Navigation

  • Home
  • About
  • Archive

Categories

  • Command line
  • Configurations
  • CSS
  • EC2
  • GIT
  • jQuery
  • Paypal
  • PHP
  • Rails
  • SQL

Installing RVM which allows you to run multiple Ruby environments on one machine

22/01/12 // No Comments //

Rails

Install from the source

  bash -s stable < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer )

Reload the bash profile

  source .bash_profile

Check if the RVM is installed. Is should return “rvm is a function”.
Note: It’s gonna take while

  type rvm | head -1

Install ruby 1.9.2. (for example)

  rvm install 1.9.2

Use the ruby you just installed
[command]
rvm use 1.9.2
[/command]

join discussion

Objects in javascript / jquery / caffeescript

16/07/11 // No Comments //

jQuery

Here is one of the way you can declare objects in javascript, using the caffeescript syntax. We are using javascript object litera in here.

// This replaces the onload
$ ->

  //Object name (does not have to be capitalize, we are not using constructor
  core =
    // Method name
    init: ->
      // Using @ is replacement for this.modules
      @modules = new Array()

    register: (module) ->
      @modules.push(module)

join discussion

Each loop in jQuery which only follows certain elements

23/06/11 // No Comments //

jQuery

This is a function I am using in my application so I could use keyboard keys to browse through a lit of tasks. My problem was that som of the tasks were hidden (in my example did not have the class “visible”) so I did not want to go through them when using the up and down keys while browsing the list of tasks. I only wanted to use click() on the next visible list item.

I had to use prevAll() & nextAll with a specific slection of a class ‘visible’ but and reaching for the first match.
This the particular piece of code in caffeescript (again, its just jquery without brackets)

  prev_list = $(current_list).prevAll('li.visible:first')

My function is a bit longer as I am checking by other stuff but this is the simplified version of it.

focus_on_prev_task = ->
    $('#data_ajax_cover li').each ->
      current_list = $(this)
      prev_list = $(current_list).prevAll('li.visible:first')
      $(prev_list).click()

join discussion

Catch the ajax:beforeSend and ajax:success on rails 3.1 remote forms with cafeescript

21/06/11 // No Comments //

jQuery

You might want to display your custom progress bar when using remote ajax forms in rails 3.1. This is how you would bind your custom functions to the events in cafeescript

  rails_remote_forms = ->
    $('form[data-remote]').bind "ajax:beforeSend", ->
      ajax_display_progress_bar()
    $('form[data-remote]').bind "ajax:success", ->
     ajax_success_message()

join discussion

Turn autocomplete off on all forms

15/06/11 // No Comments //

jQuery

When autosuggestions are turning off the focus I got rid of them like this.
The script is in caffeescript but pure jQuery is not much different

  #Turn autocomplete off on all forms
  # ============================================================================
  turn_autocomplete_off = ->
    $('form').each ->
      $(this).attr('autocomplete', 'off')
      $(this).find('input').attr('autocomplete', 'off')

join discussion

Function in Caffee Script

10/06/11 // No Comments //

jQuery

Caffee Script has become the default in rails 3.1
The syntax is pretty interesting and for those of you who do not like the brackets it is going to be a gogo.
You only need to make sure that your indenting is correct

This is how you define a simple function

my_lovely_function = ->
 alert "hello"

This is how you define a simple function with a variable

my_lovely_function = (variable) ->
 alert "hello"

And this is how you call the function

my_lovely_function variable

As you can see most of the brackets are gone. You can still use jQuery in Caffee Script. jQuery is part of Rails 3.1 as well so we are all good ;)

join discussion

Redo your “hover” events for jQuery 1.5.x

23/04/11 // No Comments //

jQuery

The new jQuery >1.5.0 does not support the syntax for the hover we have been using so far but there is a better way.

This was the old way.

	$('#users_edit p.edit_in_place_paragraph').live('hover', function (ev) {
		if (ev.type == 'mouseover') {
          // your on hover stuff..
		}
		if (ev.type == 'mouseout') {
          // your hover out stuff....
		}
	});

This is the new way, which works.

  $('#users_edit p.edit_in_place_paragraph').live({
    mouseenter:
       function()
       {
          // your on hover stuff..
       },
    mouseleave:
       function()
       {
          // your hover out stuff....
       }
    });

join discussion

Update part of the content in your database

18/01/11 // No Comments //

SQL

If you need to update part of the strings in your database you can do so using this syntax.
This is specially useful when you want to fix some URLS stored in the database.

update TABLE_NAME set FIELD_NAME = replace(FIELD_NAME, ‘old string’, ‘replace with new string);

Remember, it will replace the part of the string. Example from WordPress

update wp_posts set guid = replace(guid, 'http://localhost:8888/abc/', 'http://abc.yourdomain.com/');

This will fix http://localhost:8888/abc/?p=123 into http://abc.yourdomain.com/?p=123.

join discussion

Implement Cron into your Rails application

11/01/11 // No Comments //

Rails

There is a much better solution available for implementing Cron Jobs in Rails application then editing the crontab file on your server.
Editing the file by hand requires knowing the cron syntax and what is worst that you need to update the cron file on every server where your application is running.

Here is better solution.
Install gem called – Whenever.

gem install whenever

#Wheneverize your application, which will create an schedule file - config/schedule.rb"
wheneverize .

#Set your cron jobs in the schedule file using ruby syntax
#The syntax is pretty self explanatory 

every :reboot do
  command "sudo mount /dev/sdf /mnt/data"
  command "sudo /etc/init.d/mysql start"
end

every 1.day,  :at => "1am" do
  runner "Task.deadline_emails"
end

#After every change run this command to update the system cron
# whenever --update-crontab same_name_to_identify_the_job

#Update your deploy file (when using Capistrano)
after 'deploy', 'update_crontab'
task :update_crontab, :roles => :db do
  run "cd #{release_path} && whenever --update-crontab #{application}"
end

join discussion

Changed volumes on EC2 but the volume size has not changed?

10/01/11 // No Comments //

EC2

When you decide that you need bigger volume to store your data (in EC2), you do the following in your EC2 console.

Create a snapshot of your current volume. Create new volume from the snapshot. Attach the volume to your instance.
You might need to reboot the instance and mount the volume (/dev/sdf) to you instance. I usually start the mysql afterwords as my mysql data are stored on the volume so system can’t access them when rebooting. You might have better setup so you do not need to mount the dist manually after restart and start the mysql.

Update: There is fix for the solution above – Using Cron read more

Here comes the problem.
If you originally had 1gig volume and you have now created 100gig and use the snapshot of the 1gig the system will thing the dist has still 1gig.

You can resize the disk on the fly with the resize2fs command.
Parameters -p means it will print what is the command doing and -f mean force so it will force to do certain changes which needs to be done. (Google resize2fs for full description of the syntax)

sudo resize2fs -p -f /dev/sdf "SIZEOFYOURDISK"

Note: Replace “SIZEOFYOURDISK” with actual size of your new Volume.

join discussion

Copyright by Roman Leinwather

  • Lewro //
  • getuantify //
  • Twitter //
  • Eye Design