Class: Sufia::WorkGenerator

Inherits:
CurationConcerns::WorkGenerator
  • Object
show all
Defined in:
lib/generators/sufia/work/work_generator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.source_pathsObject

Allows us to pull templates from sufia as well as from curation_concerns



8
9
10
# File 'lib/generators/sufia/work/work_generator.rb', line 8

def self.source_paths
  [File.expand_path('../templates/', __FILE__)]
end

Instance Method Details

#create_i18nObject



24
25
26
# File 'lib/generators/sufia/work/work_generator.rb', line 24

def create_i18n
  template 'locale.en.yml.erb', "config/locales/#{file_name}.en.yml"
end

#create_modelObject



19
20
21
22
# File 'lib/generators/sufia/work/work_generator.rb', line 19

def create_model
  say_status("info", "GENERATING WORK MODEL", :blue)
  super
end

#inject_sufia_formObject



52
53
54
55
56
57
58
59
60
61
62
# File 'lib/generators/sufia/work/work_generator.rb', line 52

def inject_sufia_form
  file_path = "app/forms/curation_concerns/#{file_name}_form.rb"
  if File.exist?(file_path)
    gsub_file file_path, /CurationConcerns::Forms::WorkForm/, "Sufia::Forms::WorkForm"
    inject_into_file file_path, after: /model_class = ::.*$/ do
      "\n    self.terms += [:resource_type]\n"
    end
  else
    puts "     \e[31mFailure\e[0m  Sufia requires a #{class_name}Form object. This generator assumes that the model is defined in the file #{file_path}, which does not exist."
  end
end

#inject_sufia_work_behaviorObject



44
45
46
47
48
49
50
# File 'lib/generators/sufia/work/work_generator.rb', line 44

def inject_sufia_work_behavior
  underscored_name = name.underscore
  insert_into_file "app/models/#{underscored_name}.rb", after: 'include ::CurationConcerns::BasicMetadata' do
    "\n  include Sufia::WorkBehavior" \
    "\n  self.human_readable_type = '#{underscored_name.titleize}'"
  end
end

#inject_sufia_work_controller_behaviorObject



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/generators/sufia/work/work_generator.rb', line 64

def inject_sufia_work_controller_behavior
  file_path = "app/controllers/curation_concerns/#{plural_file_name}_controller.rb"
  if File.exist?(file_path)
    inject_into_file file_path, after: /include CurationConcerns::CurationConcernController/ do
      "\n    # Adds Sufia behaviors to the controller.\n" \
        "    include Sufia::WorksControllerBehavior\n"
    end
  else
    puts "     \e[31mFailure\e[0m  Sufia requires a #{controller_class_name} object. This generator assumes that the model is defined in the file #{file_path}, which does not exist."
  end
end

#register_workObject

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



29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/generators/sufia/work/work_generator.rb', line 29

def register_work
  config = 'config/initializers/sufia.rb'
  lastmatch = nil
  File.open(config).each_line do |line|
    lastmatch = line if line =~ /^\s*config.register_curation_concern :.*\n$/
  end
  content = "  # Injected via `rails g sufia:work #{class_name}`\n" \
            "  config.register_curation_concern :#{file_name}\n"
  content = "  # Note: order of registration affects Zotero/Arkivo\n#{content}" unless lastmatch
  anchor = lastmatch || "Sufia.config do |config|\n"
  inject_into_file config, after: anchor do
    content
  end
end