MooTools: Class Methods and Inheritance

Posted by anup.narkhede on July 12, 2010

In my last project, I enjoyed scripting using mootools and started exploring it more deeply. One of my extensive use was to define UI elements’ functionality in classes and then create multiple instances for re-usability.

Mootools makes use of Javascript’s native prototype model for inheritance. To extend a base functionality into sub class, mootools provide ‘Extends’ method. However, if you have class methods in your base model, Extends is insufficient to get them into the sub classes. After some research, I managed to solve this by using Class Mutators and overriding the Mootools ‘Extends’ to take care of class methods inheritance.

First of all, we need to define a ClassMethods mutator. Mutators belong to a class, not to their instances and they are called only when we define a new Class.

Next, we need to redefine Mootools Extends mutator:

Check out the example below:

You can see the above code working here.
http://jsfiddle.net/rZvJE/11/

Daily Blockers #2

Posted by anup.narkhede on March 02, 2010

Problem:

Solution:
Somehow my activesupport-2.3.5/lib/active_support/values/unicode_tables.dat was corrupted. Reinstalling activesupport gem fixed the issue.

Rails 3.0: Mount Multiple Apps as Engines

Posted by anup.narkhede on February 21, 2010

Since last week I have been working on upgrading a major project from Rails 2.3.5 to Rails 3.0.0.beta and also on moving other projects to use the latest version of bundler. Getting all to work was not easy, but this gave me a chance to look into the rails initialization code in detail. Based on this adventure, I tried to convert some plugins into gems and also explored ways of mounting a Rails 3.0 application into another. Here is a very basic example of how you can mount a reusable Rails 3.0 application into a container application. This might not be the best approach for developing multi apps, but it surely offers some fun :).

My setup is Rails 3.0.0.beta with ruby 1.9.1 for this example. If the steps sound confusing, you can follow the example code here http://github.com/railsbob/MyApp.

First of all, lets create a generic, reusable application. My reusable ‘login application’ with one controller is called as ‘Saas’.

To allow sharing of this application by others, we need to wrap it as a Rails::Engine. To do so, add a file named ‘engine.rb’ to config folder.

Create a ’saas.rb’ in lib folder which requires this engine.rb. This is required, as we want to load the application as Saas::Engine instead of Saas::Application.

Lets create a container app which mounts the Saas Application.

To mount it, copy the Saas application into lib of MyApp.

Saas application’s routes.rb should be modified to replace Saas::Application by Rails::Application. Also, to keep things modular, add a namespace to the routes.

The next step is to hook Saas app into the initializer process of MyApp. There are many ways to do it, but we will follow the bundler approach.

In MyApp/Gemfile, require the saas application from lib directory as a gem.

At this stage, bundler complains about missing version for saas as it is expecting a gem. So lets add a saas.gemspec at MyApp/lib/saas/saas.gemspec with basic information.

Start the server from the container app and if you go to /saas/sessions you should see the text ‘Hi from SessionsController’.

Note that even if the Saas application was modified to be an engine, it still works as a standalone application when run from its root.
The example code discussed above is available at http://github.com/railsbob/MyApp.
.

LRUG (Feb 2010)

Posted by anup.narkhede on February 11, 2010

These are the slides of my presentation at LRUG lightening talks 2010. The talk was about a brief introduction of birdpie.com.

Continuous Authority and repeated transactions using SagePay Gateway

Posted by anup.narkhede on February 04, 2010

SagePay allows us to process repeated capture transactions against a successful payment authorization. You can get more information about the introduction and instructions for obtaining a Continuous Authority Internet Merchant Number here.

This example makes use of a forked version of ActiveMerchant and SagePay Simulator environment. Please refer to the previous article for setup instructions.

Setup:

Make sure to check ‘Continuous Authority’ in the simulator account settings.

Case 1: A non 3D Authorized Transaction

response.authorization shows a value simiar to:
00001;{E5AC4385-06F8-40DD-8486-22EFB23768AE};9030;03FQG5AJA6;authorization

Capture the Payment

Repeat the transaction using previous authorization reference.

Case 2: 3D Authorized transaction

Follow the steps 1-3 for 3D Secure Payment Transaction as shown in the previous article. You’ll notice that the authorization returned by a three_d_complete process is incomplete and it looks something like “;{FB448BBF-CB72-414A-B293-316004162EEB};6598;OUWEBUCWL3;three_d_complete”
Prepend the order_id used in step 1 to this authorization reference so that it matches following format:

three_d_auth_reference = “00003;{FB448BBF-CB72-414A-B293-316004162EEB};6598;OUWEBUCWL3;three_d_complete”

Where “00003″ is the order_id used for authorization in step 1.

Capture this 3D Authorized payment for the first time by calling:

This transaction can be repeated by using the previous successful authorization (three_d_auth_reference) with a new order_id.