view_mapper 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/README.rdoc +44 -0
- data/Rakefile +0 -1
- data/VERSION +1 -1
- data/generators/view_for/view_for_generator.rb +6 -2
- data/lib/view_mapper/paperclip_templates/controller.rb +85 -0
- data/lib/view_mapper/paperclip_templates/fixtures.yml +19 -0
- data/lib/view_mapper/paperclip_templates/functional_test.rb +45 -0
- data/lib/view_mapper/paperclip_templates/helper.rb +2 -0
- data/lib/view_mapper/paperclip_templates/helper_test.rb +4 -0
- data/lib/view_mapper/paperclip_templates/layout.html.erb +17 -0
- data/lib/view_mapper/paperclip_templates/migration.rb +22 -0
- data/lib/view_mapper/paperclip_templates/model.rb +8 -0
- data/lib/view_mapper/paperclip_templates/style.css +54 -0
- data/lib/view_mapper/paperclip_templates/unit_test.rb +8 -0
- data/lib/view_mapper/paperclip_templates/view_edit.html.erb +23 -0
- data/lib/view_mapper/paperclip_templates/view_index.html.erb +24 -0
- data/lib/view_mapper/paperclip_templates/view_new.html.erb +22 -0
- data/lib/view_mapper/paperclip_templates/view_show.html.erb +17 -0
- data/lib/view_mapper/paperclip_view.rb +125 -0
- data/lib/view_mapper/view_mapper.rb +4 -0
- data/lib/view_mapper.rb +1 -0
- data/test/auto_complete_test.rb +0 -17
- data/test/expected_templates/paperclip/create_testies.rb +23 -0
- data/test/expected_templates/paperclip/edit.html.erb +31 -0
- data/test/expected_templates/paperclip/index.html.erb +24 -0
- data/test/expected_templates/paperclip/new.html.erb +30 -0
- data/test/expected_templates/paperclip/show.html.erb +28 -0
- data/test/expected_templates/paperclip/testy.rb +4 -0
- data/test/paperclip_view_test.rb +299 -0
- data/test/rails_generator/generators/components/model/model_generator.rb +45 -0
- data/test/test_helper.rb +55 -7
- data/test/view_for_generator_test.rb +2 -2
- data/view_mapper.gemspec +31 -4
- metadata +31 -4
- data/README +0 -116
    
        data/README.rdoc
    ADDED
    
    | @@ -0,0 +1,44 @@ | |
| 1 | 
            +
            =View Mapper
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            View Mapper will generate scaffolding code and map it to your existing models.
         | 
| 4 | 
            +
             | 
| 5 | 
            +
            When you run the "view_for" generator, View Mapper will generate the view scaffolding code for your model, enhanced to use the specified plugin or feature with your model's attributes. For example:
         | 
| 6 | 
            +
             | 
| 7 | 
            +
              script/generate view_for office --view auto_complete:address
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            ...will generate an "auto_complete view" for your existing office model. This is scaffolding that uses the attributes for your office model, and with code added to use the auto_complete plugin for the address field.
         | 
| 10 | 
            +
             | 
| 11 | 
            +
              script/generate view_for song --view paperclip
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            ...will generate a "paperclip view" for your existing song model. This is scaffolding that will support viewing and editing your song model and its Paperclip attachments. It's a good way to learn how to use Paperclip if you aren't familiar with it yet.
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            Currently View Mapper supports views for the auto_complete and paperclip plugins. If you don't specify any view then View Mapper by default will generate standard Rails scaffolding.
         | 
| 16 | 
            +
             | 
| 17 | 
            +
            If you also want to create a new model at the same time, then use a second View Mapper generator called "scaffold_for_view" and provide the model's attributes in the usual way:
         | 
| 18 | 
            +
             | 
| 19 | 
            +
              script/generate scaffold_for_view office address:string code:string --view auto_complete:address
         | 
| 20 | 
            +
             | 
| 21 | 
            +
            == Installation:
         | 
| 22 | 
            +
             | 
| 23 | 
            +
              gem sources -a http://gemcutter.org
         | 
| 24 | 
            +
              sudo gem install view_mapper
         | 
| 25 | 
            +
             | 
| 26 | 
            +
            == Usage:
         | 
| 27 | 
            +
             | 
| 28 | 
            +
              script/generate view_for model [ --view view_name[:param] ]
         | 
| 29 | 
            +
             | 
| 30 | 
            +
            This will generate scaffolding for an existing model. If you provide a view parameter, then View Mapper will enhance the scaffolding to use the specified plugin or feature with your model.
         | 
| 31 | 
            +
             | 
| 32 | 
            +
              script/generate scaffold_for_view model attributes [ --view view_name[:param] ]
         | 
| 33 | 
            +
             | 
| 34 | 
            +
            This is the same as view_for, but will also generate a new model at the same time.
         | 
| 35 | 
            +
             | 
