stage

Get Version

0.1.9

→ ‘stage’

What

Stage is a code template generator that utilizes helpers as presenters to reduce ruby code used in views. The initial release only works with Merb and DataMapper. Support for RubyOnRails, ActiveRecord, Sequel and other ORMs will be added as I get the time. Of course, contributions are welcome if you like the concept.

Installing

sudo gem install stage

Demonstration of usage

$ merb-gen stage page title:string content:text
exists  app
exists  app/controllers
exists  app/helpers
exists  app/models
exists  app/views
create  app/views/pages
create  app/controllers/pages.rb
create  app/helpers/pages_helper.rb
create  app/models/page.rb
create  app/views/pages/_data.html.erb
create  app/views/pages/_form.html.erb
create  app/views/pages/edit.html.erb
create  app/views/pages/index.html.erb
create  app/views/pages/new.html.erb
create  app/views/pages/show.html.erb

The controller generated:

  class Pages < Application
    before :find_page, :only => [:show, :edit, :update, :delete]

    def index
      @pages = Page.all
      display @pages
    end

    def show
      display @page
    end

    def new
      only_provides :html
      @page = Page.new
      render
    end

    def edit
      only_provides :html
      render
    end

    def create
      @page = Page.new(params[:page])
      if @page.save
        redirect url(:page, @page)
      else
        render :new
      end
    end

    def update
      if @page.update_attributes(params[:page])
        redirect url(:page, @page)
      else
        raise BadRequest
      end
    end

    def destroy
      if @page.destroy!
        redirect url(:page)
      else
        raise BadRequest
      end
    end

    private
    def find_page
      @page = Page.first(params[:id])
      raise NotFound unless @page
    end
  end

The edit page:

  <h1>Editing page</h1>

  <%= partial "form" %>

  <%= link_to 'Show', url(:page, @page) %> |
  <%= link_to 'Back', url(:pages) %>

The form partial:

  <%
    submit_label = "Update"
    submit_label = "Create" if @page.new_record?
  -%>

  <%= error_messages_for @page %>

  <% form_for(:page, :action => url(:page)) do %>
    <%= partial :data %>
    <p> <%= submit_button submit_label %> </p>
  <% end %>

The data partial (the show view calls this directly):

  <p>
    <b>Title</b><br />
    <%= page_title_value %>
  </p>

  <p>
    <b>Content</b><br />
    <%= page_content_value %>
  </p>

As you can see, there are <model>_<field>_value methods. These are defined in the pages helper:

  module Merb
    module PagesHelper
      def page_title_value
        if @action_name == "show"
          @page.title
        else
          text_field :name => "page[title]", :value => @page.title
        end
      end

      def page_content_value
        if @action_name == "show"
          @page.content
        else
          text_field :name => "page[content]", :value => @page.content
        end
      end
    end
  end

I need to get more precise with the form helpers, but for now this will do.

Forum

http://groups.google.com/group/stonean_stage?hl=en

How to submit patches

The Clone URL: git://github.com/stonean/stage.git

Read the 8 steps for fixing other people’s code and for section 8b: Submit patch to Google Groups, use the Google Group above.

I’m new to git and this whole opensource project admin gig, so please be patient with my stumbling around.

License

This code is free to use under the terms of the MIT license.

Contact

Comments and suggestions are welcome via the forum

20th April 2008
Theme extended from Paul Battley