Class: Hyrax::CollectionResourceGenerator

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

Overview

rubocop:disable Metrics/ClassLength

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.exit_on_failure?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/generators/hyrax/collection_resource/collection_resource_generator.rb', line 24

def self.exit_on_failure?
  true
end

Instance Method Details



35
36
37
38
39
40
41
# File 'lib/generators/hyrax/collection_resource/collection_resource_generator.rb', line 35

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

#create_formObject



67
68
69
70
71
72
73
74
# File 'lib/generators/hyrax/collection_resource/collection_resource_generator.rb', line 67

def create_form
  filepath = File.join('app/forms/', "#{file_name}_form.rb")
  template('collection_form.rb.erb', filepath)
  return unless include_basic_metadata?
  inject_into_file filepath, before: /include Hyrax::FormFields/ do
    "include Hyrax::FormFields(:basic_metadata)\n  "
  end
end

#create_form_specObject

TODO:

If shared specs are expanded to test for basic metadata, inject calling that test here.



77
78
79
80
# File 'lib/generators/hyrax/collection_resource/collection_resource_generator.rb', line 77

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

#create_indexerObject



82
83
84
85
86
87
88
89
# File 'lib/generators/hyrax/collection_resource/collection_resource_generator.rb', line 82

def create_indexer
  filepath = File.join('app/indexers/', "#{file_name}_indexer.rb")
  template('collection_indexer.rb.erb', filepath)
  return unless include_basic_metadata?
  inject_into_file filepath, before: /include Hyrax::Indexer/ do
    "include Hyrax::Indexer(:basic_metadata)\n  "
  end
end

#create_indexer_specObject



91
92
93
94
95
96
97
98
99
100
# File 'lib/generators/hyrax/collection_resource/collection_resource_generator.rb', line 91

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

  return unless include_basic_metadata?
  inject_into_file filepath, after: /it_behaves_like 'a Hyrax::Resource indexer'/ do
    "\n  it_behaves_like 'a Basic metadata indexer'"
  end
end

#create_metadata_configObject



43
44
45
# File 'lib/generators/hyrax/collection_resource/collection_resource_generator.rb', line 43

def 
  template('collection_metadata.yaml', File.join('config/metadata/', "#{file_name}.yaml"))
end

#create_modelObject



47
48
49
50
51
52
53
54
# File 'lib/generators/hyrax/collection_resource/collection_resource_generator.rb', line 47

def create_model
  filepath = File.join('app/models', "#{file_name}.rb")
  template('collection.rb.erb', filepath)
  return unless include_basic_metadata?
  inject_into_file filepath, before: /include Hyrax::Schema/ do
    "include Hyrax::Schema(:basic_metadata)\n  "
  end
end

#create_model_specObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/generators/hyrax/collection_resource/collection_resource_generator.rb', line 56

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

  return unless include_basic_metadata?
  inject_into_file filepath, after: /it_behaves_like 'a Hyrax::PcdmCollection'/ do
    "\n  it_behaves_like 'a model with basic metadata'"
  end
end

#set_as_the_collection_classObject

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



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/generators/hyrax/collection_resource/collection_resource_generator.rb', line 103

def set_as_the_collection_class
  config = 'config/initializers/hyrax.rb'
  lastmatch = nil
  in_root do
    File.open(config).each_line do |line|
      lastmatch = line if line.match?(/config.collection_model = /)
    end
    content = "  # Injected via `rails g hyrax:collection_resource #{class_name}`\n" \
              "  config.collection_model = '#{class_name}'\n"

    anchor = lastmatch || "Hyrax.config do |config|\n"
    inject_into_file config, after: anchor do
      content
    end
  end
end

#validate_nameObject

Raises:

  • (Thor::MalformattedArgumentError)


28
29
30
31
32
33
# File 'lib/generators/hyrax/collection_resource/collection_resource_generator.rb', line 28

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