Class: Mdwa::Generators::TemplatesGenerator

Inherits:
Rails::Generators::Base
  • Object
show all
Includes:
Rails::Generators::Migration
Defined in:
lib/generators/mdwa/templates/templates_generator.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ TemplatesGenerator

Constructor Require all entities to load the DSL of the application



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/generators/mdwa/templates/templates_generator.rb', line 23

def initialize(*args, &block)
  super
  
  # include files with entities
  # select entities that will be generated
  inside Rails.root do
    require_all MDWA::DSL::STRUCTURAL_PATH unless Dir.glob("#{MDWA::DSL::STRUCTURAL_PATH}/*.rb").count.zero?
  end
  
  # select entities that will be generated
  if entities.count.zero?
    @entities = MDWA::DSL.entities.all 
  else
    @entities = entities.collect{ |e| MDWA::DSL.entity(e) }
  end
end

Instance Attribute Details

#entitiesObject

Returns the value of attribute entities.



16
17
18
# File 'lib/generators/mdwa/templates/templates_generator.rb', line 16

def entities
  @entities
end

Instance Method Details

#entities_scaffoldObject



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/generators/mdwa/templates/templates_generator.rb', line 45

def entities_scaffold
  
  @entities.each do |entity|
    
    model = entity.generator_model
    
    puts '--------------------------------------'
    puts "- Templates for: #{entity.name} -"
    puts '--------------------------------------'
  
    copy_with_header 'scaffold/controller.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}controller.erb", entity.name
    copy_with_header 'scaffold/helper.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}helper.erb", entity.name
    copy_with_header 'scaffold/model.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}model.erb", entity.name
    
    # views
    copy_with_header 'scaffold/views/_form_fields.html.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}views/_form_fields.html.erb", entity.name
    copy_with_header 'scaffold/views/_form.html.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}views/_form.html.erb", entity.name
    copy_with_header 'scaffold/views/_list.html.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}views/_list.html.erb", entity.name
    copy_with_header 'scaffold/views/create.js.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}views/create.js.erb", entity.name
    copy_with_header 'scaffold/views/batch_update.js.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}views/batch_update.js.erb", entity.name
    copy_with_header 'scaffold/views/destroy.js.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}views/destroy.js.erb", entity.name
    copy_with_header 'scaffold/views/edit.html.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}views/edit.html.erb", entity.name
    copy_with_header 'scaffold/views/index.html.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}views/index.html.erb", entity.name
    copy_with_header 'scaffold/views/index.xls.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}views/index.xls.erb", entity.name
    copy_with_header 'scaffold/views/index.js.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}views/index.js.erb", entity.name
    copy_with_header 'scaffold/views/new.html.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}views/new.html.erb", entity.name
    copy_with_header 'scaffold/views/show.html.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}views/show.html.erb", entity.name
    copy_with_header 'scaffold/views/update.js.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}views/update.js.erb", entity.name

    # menu
    copy_with_header 'scaffold/views/menu/menu.html.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}views/menu/menu.html.erb", entity.name
  end
end

#entity_actionsObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/generators/mdwa/templates/templates_generator.rb', line 80

def entity_actions
  
  puts '--------------------------------------'
  puts "- Generating actions -"
  puts '--------------------------------------'
  
  @entities.each do |entity|
    # next iteration if entity doesn't have specifications
    next if entity.actions.actions.count.zero?

    model = entity.generator_model

    path_to_controller  = "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}controller.erb"
    controller_string   = File.read("#{Rails.root}/#{path_to_controller}")

    # hooks for code generations
    controller_hook = '#===controller_init==='
    test_hook = '#===test_init==='

    # insert in controller
    insert_into_file path_to_controller, :after => controller_hook do 
      actions = []
      entity.actions.generate_controller.each do |action_name, generation_string|
        # write the generated code only if it is not declared in the controller
        actions << "\n\n#{generation_string}" unless controller_string.include? "def #{action_name}"
      end
      actions.join
    end
    
    # generate the corresponding files
    entity.actions.actions.values.select{ |a| !a.resource? }.each do |action|
      action.template_names.each do |request, file_name|          
        case request.to_sym
        when :modalbox, :html
          copy_with_header 'actions/view.html.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}views/#{file_name}", entity unless File.exist?("#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/views/#{file_name}")
        when :ajax
          copy_with_header 'actions/view.js.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}views/#{file_name}", entity unless File.exist?("#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/views/#{file_name}")
        when :ajax_js
          copy_with_header 'actions/view.json.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}views/#{file_name}", entity unless File.exist?("#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/views/#{file_name}")
        else
          copy_with_header 'actions/view.custom.erb', "#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/#{model.space + '/'}views/#{file_name}", entity unless File.exist?("#{MDWA::DSL::TEMPLATES_PATH}#{entity.file_name}/views/#{file_name}")
        end
      end
    end

    # inject routes testing
    # if File.exist?(Rails.root + "/spec/routing/#{model.space}/#{model.plural_name}_routing_spec.rb")
    #   insert_into_file "spec/routing/#{model.space}/#{model.plural_name}_routing_spec.rb", :after => 'describe "routing" do' do
    #     routes = []
    #     entity.actions.actions.values.select {|a| !a.resource}.each do |action|
    #       routes << "\n\n\t\tit 'routes to ##{action.name}' do"
    #       routes << "\n\t\t\t#{action.method.to_s}('#{action.entity.generator_model.to_route_url}/#{'1/' if action.member?}#{action.name}').should route_to('#{action.entity.generator_model.to_route_url}##{action.name}' #{', :id => "1"' if action.member?})"
    #       routes << "\n\t\tend"
    #     end
    #     routes.join
    #   end
    # end

  end # @entities loop
  
end

#general_filesObject



41
42
43
# File 'lib/generators/mdwa/templates/templates_generator.rb', line 41

def general_files
  template 'general/routes.rb', "#{MDWA::DSL::TEMPLATES_PATH}routes.rb"
end

#testsObject



142
143
# File 'lib/generators/mdwa/templates/templates_generator.rb', line 142

def tests
end