Tag: gem

Ruby on Rails: RailsAdmin + HTTP Basic authentication

RailsAdmin is an awesome gem when you want to build a not-so-much custom admin panel. Unfortunately it's not mentioned in documentation (or at least I couldn't find it) how to use it with HTTP Basic authentication. Here is an example of how to do this (put this in your config/initializers/rails_admin.rb file):

RailsAdmin.config do |config|
  config.authenticate_with do
    authenticate_or_request_with_http_basic do |username, password|
      username == 'user' &&
      password == 'password'
    end
  end

  # Other config stuff should go here
end

You should also use gem like SettingsLogic for storing credentials like this:

RailsAdmin.config do |config|
  config.authenticate_with do
    authenticate_or_request_with_http_basic do |username, password|
      username == System::Settings.admin.username &&
      password == System::Settings.admin.password
    end
  end

  # Other config stuff should go here
end

FriendlyId Ruby on Rails gem – update (refresh) slug after each record change

If you want to have slugs that are refreshed (updated) each time you change corresponding field in your ActiveRecord models, put this at the end of your initializers/friendly_id.rb file:

module FriendlyId
  module Slugged
    def should_generate_new_friendly_id?
      return true if send(friendly_id_config.slug_column).nil? && !send(friendly_id_config.base).nil?

      change = :"#{friendly_id_config.base}_changed?"
      return true if respond_to?(change) && send(change)

      false
    end
  end
end

This will update slug each time related column (friendly_id_config.base) has changed. Keep in mind, that this method won't work if you use non-column slug method as a base for friendly ids, since it uses ActiveRecord::Dirty interface.

Copyright © 2024 Closer to Code

Theme by Anders NorenUp ↑