Project

General

Profile

Feature #6004 ยป 6004_git.diff

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

View differences:

lib/generators/redmine_plugin_scaffold/USAGE
1
Description:
2
    Scaffolds an entire resource, from model and migration to controller and views, 
3
    in a Redmine plugin.
4

  
5
Examples:
6
    `./script/generate redmine_plugin_scaffold MyPlugin post`
7
    `./script/generate redmine_plugin_scaffold MyPlugin post title:string body:text published:boolean`
8
    `./script/generate redmine_plugin_scaffold MyPlugin purchase order_id:integer amount:decimal`
lib/generators/redmine_plugin_scaffold/redmine_plugin_scaffold_generator.rb
1
require 'rails_generator/base'
2
require 'rails_generator/generators/components/scaffold/scaffold_generator'
3

  
4
class RedminePluginScaffoldGenerator < ScaffoldGenerator
5
  attr_reader :plugin_path, :plugin_name, :plugin_pretty_name
6
  
7
  def initialize(runtime_args, runtime_options = {})
8
    runtime_args = runtime_args.dup
9
    usage if runtime_args.empty?
10
    @plugin_name = "redmine_" + runtime_args.shift.underscore
11
    @plugin_pretty_name = plugin_name.titleize
12
    @plugin_path = "vendor/plugins/#{plugin_name}"
13
    super(runtime_args, runtime_options)
14
  end
15
  
16
  def destination_root
17
    File.join(RAILS_ROOT, plugin_path)
18
  end
19
  
20
  def manifest
21
    record do |m|
22
      # Check for class naming collisions.
23
      m.class_collisions("#{controller_class_name}Controller", "#{controller_class_name}Helper")
24
      m.class_collisions(class_name)
25

  
26
      # Controller, helper, views, test and stylesheets directories.
27
      m.directory(File.join('app/models', class_path))
28
      m.directory(File.join('app/controllers', controller_class_path))
29
      m.directory(File.join('app/helpers', controller_class_path))
30
      m.directory(File.join('app/views', controller_class_path, controller_file_name))
31
      m.directory(File.join('test/functional', controller_class_path))
32
      m.directory(File.join('test/unit', class_path))
33
      m.directory(File.join('test/unit/helpers', class_path))
34

  
35
      for action in scaffold_views
36
        m.template(
37
          "view_#{action}.html.erb",
38
          File.join('app/views', controller_class_path, controller_file_name, "#{action}.html.erb")
39
        )
40
      end
41
      m.template(
42
        "view_form.html.erb",
43
        File.join('app/views', controller_class_path, controller_file_name, "_form.html.erb")
44
      ) if scaffold_views.include?("edit") || scaffold_views.include?("new")
45

  
46
      # Layout and stylesheet.
47
      m.template(
48
        'controller.rb', File.join('app/controllers', controller_class_path, "#{controller_file_name}_controller.rb")
49
      )
50

  
51
      m.template('functional_test.rb', File.join('test/functional', controller_class_path, "#{controller_file_name}_controller_test.rb"))
52
      m.template('helper.rb',          File.join('app/helpers',     controller_class_path, "#{controller_file_name}_helper.rb"))
53
      m.template('helper_test.rb',     File.join('test/unit/helpers',    controller_class_path, "#{controller_file_name}_helper_test.rb"))
54
  
55
      route_file = File.join('config','routes.rb')
56
      m.template('routes.rb', route_file) unless File.file?(File.join(destination_root,route_file))
57

  
58
      m.route_resources controller_file_name
59

  
60
      m.dependency 'redmine_plugin_model', [@plugin_name.gsub(/redmine_/,''),name] + @args, :collision => :skip
61
    end
62
  end
63

  
64
  protected
65
    # Override with your own usage banner.
66
    def banner
67
      "Usage: #{$0} redmine_plugin_scaffold PluginName ModelName [field:type, field:type]"
68
    end
69
end
lib/generators/redmine_plugin_scaffold/templates/controller.rb
1
class <%= controller_class_name %>Controller < ApplicationController
2
  unloadable
3
  
4
  #layout 'admin'
5
  #menu_item :issues
6
  
7
  def index
8
    @<%= table_name %> = <%= class_name %>.all
9
  end
10
  
11
  def show
