Skip to content

Instantly share code, notes, and snippets.

@huacnlee
Last active November 18, 2015 02:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huacnlee/8b3f03882ab196f2288c to your computer and use it in GitHub Desktop.
Save huacnlee/8b3f03882ab196f2288c to your computer and use it in GitHub Desktop.
Extend Rails Application without change original files
# config/initializes/0_custom_extends.rb
# 让 Rails Autoload Path 载入 lib/custom 里面的内容
# 让 lib/custom/uploaders 里面的东西优先级更高
ActiveSupport::Dependencies.autoload_paths.delete("#{Rails.root}/app/uploaders")
ActiveSupport::Dependencies.autoload_paths.delete("#{Rails.root}/app/models")
ActiveSupport::Dependencies.autoload_paths += %W(
#{Rails.root}/lib/custom/models
#{Rails.root}/lib/custom/models/hooks
#{Rails.root}/lib/custom/models/concerns
#{Rails.root}/app/models
#{Rails.root}/lib/custom/controllers
#{Rails.root}/lib/custom/controllers/concerns
#{Rails.root}/lib/custom/services
#{Rails.root}/lib/custom/workers
#{Rails.root}/lib/custom/uploaders
#{Rails.root}/lib/custom/helpers
#{Rails.root}/lib/custom
#{Rails.root}/app/uploaders
)
# === Extend Routes ===
# Prepend routes, add more route
Rails.application.routes.prepend do
get 'foo' => "you_new_controller#foo"
end
# lib/custom/models/user.rb
require_dependency Rails.root.join('app/models/user')
puts "==== Init Custom Features ==="
# === Extend Models ===
User.send(:include, UserExt)
# === Controller ===
ApplicationController.send(:include, ApplicationControllerExt)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment