Class: Hyrax::WorkResourceGenerator

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)

16
17
18
# File 'lib/generators/hyrax/work_resource/work_resource_generator.rb', line 16

def self.exit_on_failure?
  true
end

Instance Method Details


27
28
29
30
31
32
33
# File 'lib/generators/hyrax/work_resource/work_resource_generator.rb', line 27

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

#create_controllerObject


35
36
37
# File 'lib/generators/hyrax/work_resource/work_resource_generator.rb', line 35

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

#create_formObject


63
64
65
# File 'lib/generators/hyrax/work_resource/work_resource_generator.rb', line 63

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

#create_indexerObject


84
85
86
# File 'lib/generators/hyrax/work_resource/work_resource_generator.rb', line 84

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

#create_indexer_specObject


88
89
90
91
92
# File 'lib/generators/hyrax/work_resource/work_resource_generator.rb', line 88

def create_indexer_spec
  return unless rspec_installed?
  filepath = File.join('spec/indexers/', class_path, "#{file_name}_indexer_spec.rb")
  template('indexer_spec.rb.erb', filepath)
end

#create_metadata_configObject


39
40
41
42
43
44
45
# File 'lib/generators/hyrax/work_resource/work_resource_generator.rb', line 39

def 
  template('metadata.yaml', File.join('config/metadata/', "#{file_name}.yaml"))
  return if attributes.blank?
  gsub_file File.join('config/metadata/', "#{file_name}.yaml"),
            'attributes: {}',
            { 'attributes' => attributes.collect { |arg| [arg.name, { 'type' => arg.type.to_s }] }.to_h }.to_yaml
end

#create_modelObject


47
48
49
# File 'lib/generators/hyrax/work_resource/work_resource_generator.rb', line 47

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

#create_model_specObject


51
52
53
54
55
56
57
58
59
60
61
# File 'lib/generators/hyrax/work_resource/work_resource_generator.rb', line 51

def create_model_spec
  return unless rspec_installed?
  filepath = File.join('spec/models/', class_path, "#{file_name}_spec.rb")
  template('work_spec.rb.erb', filepath)
  return if attributes.blank?
  inject_into_file filepath, after: /it_behaves_like 'a Hyrax::Work'\n/ do
    "\n  context 'includes schema defined metadata' do\n"\
    "#{attributes.collect { |arg| "    it { is_expected.to respond_to(:#{arg.name}) }\n" }.join}" \
    "  end\n"
  end
end

#create_view_specObject


101
102
103
104
105
# File 'lib/generators/hyrax/work_resource/work_resource_generator.rb', line 101

def create_view_spec
  return unless rspec_installed?
  template('work.html.erb_spec.rb.erb',
           File.join('spec/views/', class_path, "#{plural_file_name}/_#{file_name}.html.erb_spec.rb"))
end

#create_viewsObject


94
95
96
97
98
99
# File 'lib/generators/hyrax/work_resource/work_resource_generator.rb', line 94

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

#register_workObject

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


68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/generators/hyrax/work_resource/work_resource_generator.rb', line 68

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_resource #{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

#validate_nameObject

Raises:

  • (Thor::MalformattedArgumentError)

20
21
22
23
24
25
# File 'lib/generators/hyrax/work_resource/work_resource_generator.rb', line 20

def validate_name
  return unless name.strip.casecmp("work").zero?
  raise Thor::MalformattedArgumentError,
        set_color("Error: A work resource with the name '#{name}' would cause name-space clashes. "\
                  "Please use a different name.", :red)
end