Skip to content

Commit

Permalink
Merge pull request #180 from projecthydra/extracting-sufia-models-gen…
Browse files Browse the repository at this point in the history
…erator

Extracting sufia generator behavior
  • Loading branch information
jcoyne committed Aug 20, 2013
2 parents 84a6785 + 1ea58a9 commit 3a44ff5
Show file tree
Hide file tree
Showing 24 changed files with 404 additions and 230 deletions.
103 changes: 18 additions & 85 deletions lib/generators/sufia/sufia_generator.rb
Original file line number Diff line number Diff line change
@@ -1,85 +1,46 @@
# -*- encoding : utf-8 -*-
require 'rails/generators'
require 'rails/generators/migration'
require 'rails/generators/migration'

class SufiaGenerator < Rails::Generators::Base
include Rails::Generators::Migration

source_root File.expand_path('../templates', __FILE__)

argument :model_name, :type => :string , :default => "user"
desc """
This generator makes the following changes to your application:
1. Creates several database migrations if they do not exist in /db/migrate
2. Adds user behavior to the user model
3. Adds controller behavior to the application controller
4. Creates the sufia.rb configuration file
5. Copies the catalog controller into the local app
6. Adds Sufia::SolrDocumentBehavior to app/models/solr_document.rb
7. Generates mailboxer
"""

