Class: Hyrax::WorkGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Includes:
Rails::Generators::ModelHelpers
Defined in:
lib/generators/hyrax/work/work_generator.rb

Constant Summary collapse

LOCALES =
%w[en es zh de fr it pt-BR].freeze

Instance Method Summary collapse

Instance Method Details

Why all of these antics with defining individual methods? Because I want the output of Hyrax::WorkGenerator to include all the processed files.



17
18
19
20
21
22
23
# File 'lib/generators/hyrax/work/work_generator.rb', line 17

def banner
  if revoking?
    say_status("info", "DESTROYING WORK MODEL: #{class_name}", :blue)
  else
    say_status("info", "GENERATING WORK MODEL: #{class_name}", :blue)
  end
end

#create_actorObject



25
26
27
# File 'lib/generators/hyrax/work/work_generator.rb', line 25

def create_actor
  template('actor.rb.erb', File.join('app/actors/hyrax/actors', class_path, "#{file_name}_actor.rb"))
end

#create_actor_specObject



81
82
83
84
# File 'lib/generators/hyrax/work/work_generator.rb', line 81

def create_actor_spec
  return unless rspec_installed?
  template('actor_spec.rb.erb', File.join('spec/actors/hyrax/actors/', class_path, "#{file_name}_actor_spec.rb"))
end

#create_controllerObject



29
30
31
# File 'lib/generators/hyrax/work/work_generator.rb', line 29

def create_controller
  template('controller.rb.erb', File.join('app/controllers/hyrax', class_path, "#{plural_file_name}_controller.rb"))
end

#create_controller_specObject



86
87
88
89
# File 'lib/generators/hyrax/work/work_generator.rb', line 86

def create_controller_spec
  return unless rspec_installed?
  template('controller_spec.rb.erb', File.join('spec/controllers/hyrax/', class_path, "#{plural_file_name}_controller_spec.rb"))
end

#create_feature_specObject



91
92
93
94
# File 'lib/generators/hyrax/work/work_generator.rb', line 91

def create_feature_spec
  return unless rspec_installed?
  template('feature_spec.rb.erb', File.join('spec/features/', class_path, "create_#{file_name}_spec.rb"))
end

#create_formObject



37
38
39
# File 'lib/generators/hyrax/work/work_generator.rb', line 37

def create_form
  template('form.rb.erb', File.join('app/forms/hyrax', class_path, "#{file_name}_form.rb"))
end

#create_form_specObject



96
97
98
99
# File 'lib/generators/hyrax/work/work_generator.rb', line 96

def create_form_spec
  return unless rspec_installed?
  template('form_spec.rb.erb', File.join('spec/forms/hyrax/', class_path, "#{file_name}_form_spec.rb"))
end

#create_i18nObject



75
76
77
78
79
# File 'lib/generators/hyrax/work/work_generator.rb', line 75

def create_i18n
  LOCALES.each do |locale|
    template("locale.#{locale}.yml.erb", File.join('config/locales/', class_path, "#{file_name}.#{locale}.yml"))
  end
end

#create_indexerObject



33
34
35
# File 'lib/generators/hyrax/work/work_generator.rb', line 33

def create_indexer
  template('indexer.rb.erb', File.join('app/indexers', class_path, "#{file_name}_indexer.rb"))
end

#create_modelObject



45
46
47
# File 'lib/generators/hyrax/work/work_generator.rb', line 45

def create_model
  template('model.rb.erb', File.join('app/models/', class_path, "#{file_name}.rb"))
end

#create_model_specObject



106
107
108
109
# File 'lib/generators/hyrax/work/work_generator.rb', line 106

def create_model_spec
  return unless rspec_installed?
  template('model_spec.rb.erb', File.join('spec/models/', class_path, "#{file_name}_spec.rb"))
end

#create_presenterObject



41
42
43
# File 'lib/generators/hyrax/work/work_generator.rb', line 41

def create_presenter
  template('presenter.rb.erb', File.join('app/presenters/hyrax', class_path, "#{file_name}_presenter.rb"))
end

#create_viewsObject



49
50
51
52
53
54
# File 'lib/generators/hyrax/work/work_generator.rb', line 49

def create_views
  create_file File.join('app/views/hyrax', class_path, "#{plural_file_name}/_#{file_name}.html.erb") do
    "<%# This is a search result view %>\n" \
    "<%= render 'catalog/document', document: #{file_name}, document_counter: #{file_name}_counter  %>\n"
  end
end

#display_readmeObject



111
112
113
# File 'lib/generators/hyrax/work/work_generator.rb', line 111

def display_readme
  readme 'README' unless revoking?
end

#presenter_specObject



101
102
103
104
# File 'lib/generators/hyrax/work/work_generator.rb', line 101

def presenter_spec
  return unless rspec_installed?
  template('presenter_spec.rb.erb', File.join('spec/presenters/hyrax/', class_path, "#{file_name}_presenter_spec.rb"))
end

#register_workObject

Inserts after the last registered work, or at the top of the config block



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/generators/hyrax/work/work_generator.rb', line 57

def register_work
  config = 'config/initializers/hyrax.rb'
  lastmatch = nil
  in_root do
    File.open(config).each_line do |line|
      lastmatch = line if line =~ /config.register_curation_concern :(?!#{file_name})/
    end
    content = "  # Injected via `rails g hyrax:work #{class_name}`\n" \
              "  config.register_curation_concern #{registration_path_symbol}\n"
    anchor = lastmatch || "Hyrax.config do |config|\n"
    inject_into_file config, after: anchor do
      content
    end
  end
end