| 36 | 
            +
            == Views
         | 
| 37 | 
            +
             | 
| 38 | 
            +
            {auto_complete}[http://patshaughnessy.net/2009/10/1/auto_complete-scaffolding]: Generates scaffolding that uses the standard Rails auto_complete plugin for type ahead behavior.
         | 
| 39 | 
            +
             | 
| 40 | 
            +
            {paperclip}[http://patshaughnessy.net/2009/10/16/paperclip-scaffolding]: Generates scaffolding that uses Paperclip to upload and download files.
         | 
| 41 | 
            +
             | 
| 42 | 
            +
            == Examples
         | 
| 43 | 
            +
             | 
| 44 | 
            +
            For detailed examples and more information see: http://patshaughnessy.net/view_mapper
         | 
    
        data/Rakefile
    CHANGED
    
    | @@ -13,7 +13,6 @@ begin | |
| 13 13 | 
             
                gem.add_development_dependency "thoughtbot-shoulda"
         | 
| 14 14 | 
             
                # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
         | 
| 15 15 | 
             
              end
         | 
| 16 | 
            -
              Jeweler::GemcutterTasks.new
         | 
| 17 16 | 
             
            rescue LoadError
         | 
| 18 17 | 
             
              puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
         | 
| 19 18 | 
             
            end
         | 
    
        data/VERSION
    CHANGED
    
    | @@ -1 +1 @@ | |
| 1 | 
            -
            0. | 
| 1 | 
            +
            0.2.0
         | 
| @@ -30,7 +30,7 @@ class ViewForGenerator < ScaffoldGenerator | |
| 30 30 | 
             
                    @model = nil
         | 
| 31 31 | 
             
                  end
         | 
| 32 32 | 
             
                rescue NameError
         | 
| 33 | 
            -
                  logger.error "Class '#{model_name}' does not exist."
         | 
| 33 | 
            +
                  logger.error "Class '#{model_name}' does not exist or contains a syntax error and could not be loaded."
         | 
| 34 34 | 
             
                rescue ActiveRecord::StatementInvalid
         | 
| 35 35 | 
             
                  logger.error "Table for model '#{model_name}' does not exist - run rake db:migrate first."
         | 
| 36 36 | 
             
                end
         | 
| @@ -38,10 +38,14 @@ class ViewForGenerator < ScaffoldGenerator | |
| 38 38 |  | 
| 39 39 | 
             
              def custom_columns
         | 
| 40 40 | 
             
                @model.columns.reject do |col|
         | 
| 41 | 
            -
                   | 
| 41 | 
            +
                  built_in_columns.include? col.name
         | 
| 42 42 | 
             
                end
         | 
| 43 43 | 
             
              end
         | 
| 44 44 |  | 
| 45 | 
            +
              def built_in_columns
         | 
| 46 | 
            +
                BUILT_IN_COLUMNS
         | 
| 47 | 
            +
              end
         | 
| 48 | 
            +
             | 
| 45 49 | 
             
              def record
         | 
| 46 50 | 
             
                EditableManifest.new(self) { |m| yield m }
         | 
| 47 51 | 
             
              end
         | 
| @@ -0,0 +1,85 @@ | |
| 1 | 
            +
            class <%= controller_class_name %>Controller < ApplicationController
         | 
| 2 | 
            +
              # GET /<%= table_name %>
         | 
| 3 | 
            +
              # GET /<%= table_name %>.xml
         | 
| 4 | 
            +
              def index
         | 
| 5 | 
            +
                @<%= table_name %> = <%= class_name %>.all
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                respond_to do |format|
         | 
| 8 | 
            +
                  format.html # index.html.erb
         | 
| 9 | 
            +
                  format.xml  { render :xml => @<%= table_name %> }
         | 
| 10 | 
            +
                end
         | 
| 11 | 
            +
              end
         | 
| 12 | 
            +
             | 
| 13 | 
            +
              # GET /<%= table_name %>/1
         | 
| 14 | 
            +
              # GET /<%= table_name %>/1.xml
         | 
| 15 | 
            +
              def show
         | 
| 16 | 
            +
                @<%= file_name %> = <%= class_name %>.find(params[:id])
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                respond_to do |format|
         | 
| 19 | 
            +
                  format.html # show.html.erb
         | 
| 20 | 
            +
                  format.xml  { render :xml => @<%= file_name %> }
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
              # GET /<%= table_name %>/new
         | 
| 25 | 
            +
              # GET /<%= table_name %>/new.xml
         | 
| 26 | 
            +
              def new
         | 
| 27 | 
            +
                @<%= file_name %> = <%= class_name %>.new
         | 
| 28 | 
            +
             | 
| 29 | 
            +
                respond_to do |format|
         | 
| 30 | 
            +
                  format.html # new.html.erb
         | 
| 31 | 
            +
                  format.xml  { render :xml => @<%= file_name %> }
         | 
| 32 | 
            +
                end
         | 
| 33 | 
            +
              end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
              # GET /<%= table_name %>/1/edit
         | 
| 36 | 
            +
              def edit
         | 
| 37 | 
            +
                @<%= file_name %> = <%= class_name %>.find(params[:id])
         | 
| 38 | 
            +
              end
         | 
| 39 | 
            +
             | 
| 40 | 
            +
              # POST /<%= table_name %>
         | 
| 41 | 
            +
              # POST /<%= table_name %>.xml
         | 
| 42 | 
            +
              def create
         | 
| 43 | 
            +
                @<%= file_name %> = <%= class_name %>.new(params[:<%= file_name %>])
         | 
| 44 | 
            +
             | 
| 45 | 
            +
                respond_to do |format|
         | 
| 46 | 
            +
                  if @<%= file_name %>.save
         | 
| 47 | 
            +
                    flash[:notice] = '<%= class_name %> was successfully created.'
         | 
| 48 | 
            +
                    format.html { redirect_to(@<%= file_name %>) }
         | 
| 49 | 
            +
                    format.xml  { render :xml => @<%= file_name %>, :status => :created, :location => @<%= file_name %> }
         | 
| 50 | 
            +
                  else
         | 
| 51 | 
            +
                    format.html { render :action => "new" }
         | 
| 52 | 
            +
                    format.xml  { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
         | 
| 53 | 
            +
                  end
         | 
| 54 | 
            +
                end
         | 
| 55 | 
            +
              end
         | 
| 56 | 
            +
             | 
| 57 | 
            +
              # PUT /<%= table_name %>/1
         | 
| 58 | 
            +
              # PUT /<%= table_name %>/1.xml
         | 
| 59 | 
            +
              def update
         | 
| 60 | 
            +
                @<%= file_name %> = <%= class_name %>.find(params[:id])
         | 
| 61 | 
            +
             | 
| 62 | 
            +
                respond_to do |format|
         | 
| 63 | 
            +
                  if @<%= file_name %>.update_attributes(params[:<%= file_name %>])
         | 
| 64 | 
            +
                    flash[:notice] = '<%= class_name %> was successfully updated.'
         | 
| 65 | 
            +
                    format.html { redirect_to(@<%= file_name %>) }
         | 
| 66 | 
            +
                    format.xml  { head :ok }
         | 
| 67 | 
            +
                  else
         | 
| 68 | 
            +
                    format.html { render :action => "edit" }
         | 
| 69 | 
            +
                    format.xml  { render :xml => @<%= file_name %>.errors, :status => :unprocessable_entity }
         | 
| 70 | 
            +
                  end
         | 
| 71 | 
            +
                end
         | 
| 72 | 
            +
              end
         | 
| 73 | 
            +
             | 
| 74 | 
            +
              # DELETE /<%= table_name %>/1
         | 
| 75 | 
            +
              # DELETE /<%= table_name %>/1.xml
         | 
| 76 | 
            +
              def destroy
         | 
| 77 | 
            +
                @<%= file_name %> = <%= class_name %>.find(params[:id])
         | 
| 78 | 
            +
                @<%= file_name %>.destroy
         | 
| 79 | 
            +
             | 
| 80 | 
            +
                respond_to do |format|
         | 
| 81 | 
            +
                  format.html { redirect_to(<%= table_name %>_url) }
         | 
| 82 | 
            +
                  format.xml  { head :ok }
         | 
| 83 | 
            +
                end
         | 
| 84 | 
            +
              end
         | 
| 85 | 
            +
            end
         | 
| @@ -0,0 +1,19 @@ | |
| 1 | 
            +
            # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            <% unless attributes.empty? -%>
         | 
| 4 | 
            +
            one:
         | 
| 5 | 
            +
            <% for attribute in attributes -%>
         | 
| 6 | 
            +
              <%= attribute.name %>: <%= attribute.default %>
         | 
| 7 | 
            +
            <% end -%>
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            two:
         | 
| 10 | 
            +
            <% for attribute in attributes -%>
         | 
| 11 | 
            +
              <%= attribute.name %>: <%= attribute.default %>
         | 
| 12 | 
            +
            <% end -%>
         | 
| 13 | 
            +
            <% else -%>
         | 
| 14 | 
            +
            # one:
         | 
| 15 | 
            +
            #   column: value
         | 
| 16 | 
            +
            #
         | 
| 17 | 
            +
            # two:
         | 
| 18 | 
            +
            #   column: value
         | 
| 19 | 
            +
            <% end -%>
         | 
| @@ -0,0 +1,45 @@ | |
| 1 | 
            +
            require '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 => <%= table_name %>(:one).to_param
         | 
| 25 | 
            +
                assert_response :success
         | 
| 26 | 
            +
              end
         | 
| 27 | 
            +
             | 
| 28 | 
            +
              test "should get edit" do
         | 
| 29 | 
            +
                get :edit, :id => <%= table_name %>(:one).to_param
         | 
| 30 | 
            +
                assert_response :success
         | 
| 31 | 
            +
              end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
              test "should update <%= file_name %>" do
         | 
| 34 | 
            +
                put :update, :id => <%= table_name %>(:one).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 => <%= table_name %>(:one).to_param
         | 
| 41 | 
            +
                end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
                assert_redirected_to <%= table_name %>_path
         | 
| 44 | 
            +
              end
         | 
| 45 | 
            +
            end
         | 
| @@ -0,0 +1,17 @@ | |
| 1 | 
            +
            <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
         | 
| 2 | 
            +
                   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
         | 
| 3 | 
            +
             | 
| 4 | 
            +
            <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
         | 
| 5 | 
            +
            <head>
         | 
| 6 | 
            +
              <meta http-equiv="content-type" content="text/html;charset=UTF-8" />
         | 
| 7 | 
            +
              <title><%= controller_class_name %>: <%%= controller.action_name %></title>
         | 
| 8 | 
            +
              <%%= stylesheet_link_tag 'scaffold' %>
         | 
| 9 | 
            +
            </head>
         | 
| 10 | 
            +
            <body>
         | 
| 11 | 
            +
             | 
| 12 | 
            +
            <p style="color: green"><%%= flash[:notice] %></p>
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            <%%= yield %>
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            </body>
         | 
| 17 | 
            +
            </html>
         | 
| @@ -0,0 +1,22 @@ | |
| 1 | 
            +
            class <%= migration_name %> < ActiveRecord::Migration
         | 
| 2 | 
            +
              def self.up
         | 
| 3 | 
            +
                create_table :<%= table_name %> do |t|
         | 
| 4 | 
            +
            <% for attribute in attributes -%>
         | 
| 5 | 
            +
                  t.<%= attribute.type %>   :<%= attribute.name %>
         | 
| 6 | 
            +
            <% end -%>
         | 
| 7 | 
            +
            <% for attachment in attachments -%>
         | 
| 8 | 
            +
                  t.string   :<%= attachment %>_file_name
         | 
| 9 | 
            +
                  t.string   :<%= attachment %>_content_type
         | 
| 10 | 
            +
                  t.integer  :<%= attachment %>_file_size
         | 
| 11 | 
            +
                  t.datetime :<%= attachment %>_updated_at
         | 
| 12 | 
            +
            <% end -%>
         | 
| 13 | 
            +
            <% unless options[:skip_timestamps] %>
         | 
| 14 | 
            +
                  t.timestamps
         | 
| 15 | 
            +
            <% end -%>
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
              end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
              def self.down
         | 
| 20 | 
            +
                drop_table :<%= table_name %>
         | 
| 21 | 
            +
              end
         | 
| 22 | 
            +
            end
         | 
| @@ -0,0 +1,54 @@ | |
| 1 | 
            +
            body { background-color: #fff; color: #333; }
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            body, p, ol, ul, td {
         | 
| 4 | 
            +
              font-family: verdana, arial, helvetica, sans-serif;
         | 
| 5 | 
            +
              font-size:   13px;
         | 
| 6 | 
            +
              line-height: 18px;
         | 
| 7 | 
            +
            }
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            pre {
         | 
| 10 | 
            +
              background-color: #eee;
         | 
| 11 | 
            +
              padding: 10px;
         | 
| 12 | 
            +
              font-size: 11px;
         | 
| 13 | 
            +
            }
         | 
| 14 | 
            +
             | 
| 15 | 
            +
            a { color: #000; }
         | 
| 16 | 
            +
            a:visited { color: #666; }
         | 
| 17 | 
            +
            a:hover { color: #fff; background-color:#000; }
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            .fieldWithErrors {
         | 
| 20 | 
            +
              padding: 2px;
         | 
| 21 | 
            +
              background-color: red;
         | 
| 22 | 
            +
              display: table;
         | 
| 23 | 
            +
            }
         | 
| 24 | 
            +
             | 
| 25 | 
            +
            #errorExplanation {
         | 
| 26 | 
            +
              width: 400px;
         | 
| 27 | 
            +
              border: 2px solid red;
         | 
| 28 | 
            +
              padding: 7px;
         | 
| 29 | 
            +
              padding-bottom: 12px;
         | 
| 30 | 
            +
              margin-bottom: 20px;
         | 
| 31 | 
            +
              background-color: #f0f0f0;
         | 
| 32 | 
            +
            }
         | 
| 33 | 
            +
             | 
| 34 | 
            +
            #errorExplanation h2 {
         | 
| 35 | 
            +
              text-align: left;
         | 
| 36 | 
            +
              font-weight: bold;
         | 
| 37 | 
            +
              padding: 5px 5px 5px 15px;
         | 
| 38 | 
            +
              font-size: 12px;
         | 
| 39 | 
            +
              margin: -7px;
         | 
| 40 | 
            +
              background-color: #c00;
         | 
| 41 | 
            +
              color: #fff;
         | 
| 42 | 
            +
            }
         | 
| 43 | 
            +
             | 
| 44 | 
            +
            #errorExplanation p {
         | 
| 45 | 
            +
              color: #333;
         | 
| 46 | 
            +
              margin-bottom: 0;
         | 
| 47 | 
            +
              padding: 5px;
         | 
| 48 | 
            +
            }
         | 
| 49 | 
            +
             | 
| 50 | 
            +
            #errorExplanation ul li {
         | 
| 51 | 
            +
              font-size: 12px;
         | 
| 52 | 
            +
              list-style: square;
         | 
| 53 | 
            +
            }
         | 
| 54 | 
            +
             | 
| @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            <h1>Editing <%= singular_name %></h1>
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            <%% form_for(@<%= singular_name %>, :html => { :multipart => true }) do |f| %>
         | 
| 4 | 
            +
              <%%= f.error_messages %>
         | 
| 5 | 
            +
            <% for attribute in attributes -%>
         | 
| 6 | 
            +
              <p>
         | 
| 7 | 
            +
                <%%= f.label :<%= attribute.name %> %><br />
         | 
| 8 | 
            +
                <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
         | 
| 9 | 
            +
              </p>
         | 
| 10 | 
            +
            <% end -%>
         | 
| 11 | 
            +
            <% for attachment in attachments -%>
         | 
| 12 | 
            +
              <p>
         | 
| 13 | 
            +
                <%%= f.label :<%= attachment%> %><br />
         | 
| 14 | 
            +
                <%%= f.file_field :<%= attachment%> %>
         | 
| 15 | 
            +
              </p>
         | 
| 16 | 
            +
            <% end -%>
         | 
| 17 | 
            +
              <p>
         | 
| 18 | 
            +
                <%%= f.submit 'Update' %>
         | 
| 19 | 
            +
              </p>
         | 
| 20 | 
            +
            <%% end %>
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            <%%= link_to 'Show', @<%= singular_name %> %> |
         | 
| 23 | 
            +
            <%%= link_to 'Back', <%= plural_name %>_path %>
         | 
| @@ -0,0 +1,24 @@ | |
| 1 | 
            +
            <h1>Listing <%= plural_name %></h1>
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            <table>
         | 
| 4 | 
            +
              <tr>
         | 
| 5 | 
            +
            <% for attribute in attributes -%>
         | 
| 6 | 
            +
                <th><%= attribute.column.human_name %></th>
         | 
| 7 | 
            +
            <% end -%>
         | 
| 8 | 
            +
              </tr>
         | 
| 9 | 
            +
             | 
| 10 | 
            +
            <%% @<%= plural_name %>.each do |<%= singular_name %>| %>
         | 
| 11 | 
            +
              <tr>
         | 
| 12 | 
            +
            <% for attribute in attributes -%>
         | 
| 13 | 
            +
                <td><%%=h <%= singular_name %>.<%= attribute.name %> %></td>
         | 
| 14 | 
            +
            <% end -%>
         | 
| 15 | 
            +
                <td><%%= link_to 'Show', <%= singular_name %> %></td>
         | 
| 16 | 
            +
                <td><%%= link_to 'Edit', edit_<%= singular_name %>_path(<%= singular_name %>) %></td>
         | 
| 17 | 
            +
                <td><%%= link_to 'Destroy', <%= singular_name %>, :confirm => 'Are you sure?', :method => :delete %></td>
         | 
| 18 | 
            +
              </tr>
         | 
| 19 | 
            +
            <%% end %>
         | 
| 20 | 
            +
            </table>
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            <br />
         | 
| 23 | 
            +
             | 
| 24 | 
            +
            <%%= link_to 'New <%= singular_name %>', new_<%= singular_name %>_path %>
         | 
| @@ -0,0 +1,22 @@ | |
| 1 | 
            +
            <h1>New <%= singular_name %></h1>
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            <%% form_for(@<%= singular_name %>, :html => { :multipart => true }) do |f| %>
         | 
| 4 | 
            +
              <%%= f.error_messages %>
         | 
| 5 | 
            +
            <% for attribute in attributes -%>
         | 
| 6 | 
            +
              <p>
         | 
| 7 | 
            +
                <%%= f.label :<%= attribute.name %> %><br />
         | 
| 8 | 
            +
                <%%= f.<%= attribute.field_type %> :<%= attribute.name %> %>
         | 
| 9 | 
            +
              </p>
         | 
| 10 | 
            +
            <%- end -%>
         | 
| 11 | 
            +
            <% for attachment in attachments -%>
         | 
| 12 | 
            +
              <p>
         | 
| 13 | 
            +
                <%%= f.label :<%= attachment%> %><br />
         | 
| 14 | 
            +
                <%%= f.file_field :<%= attachment%> %>
         | 
| 15 | 
            +
              </p>
         | 
| 16 | 
            +
            <% end -%>
         | 
| 17 | 
            +
              <p>
         | 
| 18 | 
            +
                <%%= f.submit 'Create' %>
         | 
| 19 | 
            +
              </p>
         | 
| 20 | 
            +
            <%% end %>
         | 
| 21 | 
            +
             | 
| 22 | 
            +
            <%%= link_to 'Back', <%= plural_name %>_path %>
         | 
| @@ -0,0 +1,17 @@ | |
| 1 | 
            +
            <% for attribute in attributes -%>
         | 
| 2 | 
            +
            <p>
         | 
| 3 | 
            +
              <b><%= attribute.column.human_name %>:</b>
         | 
| 4 | 
            +
              <%%=h @<%= singular_name %>.<%= attribute.name %> %>
         | 
| 5 | 
            +
            </p>
         | 
| 6 | 
            +
             | 
| 7 | 
            +
            <% end -%>
         | 
| 8 | 
            +
            <% for attachment in attachments -%>
         | 
| 9 | 
            +
            <p>
         | 
| 10 | 
            +
              <b><%= attachment.capitalize %>:</b>
         | 
| 11 | 
            +
              <%%= link_to @<%= singular_name %>.<%= attachment%>_file_name, @<%= singular_name %>.<%= attachment%>.url %><br>
         | 
| 12 | 
            +
            </p>
         | 
| 13 | 
            +
             | 
| 14 | 
            +
            <% end -%>
         | 
| 15 | 
            +
             | 
| 16 | 
            +
            <%%= link_to 'Edit', edit_<%= singular_name %>_path(@<%= singular_name %>) %> |
         | 
| 17 | 
            +
            <%%= link_to 'Back', <%= plural_name %>_path %>
         | 
| @@ -0,0 +1,125 @@ | |
| 1 | 
            +
            module ViewMapper
         | 
| 2 | 
            +
              module PaperclipView
         | 
| 3 | 
            +
             | 
| 4 | 
            +
                def source_root_for_view
         | 
| 5 | 
            +
                  File.dirname(__FILE__) + "/paperclip_templates"
         | 
| 6 | 
            +
                end
         | 
| 7 | 
            +
             | 
| 8 | 
            +
                def manifest
         | 
| 9 | 
            +
                  m = super.edit do |action|
         | 
| 10 | 
            +
                    action unless is_model_dependency_action(action) || !valid
         | 
| 11 | 
            +
                  end
         | 
| 12 | 
            +
                  unless view_only? || !valid
         | 
| 13 | 
            +
                    add_model_actions(m)
         | 
| 14 | 
            +
                  end
         | 
| 15 | 
            +
                  m
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                def add_model_actions(m)
         | 
| 19 | 
            +
                  m.directory(File.join('test/fixtures', class_path))
         | 
| 20 | 
            +
                  m.template   'model.rb',     File.join('app/models', class_path, "#{file_name}.rb")
         | 
| 21 | 
            +
                  m.template   'unit_test.rb', File.join('test/unit', class_path, "#{file_name}_test.rb")
         | 
| 22 | 
            +
                  unless options[:skip_fixture]
         | 
| 23 | 
            +
                    m.template 'fixtures.yml', File.join('test/fixtures', "#{table_name}.yml")
         | 
| 24 | 
            +
                  end
         | 
| 25 | 
            +
                  unless options[:skip_migration]
         | 
| 26 | 
            +
                    m.migration_template 'migration.rb',
         | 
| 27 | 
            +
                                         'db/migrate',
         | 
| 28 | 
            +
                                         :assigns => { :migration_name => "Create#{class_name.pluralize.gsub(/::/, '')}" },
         | 
| 29 | 
            +
                                         :migration_file_name => "create_#{file_path.gsub(/\//, '_').pluralize}"
         | 
| 30 | 
            +
                  end
         | 
| 31 | 
            +
                end
         | 
| 32 | 
            +
             | 
| 33 | 
            +
                def is_model_dependency_action(action)
         | 
| 34 | 
            +
                  action[0] == :dependency && action[1].include?('model')
         | 
| 35 | 
            +
                end
         | 
| 36 | 
            +
             | 
| 37 | 
            +
                def attachments
         | 
| 38 | 
            +
                  if view_param
         | 
| 39 | 
            +
                    parse_attachments_from_param
         | 
| 40 | 
            +
                  elsif view_only?
         | 
| 41 | 
            +
                    inspect_model_for_attachments
         | 
| 42 | 
            +
                  else
         | 
| 43 | 
            +
                    []
         | 
| 44 | 
            +
                  end
         | 
| 45 | 
            +
                end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                def parse_attachments_from_param
         | 
| 48 | 
            +
                  view_param.split(',')
         | 
| 49 | 
            +
                end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
                def inspect_model_for_attachments
         | 
| 52 | 
            +
                  if model.respond_to?('attachment_definitions') && model.attachment_definitions
         | 
| 53 | 
            +
                    model.attachment_definitions.keys.collect { |name| name.to_s }.sort
         | 
| 54 | 
            +
                  else
         | 
| 55 | 
            +
                    []
         | 
| 56 | 
            +
                  end
         | 
| 57 | 
            +
                end
         | 
| 58 | 
            +
             | 
| 59 | 
            +
                def validate
         | 
| 60 | 
            +
                  @valid = validate_attachments
         | 
| 61 | 
            +
                  @valid &&= super
         | 
| 62 | 
            +
                end
         | 
| 63 | 
            +
             | 
| 64 | 
            +
                def validate_attachments
         | 
| 65 | 
            +
                  if !paperclip_installed
         | 
| 66 | 
            +
                    logger.error "The Paperclip plugin does not appear to be installed."
         | 
| 67 | 
            +
                    return false
         | 
| 68 | 
            +
                  elsif attachments == []
         | 
| 69 | 
            +
                    if view_only?
         | 
| 70 | 
            +
                      logger.warning "No paperclip attachments exist on the specified class."
         | 
| 71 | 
            +
                    else
         | 
| 72 | 
            +
                      logger.warning "No paperclip attachments specified."
         | 
| 73 | 
            +
                    end
         | 
| 74 | 
            +
                    return false
         | 
| 75 | 
            +
                  else
         | 
| 76 | 
            +
                    !attachments.detect { |a| !validate_attachment(a) }
         | 
| 77 | 
            +
                  end
         | 
| 78 | 
            +
                end
         | 
| 79 | 
            +
             | 
| 80 | 
            +
                def validate_attachment(attachment)
         | 
| 81 | 
            +
                  if view_only?
         | 
| 82 | 
            +
                    if !has_attachment(attachment.to_sym)
         | 
| 83 | 
            +
                      logger.error "Attachment '#{attachment}' does not exist."
         | 
| 84 | 
            +
                      return false
         | 
| 85 | 
            +
                    elsif !has_columns_for_attachment(attachment)
         | 
| 86 | 
            +
                      return false
         | 
| 87 | 
            +
                    end
         | 
| 88 | 
            +
                  end
         | 
| 89 | 
            +
                  true
         | 
| 90 | 
            +
                end
         | 
| 91 | 
            +
             | 
| 92 | 
            +
                def has_attachment(attachment)
         | 
| 93 | 
            +
                  model.attachment_definitions && model.attachment_definitions.has_key?(attachment)
         | 
| 94 | 
            +
                end
         | 
| 95 | 
            +
             | 
| 96 | 
            +
                def has_columns_for_attachment(attachment)
         | 
| 97 | 
            +
                  !paperclip_columns_for_attachment(attachment).detect { |paperclip_col| !has_column_for_attachment(attachment, paperclip_col) }
         | 
| 98 | 
            +
                end
         | 
| 99 | 
            +
             | 
| 100 | 
            +
                def has_column_for_attachment(attachment, paperclip_col)
         | 
| 101 | 
            +
                  has_column = model.columns.collect { |col| col.name }.include?(paperclip_col)
         | 
| 102 | 
            +
                  if !has_column
         | 
| 103 | 
            +
                    logger.error "Column \'#{paperclip_col}\' does not exist. First run script/generate paperclip #{name} #{attachment}."
         | 
| 104 | 
            +
                  end
         | 
| 105 | 
            +
                  has_column
         | 
| 106 | 
            +
                end
         | 
| 107 | 
            +
             | 
| 108 | 
            +
                def built_in_columns
         | 
| 109 | 
            +
                  attachments.inject(super) do |result, element|
         | 
| 110 | 
            +
                    result + paperclip_columns_for_attachment(element)
         | 
| 111 | 
            +
                  end
         | 
| 112 | 
            +
                end
         | 
| 113 | 
            +
             | 
| 114 | 
            +
                def paperclip_columns_for_attachment(attachment)
         | 
| 115 | 
            +
                  %w{ file_name content_type file_size updated_at }.collect do |col|
         | 
| 116 | 
            +
                    "#{attachment}_#{col}"
         | 
| 117 | 
            +
                  end
         | 
| 118 | 
            +
                end
         | 
| 119 | 
            +
             | 
| 120 | 
            +
                def paperclip_installed
         | 
| 121 | 
            +
                  ActiveRecord::Base.methods.include? 'has_attached_file'
         | 
| 122 | 
            +
                end
         | 
| 123 | 
            +
             | 
| 124 | 
            +
              end
         | 
| 125 | 
            +
            end
         | 
    
        data/lib/view_mapper.rb
    CHANGED
    
    
    
        data/test/auto_complete_test.rb
    CHANGED
    
    | @@ -12,23 +12,6 @@ class AutoCompleteViewTest < Test::Unit::TestCase | |
| 12 12 | 
             
              attr_reader :file_name
         | 
| 13 13 | 
             
              attr_reader :controller_singular_name
         | 
| 14 14 |  | 
| 15 | 
            -
              def generator_cmd_line(gen, args)
         | 
| 16 | 
            -
                if gen == 'view_for'
         | 
| 17 | 
            -
                  cmd_line = ['testy']
         | 
| 18 | 
            -
                else
         | 
| 19 | 
            -
                  cmd_line = ['testy', 'first_name:string', 'last_name:string', 'address:string']
         | 
| 20 | 
            -
                end
         | 
| 21 | 
            -
                (cmd_line << args).flatten
         | 
| 22 | 
            -
              end
         | 
| 23 | 
            -
             | 
| 24 | 
            -
              def generator_script_cmd_line(gen, args)
         | 
| 25 | 
            -
                ([gen] << generator_cmd_line(gen, args)).flatten
         | 
| 26 | 
            -
              end
         | 
| 27 | 
            -
             | 
| 28 | 
            -
              def new_generator_for_test_model(gen, args)
         | 
| 29 | 
            -
                Rails::Generator::Base.instance(gen, generator_cmd_line(gen, args))
         | 
| 30 | 
            -
              end
         | 
| 31 | 
            -
             | 
| 32 15 | 
             
              generators = %w{ view_for scaffold_for_view }
         | 
| 33 16 | 
             
              generators.each do |gen|
         | 
| 34 17 |  | 
| @@ -0,0 +1,23 @@ | |
| 1 | 
            +
            class CreateTesties < ActiveRecord::Migration
         | 
| 2 | 
            +
              def self.up
         | 
| 3 | 
            +
                create_table :testies do |t|
         | 
| 4 | 
            +
                  t.string   :first_name
         | 
| 5 | 
            +
                  t.string   :last_name
         | 
| 6 | 
            +
                  t.string   :address
         | 
| 7 | 
            +
                  t.string   :avatar_file_name
         | 
| 8 | 
            +
                  t.string   :avatar_content_type
         | 
| 9 | 
            +
                  t.integer  :avatar_file_size
         | 
| 10 | 
            +
                  t.datetime :avatar_updated_at
         | 
| 11 | 
            +
                  t.string   :avatar2_file_name
         | 
| 12 | 
            +
                  t.string   :avatar2_content_type
         | 
| 13 | 
            +
                  t.integer  :avatar2_file_size
         | 
| 14 | 
            +
                  t.datetime :avatar2_updated_at
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                  t.timestamps
         | 
| 17 | 
            +
                end
         | 
| 18 | 
            +
              end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
              def self.down
         | 
| 21 | 
            +
                drop_table :testies
         | 
| 22 | 
            +
              end
         | 
| 23 | 
            +
            end
         | 
| @@ -0,0 +1,31 @@ | |
| 1 | 
            +
            <h1>Editing testy</h1>
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            <% form_for(@testy, :html => { :multipart => true }) do |f| %>
         | 
| 4 | 
            +
              <%= f.error_messages %>
         | 
| 5 | 
            +
              <p>
         | 
| 6 | 
            +
                <%= f.label :first_name %><br />
         | 
| 7 | 
            +
                <%= f.text_field :first_name %>
         | 
| 8 | 
            +
              </p>
         | 
| 9 | 
            +
              <p>
         | 
| 10 | 
            +
                <%= f.label :last_name %><br />
         | 
| 11 | 
            +
                <%= f.text_field :last_name %>
         | 
| 12 | 
            +
              </p>
         | 
| 13 | 
            +
              <p>
         | 
| 14 | 
            +
                <%= f.label :address %><br />
         | 
| 15 | 
            +
                <%= f.text_field :address %>
         | 
| 16 | 
            +
              </p>
         | 
| 17 | 
            +
              <p>
         | 
| 18 | 
            +
                <%= f.label :avatar %><br />
         | 
| 19 | 
            +
                <%= f.file_field :avatar %>
         | 
| 20 | 
            +
              </p>
         | 
| 21 | 
            +
              <p>
         | 
| 22 | 
            +
                <%= f.label :avatar2 %><br />
         | 
| 23 | 
            +
                <%= f.file_field :avatar2 %>
         | 
| 24 | 
            +
              </p>
         | 
| 25 | 
            +
              <p>
         | 
| 26 | 
            +
                <%= f.submit 'Update' %>
         | 
| 27 | 
            +
              </p>
         | 
| 28 | 
            +
            <% end %>
         | 
| 29 | 
            +
             | 
| 30 | 
            +
            <%= link_to 'Show', @testy %> |
         | 
| 31 | 
            +
            <%= link_to 'Back', testies_path %>
         |