Skip to content

Instantly share code, notes, and snippets.

@radar
Last active April 12, 2021 00:38
  • Star 5 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save radar/6145622 to your computer and use it in GitHub Desktop.

Rails 4 Differences

Chapter 1

No differences.

Chapter 2

  • root :to => "dashboard#index" is now root "dashboard#index"`
  • Need to permit parameters within Subscribem::AccountsController
	def create
	  account = Account.create(account_params)
	  flash[:success] = "Your account has been successfully created."
	  redirect_to subscribem.root_url
	end

  private

  def account_params
    params(:require).permit(:name)
  end
  • Same goes for the UsersController:
def create
  account = Subscribem::Account.find_by_subdomain!(request.subdomain)
  user = account.users.create(user_params)
  force_authentication!(account, user)
  flash[:success] = "You have signed up successfully."
  redirect_to root_path
end

private
  def user_params
    params.require(:user).permit(:email, :password, :password_confirmation)
  end
  • Removed attr_accessible calls from all the models.
  • bundle install --binstubs overrides default bin directory. Do not run this command. Bad idea.
  • Can no longer duplicate named routes in Rails 4 like this:
constraints(Subscribem::Constraints::SubdomainRequired) do
  scope :module => "account" do
    root :to => "dashboard#index"
  end
end

Rails tells us: "Invalid route name, already in use: 'root'". Instead, we must do this:

constraints(Subscribem::Constraints::SubdomainRequired) do scope :module => "account" do root :to => "dashboard#index", :as => :account_root end end

See rails/rails#9704 for more information. /hat-tip Richard Schneeman

  • No longer need to remove require_dependency "subscribem/account/application_controller" from the top of controllers generated inside a scope for an engine. This is now correctly referenced as subscribem/application_controller.

Chapter 3

  • Had to move Warden initialization code:
Rails.application.config.middleware.use Warden::Manager do |manager|
  manager.default_strategies :password
end

Out of Subscribem::Engine and into config/initializer/warden.rb. This is because Rails.application is now no longer available within engine class definitions. An initializer makes more sense in this case, because we're initializing set up for the engine.

  • Needed to change scoped_to_account association definition from this:
Subscribem::Account.has_many association_name, :class_name => self.to_s

To use to_sym on the name, like this:

Subscribem::Account.has_many association_name.to_sym, :class_name => self.to_s

This is because Rails 4 no longer supports associations defined with a String.

Chapter 4

  • No changes required

Chapter 5

  • Strong parameters necessary in Account::AccountsController now.

  • Adding middleware to the stack in spec/spec_helper.rb is no longer possible, as the stack is frozen after initialization. Instead, middleware are now added to the stack using initializer blocksinsidelib/subscribem/engine.rb`, like this:

initializer 'subscribem.middleware.fake_braintree_redirect' do
  if Rails.env.test?
    require 'fake_braintree_redirect'
    Rails.application.config.middleware.insert_before \
      Apartment::Elevators::Subdomain,
      FakeBraintreeRedirect
  end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment