Class: Hyrax::IiifViewerGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/hyrax/iiif_viewer/iiif_viewer_generator.rb

Instance Method Summary collapse

Instance Method Details

#configure_viewerObject



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/generators/hyrax/iiif_viewer/iiif_viewer_generator.rb', line 27

def configure_viewer
  config = Pathname.new(dest_root).join("config", "initializers", "hyrax.rb")
  lastmatch = nil

  content = "\n  # Injected via `rails generate hyrax:iiif_viewer #{viewer}`\n" \
    "  config.iiif_av_viewer = :#{viewer}\n\n"

  File.open(config).each_line do |line|
    regex = /config.iiif_av_viewer = .*/
    if behavior == :revoke
      # When removing/revoking, we return nil if the line includes
      # our viewer name because we want to delete the line instead
      # of using it as the "after" anchor.
      lastmatch = line if line.match?(regex) && line.exclude?(viewer.to_s)
    elsif line.match?(regex)
      lastmatch = line
    end
  end
  anchor = lastmatch || "Hyrax.config do |config|\n"
  insert_into_file(config, content, after: anchor)
end

#copy_partialObject



21
22
23
24
25
# File 'lib/generators/hyrax/iiif_viewer/iiif_viewer_generator.rb', line 21

def copy_partial
  filename = "_#{viewer}.html.erb"
  dest = Pathname.new(dest_root).join("app", "views", "hyrax", "base", "iiif_viewers", filename)
  copy_file(filename, dest)
end

#copy_viewer_filesObject



13
14
15
16
17
18
19
# File 'lib/generators/hyrax/iiif_viewer/iiif_viewer_generator.rb', line 13

def copy_viewer_files
  viewer_path = Pathname.new(dest_root).join('public', viewer.to_s)
  directory(viewer.to_s, viewer_path)
  return unless behavior == :revoke && Dir.exist?(viewer_path)
  FileUtils.rmdir(viewer_path)
  say_status :remove, viewer_path, :red
end

#dest_rootObject



9
10
11
# File 'lib/generators/hyrax/iiif_viewer/iiif_viewer_generator.rb', line 9

def dest_root
  @dest_root ||= Rails.env.test? ? File.join(Hyrax::Engine.root, "tmp") : Rails.root
end