Class: TsykvasRailsTemplate::Generators::ConceptGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/tsykvas_rails_template/concept/concept_generator.rb

Constant Summary collapse

VALID_CONCEPT_NAME =
%r{\A[A-Za-z][A-Za-z0-9]*(?:(?:::|/)[A-Za-z][A-Za-z0-9]*)*\z}

Instance Method Summary collapse

Instance Method Details

#announce_next_stepsObject



79
80
81
82
83
84
85
86
87
88
# File 'lib/generators/tsykvas_rails_template/concept/concept_generator.rb', line 79

def announce_next_steps
  say ""
  say "  Concept #{class_name} scaffolded.", :green
  say "    - operations: app/concepts/#{path}/operation/"
  say "    - components: app/concepts/#{path}/component/"
  say "    - controller: app/controllers/#{controller_file_path}.rb" if options[:controller]
  say "    Add routes for #{plural_var.tr("_", " ")} in config/routes.rb."
  say "    Implement the policy at app/policies/#{singular_var}_policy.rb."
  say ""
end

#generate_componentsObject



63
64
65
66
67
68
69
70
# File 'lib/generators/tsykvas_rails_template/concept/concept_generator.rb', line 63

def generate_components
  component_actions.each do |action|
    template "component/#{action}.rb.tt",
             "app/concepts/#{path}/component/#{action}.rb"
    template "component/#{action}.html.slim.tt",
             "app/concepts/#{path}/component/#{action}.html.slim"
  end
end

#generate_controllerObject



72
73
74
75
76
77
# File 'lib/generators/tsykvas_rails_template/concept/concept_generator.rb', line 72

def generate_controller
  return unless options[:controller]

  template "controller.rb.tt",
           "app/controllers/#{controller_file_path}.rb"
end

#generate_operationsObject



56
57
58
59
60
61
# File 'lib/generators/tsykvas_rails_template/concept/concept_generator.rb', line 56

def generate_operations
  actions.each do |action|
    template "operation/#{action}.rb.tt",
             "app/concepts/#{path}/operation/#{action}.rb"
  end
end

#validate_concept_nameObject

Raises:

  • (::Thor::Error)


35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/generators/tsykvas_rails_template/concept/concept_generator.rb', line 35

def validate_concept_name
  name = concept_name.to_s.strip

  if name.empty?
    raise ::Thor::Error,
          "concept name is required (e.g. 'Crm::Property' or 'crm/property')"
  end

  if name.start_with?("::") || name.start_with?("/")
    raise ::Thor::Error,
          "concept name '#{name}' must not start with '::' or '/'"
  end

  return if name.match?(VALID_CONCEPT_NAME)

  raise ::Thor::Error,
        "concept name '#{name}' has invalid characters " \
        "(allowed: letters, digits, '::' or '/' separators; " \
        "each segment must start with a letter)"
end