Class: Somatics::Generators::ScaffoldControllerGenerator

Inherits:
Rails::Generators::NamedBase
  • Object
show all
Extended by:
TemplatePath
Includes:
Rails::Generators::ResourceHelpers
Defined in:
lib/generators/somatics/scaffold_controller/scaffold_controller_generator.rb

Direct Known Subclasses

SettingsControllerGenerator

Instance Method Summary collapse

Methods included from TemplatePath

source_root

Instance Method Details

#add_header_menu_tabObject



40
41
42
43
44
45
46
# File 'lib/generators/somatics/scaffold_controller/scaffold_controller_generator.rb', line 40

def add_header_menu_tab
  if options[:header]
    look_for = "<li><%= link_to '#{file_name.humanize}', '/admin/#{controller_file_name}', :class => (match_controller?('#{controller_file_name}'))  ? 'selected' : ''%>\n\t<ul class='sub'></ul></li>\n"
    gsub_file File.join('app/views/admin/shared', "_menu.html.erb"), look_for, ''
    append_file File.join('app/views/admin/shared', "_menu.html.erb"), look_for
  end
end

#add_resource_routeObject



55
56
57
58
59
60
61
62
# File 'lib/generators/somatics/scaffold_controller/scaffold_controller_generator.rb', line 55

def add_resource_route
  route_config =  "namespace :#{namespace_name} do "
  route_config <<  class_path.collect{|namespace| "namespace :#{namespace} do " }.join(" ")
  route_config << "resources :#{file_name.pluralize}"
  route_config << " end" * class_path.size
  route_config << " end"
  route route_config
end

#create_controller_filesObject



23
24
25
# File 'lib/generators/somatics/scaffold_controller/scaffold_controller_generator.rb', line 23

def create_controller_files
  template 'controller.rb', File.join('app/controllers', options[:namespace], class_path, "#{controller_file_name}_controller.rb")
end

#create_locales_filesObject



48
49
50
51
52
53
# File 'lib/generators/somatics/scaffold_controller/scaffold_controller_generator.rb', line 48

def create_locales_files
  # # Locales templates 
  options[:locales].each do |locale|
    template "locales_#{locale}.yml", File.join('config/locales', "#{controller_file_name}_#{locale}.yml")
  end
end

#create_view_filesObject



26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generators/somatics/scaffold_controller/scaffold_controller_generator.rb', line 26

def create_view_files
  template "partial_form.html.erb",     File.join('app/views',options[:namespace], class_path, controller_file_name, "_form.html.erb"   )
  template "partial_list.html.erb",     File.join('app/views',options[:namespace], class_path, controller_file_name, "_list.html.erb"   )
  template "partial_show.html.erb",     File.join('app/views',options[:namespace], class_path, controller_file_name, "_show.html.erb"   )
  template "partial_edit.html.erb",     File.join('app/views',options[:namespace], class_path, controller_file_name, "_edit.html.erb"   )
  template "partial_bulk.html.erb",     File.join('app/views',options[:namespace], class_path, controller_file_name, "_bulk.html.erb"   )
  template "view_index.html.erb",       File.join('app/views',options[:namespace], class_path, controller_file_name, "index.html.erb"   )
  template "view_new.html.erb",         File.join('app/views',options[:namespace], class_path, controller_file_name, "new.html.erb"     )
  template "view_show.html.erb",        File.join('app/views',options[:namespace], class_path, controller_file_name, "show.html.erb"    )
  template "view_edit.html.erb",        File.join('app/views',options[:namespace], class_path, controller_file_name, "edit.html.erb"    )
  template "builder_index.xml.builder", File.join('app/views',options[:namespace], class_path, controller_file_name, "index.xml.builder")
  template "builder_index.xls.builder", File.join('app/views',options[:namespace], class_path, controller_file_name, "index.xls.builder")
end

#inject_paper_trailObject



79
80
81
82
83
84
85
86
# File 'lib/generators/somatics/scaffold_controller/scaffold_controller_generator.rb', line 79

def inject_paper_trail
  unless options[:skip_auditing]
    # FIXME The model generator removes the model.rb already
    if File.exists?("app/models/#{singular_name}.rb")
      inject_into_class "app/models/#{singular_name}.rb", class_name, "  has_paper_trail :ignore => [:updated_at]\n"
    end
  end
end

#inject_somatics_filtersObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/generators/somatics/scaffold_controller/scaffold_controller_generator.rb', line 64

def inject_somatics_filters
  filters = ["  has_filter :id, :integer, :default => true\n", "  has_filter :created_at, :date\n"]
  filters |= attributes.collect do |attribute|
    "  has_filter :#{attribute.name}, :#{attribute.type}\n"
  end
  # FIXME The model generator removes the model.rb already
  if File.exists?("app/models/#{singular_name}.rb")
    inject_into_class "app/models/#{singular_name}.rb", class_name, filters.join('')
    inject_into_class "app/models/#{singular_name}.rb", class_name,
<<-RUBY
  # Somatics Filter (Reference: http://github.com/inspiresynergy/somatics_filter)
RUBY
  end
end