Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow users to override admin templates on a file by file basis #1226

Merged
merged 2 commits into from
Jul 1, 2013
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions padrino-admin/lib/padrino-admin/generators/admin_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@ def self.banner; "padrino-gen admin"; end
include Thor::Actions
include Padrino::Generators::Actions
include Padrino::Generators::Admin::Actions

# Look for custom template files in a generators folder under the project root
def source_paths
if File.exists? destination_root('generators', 'templates')
["#{destination_root('generators')}", File.expand_path(File.dirname(__FILE__))]
else
[File.expand_path(File.dirname(__FILE__))]
end
end

desc "Description:\n\n\tpadrino-gen admin generates a new Padrino Admin application"

Expand Down
9 changes: 9 additions & 0 deletions padrino-admin/lib/padrino-admin/generators/admin_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,15 @@ def self.banner; "padrino-gen admin_page [model]"; end
include Padrino::Generators::Actions
include Padrino::Generators::Admin::Actions

# Look for custom template files in a generators folder under the project root
def source_paths
if File.exists? destination_root('generators')
["#{destination_root('generators')}", File.expand_path(File.dirname(__FILE__))]
else
[File.expand_path(File.dirname(__FILE__))]
end
end

desc "Description:\n\n\tpadrino-gen admin_page model(s)"
argument :models, :desc => "The name(s) of your model(s)", :type => :array
class_option :skip_migration, :aliases => "-s", :default => false, :type => :boolean
Expand Down
9 changes: 9 additions & 0 deletions padrino-admin/test/generators/test_admin_app_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@
assert_match_in_file 'role.project_module :accounts, \'/accounts\'', "#{@apptmp}/sample_project/admin/app.rb"
end

# users can override certain templates from a generators/templates folder in the destination_root
it "should use custom generator templates from the project root, if they exist" do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '-d=activerecord') }
custom_template_path = "#{@apptmp}/sample_project/generators/templates/slim/app/layouts/"
`mkdir -p #{custom_template_path} && echo "h1 = 'Hello, custom generator' " > #{custom_template_path}application.slim.tt`
capture_io { generate(:admin_app, "--root=#{@apptmp}/sample_project") }
assert_match_in_file(/Hello, custom generator/, "#{@apptmp}/sample_project/admin/views/layouts/application.slim")
end

it "should generate the admin app under a different folder" do
# TODO FIXME Implement option --admin_root or something. See https://github.com/padrino/padrino-framework/issues/854#issuecomment-14749356
skip
Expand Down
11 changes: 11 additions & 0 deletions padrino-admin/test/generators/test_admin_page_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,17 @@ def self.properties
assert_match_in_file "elsif Padrino.env == :development && params[:bypass]", "#{@apptmp}/sample_project/admin/controllers/sessions.rb"
end

# users can override certain templates from a generators/templates folder in the destination_root
it "should use custom generator templates from the project root, if they exist" do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '-d=datamapper','-e=haml') }
custom_template_path = "#{@apptmp}/sample_project/generators/templates/haml/page/"
`mkdir -p #{custom_template_path} && echo "%h1= 'Hello, custom generator' " > #{custom_template_path}index.haml.tt`
capture_io { generate(:admin_app, "--root=#{@apptmp}/sample_project") }
capture_io { generate(:model, 'person', "name:string", "age:integer", "email:string", "--root=#{@apptmp}/sample_project") }
capture_io { generate(:admin_page, 'person', "--root=#{@apptmp}/sample_project") }
assert_match_in_file(/Hello, custom generator/, "#{@apptmp}/sample_project/admin/views/people/index.haml")
end

describe "renderers" do
it 'should correctly generate a new page with haml' do
capture_io { generate(:project, 'sample_project', "--root=#{@apptmp}", '-d=datamapper','-e=haml') }
Expand Down