Class: UltraLightWizard::Generators::ScaffoldGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Defined in:
lib/generators/ultra_light_wizard/scaffold_generator.rb

Instance Method Summary collapse

Instance Method Details

#arg_optionsObject



61
62
63
# File 'lib/generators/ultra_light_wizard/scaffold_generator.rb', line 61

def arg_options
  options.select {|key, value| value}.map {|key, value| "--#{key}"}.join(' ')
end

#argumentsObject



5
6
7
8
9
10
11
12
# File 'lib/generators/ultra_light_wizard/scaffold_generator.rb', line 5

def arguments
  args.inject({}) do |output, arg|
    # Accommodate argument values containing colon by matching against
    # occurance of it only
    arg_parts = arg.match(/^([^:]+)\:(.+)$/)
    output.merge(arg_parts[1] => arg_parts[2])
  end
end

#attributes_namesObject



41
42
43
# File 'lib/generators/ultra_light_wizard/scaffold_generator.rb', line 41

def attributes_names
  hashed_model_attributes.keys
end

#controller_attribute_namesObject



49
50
51
# File 'lib/generators/ultra_light_wizard/scaffold_generator.rb', line 49

def controller_attribute_names
  hashed_model_attributes.keys.map {|key| "\"#{key}\""}.join(', ')
end

#controller_class_nameObject



22
23
24
# File 'lib/generators/ultra_light_wizard/scaffold_generator.rb', line 22

def controller_class_name
  "#{class_name.pluralize}Controller"
end

#copy_configObject



81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/generators/ultra_light_wizard/scaffold_generator.rb', line 81

def copy_config
  generate "scaffold", "#{file_path} #{scaffold_attributes} #{arg_options}"

  gsub_file "app/controllers/#{file_path.pluralize}_controller.rb",
    "class #{controller_class_name} < ApplicationController",
    "class \#{controller_class_name} < ApplicationController\n  include \#{class_name}\#{step_alias.pluralize.camelize}Helper\n  helper \#{class_name}\#{step_alias.pluralize.camelize}Helper\n    CONTENT\n  gsub_file \"app/controllers/\#{file_path.pluralize}_controller.rb\",\n    \"@\#{singular_table_name}.save\\n\",\n    \"@\#{singular_table_name}.save(validation: false)\\n\"\n  gsub_file \"app/controllers/\#{file_path.pluralize}_controller.rb\",\n    \"redirect_to @\#{singular_table_name}, notice: '\#{class_name} was successfully created.'\",\n    \"redirect_to edit_\#{file_path}_\#{file_path}_\#{step_alias}_path(@\#{singular_table_name}, \#{class_name}\#{step_alias.camelize.titleize.pluralize}Helper::\#{step_alias.pluralize.upcase}.first)\"\n  inject_into_file \"app/controllers/\#{file_path.pluralize}_controller.rb\",\n    after: \"def \#{singular_table_name}_params\\n\" do\n    \"      return {} unless params[:\#{singular_table_name}].present?\\n\"\n  end\n  gsub_file \"app/views/\#{file_path.pluralize}/index.html.erb\",\n    \"new_\#{singular_table_name}_path\",\n    \"\#{file_path.pluralize}_path, method: :post\"\n  template \"app/controllers/wizard_steps_controller.rb.erb\", \"app/controllers/\#{file_path}_\#{step_alias.pluralize}_controller.rb\"\n  template \"app/helpers/wizard_steps_helper.rb.erb\", \"app/helpers/\#{file_path}_\#{step_alias.pluralize}_helper.rb\"\n  template \"app/views/wizard_step_navigation_view.html.erb\", \"app/views/\#{file_path}_\#{step_alias.pluralize}/_\#{step_alias}_navigation.html.erb\"\n  template \"app/views/wizard_step_breadcrumb_view.html.erb\", \"app/views/\#{file_path}_\#{step_alias.pluralize}/_\#{step_alias}_breadcrumb.html.erb\"\n  template \"app/assets/stylesheets/wizard_steps.scss\", \"app/assets/stylesheets/\#{file_path}_\#{step_alias.pluralize}.scss\"\n  steps.each do |step|\n    @wizard_step = step\n    template \"app/models/wizard_step_model.rb.erb\", \"app/models/\#{file_path}/\#{step}.rb\"\n    template \"app/views/wizard_step_view.html.erb\", \"app/views/\#{file_path}_\#{step_alias.pluralize}/\#{step}.html.erb\"\n  end\n  wizard_route_content = <<-CONTENT\nresources :\#{plural_file_name} do\n    resources :\#{file_path}_\#{step_alias.pluralize}, only: [:edit, :update]\n  end\n  CONTENT\n  route_file = Rails.root.join('config', 'routes.rb')\n  routes_content = File.new(route_file).read\n  main_route = \"resources :\#{plural_file_name}\\n\"\n  if routes_content.include?(main_route)\n    # replace existing route\n    gsub_file \"config/routes.rb\",\n      main_route,\n      wizard_route_content\n  else\n    # avoid messing with existing route if already has a do end block open\n    route wizard_route_content\n  end\nend\n"

#form_content(execute = false) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/generators/ultra_light_wizard/scaffold_generator.rb', line 65

def form_content(execute=false)
  if execute #prevents thor from executing too early
    @form_content ||= lambda {
      # TODO support formats other than html.erb like html.haml (autodetect)
      scaffold_form_lines = File.new(Rails.root.join('app', 'views', plural_table_name, '_form.html.erb')).readlines
      form_start_index = scaffold_form_lines.find_index {|line| line.include?('form')}
      form_end_index =  scaffold_form_lines.length - 1 - scaffold_form_lines.reverse.find_index {|line| line.include?('actions')}
      form_content_start_index = form_start_index + 1
      form_content_end_index = form_end_index - 1
      extracted_form_lines = scaffold_form_lines[form_content_start_index..form_content_end_index]
      extracted_form_lines.join
    }.()
  end
end

#hashed_model_attributesObject



34
35
36
37
38
39
# File 'lib/generators/ultra_light_wizard/scaffold_generator.rb', line 34

def hashed_model_attributes
  model_attributes.split(',').inject({}) do |output, pair|
    split_pair = pair.split(':')
    output.merge(split_pair.first => split_pair.last)
  end
end

#human_nameObject



53
54
55
# File 'lib/generators/ultra_light_wizard/scaffold_generator.rb', line 53

def human_name
  file_path.humanize
end

#index_helperObject



26
27
28
# File 'lib/generators/ultra_light_wizard/scaffold_generator.rb', line 26

def index_helper
  file_path.pluralize
end

#model_attributesObject



30
31
32
# File 'lib/generators/ultra_light_wizard/scaffold_generator.rb', line 30

def model_attributes
  arguments['attributes']
end

#plural_table_nameObject



57
58
59
# File 'lib/generators/ultra_light_wizard/scaffold_generator.rb', line 57

def plural_table_name
  file_path.pluralize
end

#scaffold_attributesObject



45
46
47
# File 'lib/generators/ultra_light_wizard/scaffold_generator.rb', line 45

def scaffold_attributes
  model_attributes.gsub(',', ' ')
end

#step_aliasObject



14
15
16
# File 'lib/generators/ultra_light_wizard/scaffold_generator.rb', line 14

def step_alias
  arguments['step_alias'] || 'step'
end

#stepsObject



18
19
20
# File 'lib/generators/ultra_light_wizard/scaffold_generator.rb', line 18

def steps
  arguments['steps'].to_s.split(',').map(&:strip)
end