Class: CurateGenerator
- Inherits:
-
Rails::Generators::Base
- Object
- Rails::Generators::Base
- CurateGenerator
- Includes:
- Rails::Generators::Migration
- Defined in:
- lib/generators/curate/curate_generator.rb
Constant Summary collapse
- DEFAULT_CURATION_CONCERNS =
[:generic_works, :datasets, :articles, :etds, :images, :documents]
Class Method Summary collapse
-
.next_migration_number(path) ⇒ Object
Implement the required interface for Rails::Generators::Migration.
Instance Method Summary collapse
-
#copy_migrations ⇒ Object
Setup the database migrations.
- #create_curate_config ⇒ Object
- #create_recipients_list ⇒ Object
-
#inject_controller_behavior ⇒ Object
Add behaviors to the application controller.
- #inject_curate_ability ⇒ Object
- #inject_curate_user ⇒ Object
-
#inject_routes ⇒ Object
The engine routes have to come after the devise routes so that /users/sign_in will work.
- #install_readme ⇒ Object
- #register_remote_identifiers ⇒ Object
- #remove_blacklight ⇒ Object
- #remove_catalog_controller ⇒ Object
- #run_migrations ⇒ Object
- #run_required_generators ⇒ Object
- #update_assets ⇒ Object
-
#update_devise_route ⇒ Object
This enables our registrations controller to run the after_update_path_for hook.
Class Method Details
.next_migration_number(path) ⇒ Object
Implement the required interface for Rails::Generators::Migration. taken from github.com/rails/rails/blob/master/activerecord/lib/generators/active_record.rb
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/generators/curate/curate_generator.rb', line 26 def self.next_migration_number(path) if @prev_migration_nr @prev_migration_nr += 1 else if last_migration = Dir[File.join(path, '*.rb')].sort.last @prev_migration_nr = last_migration.sub(File.join(path, '/'), '').to_i + 1 else @prev_migration_nr = Time.now.utc.strftime("%Y%m%d%H%M%S").to_i end end @prev_migration_nr.to_s end |
Instance Method Details
#copy_migrations ⇒ Object
Setup the database migrations
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/generators/curate/curate_generator.rb', line 63 def copy_migrations [ 'add_terms_of_service_to_user.rb', 'add_user_force_update_profile.rb', 'create_help_requests.rb', 'add_repository_id_to_user.rb', 'create_curate_proxy_deposit_rights.rb' ].each do |file| begin migration_template "migrations/#{file}", "db/migrate/#{file}" rescue Rails::Generators::Error => e say_status("warning", e., :yellow) end end end |
#create_curate_config ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/generators/curate/curate_generator.rb', line 89 def create_curate_config initializer 'curate_config.rb' do data = [] data << "Curate.configure do |config|" DEFAULT_CURATION_CONCERNS.each_with_object(data) {|curation_concern, mem| mem << " config.register_curation_concern :#{curation_concern.to_s.singularize}" } data << " # # You can override curate's antivirus runner by configuring a lambda \(or" data << " # # object that responds to call\)" data << " # config.default_antivirus_instance = lambda {|filename| … }" data << "" data << " # # Used for constructing permanent URLs" data << " # config.application_root_url = 'https://repository.higher.edu/'" data << "" data << " # # Override the file characterization runner that is used" data << " # config.characterization_runner = lambda {|filename| … }" data << "end" data << "" data.join("\n") end end |
#create_recipients_list ⇒ Object
125 126 127 |
# File 'lib/generators/curate/curate_generator.rb', line 125 def create_recipients_list create_file('config/recipients_list.yml', "---\n- [email protected]\n") end |
#inject_controller_behavior ⇒ Object
Add behaviors to the application controller
48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/generators/curate/curate_generator.rb', line 48 def inject_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 include CurateController\n" end gsub_file file_path, "layout 'blacklight'", "" else puts " \e[31mFailure\e[0m Could not find #{file_path}. To add Curate behaviors to your ApplicationController, you must include the CurateController module in the ApplicationController class definition." end end |
#inject_curate_ability ⇒ Object
135 136 137 138 139 |
# File 'lib/generators/curate/curate_generator.rb', line 135 def inject_curate_ability inject_into_file 'app/models/ability.rb', :after => /Hydra::Ability\s*\n/ do " include Curate::Ability\n\n" end end |
#inject_curate_user ⇒ Object
129 130 131 132 133 |
# File 'lib/generators/curate/curate_generator.rb', line 129 def inject_curate_user inject_into_class 'app/models/user.rb', 'User' do "\n include Curate::UserBehavior\n" end end |
#inject_routes ⇒ Object
The engine routes have to come after the devise routes so that /users/sign_in will work
82 83 84 85 86 87 |
# File 'lib/generators/curate/curate_generator.rb', line 82 def inject_routes routing_code = "\n curate_for containers: #{DEFAULT_CURATION_CONCERNS.inspect}\n" sentinel = /devise_for +:users.*$/ inject_into_file 'config/routes.rb', routing_code, { :after => sentinel, :verbose => false } gsub_file 'config/routes.rb', /^\s+root.+$/, " root 'catalog#index'" end |
#install_readme ⇒ Object
163 164 165 |
# File 'lib/generators/curate/curate_generator.rb', line 163 def install_readme readme 'README.md' end |
#register_remote_identifiers ⇒ Object
111 112 113 114 115 |
# File 'lib/generators/curate/curate_generator.rb', line 111 def register_remote_identifiers if .fetch('with_doi', false) generate 'curate:work:with_doi', DEFAULT_CURATION_CONCERNS.collect(&:to_s).join(" ") end end |
#remove_blacklight ⇒ Object
155 156 157 |
# File 'lib/generators/curate/curate_generator.rb', line 155 def remove_blacklight remove_file('app/assets/stylesheets/blacklight.css.scss') end |
#remove_catalog_controller ⇒ Object
141 142 143 144 |
# File 'lib/generators/curate/curate_generator.rb', line 141 def remove_catalog_controller say_status("warning", "Removing Blacklight's generated CatalogController...It will cause you grief", :yellow) remove_file('app/controllers/catalog_controller.rb') end |
#run_migrations ⇒ Object
159 160 161 |
# File 'lib/generators/curate/curate_generator.rb', line 159 def run_migrations rake "db:migrate" end |
#run_required_generators ⇒ Object
39 40 41 42 43 44 45 |
# File 'lib/generators/curate/curate_generator.rb', line 39 def run_required_generators generate "blacklight --devise" remove_dir('app/views/devise') generate "hydra:head -f" generate "sufia:models:install#{[:force] ? ' -f' : ''}" generate "hydra:remote_identifier:install#{[:force] ? ' -f' : ''}" end |
#update_assets ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/generators/curate/curate_generator.rb', line 146 def update_assets insert_into_file 'app/assets/stylesheets/application.css', before: /^ *\*= +require_tree +\. *$/ do " *= require curate\n" end insert_into_file "app/assets/javascripts/application.js", :before => '//= require_tree .' do "//= require curate\n" end end |
#update_devise_route ⇒ Object
This enables our registrations controller to run the after_update_path_for hook.
118 119 120 121 122 |
# File 'lib/generators/curate/curate_generator.rb', line 118 def update_devise_route gsub_file 'config/routes.rb', /^\s+devise_for :users\s*$/ do %( devise_for :users, controllers: { sessions: :sessions, registrations: :registrations}\n\n) end end |