# -*- encoding : utf-8 -*- require 'rails/generators' require 'rails/generators/migration' module Hydra class HeadGenerator < 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 a database migration for superusers if they do not exist in /db/migrate 2. Adds additional mime types to you application in the file '/config/initializers/mime_types.rb' 3. Creates config/initializers/hydra_config.rb 4. Creates config/initializers/fedora_config.rb 5. Creates config/fedora.yml and config/solr.yml which you may need to modify to tell the hydra head where to find fedora & solr 6. Creates a number of role_map config files that are used in the placeholder user roles implementation Enjoy building your Hydra Head! """ # # Config Files & Initializers # # Copy all files in templates/config directory to host config def create_configuration_files # Initializers copy_file "config/initializers/fedora_config.rb", "config/initializers/fedora_config.rb" copy_file "config/initializers/hydra_config.rb", "config/initializers/hydra_config.rb" copy_file "config/initializers/blacklight_config.rb", "config/initializers/blacklight_config.rb" copy_file "config/initializers/action_dispatch_http_upload_monkey_patch.rb", "config/initializers/action_dispatch_http_upload_monkey_patch.rb" # Role Mappings copy_file "config/role_map_cucumber.yml", "config/role_map_cucumber.yml" copy_file "config/role_map_development.yml", "config/role_map_development.yml" copy_file "config/role_map_production.yml", "config/role_map_production.yml" copy_file "config/role_map_test.yml", "config/role_map_test.yml" # Solr Mappings copy_file "config/solr_mappings.yml", "config/solr_mappings.yml" # Fedora & Solr YAML files copy_file "config/fedora.yml", "config/fedora.yml" copy_file "config/solr.yml", "config/solr.yml" # Fedora & Solr Config files directory "fedora_conf" directory "solr_conf" # directory "../../../../fedora_conf", "fedora_conf" # directory "../../../../solr_conf", "solr_conf" end # Register mimetypes required by hydra-head def add_mime_types puts "Updating Mime Types" insert_into_file "config/initializers/mime_types.rb", :before => 'Mime::Type.register_alias "text/plain", :refworks_marc_txt' do < 'include Blacklight::Catalog' do "\n # Extend Blacklight::Catalog with Hydra behaviors (primarily editing)." + "\n include Hydra::Catalog\n" + "\n # These before_filters apply the hydra access controls" + "\n before_filter :enforce_access_controls" + "\n before_filter :enforce_viewing_context_for_show_requests, :only=>:show" + "\n # This applies appropriate access controls to all solr queries" + "\n CatalogController.solr_search_params_logic << :add_access_controls_to_solr_params" end else puts " \e[31mFailure\e[0m Could not find #{model_name.underscore}.rb. To add Hydra behaviors to your Blacklight::Catalog Controllers, you must include the Hydra::Controller module in the Controller class definition. See the Hydra::Controller section in the Hydra API Docs for more info." end end # Inject call to HydraHead.add_routes in config/routes.rb def inject_hydra_routes insert_into_file "config/routes.rb", :after => 'Blacklight.add_routes(self)' do "\n # Add Hydra routes. For options, see API docs for HydraHead.routes" "\n HydraHead.add_routes(self)" end end # Add Hydra to the application controller def inject_hydra_controller_behavior puts "Adding Hydra behaviors to ApplicationController" controller_name = "ApplicationController" file_path = "app/controllers/#{controller_name.underscore}.rb" if File.exists?(file_path) insert_into_file file_path, :after => 'include Blacklight::Controller' do " \n# Adds Hydra behaviors into the application controller \n" + " include Hydra::Controller\n" + " def layout_name\n" + " 'hydra-head'\n" + " end\n" end else puts " \e[31mFailure\e[0m Could not find #{model_name.underscore}.rb. To add Hydra behaviors to your Blacklight::Catalog Controllers, you must include the Hydra::Controller module in the Controller class definition. See the Hydra::Controller section in the Hydra API Docs for more info." end end def create_migration_file migration_template 'migrations/create_superusers.rb', 'db/migrate/create_superusers.rb' end end # HeadGenerator end # Hydra