class UltraLightWizard::Generators::ScaffoldGenerator

Public Instance Methods

arg_options() click to toggle source
# 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
arguments() click to toggle source
# 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_names() click to toggle source
# File lib/generators/ultra_light_wizard/scaffold_generator.rb, line 41
def attributes_names
  hashed_model_attributes.keys
end
controller_attribute_names() click to toggle source
# 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_name() click to toggle source
# File lib/generators/ultra_light_wizard/scaffold_generator.rb, line 22
def controller_class_name
  "#{class_name.pluralize}Controller"
end
copy_config() click to toggle source
# 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",
          <<-CONTENT
class #{controller_class_name} < ApplicationController
  include #{class_name}#{step_alias.pluralize.camelize}Helper
  helper #{class_name}#{step_alias.pluralize.camelize}Helper
          CONTENT
        gsub_file "app/controllers/#{file_path.pluralize}_controller.rb",
          "@#{singular_table_name}.save\n",
          "@#{singular_table_name}.save(validation: false)\n"
        gsub_file "app/controllers/#{file_path.pluralize}_controller.rb",
          "redirect_to @#{singular_table_name}, notice: '#{class_name} was successfully created.'",
          "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)"
        inject_into_file "app/controllers/#{file_path.pluralize}_controller.rb",
          after: "def #{singular_table_name}_params\n" do
          "      return {} unless params[:#{singular_table_name}].present?\n"
        end
        gsub_file "app/views/#{file_path.pluralize}/index.html.erb",
          "new_#{singular_table_name}_path",
          "#{file_path.pluralize}_path, method: :post"
        template "app/controllers/wizard_steps_controller.rb.erb", "app/controllers/#{file_path}_#{step_alias.pluralize}_controller.rb"
        template "app/helpers/wizard_steps_helper.rb.erb", "app/helpers/#{file_path}_#{step_alias.pluralize}_helper.rb"
        template "app/views/wizard_step_navigation_view.html.erb", "app/views/#{file_path}_#{step_alias.pluralize}/_#{step_alias}_navigation.html.erb"
        template "app/views/wizard_step_breadcrumb_view.html.erb", "app/views/#{file_path}_#{step_alias.pluralize}/_#{step_alias}_breadcrumb.html.erb"
        template "app/assets/stylesheets/wizard_steps.scss", "app/assets/stylesheets/#{file_path}_#{step_alias.pluralize}.scss"
        steps.each do |step|
          @wizard_step = step
          template "app/models/wizard_step_model.rb.erb", "app/models/#{file_path}/#{step}.rb"
          template "app/views/wizard_step_view.html.erb", "app/views/#{file_path}_#{step_alias.pluralize}/#{step}.html.erb"
        end
        wizard_route_content = <<-CONTENT
resources :#{plural_file_name} do
    resources :#{file_path}_#{step_alias.pluralize}, only: [:edit, :update]
  end
        CONTENT
        route_file = Rails.root.join('config', 'routes.rb')
        routes_content = File.new(route_file).read
        main_route = "resources :#{plural_file_name}\n"
        if routes_content.include?(main_route)
          # replace existing route
          gsub_file "config/routes.rb",
            main_route,
            wizard_route_content
        else
          # avoid messing with existing route if already has a do end block open
          route wizard_route_content
        end
      end
form_content(execute=false) click to toggle source
# 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_attributes() click to toggle source
# 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_name() click to toggle source
# File lib/generators/ultra_light_wizard/scaffold_generator.rb, line 53
def human_name
  file_path.humanize
end
index_helper() click to toggle source
# File lib/generators/ultra_light_wizard/scaffold_generator.rb, line 26
def index_helper
  file_path.pluralize
end
model_attributes() click to toggle source
# File lib/generators/ultra_light_wizard/scaffold_generator.rb, line 30
def model_attributes
  arguments['attributes']
end
plural_table_name() click to toggle source
# File lib/generators/ultra_light_wizard/scaffold_generator.rb, line 57
def plural_table_name
  file_path.pluralize
end
scaffold_attributes() click to toggle source
# File lib/generators/ultra_light_wizard/scaffold_generator.rb, line 45
def scaffold_attributes
  model_attributes.gsub(',', ' ')
end
step_alias() click to toggle source
# File lib/generators/ultra_light_wizard/scaffold_generator.rb, line 14
def step_alias
  arguments['step_alias'] || 'step'
end
steps() click to toggle source
# File lib/generators/ultra_light_wizard/scaffold_generator.rb, line 18
def steps
  arguments['steps'].to_s.split(',').map(&:strip)
end