Archive

Archive for the ‘Development’ Category

Git Philosophy And Practice References

March 3rd, 2010 mindtonic No comments
Categories: Git Tags:

Treasures Recommended by Emphasized Insanity

January 7th, 2010 mindtonic No comments

quantity – quantities and measurements for Ruby.
phone – phone number parsing and validation for Ruby.
simple_facebook_connect – adds Facebook connect’s signin/signout options to your Rails application.
snippy – pastie/gist clone on Rails.
carmen – A simple collection of geographic names and abbreviations for Rails apps (includes replacements for country_select and state_select)
phd – Heroku still deployments for passenger.
tartarus – an exception notifier for rails.

Emphasized Insanity

Categories: Ruby On Rails Tags:

Sudo To Root

December 2nd, 2009 mindtonic No comments

Easy enough:

sudo su -

Enter your password… With great power comes great responsibility.

Clear out MySql: svcadm clear mysql

Categories: Unix Tags:

Converting attachment_fu to Paperclip

October 1st, 2009 mindtonic No comments

I am making the transition from attachment_fu to paperclip on a very large project of mine. I wrote the following method, inside of my old attachment_fu model ItemImage to do the work. I wrote it to run from inside the rails console. It gives handy messages out letting you know what happened. In my case you run:

$ script/console
>> ItemImage::convert_fu
def self.convert_fu
  for item in Item.all
    # has an image
    if item.item_image
      # the image exists
      filename = "public" + item.item_image.public_filename
      if File::exists?(filename)
        image = File.open(filename)
        item.image = image
        item.save
        puts "#{item.name} Image Converted"
      else
        puts "====> #{item.name} Image could not be found"
      end
    else
      puts "#{item.name} has no image"
    end
  end
end

Reference: http://thewebfellas.com/blog/2008/11/2/goodbye-attachment_fu-hello-paperclip

Categories: Gems, Ruby On Rails Tags: ,

MailChimp For Site Mailings

September 27th, 2009 mindtonic No comments

An interesting service to manage mailing lists:

http://www.mailchimp.com/

http://github.com/mandarinsoda/acts_as_chimp/

Categories: Development Tags:

Calculating The Number Of Months Between Two Dates In Ruby

September 17th, 2009 mindtonic 1 comment

My research led me here: http://www.francisfish.com/getting_the_number_of_months_between_two_dates_in_rubyrails.htm

This is the method I came up with:

def self.months_between( startdate=Time.now, enddate=Time.now )
  startdate ||= Time.now
  enddate ||= Time.now

  (enddate.month - startdate.month) + 12 * (enddate.year - startdate.year)
end
Categories: Ruby Tags:

Convert MySql Datetime to Javascript Date Object

August 13th, 2009 mindtonic No comments
  function mysqlTimeStampToDate(timestamp) {
    //function parses mysql datetime string and returns javascript Date object
    //input has to be in this format: 2007-06-05 15:26:02
    var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
    var parts=timestamp.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
    return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
  }

Source: http://snippets.dzone.com/posts/show/4132

Categories: PHP Tags: ,

Rails Javascript and CSS Compression

July 30th, 2009 mindtonic No comments

To speed up site loading, there are two great plugins for “compressing” your stylesheets and javascript.

The first is bundle-fu. This plugin does not actually compress the files, but rather combines all of the individual documents into one before shipping it out to the client browser.

$ script/plugin install git://github.com/timcharper/bundle-fu.git

And then you simply wrap your sheet and script calls in the bundle method:

  <% bundle do %>
    ...
    <%= javascript_include_tag "prototype" %>
    <%= stylesheet_link_tag "basic.css" %>
    <%= calendar_date_select_includes "red" %>
    
    ...
  <% end %>

The Second plugin, which actually does compress all of your files, is sbecker’s asset_packager. This one is a lot more involved, so check out the github site for more information.

Calendar Date Select Magic For Rails

July 27th, 2009 mindtonic No comments

Calendar Date Select is a new(ish) “date and time picker”, developed by Tim Harper, designed primarily for developers to use in Rails applications. It uses the standard Prototype JavaScript library, and is easily installed as a Rails plugin:

script/plugin install http://calendardateselect.googlecode.com/svn/tags/calendar_date_select

The demo seems to be down, but here is some more detailed info: http://ianli.com/site/HowTo/UseCalendarDateSelectRailsPlugin. You can also see the original contents of the demo site about halfway down the Google Code wiki for the project: http://code.google.com/p/calendardateselect/wiki/CalendarDateSelect

On the surface this is a very easy plugin to install and use. It seems the common problem is that there is very little documentation available and the demo server is down. After installing the gem, I was able to run “gem server” and view the rdoc for the plugin. A couple of things I thought would be helpful to repost here:


Form Helpers:

calendar_date_select(object, method, options={})

Similar to the difference between text_field_tag and text_field, this method behaves like text_field

It receives the same options as calendar_date_select_tag. Need for time selection is automatically detected by checking the corresponding column meta information of Model#columns_hash

calendar_date_select_tag( name, value = nil, options = {})

Similar to text_field_tag, but adds a calendar picker, naturally.
Arguments

  +name+ - the html name of the tag
  +value+ - When specified as a string, uses value verbatim.  When Date, DateTime, Time, it converts it to a string basd off the format set by CalendarDateSelect#format=
  +options+ - ...

