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.


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

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


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

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

#create_actor_specObject


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

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


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

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

#create_controller_specObject


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

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


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

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


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

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

#create_form_specObject


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

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


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

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


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

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

#create_modelObject


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

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

#create_model_specObject


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

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


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

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

#create_viewsObject


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

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


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

def display_readme
  readme 'README' unless revoking?
end

#presenter_specObject


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

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


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

def register_work
  config = 'config/initializers/hyrax.rb'
  lastmatch = nil
  in_root do
    File.open(config).each_line do |line|
      lastmatch = line if line.match?(/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