Class: Fullstack::Admin::ScaffoldGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/fullstack/admin/scaffold_generator.rb

Instance Method Summary collapse

Instance Method Details

#add_routesObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/generators/fullstack/admin/scaffold_generator.rb', line 15

def add_routes
  routes_rb = Rails.root.join('config', 'routes.rb') 
  
  altered_lines = []
  File.open(routes_rb) do |file|
    lines = file.read.split("\n")
    lines.each do |line|
      if line =~ /^ \s* namespace \s+ :#{scope} \s+ do /x
        leading_space = line.match(/^(\s*)/)[1] + "  "
        line = [line,  "#{leading_space}resources :#{plural_name}, :except => [:show]"].join("\n")
      end
      altered_lines << line
    end
  end
  
  File.open(routes_rb, 'w') do |file|
   file.write(altered_lines.join("\n"))
  end
  
  say("addedd 'resources :#{plural_name}, :except => [:show]' to namespace ':#{scope}' in routes.rb")
end

#append_to_menuObject



46
47
48
49
50
51
52
53
54
55
# File 'lib/generators/fullstack/admin/scaffold_generator.rb', line 46

def append_to_menu
  placeholder_text = "FULLSTACK_PLACEHOLDER"
  gsub_file(Rails.root.join('app', 'views', scope, "shared", "_nav.html.erb"), /\<\!-- #{placeholder_text} --\>\n/) do
<<-str
<%= nav_item t('active_record.models.#{plural_name}', :default => "#{controller_class_name}"), #{scope}_#{plural_name}_path %>
<!-- #{placeholder_text} -->
    
str
  end
end

#create_controller_filesObject



11
12
13
# File 'lib/generators/fullstack/admin/scaffold_generator.rb', line 11

def create_controller_files
  template 'controller.rb', File.join("app/controllers/", scope, "#{file_name.pluralize}_controller.rb")
end

#create_viewsObject



37
38
39
40
41
42
43
44
# File 'lib/generators/fullstack/admin/scaffold_generator.rb', line 37

def create_views
  if options[:views]
    directory('views', Rails.root.join('app', 'views', scope, plural_name))
    if has_timestamps? || title_column
      template "_filter.html.erb", Rails.root.join('app', 'views', scope, plural_name, "_filter.html.erb")
    end
  end
end