Options
:embedded

Put the calendar straight into the form, rather than using a popup type of form.

  <%= calendar_date_select_tag "name", "2007-01-01", :embedded => true %>

:hidden

Use a hidden element instead of a text box for a pop up calendar. Not compatible with :embedded => true. You‘ll probably want to use an onchange callback to do something with the value.

  
  <%= calendar_date_select_tag "hidden_date_selector", "", :hidden => "true", :onchange => "$('cds_value').update($F(this));" %>

:image

Specify an alternative icon to use for the date picker.

To use /images/groovy.png:

  <%= calendar_date_select_tag "altered_image", "", :image => "groovy.png" %>

:minute_interval

Specifies the minute interval used in the hour/minute selector. Default is 5.

  <%= calendar_date_select_tag "month_year_selector_label", "", :minute_interval => 15 %>

:month_year

Customize the month and year selectors at the top of the control.

Valid values:

 * "dropdowns" (default) - Use a separate dropdown control for both the month and year
 * "label" - Use static text to show the month and the year.

   <%= calendar_date_select_tag "month_year_selector_label", "", :month_year => "label" %>

:popup => ‘force‘

Forces the user to use the popup calendar by making it‘s text-box read-only and causing calendar_date_select to override it‘s default behavior of not allowing selection of a date on a target element that is read-only.

  <%= calendar_date_select_tag "name", "2007-01-01", :popup => "force" %>

:time

Show time in the controls. There‘s three options:

 * +true+ - show an hour/minute selector.
 * +false+ - don't show an hour/minute selector.
 * +"mixed"+ - Show an hour/minute selector, but include a "all day" option - allowing them to choose whether or not to specify a time.

:year_range

Limit the year range. You can pass in an array or range of ruby Date/Time objects or FixNum‘s.

  <%= calendar_date_select_tag "e_date", nil, :year_range => 10.years.ago..0.years.from_now %>
  <%= calendar_date_select_tag "e_date", nil, :year_range => [0.years.ago, 10.years.from_now] %>
  <%= calendar_date_select_tag "e_date", nil, :year_range => 2000..2007 %>
  <%= calendar_date_select_tag "e_date", nil, :year_range => [2000, 2007] %>

CALLBACKS

The following callbacks are available:

 * before_show / after_show
 * before_close / after_close
 * after_navigate - Called when navigating to a different month. Passes first parameter as a date object refering to the current month viewed
 * onchange - Called when the form input value changes

  <%= calendar_date_select_tag "event_demo", "",
    :before_show => "log('Calendar Showing');" ,
    :after_show => "log('Calendar Shown');" ,
    :before_close => "log('Calendar closing');" ,
    :after_close => "log('Calendar closed');",
    :after_navigate => "log('Current month is ' + (param.getMonth()+1) + '/' + (param.getFullYear()));",
    :onchange => "log('value changed to - ' + $F(this));"

}}}

All callbacks are executed within the context of the target input element. If you‘d like to access the CalendarDateSelect object itself, you can access it via "this.calendar_date_select".

For example:

  <%= calendar_date_select_tag "event_demo", "", :after_navigate => "alert('The current selected month is ' + this.calendar_date_select.selected_date.getMonth());" ,

For use with Form Builders

calendar_date_select(method, options = {})


Controllers:

In my application, I am using a custom form builder, so I used the calendar_date_elect with the form builder. The options hash appears to be the same as in the other view tags, so I just passed along what I needed:

<%= f.calendar_date_select "date", :embedded => true, :year_range => 1.year.ago..5.years.from_now, :time => false %>

I was pleased to discover that, utilizing some of the magic of Rails, the date property on my event was automatically turned into a datetime attribute, and on editing, was reloaded into the calendar as expected.

Categories: Gems, Ruby On Rails Tags: ,

Working With Server Processes

July 27th, 2009 mindtonic No comments

A simple little entry, I know, but a powerful tool to use and remember!

Display full information about each of the processes currently running.:

ps -ef

Output:
UID PID PPID C STIME TTY TIME CMD
hope 29197 18961 0 Sep27 ? 00:00:06 sshd: hope@pts/87
hope 32097 29197 0 Sep27 pts/87 00:00:00 -csh
hope 7209 32097 0 12:17 pts/87 00:00:00 ps -ef

To see just Ruby processes:

ps -ef | grep ruby

How to Kill Processes for a User

I had a BackgrounDRb process that just wouldn’t give it up, for a month, really! When all else failed, this worked:

kill -9 `ps -u username | grep -v PID | awk '{ printf ("%s ", $1); }'`

Replace username with the actual user name. Of course you should only use the -9 option when you really have to.

The New Way

Instead of going through all of that, you can simply use the pkill command if you already know the process name or part of it.

$ pkill packet_worker_runner

It’s as simple as that. You should note that pkill will kill all processes matching the search text, in this case backgroundrb processes which can be identified by the packet_worker_runner process name.

If you want to see what process names are matched before using the pkill command, you can use the ps command as described above.

$ ps -ef | grep packet_worker_runner

Categories: Development, Unix Tags: ,