Permalink
Browse files

support for customizing generators

  • Loading branch information...
1 parent 05cfabf commit 3a459122716cd8f452719155209e85ad1dcbcae2 @laeciofreitas laeciofreitas committed Nov 24, 2011
View
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
- vraptor-scaffold (1.2.2)
+ vraptor-scaffold (1.3.0)
activesupport (= 3.0.1)
rake (= 0.9.2)
thor (= 0.14.6)
@@ -5,7 +5,7 @@ def initialize(model, attributes={})
@model = model
@attributes = attributes
end
-
+
def model_parameter_name
@model.camelize(:lower)
end
@@ -41,4 +41,19 @@ def controller_test_class_name
def base_path
model_parameter_name.pluralize
end
+
+ protected
+ def define_source_paths
+ source_paths << File.expand_path(template_path) if File.exists?(template_path)
+ source_paths << File.join(File.dirname(__FILE__), source_root)
+ end
+
+ def source_root
+ raise "abstract method"
+ end
+
+ def template_path
+ raise "abstract method"
+ end
+
end
@@ -1,13 +1,18 @@
class ControllerGenerator < BaseScaffold
-
- def self.source_root
- File.join File.dirname(__FILE__), "templates"
- end
-
+
def build
+ define_source_paths
template("controller.erb", Configuration.main_class_path(Configuration.controllers_package, "#{controller_class_name}.java"))
template("controller_test.erb", Configuration.test_class_path(Configuration.controllers_package, "#{controller_test_class_name}.java"))
end
+
+ def template_path
+ "src/templates/controller"
+ end
+
+ def source_root
+ "controller_generator/templates"
+ end
def path
"/#{base_path}"
@@ -1,20 +1,26 @@
class ModelGenerator < BaseScaffold
- def self.source_root
- File.join File.dirname(__FILE__), "templates"
- end
-
def build
+ define_source_paths
template("model.erb", Configuration.main_class_path(Configuration.models_package, "#{class_name}.java"))
template("model_test.erb", Configuration.test_class_path(Configuration.models_package, "#{test_class_name}.java"))
map_orm_class if Configuration.hibernate?
end
+ def template_path
+ "src/templates/model"
+ end
+
+ def source_root
+ "model_generator/templates"
+ end
+
private
def map_orm_class
file = File.join Configuration::MAIN_RESOURCES, "hibernate.cfg.xml"
model = "#{Configuration.full_models_package}.#{class_name}"
line_break = "\n "
inject_into_file(file, " <mapping class='#{model}'/>#{line_break}", :before => "</session-factory>")
end
+
end
@@ -1,10 +1,7 @@
class RepositoryGenerator < BaseScaffold
- def self.source_root
- File.join File.dirname(__FILE__), "templates"
- end
-
def build
+ define_source_paths
template("repository.erb", Configuration.main_class_path(Configuration.repositories_package, "#{repository_interface_name}.java"))
template("repository_impl.erb", Configuration.main_class_path(Configuration.repositories_package, "#{repository_impl_name}.java"))
template("repository_test.erb", Configuration.test_class_path(Configuration.repositories_package, "#{repository_test_class_name}.java"))
@@ -24,4 +21,12 @@ def orm_class
orm_parameter_name.camelize
end
+ def template_path
+ "src/templates/repository"
+ end
+
+ def source_root
+ "repository_generator/templates"
+ end
+
end
@@ -1,9 +1,5 @@
class FreemarkerGenerator < TemplateEngineGenerator
- def self.source_root
- File.dirname(__FILE__)
- end
-
def view_folder
"views"
end
@@ -15,4 +11,13 @@ def extension
def path
"${base}/#{base_path}"
end
+
+ def template_path
+ "src/templates/engine"
+ end
+
+ def source_root
+ "template_engine_generator/freemarker_generator/templates"
+ end
+
end
@@ -1,9 +1,5 @@
class JspGenerator < TemplateEngineGenerator
- def self.source_root
- File.dirname(__FILE__)
- end
-
def view_folder
"jsp"
end
@@ -15,4 +11,13 @@ def extension
def path url=""
%Q{${pageContext.request.contextPath}/#{base_path}#{url}}
end
+
+ def template_path
+ "src/templates/engine"
+ end
+
+ def source_root
+ "template_engine_generator/jsp_generator/templates"
+ end
+
end
@@ -1,6 +1,7 @@
class TemplateEngineGenerator < BaseScaffold
def build
+ define_source_paths
create_view("index")
create_view("show")
create_view("form")
@@ -9,7 +10,7 @@ def build
end
def create_view(template_name, file_name=template_name)
- template("templates/#{template_name}.erb", "#{views_path}/#{file_name}.#{extension}")
+ template("#{template_name}.erb", "#{views_path}/#{file_name}.#{extension}")
end
def views_path
@@ -9,6 +9,14 @@
it "paths to compound model name" do
ControllerGenerator.new("orderItem", build_attributes).path.should == "/orderItems"
end
+
+ it "template path off vraptor-scaffold" do
+ ControllerGenerator.new("category", build_attributes).template_path == "src/templates/controller"
+ end
+
+ it "source root path in vraptor-scaffold" do
+ ControllerGenerator.new("category", build_attributes).source_root == "controller_generator/templates"
+ end
describe "generating from a lowercased name" do
before(:each) do
@@ -2,6 +2,14 @@
describe FreemarkerGenerator do
+ it "template path off vraptor-scaffold" do
+ FreemarkerGenerator.new("category", build_attributes).template_path == "src/templates/engine"
+ end
+
+ it "source root path in vraptor-scaffold" do
+ ControllerGenerator.new("category", build_attributes).source_root == "freemarker_generator/templates"
+ end
+
it "should append context to path" do
FreemarkerGenerator.new("myModel", build_attributes).path.should == "${base}/myModels"
end
@@ -6,6 +6,14 @@
JspGenerator.new("myModel", build_attributes).path("/edit").should == "${pageContext.request.contextPath}/myModels/edit"
end
+ it "template path off vraptor-scaffold" do
+ JspGenerator.new("category", build_attributes).template_path == "src/templates/engine"
+ end
+
+ it "source root path in vraptor-scaffold" do
+ JspGenerator.new("category", build_attributes).source_root == "jsp_generator/templates"
+ end
+
context "simple model name" do
before(:all) do
model = "product"
@@ -2,6 +2,14 @@
describe ModelGenerator do
+ it "template path off vraptor-scaffold" do
+ ModelGenerator.new("category", build_attributes).template_path == "src/templates/model"
+ end
+
+ it "source root path in vraptor-scaffold" do
+ ModelGenerator.new("category", build_attributes).source_root == "model_generator/templates"
+ end
+
context "jpa" do
before(:each) do
mock_config_file

0 comments on commit 3a45912

Please sign in to comment.