-
Notifications
You must be signed in to change notification settings - Fork 78
/
install_generator.rb
137 lines (114 loc) · 4.99 KB
/
install_generator.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
require 'rails/generators'
require 'rails/generators/migration'
module Sufia
class Install < 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. Runs curation_concerns:install and sufia:models:install
2. Adds Sufia's abilities into the Ability class
3. Adds controller behavior to the application controller
4. Copies the catalog controller into the local app
5. Adds Sufia::SolrDocumentBehavior to app/models/solr_document.rb
6. Installs Blacklight gallery
"""
def run_required_generators
generate "curation_concerns:install -f"
generate "sufia:models:install --skip-curation-concerns" # skip curation_concerns installer because it was alredy run by
end
# TODO: make the curation_concerns installer take a --skip-assets flag
def remove_curation_concerns_scss
remove_file 'app/assets/stylesheets/curation_concerns.css.scss'
end
def banner
say_status("info", "GENERATING SUFIA", :blue)
end
def insert_abilities
insert_into_file 'app/models/ability.rb', after: /CurationConcerns::Ability/ do
"\n include Sufia::Ability\n"
end
end
# Add behaviors to the application controller
def inject_sufia_application_controller_behavior
file_path = "app/controllers/application_controller.rb"
if File.exist?(file_path)
insert_into_file file_path, after: 'CurationConcerns::ApplicationControllerBehavior' do
" \n # Adds Sufia behaviors into the application controller \n" \
" include Sufia::Controller\n"
end
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."
end
end
def use_blacklight_layout_theme
file_path = "app/controllers/application_controller.rb"
return unless File.exist?(file_path)
gsub_file file_path, /with_themed_layout '1_column'/, "layout 'sufia-one-column'"
end
def catalog_controller
copy_file "catalog_controller.rb", "app/controllers/catalog_controller.rb"
end
def copy_helper
copy_file 'sufia_helper.rb', 'app/helpers/sufia_helper.rb'
end
def add_sufia_assets
insert_into_file 'app/assets/stylesheets/application.css', after: ' *= require_self' do
"\n *= require sufia"
end
gsub_file 'app/assets/javascripts/application.js',
'//= require_tree .', '//= require sufia'
end
def tinymce_config
copy_file "config/tinymce.yml", "config/tinymce.yml"
end
# The engine routes have to come after the devise routes so that /users/sign_in will work
def inject_routes
gsub_file 'config/routes.rb', /root (:to =>|to:) "catalog#index"/, ''
gsub_file 'config/routes.rb', /('welcome#index')/, '\1, as: :welcome_root' # Name welcome root to from CurationConcerns
routing_code = "\n Hydra::BatchEdit.add_routes(self)\n" \
" # This must be the very last route in the file because it has a catch-all route for 404 errors.\n" \
" # This behavior seems to show up only in production mode.\n" \
" 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.exist?(file_path)
inject_into_file file_path, after: /include CurationConcerns::SolrDocumentBehavior/ do
"\n # Adds Sufia behaviors to the SolrDocument.\n" \
" include Sufia::SolrDocumentBehavior\n"
end
else
puts " \e[31mFailure\e[0m Sufia requires a SolrDocument object. This generator assumes that the model is defined in the file #{file_path}, which does not exist."
end
end
def inject_sufia_form
file_path = "app/forms/curation_concerns/generic_work_form.rb"
if File.exist?(file_path)
gsub_file file_path, /CurationConcerns::Forms::WorkForm/, "Sufia::Forms::WorkForm"
inject_into_file file_path, after: /model_class = ::GenericWork/ do
"\n include HydraEditor::Form::Permissions" \
"\n self.terms += [:resource_type]\n"
end
else
puts " \e[31mFailure\e[0m Sufia requires a GenericWorkForm object. This generator assumes that the model is defined in the file #{file_path}, which does not exist."
end
end
def install_sufia_600
generate "sufia:upgrade600"
end
def install_sufia_700
generate "sufia:upgrade700"
end
def install_blacklight_gallery
generate "blacklight_gallery:install"
end
def install_admin_stats
generate "sufia:admin_stat"
end
end
end