Class: Padrino::Generators::Scaffold
- Inherits:
-
Thor::Group
- Object
- Thor::Group
- Padrino::Generators::Scaffold
- Includes:
- Padrino::Generators, Actions, Components::Actions, Runner, Thor::Actions
- Defined in:
- lib/lazy-head-gen/scaffold.rb
Class Method Summary collapse
-
.banner ⇒ Object
Defines the banner for this CLI generator.
-
.source_root ⇒ Object
Define the source template root.
Instance Method Summary collapse
Class Method Details
.banner ⇒ Object
Defines the banner for this CLI generator
11 |
# File 'lib/lazy-head-gen/scaffold.rb', line 11 def self.; "padrino g scaffold [name]"; end |
.source_root ⇒ Object
Define the source template root
9 |
# File 'lib/lazy-head-gen/scaffold.rb', line 9 def self.source_root; File.(File.dirname(__FILE__)); end |
Instance Method Details
#create_scaffold ⇒ Object
44 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 78 79 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 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/lazy-head-gen/scaffold.rb', line 44 def create_scaffold self.destination_root = [:root] if in_app_root? app = [:app_path] check_app_existence(app) @app_name = fetch_app_name(app) say "Generating a Padrino scaffold for '#{name}' in app '#{@app_name}'", :green # Set variables @pluralized = name.to_s.underscore.pluralize @singular = name.to_s.underscore.singularize @controller = @pluralized.camelize @model = @singular.camelize @properties = properties @create_full = [:create_full_actions] # Create model if invalids = invalid_fields(@properties) say "Invalid property name:", :red say " #{invalids.join(", ")}" raise SystemExit end unless include_component_module_for(:orm) say "You need an ORM adapter to create this scaffold's model", :red return end # TODO: if model path does not equal "." should we check app exists? # model_path = options[:model_path] model_path = "." template "templates/scaffold/model.rb.tt", destination_root(model_path, "models", "#{@singular}.rb") template "templates/scaffold/model_test.rb.tt", destination_root("test", model_path, "models", "#{@singular}_test.rb") # Create controller template "templates/scaffold/controller.rb.tt", destination_root(app, "controllers", "#{@pluralized}.rb") template "templates/scaffold/controller_test.rb.tt", destination_root("test", app, "controllers", "#{@pluralized}_controller_test.rb") template "templates/scaffold/helper.rb.tt", destination_root(app, "helpers", "#{@pluralized}_helper.rb") # Create views views = ["index", "show"] if @create_full views << "new" << "edit" end views.each do |view| @view = view template "templates/scaffold/view.erb.tt", destination_root(app, "views", "#{@pluralized}", "#{@view}.erb") end # Create a migration unless skipped create_model_migration("create_#{@pluralized}", name, properties) unless [:skip_migration] # Check if blueprints file exists and copy into place if not if !File.exist?(destination_root("test", "blueprints.rb")) template "templates/scaffold/blueprints.rb.tt", destination_root("test", "blueprints.rb") end # Insert into text_config.rb require blueprints.rb require_string = "\nrequire File.expand_path(File.dirname(__FILE__) + \"/blueprints.rb\")" test_config_contents = File.read(destination_root("test", "test_config.rb")) insert_into_file destination_root("test", "test_config.rb"), require_string, :after => "require File.expand_path('../../config/boot', __FILE__)" # Check if a blueprint for the model has already been created as # we don't want to wipe out an existing one blueprints_contents = File.read(destination_root("test", "blueprints.rb")) if !blueprints_contents.match("#{@model}.blueprint do") # build a string of properties to insert into the blueprint entry props = "" @properties.each do |property| values = property.split(":") faker = "" case values.last when "boolean" faker += "true" when "datetime" faker += "DateTime.now" when "integer" faker += "1 + rand(100)" else faker += "Faker::Lorem.sentence" end props += "\t#{values.first} { #{faker} }" props += "\n" unless property == @properties.last end # Format the blueprint entry model_blueprint = <<-MODEL #{@model}.blueprint do #{props} end MODEL # Append blueprints.rb append_file destination_root("test", "blueprints.rb"), model_blueprint else say "Blueprints entry already exists for '#{@singular}'", :yellow end say "Scaffold generation for '#{name}' in app '#{app}' completed", :green else say "You are not at the root of a Padrino application! (config/boot.rb not found)", :red end end |