Class: PartialForm::Generators::PartialsGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/partial_form/partials_generator.rb

Constant Summary collapse

SELECTION_GLOBS =
{
  default: "_{wrapper,text_field,label,submit}*.erb",
  all: "*.erb"
}
FORM_PARTIALS_PATH =
Pathname(__dir__).join("../../../app/views/layouts/form")

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

:nodoc:



12
13
14
15
16
17
18
19
20
# File 'lib/generators/partial_form/partials_generator.rb', line 12

def self.banner # :nodoc:
  <<~BANNER
    rails g partial_form:partials SELECTION [options]

    Copies partial templates to your application.
    You can choose if all partials or only a subset is copied.
    Pick on of: #{SELECTION_GLOBS.keys.join(", ")}
  BANNER
end

Instance Method Details

#copy_partialsObject



33
34
35
36
37
38
39
# File 'lib/generators/partial_form/partials_generator.rb', line 33

def copy_partials
  pattern = SELECTION_GLOBS[@selection.to_sym]
  filenames = FORM_PARTIALS_PATH.glob(pattern).map { _1.basename }
  filenames.each do |filename|
    copy_file filename, "app/views/layouts/form/#{filename}"
  end
end

#validate_selectionObject



22
23
24
25
26
27
28
29
30
31
# File 'lib/generators/partial_form/partials_generator.rb', line 22

def validate_selection
  @selection = given_selection
  valid_selections = SELECTION_GLOBS.keys.map(&:to_s)
  if @selection.blank?
    @selection = ask("Which partials do you want to copy?", limited_to: valid_selections)
  end
  unless valid_selections.include?(@selection)
    raise Thor::Error, "Please choose a valid selection (first parameter): #{valid_selections.join(", ")}"
  end
end