/
install_generator.rb
108 lines (92 loc) · 4.31 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
require 'rails/generators'
module CurationConcerns
class Install < Rails::Generators::Base
source_root File.expand_path('../templates', __FILE__)
argument :model_name, type: :string, default: 'user'
class_option :'skip-assets', type: :boolean, default: false, desc: "Skip generating javascript and css assets into the application"
desc 'This generator makes the following changes to your application:
1. Runs installers for blacklight & hydra-head (which also install & configure devise)
2. Runs curation_concerns:models:install
3. Adds controller behavior to the application controller
4. Injects CurationConcerns routes
5. Adds CurationConcerns abilities into the Ability class
6. Copies the catalog controller into the local app
7. Adds CurationConcerns::SolrDocumentBehavior to app/models/solr_document.rb
8. Adds local authority files to the application
9. Copies modified simple_form initializers
'
def run_required_generators
say_status('warning', '[CurationConcerns] GENERATING BLACKLIGHT', :yellow)
generate 'blacklight:install --devise'
say_status('warning', '[CurationConcerns] GENERATING HYDRA-HEAD', :yellow)
generate 'hydra:head -f'
say_status('warning', '[CurationConcerns] GENERATING CURATION_CONCERNS MODELS', :yellow)
generate "curation_concerns:models#{options[:force] ? ' -f' : ''}"
end
def inject_application_controller_behavior
inject_into_file 'app/controllers/application_controller.rb', after: /Hydra::Controller::ControllerBehavior\s*\n/ do
"\n # Adds CurationConcerns behaviors to the application controller.\n" \
" include CurationConcerns::ApplicationControllerBehavior\n"
end
end
def replace_blacklight_layout
gsub_file 'app/controllers/application_controller.rb', /layout 'blacklight'/,
"include CurationConcerns::ThemedLayoutController\n with_themed_layout '1_column'\n"
end
def insert_builder
insert_into_file 'app/models/search_builder.rb', after: /include Hydra::AccessControlsEnforcement/ do
"\n include CurationConcerns::SearchFilters\n"
end
end
def inject_routes
# Remove root route that was added by blacklight generator
gsub_file 'config/routes.rb', /root (:to =>|to:) "catalog#index"/, ''
inject_into_file 'config/routes.rb', after: /devise_for :users\s*\n/ do
" mount CurationConcerns::Engine, at: '/'\n"\
" resources :welcome, only: 'index'\n"\
" root 'welcome#index'\n"\
" curation_concerns_collections\n"\
" curation_concerns_basic_routes\n"\
" curation_concerns_embargo_management\n"\
end
end
def inject_ability
inject_into_file 'app/models/ability.rb', after: /Hydra::Ability\s*\n/ do
" include CurationConcerns::Ability\n"\
" self.ability_logic += [:everyone_can_create_curation_concerns]\n\n"
end
end
def catalog_controller
copy_file "catalog_controller.rb", "app/controllers/catalog_controller.rb"
end
# Add behaviors to the SolrDocument model
def inject_solr_document_behavior
file_path = 'app/models/solr_document.rb'
if File.exist?(file_path)
inject_into_file file_path, after: /include Blacklight::Solr::Document.*$/ do
"\n # Adds CurationConcerns behaviors to the SolrDocument.\n" \
" include CurationConcerns::SolrDocumentBehavior\n"
end
else
puts " \e[31mFailure\e[0m CurationConcerns requires a SolrDocument object. This generators assumes that the model is defined in the file #{file_path}, which does not exist."
end
end
def assets
generate 'curation_concerns:assets' unless options[:'skip-assets']
end
def add_helper
copy_file 'curation_concerns_helper.rb', 'app/helpers/curation_concerns_helper.rb'
end
def local_authorities
copy_file "config/authorities/licenses.yml"
copy_file "config/authorities/rights_statements.yml"
copy_file "config/authorities/resource_types.yml"
end
def simple_form_initializers
copy_file 'config/initializers/simple_form.rb',
'config/initializers/simple_form.rb'
copy_file 'config/initializers/simple_form_bootstrap.rb',
'config/initializers/simple_form_bootstrap.rb'
end
end
end