Class: Rails::Generators::ContentGenerator

Inherits:
NamedBase
  • Object
show all
Defined in:
lib/generators/rails/content/content_generator.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ ContentGenerator

Returns a new instance of ContentGenerator.



20
21
22
23
24
25
# File 'lib/generators/rails/content/content_generator.rb', line 20

def initialize(*args)
  super

  @content_mode = !options[:new].nil?
  @content_title = options[:new].presence
end

Instance Method Details

#add_content_routeObject



69
70
71
72
73
# File 'lib/generators/rails/content/content_generator.rb', line 69

def add_content_route
  return if @content_mode

  route "resources :#{plural_file_name}, module: :content, only: %w[#{actions.join(" ")}]"
end

#add_root_actionObject



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/generators/rails/content/content_generator.rb', line 86

def add_root_action
  return if @content_mode
  return unless should_include_root?

  controller_file = "app/controllers/content/#{plural_file_name}_controller.rb"
  return unless File.exist?(File.join(destination_root, controller_file))

  root_action = "  def root\n    @resource = Content::#{class_name}.root\n\n    render :show\n  end\n\n"

  inject_into_file controller_file, root_action, after: "class Content::#{plural_class_name}Controller < ApplicationController\n"
end

#add_root_routeObject



105
106
107
108
109
110
111
# File 'lib/generators/rails/content/content_generator.rb', line 105

def add_root_route
  return if @content_mode
  return unless should_include_root?
  return if root_route_exists?

  route "root to: \"content/#{plural_file_name}#root\""
end

#create_content_directoryObject



63
64
65
66
67
# File 'lib/generators/rails/content/content_generator.rb', line 63

def create_content_directory
  return if @content_mode

  FileUtils.mkdir_p(content_directory)
end

#create_content_fileObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/generators/rails/content/content_generator.rb', line 27

def create_content_file
  return unless @content_mode

  @title = @content_title

  if template_file
    create_file File.join(content_directory, filename_from_template), ERB.new(File.read(template_file)).result(binding)
  else
    create_file File.join(content_directory, filename_from_template), "---\n---\n"
  end
end

#create_controllerObject



45
46
47
48
49
# File 'lib/generators/rails/content/content_generator.rb', line 45

def create_controller
  return if @content_mode

  template "controller.rb.tt", File.join("app/controllers/content", "#{plural_file_name}_controller.rb")
end

#create_data_sourcesObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/generators/rails/content/content_generator.rb', line 75

def create_data_sources
  return if @content_mode
  return if options[:data].empty?

  options[:data].each do |source|
    name, extension = source.split(".", 2)

    create_file File.join("app", "content", "data", "#{name}.#{extension || "yml"}"), ""
  end
end

#create_modelObject



39
40
41
42
43
# File 'lib/generators/rails/content/content_generator.rb', line 39

def create_model
  return if @content_mode

  template "model.rb.tt", File.join("app/models/content", "#{file_name}.rb")
end

#create_root_content_fileObject



98
99
100
101
102
103
# File 'lib/generators/rails/content/content_generator.rb', line 98

def create_root_content_file
  return if @content_mode
  return unless should_include_root?

  template "root.erb.tt", File.join(content_directory, "root.erb")
end

#create_viewsObject



51
52
53
54
55
56
57
58
59
60
61
# File 'lib/generators/rails/content/content_generator.rb', line 51

def create_views
  return if @content_mode

  empty_directory view_directory

  actions.each do |action|
    next if action == "show" && options[:inline]

    template "#{action}.html.erb.tt", File.join(view_directory, "#{action}.html.erb")
  end
end