Brunch, the Static site generator

In most web apps, there are two types of pages. One are those just for visitors who are not yet our customers and other belongs to APP which provides the core functionality of the service.

In most scenarios all these non service based content pages are combined into app, hence overloading the app with irrelevant code resulting in slow response time and necessity to update and deploy the app for minor text changes. Continue reading “Brunch, the Static site generator”

Reading & Writing DBF files in ruby

DBFs are a set of database file formats similar to sqlite, except that each file will have only one table. The support for reading & writing dbase file formats in ruby is pretty rudimentary.

The following are the gems that can be used to read & write dbase files.

1. SHP

The file format it supports is xbase. It supports creation of columns of type string, integer and float only. The type codes for columns types are
0 -> string, 1 -> integer and 2 -> float respectively. Even though date is a valid xbase column type, it is not supported. Simple usage is

Add

to you Gemfile.

Then the code to read the data from dbase is

While reading is kind of straight forward, writing to the dbf is a complicated affair. You have to create the file first then add field specifications and then add data.

2. DBF
This gem can only be used for reading from dbf files. One cannot use it to create dbf files.
To use as usual add to you Gemfile

and

3. Rbase

Rbase lacks documentation. But if you are used to wading through code in the wild, you will quickly get a hang of it and start writing & reading dbase files using this. It definitely provides a rich interface to reading
To read from a dbase file.

Sticky Footer Bar using jquery when scrolling

The zurb foundation has a sticky top bar. But they don’t have a sticky footer / lower bar. This is an attempt to implement the sticky footer bar similar to that found in cnn.com

Let the footer id be sticky-footer. When we scroll up we check for the current position of the scroll of the window and add the sticky css class to the footer. When the scroll reaches the original postion of the footer we restore the original positioning.
The css required for fixed footer is

The javascript that adds and removes the fixed class is

JSfiddle demo

Fetching value of hidden input field in capybara

There can be many instances in testing with capybara where you might need to interact with hidden fields. By default capybara’s find_field selector ignores hidden fields.
To get the value of the hidden field you can use page.all

To set the value of the hidden field you can use

Reading from and writing to files in node.js

NodeJS is an awesome platform to build applications in javascript which can run on the server.
It allows you to read n write to files asynchronously and synchronously

Writing Data to files

The easiest way would be write synchronously (i.e. wait till the writing operating is finished)

But the preferable way of writing to files will be asynchronous in context of web apps.

In this case the program will continue to execute next instruction without waiting for the file write to complete. Once the file writing is completed, the third parameter (closure ) is executed. Above method overwrite the current file. So if you want to append the data, use Continue reading “Reading from and writing to files in node.js”

Customizing Mailboxer mailer views

To copy the mailboxer mailer views to your rails app run

This will generate two folders, message_mailer and notification_mailer with html & text views for messages, notification and message replies respectively.

Next create a modify the initializer of mailboxer to include the following config

Add the following method to your messageable model

To change the title of you mail messages

open config/locales/en.yml and you will see the following line belonging to each mail you can the below line to suit to your needs

Adding TinyMce editor to Rails Admin

RailsAdmin is a Rails engine that provides an easy-to-use interface for managing your data. Rails admin is like bootstrap admin interface for your web app / site. To add tinymce editor to all textareas in rails_admin first add the tinymce-rails gem to your Gemfile and run bundle install.

Gemfile

Now got to app/assests/javascripts/rails_admin folder and include the tinymce javascript libraries to rails_admin.js.erb file like this

Now create a javascript file called tinymce.js in app/assets/javascripts/rails_admin folder and add the following code to invoke tinymce on all textareas

A note on pjax line. RailsAdmin uses pjax for better user experience so we have to trigger the tinymce_load function when ever pjax is called so that the new textareas are also tinymced!

then include this file in rails_admin.js.erb file like

Now restart your server and you have tinymce loading on all textareas. For more customization options of tinymce please refer their website.

 

A treatise on mailboxer ruby gem

Mailbox Gem

Mailboxer gem adds messaging capability to your models.By far of all messaging gems we have found this one easy to implement / add to your models with good documentation.

Mailboxer Installation

In you rails application add

to your Gemfile and run bundle install to install it.

Now go to rails application directory and run to generate the necessary migrations and then migrate the changes to your database

Configuring Maiboxer

In rails you can create an intializer called mailboxer.rb in folder /config/intializer and modify the default behaviour of mailboxer. An example intializer. In our setup we are not interested in user emails. so we have just disabled it. Mailboxer automatically generates a initializer for you can go and change the default settings.

Preparing the Model

In your user model add acts_as_messageable and implement the method name as shown below

Basic Messaging

In mailboxer when an user send message to another user, a conversation is started. So there will be a notification is created with the content of the message and a receipt of notification is add to sent box of the sending user and a receipt of notification is added to the inbox of receiving user.

mailboxer flow (1)

To send a message to user2 from user1

To send message to multiple users use (just pass and array of recipients to send_message function.

To reply to the message

Retrieve all messages of the user

To read the contents of a conversation user get the receipts of the message for him and reads the messages. The following snippet gets the messages in a conversation in reverse chronological order.

Misc stuff

To change the views of email messages

the views are added to your app/views folders so you can edit them.

Thats all for now folks. If you need more info just post in the comments i will try to answer

Update: Follow up tutorial on how to customize mailboxer views

References

  1. https://github.com/ging/mailboxer
  2. http://www.rubydoc.info/github/ging/mailboxer/frames

Calling View helper methods from Controller / Model in Rails 3.* and Rails 2.*

To access view helper methods from controller / models in rails 3 you have to use view_context method. Example

In rails 2 you have to user @template instance variable as shown below

Just a note that you should avoid calling these functions within the controllers to follow mvc pattern conventions.

How to Install Php Extension

The easiest way to install php extension is using pecl.

To install you have to run

But if you have the sources of the php extension then.

if you have php installed in another location. Lets assume php is installed in /opt/php
To install the extension you have to do is first download the extension and then unzip it and run the phpize and make and make install. Example commands to run

Then to enable you have to add in your php.ini file