class Sufia::WorkGenerator

Public Class Methods

source_paths() click to toggle source

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

# File lib/generators/sufia/work/work_generator.rb, line 8
def self.source_paths
  [File.expand_path('../templates/', __FILE__)]
end

Public Instance Methods

create_i18n() click to toggle source
# 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_model() click to toggle source
Calls superclass method
# File lib/generators/sufia/work/work_generator.rb, line 19
def create_model
  say_status("info", "GENERATING WORK MODEL", :blue)
  super
end
inject_sufia_form() click to toggle source
# 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_behavior() click to toggle source
# 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_behavior() click to toggle source
# 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_work() click to toggle source

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

# 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