From 04e3cf0250bf2538d2a36060fe5f5de28052cbdc Mon Sep 17 00:00:00 2001 From: Justin Coyne Date: Sat, 5 Oct 2024 17:55:09 -0500 Subject: [PATCH] Consolidate generated additions to the User class (#3341) Give the string_display_key a default value --- app/models/concerns/blacklight/user.rb | 2 +- lib/generators/blacklight/user_generator.rb | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/app/models/concerns/blacklight/user.rb b/app/models/concerns/blacklight/user.rb index 0a24923ef..15cef6199 100644 --- a/app/models/concerns/blacklight/user.rb +++ b/app/models/concerns/blacklight/user.rb @@ -5,7 +5,7 @@ module Blacklight::User # SEE ALSO: The lib/blacklight/generator/user_generator.rb class for where this # is generated into the hosting application. included do - class_attribute :string_display_key + class_attribute :string_display_key, default: :email has_many :bookmarks, dependent: :destroy, as: :user has_many :searches, dependent: :destroy, as: :user diff --git a/lib/generators/blacklight/user_generator.rb b/lib/generators/blacklight/user_generator.rb index 7faf3e7b0..8d4421501 100644 --- a/lib/generators/blacklight/user_generator.rb +++ b/lib/generators/blacklight/user_generator.rb @@ -34,13 +34,6 @@ def generate_devise_assets generate "devise", model_name.classify generate "devise_guests", model_name.classify - # add the #to_s to the model. - insert_into_file("app/models/#{model_name}.rb", before: /end(\n| )*$/) do - "\n # Configuration added by Blacklight; Blacklight::User uses a method key on your\n" \ - " # user class to get a user-displayable login/identifier for\n" \ - " # the account.\n" \ - " self.string_display_key ||= :email\n" - end gsub_file("config/initializers/devise.rb", "config.sign_out_via = :delete", "config.sign_out_via = :get") end @@ -49,13 +42,19 @@ def inject_blacklight_user_behavior file_path = "app/models/#{model_name.underscore}.rb" if File.exist?(File.expand_path(file_path, destination_root)) inject_into_class file_path, model_name.classify do - "\n # Connects this user object to Blacklights Bookmarks." \ - "\n include Blacklight::User\n" + <<~EOS + # Connects this user object to Blacklights Bookmarks. + include Blacklight::User + + # Blacklight::User uses a method on your User class to get a user-displayable + # label (e.g. login or identifier) for the account. Blacklight uses `email' by default. + # self.string_display_key = :email + EOS end else say_status "warning", <<~EOS, :yellow Blacklight authenticated user functionality not installed, as a user model - could not be found at /app/models/user.rb. If you used a different name, + could not be found at #{file_path}. If you used a different name, please re-run the migration and provide that name as an argument. E.g.: `rails -g blacklight:user client`