-
Notifications
You must be signed in to change notification settings - Fork 253
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract blacklight generators into more granular generator
- Loading branch information
Showing
8 changed files
with
139 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
module Blacklight | ||
class ControllerGenerator < Rails::Generators::Base | ||
|
||
source_root File.expand_path('../templates', __FILE__) | ||
|
||
argument :controller_name , type: :string , default: "catalog" | ||
|
||
desc """ | ||
This generator makes the following changes to your application: | ||
1. Injects behavior into your user application_controller.rb | ||
2. Creates a Blacklight::Catalog-based controller | ||
3. Adds routes for your controller | ||
Thank you for Installing Blacklight. | ||
""" | ||
|
||
# Add Blacklight to the application controller | ||
def inject_blacklight_controller_behavior | ||
inject_into_class "app/controllers/application_controller.rb", "ApplicationController" do | ||
" # Adds a few additional behaviors into the application controller \n" + | ||
" include Blacklight::Controller\n" + | ||
" layout 'blacklight'\n\n" | ||
end | ||
end | ||
|
||
# Generate blacklight catalog controller | ||
def create_blacklight_catalog | ||
template "catalog_controller.rb", "app/controllers/#{controller_name}_controller.rb" | ||
end | ||
|
||
def inject_blacklight_routes | ||
# These will end up in routes.rb file in reverse order | ||
# we add em, since each is added at the top of file. | ||
# we want "root" to be FIRST for optimal url generation. | ||
route("blacklight_for :#{controller_name}") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
require 'rails/generators' | ||
|
||
module Blacklight | ||
class DocumentGenerator < Rails::Generators::Base | ||
include Rails::Generators::Migration | ||
|
||
source_root File.expand_path('../templates', __FILE__) | ||
|
||
argument :model_name, :type => :string , :default => "solr_document" | ||
|
||
desc """ | ||
This generator makes the following changes to your application: | ||
1. Creates a blacklight document in your /app/models directory | ||
""" | ||
def create_solr_document | ||
template "solr_document.rb", "app/models/#{model_name}.rb" | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
require 'rails/generators' | ||
require 'rails/generators/migration' | ||
|
||
module Blacklight | ||
class UsersGenerator < Rails::Generators::Base | ||
include Rails::Generators::Migration | ||
|
||
source_root File.expand_path('../templates', __FILE__) | ||
|
||
argument :model_name, :type => :string , :default => "user" | ||
class_option :devise , :type => :boolean, :default => false, :aliases => "-d", :desc => "Use Devise as authentication logic (this is default)." | ||
|
||
desc """ | ||
This generator makes the following changes to your application: | ||
1. Creates a devise-based user model | ||
2. Injects blacklight-specific behavior into your user model | ||
""" | ||
# Install Devise? | ||
def generate_devise_assets | ||
if options[:devise] | ||
gem "devise" | ||
gem "devise-guests", "~> 0.3" | ||
|
||
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 | ||
end | ||
|
||
# Add Blacklight to the user model | ||
def inject_blacklight_user_behavior | ||
file_path = "app/models/#{model_name.underscore}.rb" | ||
if File.exists?(file_path) | ||
inject_into_class file_path, model_name.classify do | ||
"\n attr_accessible :email, :password, :password_confirmation if Rails::VERSION::MAJOR < 4\n" + | ||
"# Connects this user object to Blacklights Bookmarks. " + | ||
"\n include Blacklight::User\n" | ||
end | ||
else | ||
say_status("warning", "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. Such as `rails -g blacklight client`", :yellow) | ||
end | ||
end | ||
end | ||
end |