[Tips] Limit Class Only Be Used in Development Environment

In my project, I have to dump data from Mysql to MongoDB. And, this feature only used in development. However, Rails will load any classes under folder “app/” automatically . So, how can I avoid “Development-Only Class” loading in production environment?

let’s check it out~

If you have a class named: DumpDataFromOldServer and it located at folder ‘/app/development_only’. like this:

1
2
3
4
5
# app/development_only/dump_data_from_old_server.rb

class DumpDataFromOldServer < ActiveRecord::Base
    ....
end

Step1. Application.rb

you still need to let Rails know which folders should be loaded by using config.eager_load_paths.

ps. what’s difference between eager_load and auto_load?

please check this http://stackoverflow.com/questions/19773266/confusing-about-autoload-paths-vs-eager-load-paths-in-rails-4

1
 config.eager_load_paths += %W(#{Rails.root}/app/development_only)

It will be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# config/application.rb

require File.expand_path('../boot', __FILE__)

Bundler.require(*Rails.groups)

module MyAwesomeProject
  class Application < Rails::Application
   ...
   
   config.eager_load_paths += %W(#{Rails.root}/app/development_only)
   
   ...
   end 
end

Step2. Update your environment file

ref: http://stackoverflow.com/questions/13756986/how-to-blacklist-directory-loading-in-rails

Add these lines

1
2
3
4
5
6
7
  path_rejector = lambda { |s| s.include?("app/development_only") }

  # Remove the path from being loaded when Rails starts:
  config.eager_load_paths = config.eager_load_paths.reject(&path_rejector)

  # Remove the path from being lazily loaded
  ActiveSupport::Dependencies.autoload_paths.reject!(&path_rejector)

It will be:

1
2
3
4
5
6
7
8
9
10
11
# config/environments/production.rb
Rails.application.configure do
  
  ...

  path_rejector = lambda { |s| s.include?("app/development_only") }

  config.eager_load_paths = config.eager_load_paths.reject(&path_rejector)

  ActiveSupport::Dependencies.autoload_paths.reject!(&path_rejector)
end

Step3. Test

1
2
3
4
5
6
7
8
9
10
11
# In Development console: rails c 

> DumpDataFromOldServer.all.first 

DumpDataFromOldServer Load (0.6ms)  SELECT  `dump_data_from_old_server`.* FROM `dump_data_from_old_server `  LIMIT 1
=> #<DumpDataFromOldServer id: 1, created_at: "2014-05-09 16:59:55">


# In Production console: rails c -e production
> DumpDataFromOldServer.all.first
NameError: uninitialized constant DumpDataFromOldServer
Comments

Build Up a Homemade Server-to-server Interactions With Google API Using Ruby - Take Google Analytics as an Example

Since ‘google-api-ruby-client’ make a big change from 0.8 to 0.9 when my project is running out of time, and also I met this issue, So, I make this decision, let’s build a API client by myself.

This implementation refer to Google guideline

Read on →

For Dummies Stress Test Using JMeter in Rails 4 + Ruby 2.2

jmeter generated by ruby-jemer

Read on →
Comments

HowTo Performance Test in Rails 4

Inspired by

  1. PERFORMANCE TESTING RAILS AGAINST REAL DATA

  2. Performance Testing

  3. http://railscasts.com/episodes/368-miniprofiler

Read on →
Comments

Sign Up by Devise and Crop User Avatar Then Upload by Carrierwave Using Ruby on Rails

ref:

  1. http://railscasts.com/episodes/182-cropping-images-revised
  2. http://stackoverflow.com/questions/12762728/how-to-crop-image-on-upload-with-rails-carrierwave-and-minimagick
  3. https://coderwall.com/p/e9d_ja/using-carrierwave-uploader-for-tableless-model-in-rails
  4. http://stackoverflow.com/questions/24262388/carrierwave-processing-only-after-the-model-has-been-saved-model-id-is-nil

What I done in this article

1. Click Upload Image

Step 1. click_upload_image

2. Choose Image And Preview

Step 2. choose image and preview

3. Edit Avatar And Preview Image Synchronously

Step 3. Edit Avatar And Preview Image Synchronously

4. Upload to AWS S3

Step 4. Upload to AWS S3

5. Image Cropped And Uploaded

Step 5. Image Cropped And Uploaded

Read on →
Comments

String Handling Between JavaScript and Ruby

javascript 在做 escape 時,編碼出來的字,和Ruby的 CGI.escapeURI.escape 是不同的!

舉例來說:

browser console下

escape("台中市")
> "%u53F0%u4E2D%u5E02"

但是在rails c

CGI.escape("台中市")
> "%E5%8F%B0%E4%B8%AD%E5%B8%82"

Why?

Read on →
Comments
Google Analytics Alternative