Working with Rails Time-zone

How to work with time-zone in rails Time-zone leaks: This is an issue where time-zone of one request is passed to another request. Ex Scenario: Lets say we have web-server running on a single dyno and we have three different users with different timezones('Mumbai', 'Central America' and 'London'). Here is my code setup that will produce time-zone leaks, class UserController def method_one_with_time_zone puts Time.zone #before_filter sets the time zone for this method execution. end def method_two_without_time_zone puts Time.zone #This method doesn't call any before filter, So it will

apipie-rails gem: best documentation tool

Apipie-rails is a DSL and Rails engine for documenting you RESTful API. Instead of traditional use of #comments, Apipie lets you "describe the code by code". This brings advantages like: It uses ruby syntax Reusable doc for other purposes (Best for validations) Easier to extend and maintain apipie-rails gem owner has documented it in very nice way with examples. Refer [Github Documentaion(github doc)][1] Path to documentation: The documentation is available right in your app (by default under "/apipie" path.) Also you can provide your own path using initializers. For more information refer [here(config

Customizing Devise authentication, to disable or enable user authentication.

If we need to support additional parameters for devise authentication besides normal(username/email with password). We can do this in devise by overiding an existing method, 'active_for_authentication?'. After authenticating a user and in each request, Devise checks if your model is active by calling model.active_for_authentication? Consider a feature 'Lock sub user', here we can add a boolean flag 'is_locked', based on this flag we enable or disable the authentication for the sub-user: def active_for_authentication? super && !is_locked end Instead of !is_locked, we can also add a custom method which:

How to Introduce BatmanJS to existing Rails application

While evaluating [BatmanJS(BatmanJS)][1] for one of the biggest application here @Kiprosh, I found it very much Rails like and started drawing parallels when I was doing MVP with Rails and BatmanJS. Note: This is not a tutorial for [getting started(Getting Started)][2] or [building rails app with BatmanJS(Another getting started)][3] . I personally like [Zack hubert's blog(Zack hubert's blog)][4] and [Batman for SuperHeroes(Batman for super heroes)][5] If you are book lover, you will certainly like [cookbook(Cookbook for BatmanJS)][6] I am sharing my experience on possible approaches/concepts to move view-layer

PJAX Related performance improvements

PJAX Related performance improvements To know about PJAX [(Click Here)(Pjax Railscast)][1] Post from Kavita: [How to PJAX][2] Pjax is a great solution when we want to do partial refreshing of page at the same time maintaining the history of user navigation, But it has few issues when the app grows Basically PJAX works like this, Issuing a PJAX request to refresh the section of the page( eg: #conainer). Basically it fires an html ajax. Rails server handles the html request and renders the complete html page. These html template is parsed by the middleware, which removes the

Example usage of Redis - LUA script in Rails

This topic is about using Redis and LUA script in Rails. Before proceeding, you may like to read [ Introduction to LUA script with Redis][1] Redis doesn't have any command to calculate total number of keys We'll use [zrangebyscore][2] to find number of members between index range. Instal [redis-rb][3] Start rails console Prepare a dataset based on timestamp > t10 = 10.minutes.ago => 1386935151 > t20 = 20.minutes.ago => 1386934568 > t30 = 30.minutes.ago => 1386933971 > t40 = 40.minutes.ago => 1386933374 This is non-realistic data, may vary for you. Populate redis with Sorted Set > redis = Redis.new # initialize redis => # zadd -

send email attachments via postmark-api

Previously, I have added post on send email from application via postmark. [Check Here..][1] Now will see how to send attachment with email. In rails, action-mailer provides an easy way to add attachment file as attachments['filename.jpg'] = File.read('/path/to/filename.jpg') Links: [rails-guide-on-attchments(send attachments with email)][2] , [rails-attachments][3] Sending attachments via Postmark-api require "open-uri" def send_refer_pt_request(params, attachment) # attachment -> url of file saved on S3 if attachment.present? mime_type = MIME::Types.type_for(attachment).first url_data = open(attachment).read() attachments["#{attachment.split('/').last}"] = { mime_type:

Query String in rails

Most of the times we look up to pass extra params in url. This post will give an overview of possible query string formation but in rails way. For this, we know the common syntax for query string is "/controller/action?name='A'&id=1", where multiple params are separated by '&' literal. Here are the common rails ways: to_param => Returns a String, which Action Pack uses for constructing an URL to this object. i) When operated over hash, it will assignment string. For say, => {a: 1}.to_param => "a=1" ii) When

Reload or Add routes at runtime in Rails 3

In one of our eCommerce app, we needed SEO friendly Menu urls like /brandy/cognac. For this, we added a generic route /:parent/:child/:grandchild at the bottom of config/routes.rb. With this approach, there was an issue with invalid urls like /this/is/wrong being redirected to specified controller action. To fix this, we used Menu names to generate specific urls. But the routes would become invalid if Admin changes the menu name. So we needed a way to reload routes once the menu is updated. I found a hack mentioned in [this blog(Blog)][1] that allow

Alternative way to highlight the current active page

This post is very much similar to the post of Abhijit. [Highlight the active page (Highight the active page)][1] by Abhijit. This would work very well if we have simpler routes, if we have complex routes(Modular routes) in our app where we have similar urls with same action then we need to enhance this approach. For e.g There are three links on the same page as follows. 'web/dashboard' 'mweb/dashboard' 'mapp/dashboard' All these are pointing to same action. i.e Dashboard. So selecting active tab based on action name will fail here and will highlight