12
    @<%= file_name %> = <%= class_name %>.find(params[:id])
13
  end
14
  
15
  def new
16
    @<%= file_name %> = <%= class_name %>.new
17
  end
18
  
19
  def edit
20
    @<%= file_name %> = <%= class_name %>.find(params[:id])
21
  end
22
  
23
  def create
24
    @<%= file_name %> = <%= class_name %>.new(params[:<%= file_name %>])
25
    if @<%= file_name %>.save
26
      flash[:notice] = l(:notice_successful_create)
27
      redirect_to(@<%= file_name %>)
28
    else
29
     render :action => "new"
30
    end
31
  end
32
  
33
  def update
34
    @<%= file_name %> = <%= class_name %>.find(params[:id])
35
    if @<%= file_name %>.update_attributes(params[:<%= file_name %>])
36
      flash[:notice] = l(:notice_successful_update)
37
      redirect_to(@<%= file_name %>)
38
    else
39
      render :action => "edit"
40
    end
41
  end
42
  
43
  def destroy
44
    @<%= file_name %> = <%= class_name %>.find(params[:id])
45
    @<%= file_name %>.destroy
46
    flash[:notice] = l(:notice_successful_delete)
47
    redirect_to(<%= table_name %>_url)
48
  end
49
end
lib/generators/redmine_plugin_scaffold/templates/functional_test.rb
1
require File.dirname(__FILE__) + '/../test_helper'
2

  
3
class <%= controller_class_name %>ControllerTest < ActionController::TestCase
4
  test "should get index" do
5
    get :index
6
    assert_response :success
7
    assert_not_nil assigns(:<%= table_name %>)
8
  end
9

  
10
  test "should get new" do
11
    get :new
12
    assert_response :success
13
  end
14

  
15
  test "should create <%= file_name %>" do
16
    assert_difference('<%= class_name %>.count') do
17
      post :create, :<%= file_name %> => { }
18
    end
19

  
20
    assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>))
21
  end
22

  
23
  test "should show <%= file_name %>" do
24
    get :show, :id => <%= class_name %>.find(1).to_param
25
    assert_response :success
26
  end
27

  
28
  test "should get edit" do
29
    get :edit, :id => <%= class_name %>.find(1).to_param
30
    assert_response :success
31
  end
32

  
33
  test "should update <%= file_name %>" do
34
    put :update, :id => <%= class_name %>.find(1).to_param, :<%= file_name %> => { }
35
    assert_redirected_to <%= file_name %>_path(assigns(:<%= file_name %>))
36
  end
37

  
38
  test "should destroy <%= file_name %>" do
39
    assert_difference('<%= class_name %>.count', -1) do
40
      delete :destroy, :id => <%= class_name %>.find(1).to_param
41
    end
42

  
43
    assert_redirected_to <%= table_name %>_path
44
  end
45
end
lib/generators/redmine_plugin_scaffold/templates/helper.rb
1
module <%= controller_class_name %>Helper
2
end
lib/generators/redmine_plugin_scaffold/templates/helper_test.rb
1
require File.dirname(__FILE__) + '/../../test_helper'
2
require 'action_view/test_case'
3

  
4
class <%= controller_class_name %>HelperTest < ActionView::TestCase
5
end
lib/generators/redmine_plugin_scaffold/templates/routes.rb
1
ActionController::Routing::Routes.draw do |map|
2
end
lib/generators/redmine_plugin_scaffold/templates/view_edit.html.erb
1
<h2><%%= link_to l(:label_<%=singular_name%>_plural), <%=plural_name%>_path %> &#187; <%%=h @<%=singular_name%>.<%=attributes.first.name %>%></h2>
2

  
3
<%% labelled_tabular_form_for :<%=singular_name%>, @<%=singular_name%>, :url => {:controller=>'<%=plural_name%>', :action=>'update'}, :html => { :class => nil, :method => :put } do |f| -%>
4
	<%%= render :partial => 'form', :locals => { :f => f } %>
5
	<p><%%= submit_tag l(:button_update) %></p>
6
<%% end -%>
lib/generators/redmine_plugin_scaffold/templates/view_form.html.erb
1
<%%= error_messages_for '<%=singular_name%>' %>
2

  
3
<!--[form:<%=singular_name%>]-->
4
<div class="box tabular">
5
  <% for attribute in attributes -%>
