class EmergentCoreGenerator < Rails::Generator::NamedBase include RestfulX::Configuration attr_reader :project_name, :flex_project_name, :base_package, :base_folder, :command_controller_name, # Added :model_names, :event_names attr_reader :belongs_tos, :has_manies, :has_ones attr_reader :controller_name, :controller_class_path, :controller_file_path, :controller_class_nesting, :controller_class_nesting_depth, :controller_class_name, :controller_underscore_name, :controller_singular_name, :controller_plural_name alias_method :controller_file_name, :controller_underscore_name alias_method :controller_table_name, :controller_plural_name def initialize(runtime_args, runtime_options = {}) super @project_name, @flex_project_name, @command_controller_name, @base_package, @base_folder = extract_names @controller_name = @name.pluralize base_name, @controller_class_path, @controller_file_path, @controller_class_nesting, @controller_class_nesting_depth = extract_modules(@controller_name) @controller_class_name_without_nesting, @controller_underscore_name, @controller_plural_name = inflect_names(base_name) @controller_singular_name=base_name.singularize if @controller_class_nesting.empty? @controller_class_name = @controller_class_name_without_nesting else @controller_class_name = "#{@controller_class_nesting}::#{@controller_class_name_without_nesting}" end extract_relationships end def manifest record do |m| m.dependency 'scaffold', [name] + @args, :skip_migration => true, :collision => :skip unless options[:flex_only] # Create Flex AS3 Model... m.template 'ruboss_scaffold/model.as.erb', File.join("app", "flex", base_folder, "models/domain", "#{@class_name}.as"), :assigns => { :resource_controller_name => "#{file_name.pluralize}" } # Create PresentationModel which presents the Model to the View... m.template 'ruboss_scaffold/presentation_model.as.erb', File.join("app", "flex", base_folder, "models/presentation", "#{@class_name.pluralize}Model.as") # Create your view folder to hold the 5+ view classes per resource m.directory File.join('app/flex', base_package, "views/#{@class_name.pluralize.dcfirst}") # Create resource container View (acts as a component container)... m.template 'ruboss_scaffold/views_panel.mxml.erb', File.join("app", "flex", base_folder, "views/#{@class_name.pluralize.dcfirst}", "#{@class_name.pluralize}View.mxml") # Create IndexView m.template 'ruboss_scaffold/index_view.mxml.erb', File.join("app", "flex", base_folder, "views/#{@class_name.pluralize.dcfirst}", "#{@class_name.pluralize}IndexView.mxml") # Create EditIndexView (multi-resource edit panel) m.template 'ruboss_scaffold/edit_index_view.mxml.erb', File.join("app", "flex", base_folder, "views/#{@class_name.pluralize.dcfirst}", "#{@class_name.pluralize}EditIndexView.mxml") # Create ItemRenderer (aka TileView).... Used for lists m.template 'ruboss_scaffold/item_renderer.mxml.erb', File.join("app", "flex", base_folder, "views/#{@class_name.pluralize.dcfirst}", "#{@class_name}ItemRenderer.mxml") # Create ViewPresenter to hold all view logic m.template 'ruboss_scaffold/presenter.as.erb', File.join('app/flex', base_folder, "views/#{@class_name.pluralize.dcfirst}", "#{@class_name.pluralize}Presenter.as") # Create Manager to hold all model logic m.template 'ruboss_scaffold/manager.as.erb', File.join('app/flex', base_folder, "controllers/manager", "#{@class_name}Manager.as") # Create View event map m.template 'ruboss_scaffold/view_map.mxml.erb', File.join('app/flex', base_folder, "controllers/map/view", "#{@class_name.pluralize}NavigationEventMap.mxml") # Create Ruby Controller... m.template 'ruboss_scaffold/controller.rb.erb', File.join("app/controllers", controller_class_path, "#{controller_underscore_name}_controller.rb"), :collision => :force unless options[:flex_only] # Create a new generated ActiveRecord model based on the Ruboss templates. m.template 'ruboss_scaffold/model.rb.erb', File.join("app", "models", "#{file_name}.rb"), :collision => :force unless options[:flex_only] unless options[:skip_fixture] m.template 'fixtures.yml.erb', File.join("test", "fixtures", "#{table_name}.yml"), :collision => :force unless options[:flex_only] end unless options[:skip_migration] m.directory 'schema/migration' m.migration_template 'migration.rb.erb', 'db/migrate', :assigns => { :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}" }, :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}" unless options[:flex_only] end m.dependency 'rx_controller', [name] + @args, :collision => :force end protected def extract_relationships @belongs_tos = [] @has_ones = [] @has_manies = [] # Figure out has_one, has_many and belongs_to based on args @args.each do |arg| if arg =~ /^has_one:/ # arg = "has_one:arg1,arg2", so all the has_one are together @has_ones = arg.split(':')[1].split(',') elsif arg =~ /^has_many:/ # arg = "has_many:arg1,arg2", so all the has_many are together @has_manies = arg.split(":")[1].split(",") elsif arg =~ /^belongs_to:/ # belongs_to:arg1,arg2 @belongs_tos = arg.split(":")[1].split(',') end end # Remove the has_one and has_many arguments since they are # not for consumption by the scaffold generator, and since # we have already used them to set the @belongs_tos, @has_ones and # @has_manies. @args.delete_if { |elt| elt =~ /^(has_one|has_many|belongs_to):/ } end def add_options!(opt) opt.separator '' opt.separator 'Options:' opt.on("--flex-only", "Scaffold Flex code only", "Default: false") { |v| options[:flex_only] = v} end def model_names @model_names = [] if File.exists?("app/flex/#{base_folder}/models/domain") @model_names = list_as_files("app/flex/#{base_folder}/models/domain") end end def list_as_files(dir_name) Dir.entries(dir_name).grep(/\.as$/).map { |name| name.sub(/\.as$/, "") } end end