Posts

Showing posts from November, 2014

Ruby conditions + multiple ORs

Code refactoring for multiple OR conditions, Here is an example of weird looking conditional statement if name.blank? || surname.blank? || nickname.blank? || aka.blank? ... end Lets try and refactor this Option 1  if [name, surname, nickname, aka].any? { |field| field.blank? } ... end Option 2 if [name, surname, nickname, aka].any?(&:blank?) ... end

Configure error libltdl while installing imagemagick

Image
Install ImageMagick Error Solution sudo apt-get install libltdl-dev

Rails Helpers and default behavior

By Default all helpers are included in all views in Rails. So your UsersHelper methods are available in views of other controller lets say your ProductsController views. So if you wanted to change the default behavior, You need to make a small change in inside config/application.rb config.action_controller.include_all_helpers = false ApplicationHelper is still included in all views. ProductsHelper is only included in ProductsHelper views. If that controller inherits from BaseController, then BaseHelper will be included as well. And here’s is one more trick that you can use For example, We like to have a LayoutHelper that the application layout template can use: app/helpers/layout_helper.rb module LayoutHelper def layout_helper_method "Some layout logic" end end app/helpers/application_helper.rb module ApplicationHelper include LayoutHelper def global_helper_method "Some application logic" end end With include_all_helpers off, t

Define method dynamically in Rails

We are going to create two types of methods Class method and Instance method Lets take an example of class method class User < ActiveRecord::Base define_singleton_method method_name do .. end end define_singleton_method method will create class method, you can use this code in loop to create multiple methods. And you can call this method with class name and method_name so in this case User.method_name Now lets look at instance method class User < ActiveRecord::Base define_method method_name do .. end end   define_method method will create an instance method. And you can call this method with instance of class(object) and method_name so in this case User.first.method_name

Git diff and Patch file

A patch file contains a set of changes (i.e., a commit) that can be applied to any branch, in any order. You can convert git commits and differences into patch files. Those can be used to apply to a different repository or by someone else (e.g. sent via e-mail). git diff > changes.patch ">" this sign writes STDOUT to a file This basically creates a new file named "changes.patch" in your application root folder. To apply a patch file you need to use git apply changes.patch

Rails Cache on Development Environment

Set perform caching to true inside config/environments/development.rb, you need to restart the server config.action_controller.perform_caching = true   After that install memcached as a gem gem install memcached Add sudo if you are using system ruby, You can also use bundler If you are getting this error "DalliError: No server available" Then you need to install memcached sudo apt-get install memcached

Improve ruby code using Code Analyzer

Rubocop is a code analyzer gem which allows you to write code that follows the style guide dictated by the Ruby community, The gem also has lots of configuration options Let's install Rubocop gem install rubocop cd your_rails_app   rubocop This will scan all of your Ruby files in your application. rubocop app This will scan only the app directory. rubocop -o code.rb -o writes output to a file instead of STDOUT