Class: Stager::ControllerGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Defined in:
lib/generators/stager/controller/controller_generator.rb

Instance Method Summary collapse

Instance Method Details

#create_controllerObject



50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/generators/stager/controller/controller_generator.rb', line 50

def create_controller
  template 'controller.rb', "app/controllers/#{plural_name}_controller.rb"
  template 'helper.rb', "app/helpers/#{plural_name}_helper.rb" unless options[:skip_helper]
  
  namespaces = plural_name.split('/')
  resource = namespaces.pop
  if namespaces.present?
    route "namespace(:#{namespaces.first}) { resources :#{resource} }"
  else
    route "resources :#{resource}"
  end
  
  template "spec/controller.rb", "spec/controllers/#{plural_name}_controller_spec.rb"
end

#create_viewsObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/generators/stager/controller/controller_generator.rb', line 65

def create_views
  unless options[:skip_views]
    controller_actions.each do |action|
      if %w[index show new edit].include?(action)
        template "views/#{action}.html.haml", "app/views/#{plural_name}/#{action}.html.haml"
      end
    end
    
    if form_partial?
      template "views/_form.html.haml", "app/views/#{plural_name}/_form.html.haml"
    end
  end
end

#set_optionsObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/generators/stager/controller/controller_generator.rb', line 15

def set_options
  @controller_actions = []
  @model_attributes = []
  @invert_actions = false
  @namespaced_model = false
  
  model_and_controller_args.each do |arg|
    if arg == '!'
      @invert_actions = true
    elsif arg.include?(':')
      @model_attributes << Rails::Generators::GeneratedAttribute.new(*arg.split(':'))
    else
      @controller_actions << arg
      @controller_actions << 'create' if arg == 'new'
      @controller_actions << 'update' if arg == 'edit'
    end
  end
  
  if options[:namespaced_model]
    @namespaced_model = true
  end
  
  if @invert_actions || @controller_actions.empty?
    @controller_actions = all_actions - @controller_actions
  end
  
  if @model_attributes.empty?
    if model_exists?
      model_columns_for_attributes.each do |column|
        @model_attributes << Rails::Generators::GeneratedAttribute.new(column.name.to_s, column.type.to_s)
      end
    end
  end
end