• 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

Delayed job stopped, run it again to send hanging mails

05/05/12 // No Comments //

Rails

RAILS_ENV=production script/delayed_job start

join discussion

Running a method form the server itself

04/05/12 // No Comments //

Rails

In this case its the method in the task model.

script/rails runner -e production Task.deadline_reached_emails

join discussion

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

Copyright by Roman Leinwather

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