Project

General

Profile

Feature #6004 ยป 6004_git.diff

Jean-Baptiste Barth, 2010-08-04 22:16

View differences:

lib/generators/redmine_plugin_scaffold/USAGE
Description:
Scaffolds an entire resource, from model and migration to controller and views,
in a Redmine plugin.
Examples:
`./script/generate redmine_plugin_scaffold MyPlugin post`
`./script/generate redmine_plugin_scaffold MyPlugin post title:string body:text published:boolean`
`./script/generate redmine_plugin_scaffold MyPlugin purchase order_id:integer amount:decimal`
lib/generators/redmine_plugin_scaffold/redmine_plugin_scaffold_generator.rb
require 'rails_generator/base'
require 'rails_generator/generators/components/scaffold/scaffold_generator'
class RedminePluginScaffoldGenerator < ScaffoldGenerator
attr_reader :plugin_path, :plugin_name, :plugin_pretty_name
def initialize(runtime_args, runtime_options = {})
runtime_args = runtime_args.dup
usage if runtime_args.empty?
@plugin_name = "redmine_" + runtime_args.shift.underscore
@plugin_pretty_name = plugin_name.titleize
@plugin_path = "vendor/plugins/#{plugin_name}"
super(runtime_args, runtime_options)
end
def destination_root
File.join(RAILS_ROOT, plugin_path)
end
def manifest
record do |m|
# Check for class naming collisions.
m.class_collisions("#{controller_class_name}Controller", "#{controller_class_name}Helper")
m.class_collisions(class_name)
# Controller, helper, views, test and stylesheets directories.
m.directory(File.join('app/models', class_path))
m.directory(File.join('app/controllers', controller_class_path))
m.directory(File.join('app/helpers', controller_class_path))
m.directory(File.join('app/views', controller_class_path, controller_file_name))
m.directory(File.join('test/functional', controller_class_path))
m.directory(File.join('test/unit', class_path))
m.directory(File.join('test/unit/helpers', class_path))
for action in scaffold_views
m.template(
"view_#{action}.html.erb",
File.join('app/views', controller_class_path, controller_file_name, "#{action}.html.erb")
)
end
m.template(
"view_form.html.erb",
File.join('app/views', controller_class_path, controller_file_name, "_form.html.erb")
) if scaffold_views.include?("edit") || scaffold_views.include?("new")
# Layout and stylesheet.
m.template(
'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
)
m.template('functional_test.rb', File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb"))
m.template('helper.rb', File.join('app/helpers', controller_class_path, "#{controller_file_name}_helper.rb"))
m.template('helper_test.rb', File.join('test/unit/helpers', controller_class_path, "#{controller_file_name}_helper_test.rb"))
route_file = File.join('config','routes.rb')
m.template('routes.rb', route_file) unless File.file?(File.join(destination_root,route_file))
m.route_resources controller_file_name
m.dependency 'redmine_plugin_model', [@plugin_name.gsub(/redmine_/,''),name] + @args, :collision => :skip
end
end
protected
# Override with your own usage banner.
def banner
"Usage: #{$0} redmine_plugin_scaffold PluginName ModelName [field:type, field:type]"
end
end
lib/generators/redmine_plugin_scaffold/templates/controller.rb
class <%= controller_class_name %>Controller < ApplicationController
unloadable
#layout 'admin'
#menu_item :issues
def index
@<%= table_name %> = <%= class_name %>.all
end
def show
@<%= file_name %> = <%= class_name %>.find(params[:id])
end
def new
@<%= file_name %> = <%= class_name %>.new
end
def edit
@<%= file_name %> = <%= class_name %>.find(params[:id])
end
def create
@<%= file_name %> = <%= class_name %>.new(params[:<%= file_name %>])
if @<%= file_name %>.save
flash[:notice] = l(:notice_successful_create)
redirect_to(@<%= file_name %>)
else
render :action => "new"
end
end
def update
@<%= file_name %> = <%= class_name %>.find(params[:id])
if @<%= file_name %>.update_attributes(params[:<%= file_name %>])
flash[:notice] = l(:notice_successful_update)
redirect_to(@<%= file_name %>)
else
render :action => "edit"
end
end
def destroy
@<%= file_name %> = <%= class_name %>.find(params[:id])
@<%= file_name %>.destroy
flash[:notice] = l(:notice_successful_delete)
redirect_to(<%= table_name %>_url)
end
end
lib/generators/redmine_plugin_scaffold/templates/functional_test.rb
require File.dirname(__FILE__) + '/../test_helper'
class <%= controller_class_name %>ControllerTest < ActionController::TestCase
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:<%= table_name %>)
end
test "should get new" do
get :new
assert_response :success
end
test "should create <%= file_name %>" do
assert_difference('<%= class_name %>.count') do
post :create, :<%= file_name %> => { }
end
assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>))
end
test "should show <%= file_name %>" do
get :show, :id => <%= class_name %>.find(1).to_param
assert_response :success
end
test "should get edit" do
get :edit, :id => <%= class_name %>.find(1).to_param
assert_response :success
end
test "should update <%= file_name %>" do
put :update, :id => <%= class_name %>.find(1).to_param, :<%= file_name %> => { }
assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>))
end
test "should destroy <%= file_name %>" do
assert_difference('<%= class_name %>.count', -1) do
delete :destroy, :id => <%= class_name %>.find(1).to_param
end
assert_redirected_to <%= table_name %>_path
end
end
lib/generators/redmine_plugin_scaffold/templates/helper.rb
module <%= controller_class_name %>Helper
end
lib/generators/redmine_plugin_scaffold/templates/helper_test.rb
require File.dirname(__FILE__) + '/../../test_helper'
require 'action_view/test_case'
class <%= controller_class_name %>HelperTest < ActionView::TestCase
end
lib/generators/redmine_plugin_scaffold/templates/routes.rb
ActionController::Routing::Routes.draw do |map|
end
lib/generators/redmine_plugin_scaffold/templates/view_edit.html.erb
<h2><%%= link_to l(:label_<%=singular_name%>_plural), <%=plural_name%>_path %> &#187; <%%=h @<%=singular_name%>.<%=attributes.first.name %>%></h2>
<%% labelled_tabular_form_for :<%=singular_name%>, @<%=singular_name%>, :url => {:controller=>'<%=plural_name%>', :action=>'update'}, :html => { :class => nil, :method => :put } do |f| -%>
<%%= render :partial => 'form', :locals => { :f => f } %>
<p><%%= submit_tag l(:button_update) %></p>
<%% end -%>
lib/generators/redmine_plugin_scaffold/templates/view_form.html.erb
<%%= error_messages_for '<%=singular_name%>' %>
<!--[form:<%=singular_name%>]-->
<div class="box tabular">
<% for attribute in attributes -%>
<p><%%= f.<%= attribute.field_type %> :<%= attribute.name %> %></p>
<% end -%>
</div>
<!--[eoform:<%=singular_name%>]-->
lib/generators/redmine_plugin_scaffold/templates/view_index.html.erb
<div class="contextual">
<%%= link_to l(:label_<%=singular_name%>_new), {:controller=>'<%=plural_name%>',:action=>'new'},
:class => 'icon icon-add' %>
</div>
<h2><%%=l(:label_<%=singular_name%>_plural)%></h2>
<%% if @<%=plural_name%>.any? -%>
<table class="list">
<thead>
<tr>
<% for attribute in attributes -%>
<th><%%= l(:field_<%=attribute.column.name%>)%></th>
<% end -%>
<th> </th>
</tr>
</thead>
<tbody>
<%% @<%= plural_name %>.each do |<%= singular_name %>| -%>
<tr class="<%=singular_name%> <%%= cycle("odd", "even") %>">
<% for attribute in attributes -%>
<% if attribute==attributes.first %>
<td class="<%=attribute.name%>"><%%=link_to h(<%= singular_name %>.<%= attribute.name %>), {:controller => '<%=plural_name%>', :action => 'show', :id => <%=singular_name%>} %></td>
<% else %>
<td class="<%=attribute.name%>"><%%=h <%= singular_name %>.<%= attribute.name %> %></td>
<% end %>
<% end -%>
<td class="buttons">
<%%= link_to l(:button_update), {:controller => '<%=plural_name%>', :action => 'edit', :id => <%=singular_name%>},
:class => 'icon icon-edit' %>
<%%= link_to l(:button_delete), {:controller => '<%=plural_name%>', :action => 'destroy', :id => <%=singular_name%>},
:class => 'icon icon-del', :confirm => l(:text_are_you_sure), :method => :delete %>
</td>
</tr>
<%% end -%>
</tbody>
</table>
<%% else -%>
<p class="nodata"><%%= l(:label_no_data) %></p>
<%% end -%>
<%% html_title(l(:label_<%= singular_name %>_plural)) -%>
lib/generators/redmine_plugin_scaffold/templates/view_new.html.erb
<h2><%%= link_to l(:label_<%=singular_name%>_plural), <%=plural_name%>_path %> &#187; <%%=l(:label_<%=singular_name%>_new) %></h2>
<%% labelled_tabular_form_for :<%=singular_name%>, @<%=singular_name%>, :url => {:controller=>'<%=plural_name%>', :action=>'create'}, :html => { :class => nil } do |f| -%>
<%%= render :partial => 'form', :locals => { :f => f } %>
<p><%%= submit_tag l(:button_create) %></p>
<%% end -%>
lib/generators/redmine_plugin_scaffold/templates/view_show.html.erb
<div class="contextual">
<%%= link_to l(:button_update), {:controller => '<%=plural_name%>', :action => 'edit', :id => @<%=singular_name%>},
:class => 'icon icon-edit' %>
<%%= link_to l(:button_delete), {:controller => '<%=plural_name%>', :action => 'destroy', :id => @<%=singular_name%>},
:class => 'icon icon-del', :confirm => l(:text_are_you_sure), :method => :delete %>
</div>
<h2><%%= link_to l(:label_<%=singular_name%>_plural), <%=plural_name%>_path %> &#187; <%%=h @<%=singular_name%>.<%=attributes.first.name %>%></h2>
<% for attribute in attributes -%>
<p>
<b><%%= l(:field_<%=attribute.column.name%>)%>:</b>
<%%=h @<%= singular_name %>.<%= attribute.name %> %>
</p>
<% end -%>
<%% html_title(l(:label_<%=singular_name%>_plural)) -%>
    (1-1/1)