require 'rails/generators/migration' class Spud::ModuleGenerator < ::Rails::Generators::Base desc "Creates a new model, dashboard module, and frontend route" argument :module_name, :type => :string argument :attributes, :type => :array, :default => [], :banner => "field[:type][:index] field[:type][:index]" source_root File.expand_path('../templates', __FILE__) def create_module template "admin_controller.rb.erb", "app/controllers/admin/#{module_name_formatted}_controller.rb" template "controller.rb.erb", "app/controllers/#{module_name_formatted}_controller.rb" template "views/admin/index.html.erb", "app/views/admin/#{module_name_formatted}/index.html.erb" template "views/admin/new.html.erb", "app/views/admin/#{module_name_formatted}/new.html.erb" template "views/admin/edit.html.erb", "app/views/admin/#{module_name_formatted}/edit.html.erb" template "views/admin/_form.html.erb", "app/views/admin/#{module_name_formatted}/_form.html.erb" template "views/frontend/index.html.erb", "app/views/#{module_name_formatted}/index.html.erb" template "views/frontend/show.html.erb", "app/views/#{module_name_formatted}/show.html.erb" environment("Spud::Core.config.admin_applications += [{:name => '#{module_name_formatted.humanize.titlecase}', :thumbnail => \"/assets/admin/portfolio.png\",:url => \"/admin/#{module_name_formatted}\"}]") create_routes invoke "model", [module_name_formatted.singularize] + attributes end private def module_name_formatted module_name.pluralize.downcase.underscore end def create_routes route < [:index, :show] EOF end def field_for_attribute(type, name) case type when 'integer' "<%= f.number_field :#{name} %>" when 'text' "<%= f.text_area :#{name}, :rows => 4 %>" when 'boolean' "<%= f.check_box :#{name} %>" else "<%= f.text_field :#{name} %>" end end end