class Schofield::Generators::Schofield

Public Instance Methods

generate() click to toggle source
# File lib/generators/schofield/schofield_generator.rb, line 22
def generate
  Responses.generator    = self
  Responses.re_ask       = options[:ask_questions]
  Level.tables_to_ignore = tables_to_ignore
  Levels.models          = models
  Responses.save
  Association.reference_parents
  Responses.save
  Association.report
end
generate_controllers() click to toggle source
# File lib/generators/schofield/schofield_generator.rb, line 37
def generate_controllers
  Levels.all.select(&:controllers?).each do |level|
    source = level.join? ? 'join_controller.erb' : 'controller.erb'
    destination = File.join('app/controllers/admin', "#{level.name.tableize}_controller.rb")
    template_with_context(source, Proc.new{level}, destination)
  end
end
generate_models() click to toggle source
# File lib/generators/schofield/schofield_generator.rb, line 56
def generate_models
Levels.all.select(&:models?).each do |level|
    destination = File.join('app/models', "#{level.name}.rb")
    template_with_context('model.erb', Proc.new{level}, destination)
  end
end
generate_navigation() click to toggle source
# File lib/generators/schofield/schofield_generator.rb, line 63
def generate_navigation
  inject_into_file(File.join(Rails.root, 'app/views/admin/shared', '_navigation.haml'), Navigation.generate, :after => /- if current_user\n/)
end
generate_routes() click to toggle source
# File lib/generators/schofield/schofield_generator.rb, line 33
def generate_routes
  inject_into_file(File.join(Rails.root, 'config', 'routes.rb'), Routes.generate, :before => /end\s*\Z/)
end
generate_tables() click to toggle source
# File lib/generators/schofield/schofield_generator.rb, line 67
      def generate_tables
        tables = []
        Levels.all.select(&:tables?).each do |level|
          columns_level = level.subclass? ? level.superclass : level
          columns = columns_level.attributes.select{ |a| columns_level.form_field?(a) && !a.text? }.sort_by(&:weight).map(&:to_column)
          if level.nested?
            tables << <<-STRING.gsub(/^ {12}/, '')
              def #{level.name.pluralize}_table
                [
                  #{columns.join(",\n                  ")}
                ]
              end
            STRING
          else
            tables << <<-STRING.gsub(/^ {12}/, '')
              def render_#{level.name.pluralize} collection
                table_renderer(:#{level.name.pluralize}, collection).render(
                  #{columns.join(",\n                  ")}
                )
              end
            STRING
          end
        end
        create_file 'app/helpers/admin/table_definitions_helper.rb' do
          "module Admin::TableDefinitionsHelper\n\n#{tables.join("\n\n")}\nend"
        end
      end
generate_views() click to toggle source
# File lib/generators/schofield/schofield_generator.rb, line 45
def generate_views
  Levels.all.select(&:views?).each do |level|
    template_with_context('form.erb', Proc.new{level}, view_path(level, '_form.haml'))
    create_file(view_path(level, 'new.haml'), %q(= render :partial => 'form', :layout => 'admin/shared/module_layout'))
    template_with_context('show.erb', Proc.new{level}, view_path(level, 'show.haml'))
    if !level.nested?
      template_with_context('index.erb', Proc.new{level}, view_path(level, 'index.haml'))
    end
  end
end

Private Instance Methods

models() click to toggle source
# File lib/generators/schofield/schofield_generator.rb, line 107
def models
  @models ||= tables.map { |t| t.camelize.singularize.constantize }
end
tables() click to toggle source
# File lib/generators/schofield/schofield_generator.rb, line 111
def tables
  ActiveRecord::Base.connection.tables - tables_to_ignore
end
tables_to_ignore() click to toggle source
# File lib/generators/schofield/schofield_generator.rb, line 99
def tables_to_ignore
  @tables_to_ignore ||= %w( schema_migrations ) + options[:tables_to_ignore].split(',').map{ |e| e.strip.tableize }
end
template_with_context(source, context, destination=nil, config={}, &block) click to toggle source
# File lib/generators/schofield/schofield_generator.rb, line 115
def template_with_context(source, context, destination=nil, config={}, &block)
  destination ||= source
  source  = File.expand_path(find_in_source_paths(source.to_s))
  create_file destination, nil, config do
    content = ERB.new(::File.binread(source), nil, '-').result(context)
    content = block.call(content) if block
    content
  end
end
view_path(level, file_name) click to toggle source
# File lib/generators/schofield/schofield_generator.rb, line 103
def view_path level, file_name
  File.join('app/views/admin', level.name.tableize, file_name)
end