Class: CurateGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
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]

Instance Method Summary collapse

Instance Method Details

#copy_migrationsObject

Setup the database migrations



49
50
51
# File 'lib/generators/curate/curate_generator.rb', line 49

def copy_migrations
  rake 'curate_engine:install:migrations'
end

#create_curate_configObject



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/generators/curate/curate_generator.rb', line 72

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 << "  # # Used to load values for constructing SOLR searches"
    data << "  search_config_file = File.join(Rails.root, 'config', 'search_config.yml')"
    data << "  config.search_config = YAML::load(File.open(search_config_file))[Rails.env].with_indifferent_access"
    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_manager_usernamesObject



111
112
113
# File 'lib/generators/curate/curate_generator.rb', line 111

def create_manager_usernames
  create_file('config/manager_usernames.yml', "development:\n  manager_usernames:\n  - [email protected]\ntest:\n  manager_usernames:\n  - [email protected]\nproduction:\n  manager_usernames:\n  - [email protected]\n")
end

#create_predicate_mappingObject



157
158
159
# File 'lib/generators/curate/curate_generator.rb', line 157

def create_predicate_mapping
  copy_file Rails.root.join("../../lib/generators/curate/predicate_mapping/predicate_mappings.yml"), Rails.root.join("config/predicate_mappings.yml"), force: true 
end

#create_recipients_listObject



115
116
117
# File 'lib/generators/curate/curate_generator.rb', line 115

def create_recipients_list
  create_file('config/recipients_list.yml', "---\n- [email protected]\n")
end

#create_search_configObject



62
63
64
65
66
67
68
# File 'lib/generators/curate/curate_generator.rb', line 62

def create_search_config
  create_file('config/search_config.yml', "development:\ntest:\nproduction:\n", force: true)
  search_options = "  catalog:\n  people:\n"
  inject_into_file 'config/search_config.yml', search_options, after: /development\:\n/, force: true
  inject_into_file 'config/search_config.yml', search_options, after: /test\:\n/, force: true
  inject_into_file 'config/search_config.yml', search_options, after: /production\:\n/, force: true
end

#inject_controller_behaviorObject

Add behaviors to the application controller



34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/generators/curate/curate_generator.rb', line 34

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_abilityObject



125
126
127
128
129
# File 'lib/generators/curate/curate_generator.rb', line 125

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_userObject



119
120
121
122
123
# File 'lib/generators/curate/curate_generator.rb', line 119

def inject_curate_user
  inject_into_file 'app/models/user.rb', after: /include Sufia\:\:User.*$/ do
    "\n  include Curate::UserBehavior\n"
  end
end

#inject_routesObject

The engine routes have to come after the devise routes so that /users/sign_in will work



54
55
56
57
58
59
# File 'lib/generators/curate/curate_generator.rb', line 54

def inject_routes
  routing_code = "\n  curate_for\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_readmeObject



153
154
155
# File 'lib/generators/curate/curate_generator.rb', line 153

def install_readme
  readme 'README.md'
end

#register_remote_identifiersObject



98
99
100
101
102
# File 'lib/generators/curate/curate_generator.rb', line 98

def register_remote_identifiers
  if options.fetch('with_doi', false)
    generate 'curate:work:with_doi', DEFAULT_CURATION_CONCERNS.collect(&:to_s).join(" ")
  end
end

#remove_blacklightObject



145
146
147
# File 'lib/generators/curate/curate_generator.rb', line 145

def remove_blacklight
  remove_file('app/assets/stylesheets/blacklight.css.scss')
end

#remove_catalog_controllerObject



131
132
133
134
# File 'lib/generators/curate/curate_generator.rb', line 131

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_migrationsObject



149
150
151
# File 'lib/generators/curate/curate_generator.rb', line 149

def run_migrations
  rake "db:migrate"
end

#run_required_generatorsObject



25
26
27
28
29
30
31
# File 'lib/generators/curate/curate_generator.rb', line 25

def run_required_generators
  generate "blacklight --devise"
  remove_dir('app/views/devise')
  generate "hydra:head -f"
  generate "sufia:models:install#{options[:force] ? ' -f' : ''}"
  generate "hydra:remote_identifier:install#{options[:force] ? ' -f' : ''}"
end

#update_assetsObject



136
137
138
139
140
141
142
143
# File 'lib/generators/curate/curate_generator.rb', line 136

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_routeObject

This enables our registrations controller to run the after_update_path_for hook.



105
106
107
108
109
# File 'lib/generators/curate/curate_generator.rb', line 105

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