# Implement the required interface for Rails::Generators::Migration.
# taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
def self.next_migration_number(path)
unless @prev_migration_nr
@prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
else
@prev_migration_nr += 1
end
@prev_migration_nr.to_s
end
1. Runs sufia-models:install
2. Adds controller behavior to the application controller
3. Copies the catalog controller into the local app
"""

# Setup the database migrations
def copy_migrations
# Can't get this any more DRY, because we need this order.
%w{acts_as_follower_migration.rb add_social_to_users.rb create_single_use_links.rb add_ldap_attrs_to_user.rb
add_avatars_to_users.rb create_checksum_audit_logs.rb create_version_committers.rb
add_groups_to_users.rb create_local_authorities.rb create_trophies.rb}.each do |f|
better_migration_template f
end
end

# Add behaviors to the user model
def inject_sufia_user_behavior
file_path = "app/models/#{model_name.underscore}.rb"
if File.exists?(file_path)
inject_into_class file_path, model_name.classify do
"# Connects this user object to Sufia behaviors. " +
"\n include Sufia::User\n"
end
else
puts " \e[31mFailure\e[0m Sufia requires a user object. This generators assumes that the model is defined in the file #{file_path}, which does not exist. If you used a different name, please re-run the generator and provide that name as an argument. Such as \b rails -g sufia client"
end
def run_required_generators
generate "blacklight --devise"
generate "hydra:head -f"
generate "sufia:models:install"
end

# Add behaviors to the application controller
def inject_sufia_controller_behavior
def inject_sufia_controller_behavior
controller_name = "ApplicationController"
file_path = "app/controllers/application_controller.rb"
if File.exists?(file_path)
insert_into_file file_path, :after => 'include Blacklight::Controller' do
" \n# Adds Sufia behaviors into the application controller \n" +
if File.exists?(file_path)
insert_into_file file_path, :after => 'include Blacklight::Controller' do
" \n# Adds Sufia behaviors into the application controller \n" +
" include Sufia::Controller\n"
end
gsub_file file_path, "layout 'blacklight'", "layout :search_layout"
else
puts " \e[31mFailure\e[0m Could not find #{file_path}. To add Sufia behaviors to your Controllers, you must include the Sufia::Controller module in the Controller class definition."
puts " \e[31mFailure\e[0m Could not find #{file_path}. To add Sufia behaviors to your Controllers, you must include the Sufia::Controller module in the Controller class definition."
end
end

def create_configuration_files
copy_file "config/sufia.rb", "config/initializers/sufia.rb"
copy_file "config/redis.yml", "config/redis.yml"
copy_file "config/redis_config.rb", "config/initializers/redis_config.rb"
copy_file "config/resque_admin.rb", "config/initializers/resque_admin.rb"
copy_file "config/resque_config.rb", "config/initializers/resque_config.rb"
end


def catalog_controller
copy_file "catalog_controller.rb", "app/controllers/catalog_controller.rb"
end


# The engine routes have to come after the devise routes so that /users/sign_in will work
def inject_routes
Expand All @@ -92,37 +53,9 @@ def inject_routes
mount Sufia::Engine => '/'\n"
sentinel = /devise_for :users/
inject_into_file 'config/routes.rb', routing_code, { :after => sentinel, :verbose => false }

end

# Add behaviors to the SolrDocument model
def inject_sufia_solr_document_behavior
file_path = "app/models/solr_document.rb"
if File.exists?(file_path)
inject_into_class file_path, "SolrDocument" do
" # Adds Sufia behaviors to the SolrDocument.\n" +
" include Sufia::SolrDocumentBehavior\n"
end
else
puts " \e[31mFailure\e[0m Sufia requires a SolrDocument object. This generators assumes that the model is defined in the file #{file_path}, which does not exist."
end
end

def install_mailboxer
generate "mailboxer:install"
end

private

def better_migration_template (file)
begin
migration_template "migrations/#{file}", "db/migrate/#{file}"
sleep 1 # ensure scripts have different time stamps
rescue
puts " \e[1m\e[34mMigrations\e[0m " + $!.message
end
end

end
end


14 changes: 0 additions & 14 deletions lib/generators/sufia/templates/config/clamav.rb

This file was deleted.

31 changes: 0 additions & 31 deletions lib/generators/sufia/templates/config/mailboxer.rb

This file was deleted.

17 changes: 0 additions & 17 deletions lib/generators/sufia/templates/config/setup_mail.rb

This file was deleted.

82 changes: 0 additions & 82 deletions lib/generators/sufia/templates/config/sufia.rb

This file was deleted.

99 changes: 99 additions & 0 deletions sufia-models/lib/generators/sufia/models/install_generator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# -*- encoding : utf-8 -*-
require 'rails/generators'
require 'rails/generators/migration'

class Sufia::Models::InstallGenerator < Rails::Generators::Base
include Rails::Generators::Migration

source_root File.expand_path('../templates', __FILE__)

argument :model_name, :type => :string , :default => "user"
desc """
This generator makes the following changes to your application:
1. Creates several database migrations if they do not exist in /db/migrate
2. Adds user behavior to the user model
3. Creates the sufia.rb configuration file
4. Adds Sufia::SolrDocumentBehavior to app/models/solr_document.rb
5. Generates mailboxer
"""

# Implement the required interface for Rails::Generators::Migration.
# taken from http://github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
def self.next_migration_number(path)
unless @prev_migration_nr
@prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i
else
@prev_migration_nr += 1
end
@prev_migration_nr.to_s
end

# Setup the database migrations
def copy_migrations
# Can't get this any more DRY, because we need this order.
[
"acts_as_follower_migration.rb",
"add_social_to_users.rb",
"create_single_use_links.rb",
"add_ldap_attrs_to_user.rb",
"add_avatars_to_users.rb",
"create_checksum_audit_logs.rb",
"create_version_committers.rb",
"add_groups_to_users.rb",
"create_local_authorities.rb",
"create_trophies.rb"
].each do |f|
better_migration_template f
end
end

# Add behaviors to the user model
def inject_sufia_user_behavior
file_path = "app/models/#{model_name.underscore}.rb"
if File.exists?(file_path)
inject_into_class file_path, model_name.classify do
"# Connects this user object to Sufia behaviors. " +
"\n include Sufia::User\n"
end
else
puts " \e[31mFailure\e[0m Sufia requires a user object. This generators assumes that the model is defined in the file #{file_path}, which does not exist. If you used a different name, please re-run the generator and provide that name as an argument. Such as \b rails -g sufia client"
end
end

def create_configuration_files
copy_file "config/sufia.rb", "config/initializers/sufia.rb"
copy_file "config/redis.yml", "config/redis.yml"
copy_file "config/redis_config.rb", "config/initializers/redis_config.rb"
copy_file "config/resque_admin.rb", "config/initializers/resque_admin.rb"
copy_file "config/resque_config.rb", "config/initializers/resque_config.rb"
end

# Add behaviors to the SolrDocument model
def inject_sufia_solr_document_behavior
file_path = "app/models/solr_document.rb"
if File.exists?(file_path)
inject_into_class file_path, "SolrDocument" do
" # Adds Sufia behaviors to the SolrDocument.\n" +
" include Sufia::SolrDocumentBehavior\n"
end
else
puts " \e[31mFailure\e[0m Sufia requires a SolrDocument object. This generators assumes that the model is defined in the file #{file_path}, which does not exist."
end
end

def install_mailboxer
generate "mailboxer:install"
end

private

def better_migration_template(file)
begin
migration_template "migrations/#{file}", "db/migrate/#{file}"
sleep 1 # ensure scripts have different time stamps
rescue
puts " \e[1m\e[34mMigrations\e[0m " + $!.message
end
end

end
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ClamAV.instance.loaddb() if defined? ClamAV
Loading

0 comments on commit 3a44ff5

Please sign in to comment.