Devise: Resource specific Logic in Rails Routes : Examples

If you are willing to implement or learn how to route a particular URL request to specific controller on the basis of which role the currently logged-in user is.

# in routes.rb
authenticate :user, -> (user) { user.stylist? } do
  resources :stylist_profiles, only: [:index, :show], path: '/profile'
end

authenticate :user, -> (user) { user.customer? } do
  resources :customer_profiles, only: [:index, :show], path: '/profile'
end

The URL /profile or /profile/1 will route to StylistProfileController if user’s role is Stylist and will route to CustomerProfileController if user’s role is Customer .


condition = -> (u) {
  ['qa', 'development', 'staging'].include?(Rails.env) || (u.admin? && Rails.env.production?)
}

authenticate :user, condition do
  mount Sidekiq::Web => '/sidekiq'
  mount RedisBrowser::Web => '/redis-browser'
end

Some additional examples

# in config/routes.rb
# Home page is root for UnAuthenticated visitors
#  This shall be at the top
unauthenticated :user do
  root 'pages#home', page: 'home'
end

authenticate :user, ->(user) {user.vehicle_owner? } do
  root 'vehicle_owners/quote_requests#index'
end

authenticate :user, ->(user) {user.garage_owner? } do
  root 'garage_owners/quote_requests#index'
end
Advertisements

RSpec : Conditionals in before/after callbacks

Conditions

When you add a conditions hash to before(:example) or before(:context), RSpec will only apply that hook to groups or examples that match the conditions. e.g.

RSpec.configure do |config|
  config.before(:example, :authorized => true) do
     :authorized_user
  end
end

describe Something, :authorized => true do
  # The before hook will run in before each example in this group.
end

describe SomethingElse do
  it "does something", :authorized => true do
    # The before hook will run before this example.
  end

  it "does something else" do
    # The hook will not run before this example.
  end
end

 

Or

config.before(:each) do |ex|
  if ex.metadata[:admin] == true
    Rails.application.load_seed # loading seeds
  end
end

it "should auto fill the shipping date field to today's date", :admin => true  do
  expect(User.find_by_email('user@example.com')).not_to eq(nil)
end

Sources

http://www.rubydoc.info/github/rspec/rspec-core/RSpec/Core/Hooks#before-instance_method

Things to be considered while using UNLESS / IF in Ruby

If statement is simple and every body has ground knowledge of what and how to use. I guess everybody has knowledge of using Boolean Expression in IF statement.

However the tricky part is, we need to think differently while writing Boolean Expression in UNLESS statement.

Example;

if x == 1 && y == 2
  // Do Something when conditions meet
end

If you want to do same job when conditions do not meet at all using UNLESS

unless x == 1 && y == 2
  // Do Something when conditions meet
end

This is WRONG!!

Right example:

unless x == 1 || y == 2
  // Do Something when conditions meet
end

Think the Boolean Algebra

In electrical and computer engineering, De Morgan’s laws are commonly written as:

\overline{A \cdot B} \equiv \overline {A} + \overline {B}

and

\overline{A + B} \equiv \overline {A} \cdot \overline {B},

where:

  •  \cdot is a logical AND
  • + is a logical OR
  • the overbar is the logical NOT of what is underneath the overbar.