Skip to content

Commit

Permalink
Extract blacklight generators into more granular generator
Browse files Browse the repository at this point in the history
  • Loading branch information
cbeer committed Feb 19, 2015
1 parent da1dd79 commit c423ad4
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 92 deletions.
38 changes: 38 additions & 0 deletions lib/generators/blacklight/controller_generator.rb
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
20 changes: 20 additions & 0 deletions lib/generators/blacklight/document_generator.rb
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
44 changes: 19 additions & 25 deletions lib/generators/blacklight/install_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ class Install < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)

argument :model_name , type: :string , default: "user"
argument :controller_name, type: :string , default: "catalog"
argument :document_name, type: :string , default: "solr_document"

class_option :devise , type: :boolean, default: false, aliases: "-d", desc: "Use Devise as authentication logic."
class_option :jettywrapper, type: :boolean, default: false, desc: "Use jettywrapper to download and control Jetty"
class_option :marc , type: :boolean, default: false, aliases: "-m", desc: "Generate MARC-based demo ."
Expand All @@ -17,7 +20,6 @@ class Install < Rails::Generators::Base
5. Adds example configurations for dealing with MARC-like data
6. Adds Blacklight routes to your ./config/routes.rb
Thank you for Installing Blacklight.
"""

Expand All @@ -36,7 +38,6 @@ def add_rsolr_gem
gem "rsolr", "~> 1.0.6"
end


def bundle_install
Bundler.with_clean_env do
run "bundle install"
Expand All @@ -49,32 +50,32 @@ def bundle_install
def copy_public_assets
generate "blacklight:assets"
end

def generate_blacklight_document
generate 'blacklight:document', document_name
end

def generate_blacklight_models
generate 'blacklight:models'
end

def generate_blacklight_users

generator_args = []
generator_args = [model_name]
if options[:devise]
generator_args << "--devise #{options[:devise]}"
end

generate 'blacklight:models', generator_args.join(" ")
generate 'blacklight:users', generator_args.join(" ")
end

# 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" +
" # Please be sure to impelement current_user and user_session. Blacklight depends on \n" +
" # these methods in order to perform user specific actions. \n\n" +
" layout 'blacklight'\n\n"
end
def generate_controller
generate 'blacklight:controller', controller_name
end

# Generate blacklight catalog controller
def create_blacklight_catalog
copy_file "catalog_controller.rb", "app/controllers/catalog_controller.rb"
end
def add_default_catalog_route
route("root to: \"#{controller_name}#index\"")
end

def generate_blacklight_marc_demo
if options[:marc]
Expand All @@ -88,13 +89,6 @@ def generate_blacklight_marc_demo
end
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 :catalog')
route('root :to => "catalog#index"')
end

def add_sass_configuration

Expand All @@ -111,7 +105,7 @@ def inject_blacklight_i18n_strings
end

def add_blacklight_initializer
template "config/initializers/blacklight_initializer.rb"
template "config/initializers/blacklight_initializer.rb" if Rails::VERSION::MAJOR < 4
end
end
end
58 changes: 0 additions & 58 deletions lib/generators/blacklight/models_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,51 +7,11 @@ class ModelsGenerator < Rails::Generators::Base

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 several database migrations if they do not exist in /db/migrate
2. Creates config/blacklight.yml with a default configuration
3. Injects behavior into your user model
4. Creates a blacklight document in your /app/models directory
"""

def check_arguments
if File.exists?("app/models/#{model_name}.rb") and options[:devise]
puts "Because you have selected \"#{model_name}\", which is an existing class, you will need to install devise manually and then run this generator without the Devise option. You can find additional information here: https://github.com/plataformatec/devise. \n Please be sure to include a to_s method in #{model_name} that returns the users name or email, as this will be used by Blacklight to provide a link to user specific information."
exit
end
end

# 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

# Copy all files in templates/config directory to host config
def create_configuration_files
copy_file "config/blacklight.yml", "config/blacklight.yml"
Expand All @@ -64,23 +24,5 @@ def copy_migrations
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

def create_solr_document
copy_file "solr_document.rb", "app/models/solr_document.rb"
end

end
end
3 changes: 1 addition & 2 deletions lib/generators/blacklight/templates/catalog_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
# -*- encoding : utf-8 -*-
#
class CatalogController < ApplicationController
class <%= controller_name.classify %>Controller < ApplicationController
include Blacklight::Catalog
Expand Down
2 changes: 1 addition & 1 deletion lib/generators/blacklight/templates/solr_document.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- encoding : utf-8 -*-
class SolrDocument
class <%= model_name.classify %>

include Blacklight::Solr::Document

Expand Down
7 changes: 1 addition & 6 deletions lib/generators/blacklight/test_support_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,7 @@ module Blacklight
class TestSupport < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
desc """
Installs a jetty container with a solr installed in it. A solr setup known
good with default blacklight setup, including solr conf files for out
of the box blacklight.
Also adds jetty_path key to blacklight.yml for selected environment, to refer
to this install.
Generate blacklight testing configurations for blacklight's own tests, or for blacklight plugins to use for testing
"""
def alternate_controller
copy_file "alternate_controller.rb", "app/controllers/alternate_controller.rb"
Expand Down
59 changes: 59 additions & 0 deletions lib/generators/blacklight/users_generator.rb
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

0 comments on commit c423ad4

Please sign in to comment.