Method: Mdwa::Generators::CodeGenerator#entities_actions
- Defined in:
- lib/generators/mdwa/code/code_generator.rb
#entities_actions ⇒ Object
Generate actions for entities. Generate controller actions and routes
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 |
# File 'lib/generators/mdwa/code/code_generator.rb', line 162 def entities_actions @all_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 = "app/controllers/#{model.space}/#{model.plural_name}_controller.rb" controller_string = File.read("#{Rails.root}/#{path_to_controller}") path_to_routes = 'config/routes.rb' # # inject methods in the controller # decide if code is included after class declaration or after cancan load code. cancan_load = "load_and_authorize_resource :class => \"#{model.klass}\"" if controller_string.include? cancan_load after = cancan_load else inherit_controller = 'A::BackendController' if model.space == 'a' after = "class #{model.controller_name}Controller < #{inherit_controller || 'ApplicationController'}" end # insert in controller insert_into_file path_to_controller, :after => after 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 # inject routes declarations insert_into_file path_to_routes, :after => "controller :#{model.plural_name} do" do routes = [] entity.actions.generate_routes.each do |action_name, generation_string| routes << "\n\t\t\t#{generation_string}" end routes.join 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 # 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 template 'views/view.html.erb', "app/views/#{model.space}/#{model.plural_name}/#{file_name}" unless File.exist?("#{Rails.root}/app/views/#{model.space}/#{model.plural_name}/#{file_name}") when :ajax template 'views/view.js.erb', "app/views/#{model.space}/#{model.plural_name}/#{file_name}" unless File.exist?("#{Rails.root}/app/views/#{model.space}/#{model.plural_name}/#{file_name}") when :ajax_js template 'views/view.json.erb', "app/views/#{model.space}/#{model.plural_name}/#{file_name}" unless File.exist?("#{Rails.root}/app/views/#{model.space}/#{model.plural_name}/#{file_name}") else template 'views/view.custom.erb', "app/views/#{model.space}/#{model.plural_name}/#{file_name}" unless File.exist?("#{Rails.root}/app/views/#{model.space}/#{model.plural_name}/#{file_name}") end end end end # iteration over entities end |