6
  <p><%%= f.<%= attribute.field_type %> :<%= attribute.name %> %></p>
7
  <% end -%>
8
</div>
9
<!--[eoform:<%=singular_name%>]-->
lib/generators/redmine_plugin_scaffold/templates/view_index.html.erb
1
<div class="contextual">
2
  <%%= link_to l(:label_<%=singular_name%>_new), {:controller=>'<%=plural_name%>',:action=>'new'},
3
      :class => 'icon icon-add' %>
4
</div>
5

  
6
<h2><%%=l(:label_<%=singular_name%>_plural)%></h2>
7

  
8
<%% if @<%=plural_name%>.any? -%>
9
<table class="list">		
10
  <thead>
11
    <tr>
12
      <% for attribute in attributes -%>
13
      <th><%%= l(:field_<%=attribute.column.name%>)%></th>
14
      <% end -%>
15
      <th> </th>
16
    </tr>
17
  </thead>
18
  <tbody>
19
  <%% @<%= plural_name %>.each do |<%= singular_name %>| -%>
20
    <tr class="<%=singular_name%> <%%= cycle("odd", "even") %>">
21
    <% for attribute in attributes -%>
22
      <% if attribute==attributes.first %>
23
        <td class="<%=attribute.name%>"><%%=link_to h(<%= singular_name %>.<%= attribute.name %>), {:controller => '<%=plural_name%>', :action => 'show', :id => <%=singular_name%>} %></td>
24
      <% else %>
25
        <td class="<%=attribute.name%>"><%%=h <%= singular_name %>.<%= attribute.name %> %></td>
26
      <% end %>
27
    <% end -%>
28
    <td class="buttons">
29
      <%%= link_to l(:button_update), {:controller => '<%=plural_name%>', :action => 'edit', :id => <%=singular_name%>},
30
          :class => 'icon icon-edit' %>
31
      <%%= link_to l(:button_delete), {:controller => '<%=plural_name%>', :action => 'destroy', :id => <%=singular_name%>},
32
          :class => 'icon icon-del', :confirm => l(:text_are_you_sure), :method => :delete %>
33
    </td>
34
  </tr>
35
  <%% end -%>
36
  </tbody>
37
</table>
38

  
39
<%% else -%>
40
<p class="nodata"><%%= l(:label_no_data) %></p>
41
<%% end -%>
42

  
43
<%% html_title(l(:label_<%= singular_name %>_plural)) -%>
lib/generators/redmine_plugin_scaffold/templates/view_new.html.erb
1
<h2><%%= link_to l(:label_<%=singular_name%>_plural), <%=plural_name%>_path %> &#187; <%%=l(:label_<%=singular_name%>_new) %></h2>
2

  
3
<%% labelled_tabular_form_for :<%=singular_name%>, @<%=singular_name%>, :url => {:controller=>'<%=plural_name%>', :action=>'create'}, :html => { :class => nil } do |f| -%>
4
	<%%= render :partial => 'form', :locals => { :f => f } %>
5
	<p><%%= submit_tag l(:button_create) %></p>
6
<%% end -%>
lib/generators/redmine_plugin_scaffold/templates/view_show.html.erb
1
<div class="contextual">
2
  <%%= link_to l(:button_update), {:controller => '<%=plural_name%>', :action => 'edit', :id => @<%=singular_name%>},
3
      :class => 'icon icon-edit' %>
4
  <%%= link_to l(:button_delete), {:controller => '<%=plural_name%>', :action => 'destroy', :id => @<%=singular_name%>},
5
      :class => 'icon icon-del', :confirm => l(:text_are_you_sure), :method => :delete %>
6
</div>
7

  
8
<h2><%%= link_to l(:label_<%=singular_name%>_plural), <%=plural_name%>_path %> &#187; <%%=h @<%=singular_name%>.<%=attributes.first.name %>%></h2>
9

  
10
<% for attribute in attributes -%>
11
<p>
12
  <b><%%= l(:field_<%=attribute.column.name%>)%>:</b>
13
  <%%=h @<%= singular_name %>.<%= attribute.name %> %>
14
</p>
15
<% end -%>
16

  
17
<%% html_title(l(:label_<%=singular_name%>_plural)) -%>
    (1-1/1)