Ramaze Plugin

38 views
Skip to first unread message

Lucas Sampaio

unread,
Apr 6, 2012, 2:52:30 PM4/6/12
to ram...@googlegroups.com
Hello everybody,

I'm starting to program in Ruby and I'm really impressed with Ramaze's simplicity and flexibility, and I wan't to spend my time with this framework. But I can't found any doc about plugins... is it possible in Ramaze? How can I extend it's functionality through plugins or even gems?

Att,
Lucas Sampaio

Yorick Peterse

unread,
Apr 6, 2012, 3:39:06 PM4/6/12
to ram...@googlegroups.com
Lucas,

What exactly do you want to extend?

Yorick

> Lucas Sampaio --
> You received this message because you are subscribed to the Google
> Groups "Ramaze" group.
> To view this discussion on the web visit
> https://groups.google.com/d/msg/ramaze/-/o4QtoIdFasAJ.
> To post to this group, send email to ram...@googlegroups.com.
> To unsubscribe from this group, send email to
> ramaze+un...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/ramaze?hl=en.

signature.asc

Lucas Sampaio

unread,
Apr 6, 2012, 5:18:11 PM4/6/12
to ram...@googlegroups.com
Well, I'm wondering if I can create routes like Rails resources, something like including:
"resource :user"

In a User controller create for me seven routes, just like Rails do. If I don't wan't some route, I write:
"resource :user, except=>[:new, :edit]"
or something like that.

My goal is create my common routes using a single command. How can I do that?

More: how can I use "respond_with @user" and Ramaze give me an edit page with "/user/:id" url?

That's the two features that I really miss from Rails.

Att,
Lucas S. Magalhães
Graduando de Engenharia de Computação, 2009/1
Tel.: (27) 9942-8278
Skype: lucassmagal

Yorick Peterse

unread,
Apr 6, 2012, 5:49:46 PM4/6/12
to ram...@googlegroups.com
Lucas,

Simply put: these things don't exist in Ramaze. Ramaze's routing system
doesn't rely on developers explicitly defining which classes and which
methods are available for routes, instead a controller is mapped to a
URI and all public instance methods become available for HTTP requests.

For example, in Rails you might do something like the following:

    class Posts < ApplicationController
      def index

      end

      def not_me

      end
    end

    resource :posts, :except => [:not_me]

In Ramaze you'd do the following instead:

    class Posts < Ramaze::Controller
      map '/posts'

      def index

      end

      private

      def not_me

      end
    end

Because Posts#not_me is declared as private Ramaze will not allow users
to access the method using HTTP requests, this works similar to Rails'
:except parameter (though it feels much more natural).

This behaviour means you don't have to create special methods and call
these, you simply declare your methods the way they're supposed to be
declared.


> More: how can I use "respond_with @user" and Ramaze give me an edit
> page with "/user/:id" url?

This is something that, although possible, does not come with Ramaze
itself. The reason for this is that unlike Rails Ramaze doesn't make any
assumptions about what database toolkit you're using. Because Rails
assumes ActiveRecord it's possible to ship these sort of helpers out of
the box but in the case of Ramaze that would probably lead to overly
complex code that most might never use.

Having said that, there is an alternative (of which I feel that it's
more correct): Ramaze::Controller.r() and Ramaze::Controller.a(). The
first method can be used to return an object containing the URL details
of a given method and a set of parameters, the second method can be used
to turn these details into an anchor tag along with some text. Some
examples:

    Posts.r(:index).to_s    # => /posts/index
    Posts.r(:edit, 10).to_s # => /posts/edit/10

Creating anchor tags:

    Posts.a('Posts', :index) # => <a href="/posts/index">Posts</a>

Yorick
signature.asc

Lucas Sampaio

unread,
Apr 6, 2012, 6:25:46 PM4/6/12
to ram...@googlegroups.com
Hello,

That makes sense, thank you. About routing, how can I get named parameters in url's? Like "/users/:id/edit"...


Att,
Lucas S. Magalhães
Graduando de Engenharia de Computação, 2009/1
Tel.: (27) 9942-8278
Skype: lucassmagal



Yorick Peterse

unread,
Apr 6, 2012, 6:44:09 PM4/6/12
to ram...@googlegroups.com
Lucas,

Ramaze maps controllers, methods and parameters to URLs:


    class Posts < Ramaze::Controller
      map '/posts'

      def index
        # ...
      end

      def edit(id)

      end
    end

In this example the URL for editing an existing post would be
/posts/edit/10 (where 10 is the ID of the post). If you want the ID to
come before the method you can use Ramaze::Route
(http://ramaze.net/documentation/file.routes.html) to provide an extra
route for this:

    Ramaze::Route[%r{/posts/(\d+)/edit}] = '/posts/edit/%s'

If there are any other segments after the /edit part you might want to
use lambda routes instead as regex based routes only use the first
matched group for replacing placeholder values (due to the use of
#match() instead of #scan()):

    Ramaze::Route['Posts Controller'] = lambda do |path, request|
      if path =~ %r{/posts/(\d+)/([a-zA-Z_]*)(/.+)*}
        return "/posts/#{$2}/#{$1}/#{$3}"
      end

      return path
    end

This allows you to use URLs such as /posts/10/edit/example/and/so/on.
It's a bit more complex than you might hope for but it also gives you
some extra flexibility.

Yorick
signature.asc
Reply all
Reply to author
Forward
0 new messages