class Casein::ScaffoldGenerator

Public Class Methods

next_migration_number(dirname) click to toggle source
# File lib/generators/casein/scaffold/scaffold_generator.rb, line 13
def self.next_migration_number(dirname)
  if ActiveRecord::Base.timestamped_migrations
    Time.now.utc.strftime('%Y%m%d%H%M%S')
  else
    format('%.3d', (current_migration_number(dirname) + 1))
  end
end

Public Instance Methods

generate_files() click to toggle source
# File lib/generators/casein/scaffold/scaffold_generator.rb, line 21
def generate_files
  @plural_route = plural_name
  @read_only = options[:read_only]
  @no_index = options[:no_index]

  template 'controller.rb', "app/controllers/casein/#{plural_name}_controller.rb"
  template 'views/index.html.erb', "app/views/casein/#{plural_name}/index.html.erb" unless @no_index
  template 'views/show.html.erb', "app/views/casein/#{plural_name}/show.html.erb"
  template 'views/new.html.erb', "app/views/casein/#{plural_name}/new.html.erb" unless @read_only
  template 'views/_form.html.erb', "app/views/casein/#{plural_name}/_form.html.erb"
  template 'views/_table.html.erb', "app/views/casein/#{plural_name}/_table.html.erb"

  add_namespace_to_routes
  add_to_routes
  add_to_navigation unless @no_index

  if options[:create_model_and_migration]
    template 'model.rb', "app/models/#{singular_name}.rb"
    migration_template 'migration.rb', "db/migrate/create_#{plural_name}.rb"
  end
end

Protected Instance Methods

add_namespace_to_routes() click to toggle source

replacements for standard Rails generator route. This one only adds once

# File lib/generators/casein/scaffold/scaffold_generator.rb, line 46
def add_namespace_to_routes
  puts '   casein     adding namespace to routes.rb'
  file_to_update = Rails.root + 'config/routes.rb'
  line_to_add = 'namespace :casein do'
  insert_sentinel = 'Application.routes.draw do'
  if File.read(file_to_update).scan(/(#{Regexp.escape(line_to_add.to_s)})/mi).blank?
    gsub_add_once plural_name, file_to_update, "\n#Casein routes\n" + line_to_add + "\nend\n", insert_sentinel
  end
end
add_to_navigation() click to toggle source
# File lib/generators/casein/scaffold/scaffold_generator.rb, line 74
def add_to_navigation
  puts "   casein     adding #{plural_name} to left navigation bar"
  file_to_update = Rails.root + 'app/views/casein/layouts/_tab_navigation.html.erb'
  line_to_add = "<li id=\"tab-#{@plural_route}\"><%= link_to \"#{plural_name.humanize.capitalize}\", casein_#{@plural_route}_path %></li>"
  insert_sentinel = '<!-- SCAFFOLD_INSERT -->'
  gsub_add_once plural_name, file_to_update, line_to_add, insert_sentinel
end
add_to_routes() click to toggle source
# File lib/generators/casein/scaffold/scaffold_generator.rb, line 56
def add_to_routes
  puts "   casein     adding #{plural_name} resources to routes.rb"
  file_to_update = Rails.root + 'config/routes.rb'

  line_to_add = if @no_index && @read_only
                  "resources :#{plural_name}, only: [:show]"
                elsif @no_index
                  "resources :#{plural_name}, except: [:index]"
                elsif @read_only
                  "resources :#{plural_name}, only: [:index, :show]"
                else
                  "resources :#{plural_name}"
                end

  insert_sentinel = 'namespace :casein do'
  gsub_add_once plural_name, file_to_update, '    ' + line_to_add, insert_sentinel
end
field_type(type) click to toggle source
# File lib/generators/casein/scaffold/scaffold_generator.rb, line 98
def field_type(type)
  case type.to_s.to_sym
  when :integer, :float, :decimal   then :text_field
  when :date                        then :date_select
  when :time, :timestamp            then :time_select
  when :datetime                    then :datetime_select
  when :string                      then :text_field
  when :text                        then :text_area
  when :boolean                     then :check_box
  else
    :text_field
  end
end
gsub_add_once(_m, file, line, sentinel) click to toggle source
# File lib/generators/casein/scaffold/scaffold_generator.rb, line 82
def gsub_add_once(_m, file, line, sentinel)
  unless options[:pretend]
    gsub_file file, /(#{Regexp.escape("\n#{line}")})/mi do |_match|
      ''
    end
    gsub_file file, /(#{Regexp.escape(sentinel)})/mi do |match|
      "#{match}\n#{line}"
    end
  end
end
gsub_file(path, regexp, *args, &block) click to toggle source
# File lib/generators/casein/scaffold/scaffold_generator.rb, line 93
def gsub_file(path, regexp, *args, &block)
  content = File.read(path).gsub(regexp, *args, &block)
  File.open(path, 'wb') { |file| file.write(content) }
end