Class: Blacklight::UserGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/blacklight/user_generator.rb

Instance Method Summary collapse

Instance Method Details

#generate_devise_assetsObject

Install Devise?



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/generators/blacklight/user_generator.rb', line 19

def generate_devise_assets
  return unless options[:devise]

  gem "devise"
  gem "devise-guests", "~> 0.6"

  Bundler.with_clean_env do
    run "bundle install"
  end

  generate "devise:install"
  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  # Method added by Blacklight; Blacklight uses #to_s on your\n" \
    "  # user class to get a user-displayable login/identifier for\n" \
    "  # the account.\n" \
    "  def to_s\n" \
    "    email\n" \
    "  end\n"
  end
  gsub_file("config/initializers/devise.rb", "config.sign_out_via = :delete", "config.sign_out_via = :get")
end

#inject_blacklight_user_behaviorObject

Add Blacklight to the user model



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/generators/blacklight/user_generator.rb', line 46

def inject_blacklight_user_behavior
  file_path = "app/models/#{model_name.underscore}.rb"
  if File.exist?(file_path)
    inject_into_class file_path, model_name.classify do
      "\n  if Blacklight::Utils.needs_attr_accessible?" \
      "\n    attr_accessible :email, :password, :password_confirmation" \
      "\n  end" \
      "\n  # Connects this user object to Blacklights Bookmarks." \
      "\n  include Blacklight::User\n"
    end
  else
    say_status "warning", <<-EOS.strip_heredoc, :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, 
      please re-run the migration and provide that name as an argument. E.g.:

        `rails -g blacklight:user client`
    EOS
  end
end