 Good afternoon everyone Welcome to my talk. I'm going to be speaking about Pedrino the elegant web framework Just a first off really quick who am I my name is Joshua Hull and you can find me at Twitter at Josh buddy As well too if you want to get the slides for this presentation Just go ahead you can go to that github link there or as well I just tweeted where they are just clone the repo and you can view them. It's a bunch of images I'm sorry, but then you can follow along So Pedrino a web framework you might be asking the question. Why would I care about yet another web framework and Ruby and The simplest reason I can give you is that Pedrino is built on top of Sinatra, and I I don't know about you Like first of all everyone here has everyone use Sinatra Okay, cool. Yeah, I don't even need a show of hands Everyone knows what Sinatra is in the reason why everyone knows what Sinatra is is like Here's the example from the home page of Sinatra That's five lines of code and you can show that to anyone at all. They understand instantly what's going on And then as well too, you know You think about when you're trying to teach a web framework to someone who's maybe new to Programming or new to programming for the web and you're trying to get them into rails And there's all these directories and all these files everywhere. It's it's kind of complex It's a steep learning curve at first versus something like Sinatra, which is just so simple and so easy to instantly grasp so Pedrino is built on top of this foundation and This is a good foundation when I first saw Sinatra my thought was well Why didn't I think of that? That's so simple and you know, I was a little jealous. I'm like man You know, that's so obvious and if you actually do a Google search for Sinatra inspired web frameworks You can basically find a Sinatra lookalike in any language you want And this is just like on the first page of Google myself is what I found So Sinatra is really cool. And you know, we've covered it's easy to learn. It's minimal It appeals to the Ruby hacker and all of us that like to write our own thing and it doesn't give you anything extra You only take what you need in Sinatra But there's a problem and that is if you write a lot of Sinatra web applications You run into this thing where you're reinventing the wheel every time you're doing the same common patterns And if you're a good programmer, you take these common patterns you abstract them You put them in a gem You have Sinatra plugins and you use these things so If you just take a look at the Sinatra book for instance, here's a quote from it It says using partials in your views is a great way to keep them clean Although the last talk contradicted that Since Sinatra takes the hands-off approach to a framework design You'll have to implement a partial handler yourself. And that's you know what totally within their rights to tell us that Go do it yourself. I love that. I love that Sinatra doesn't assume I need partials If it's just a rest API, I don't need partials. So why give me something I don't need so what Pedro attempts to do is it comes up with a common set of extensions in Sinatra that makes Sinatra really useful and Like Sinatra all these components you take them or you leave them So here's just a really quick high-level overview of the components that you have available in Pedro You know you have application and model generation in the rest of it. You have really advanced routing You have helpers for doing all your view side stuff. So outputs tags assets Forms in the rest of it as well. You have a really nice mailing component for just sending emails out You have a Django like admin interface that gives you scaffolding very quickly gives you a beautiful looking admin Panel as well. You have a unified logger that you can use It has a really good code reloading So you never have to quit your Sinatra app and reload it in order to have it reload in Dev mode It's really excellent and as well it has localization built in so will not built in you can take localization If you want and then that lets you do it in any language you want So just looking over what Padrino is like I've mentioned it's built on tops on top of Sinatra So feel free to use all of your existing Sinatra extensions It follows then that it's built on top of rack. You can use any piece of rack middle where you want And then as well we have that approach of parsimony You just take what you want and you leave behind the stuff that you don't want In terms of what you get out of the box with Padrina You get an MVC structure and there's a couple different types of structure You can choose as well. You get a really nice set of generators that understand the components that you've picked for your application And then here's an easy pathway to migrate from Sinatra to something more advanced So if you've written a prototype for instance in Sinatra Maybe you don't want to port the whole thing over to rails Maybe you just need a couple more little things to make it really awesome. This is a great way to get into that So for the first feature I'm going to show you that's the the generation and here's some of the switches Out of the box that you get with the Padrino generator. You can pick your ORM and For instance those last two ORMs were just added today. We just had a release today so you cover basically every kind of data ORM that you could think of you have a wide variety of test Suites you can pick from as well javascript rendering Style sheet mocking you have all these components built in But what's really nice about this is when you pick which components you want and then it comes time to actually generate something It's the same commands to say generate a model. So you generate a product model But Padrino remembers which things you've picked and it applies those accordingly So say for instance in this example here and you generated for that tub for the bottom one there active record and riot So here for instance is what your model would look like and it extends from active record base Here's your blank test for you to fill in and this is a riot if you haven't used riot by the way It's a really awesome really brief concise testing framework and Ruby. It's really cool and this is Exactly what you get out of the box with the generator, but you know backing up to our Example here say we use SQL in our spec. So here's what it looks like if you've used these options to generate your project Now you have it extending from SQL model and then as well, too You have an our spec looking test again. It's blank. You have to fill in the blanks, but it gives you a really nice set of blanks As well too for generating a project. Here's what One of the styles of project layouts looks like this is called the tiny Layout and this is very much akin to the MIRB flat layout and pretty much puts all your controllers in one file All your models in one file the rest of it Here's what a normal layout looks like and it gives you directories for your controllers directories for your models And if this looks familiar, that's because it's very very familiar. This is very similar to a rails application layout So let's take a look at generating some really basic things. So for instance generating a controller So you want to generate a controller you want to put three actions on it index new create with those verbs Here's what you get when you generate that looks about right to me Say you want to generate a model again You specify some fields that you want to generate and it understands of course, which orm you've picked and it gives you the right Migration in the right base class for that So you just want to migration it understands this magic syntax of add fields to users or add X to users And you can give it a bunch of fields and it just gives you exactly what you'd expect It creates those fields or otherwise it deletes them on the downside and of course this respects your orm that you've picked As well to generating a mailer This is something that I'm sure we've all had to do you can give it a few default actions And it generates for you a nice default mailer fill in the blanks Another really nice thing in Pedrino is sub applications And what this lets you do is a create little apps inside your big project So for instance take this example here You build a project called catalog and you have an app inside called one and app inside called two Maybe like more appropriately you would like generate an app called blog inside there Maybe you'd have like a forum customer support You have a bunch of sub applications that aren't really tied together in any way whatsoever so by generating those you get those mounted at those mount points and then those Can each of those be referenced by the different generators so you can build controllers for your forum You build controllers for your blog Whatever it is and just a note about this is this is really cool because then say you've written a really great blog for your website Now you can just copy that and steal it and use it for Completely different site that you've built in Padrino These are reusable apps and as well these are just standard rack Sinatra applications you can use it inside rails 3 you can use it inside anything you'd like It's very standard out of the box So now another great feature that Padrino gives you is the routing in the controller side of things So this is very familiar to us if we if we used Sinatra and this is what it looks like in Sinatra Here's the same example Converted for Padrino. You don't have to do this, but if you do do this you get this You get URLs so you can not have to hard code URLs everywhere inside your views Take a more complicated example say account with an ID So in Padrino again, this is how you could write this Account you say with ID and again you can just generate the URL With your account on it You can shorten this and just make it look like this instead URL account 3 and it understands where the parameters go in it correctly does its thing Say that you don't want to be restricted to that style of URL for whatever reason you can change it up using the map option And here you can put slash ID slash colon ID to map that variable or dash Colon ID it doesn't matter you can put it in brackets Doesn't you can do anything you'd like with it and you can as well do regex matching on those two if you've noticed here one of the key things is this controllers method and This is really what lets you do some really neat things with the routing and controllers inside of Sinatra and inside of Padrino So we're gonna look at these areas here. So for the first one namespacing Maybe you have a bunch of routes and you want to put them behind slash something So in this example here, you want it inside slash admin So now when you go to render this URL you say URL admin index and this gives you slash admin slash index But You're not limited to just doing that as well. You could have multiple controllers Inside here and then as well each of those can have their own Name space so that you can separate them out that way and then do URL and admin index URL user index and they all go to the right place one of the really great features that Padrino provides is Provides and in this case here we often need to do like response to in rails or you need to do some kind of Content tight matching So here's your your normal index Just a get but here it is with the provides So now this will match on.js or match on the right content type and this will return some JavaScript Often in controllers, you have multiple things that you want to be able to provide so you just supplied an array of Types to match for as well There's a catch all type colon any and this lets you match the rest of them But this can get a bit tiresome You probably want to dry up your code if you have a lot of controllers that looks a thing like this a lot of controller actions So you just provide that at the in the at the class level for the controller You say provides JS any and now all of the actions inside there will Respect those provides that you've passed it and respect the extensions that are passed to it in the URL And as well too you can override that again at the action level if maybe you have an action It doesn't need JS. You can just override that on a per-action basis Something that Pedro has his filters, of course Sinatra has filters too, but Where this gets interesting is like for instance this example here would work in Sinatra So you can just assign a variable have it available in your views But you can also use Pedro before Filters to route to a different set of actions So if this before block here returns false, it doesn't match on that controller and In this example here that first set of controllers if the user is an admin It will not match that and it'll look for a next available match And in this case here it will go down to the second one because there's no before filter to stop it And of course you have the after filters just like you have in Sinatra too Another great feature is nesting and here you have again just a normal index view on top of a Details controller, but maybe you want this nested inside of a product. You can do that just using parent You can do it two levels deep Or you can again put this up on the controller level so that everything inside your controller nests that way As well we have layouts, so if you have a controller you can disable the layout for all the views inside as well to You can provide a custom layout for certain views You can do that on a per action basis or on a per controller basis. It's very flexible and makes your code incredibly dry So for the next section we'll look at the output helpers quickly You have content for and yield content So in this example here where you have a view that needs to have a special style sheet You can just capture that content and then inside your layout you just yield that content back out again So it's just as though you had written this here and just put that style sheet link tag directly there As well to you have content capture a concat content and capture html And this lets you like capture a piece of html wrap it inside a tag So here's a highly contrived example wrap with tag. What an awesome paragraph should be pretty understandable It's very few lines of code and it just makes your life easy So here you go. There's the output and as well to we have tag helpers So just doing a normal tag something like this where you have an attribute and a bunch of options As well it can have content inside There's some nice syntactical sugar for doing a content tag and again. This is a tag name with some content inside it You can pass attributes to it and as well to it can capture the block So you can just do it in this style the last one here As well to there's a little bit of sugar for doing input tags So this just makes it a little bit nicer for writing that input tags inside Padrina As well you have helpers for all the assets So this is just a common set of asset helpers. So doing flash messages Doing link twos and it obeys the if and unless Link to syntax that I think rails and action view use you have a mail to link as well You can do images style sheets javascripts fave icons feeds Go take a look at the docs, but it makes it very easy to do this sort of thing as well too, of course, we have forms and This should be fairly familiar I would I think if you've been using any rails at all and here you can provide a custom HTTP method You give it a URL and then you organize things with feel set tags You have your inputs and you have a beautiful form at the end of it Of course as well you want to build a form around a particular object that comes from your ORM So you have a form builder as well And this just takes some object that you give it and it builds a form around it So again, you have your form tag You get air messages back from it and then as well till you have a bunch of fields and notice here too that In that bottom part there You're actually have a form for a sub part of that object so you can create nested forms and as well There's more nested form support more advanced support coming So what if for instance the object you're giving it doesn't support an errors method, which is underlyingly what this is using Well, Padrino gives you an abstract form builder, which lets you create your own forms And here's the methods that you have to override or not override depending on what you want to do with your form And then once you actually get out into the views you can just specify Hey use this builder when you're building a form or you can just set it right across your application Always use this form builder. So it makes it really easy to override and just do custom forms As well to Padrino comes with a set of format helpers And a lot of this is just borrowed from various other frameworks escaping HTML pluralizing word wrap You know very typical things that you want to do with text so in our example we looked at rendering and Padrino gives you a couple nice bits of sugar for rendering So here is the Sinatra way of doing things you say herb index And that's great. That works just fine It gets a little hairy though when you start having nested views and you have like colon or two sim or whatever It just looks a little fuzzy Padrino gives you a different way of doing this and both can still be used You just say render herb index or render Hamel index or you just say render index and in that case it will just find the first engine available to it that matches It'll rather it'll find the first view available that matches one of the engines installed As well to you have the partial helpers so this just Looks for underscore dot item and then again looks for an available engine to it And you can pass it an object or you can pass it a collection of objects And it will render over them and give you a counter inside if you're doing a collection And as well you can write your own helpers. This is More or less lifted from Identical to Sinatra. I believe it is actually identical to Sinatra You just define a method that you want to use render pony and then it will put out the string pony time or you can Just hook up modules and of course these methods are available inside your views and controllers Another feature in Padrino that's hasn't quite landed yet But it's very close to landing is the unobtrusive JavaScript helpers and this Is available on forms and links so it's coming soon But we have support for all of the scripting engines that we support And so for instance, here's what it looks like you just say remote true And then in terms of the HTML this generates data remote equals true just like that and then that's picked up by the JavaScript and it does the right thing and the same thing with links you say remote true it inserts its custom attribute and Confirmations for destroying things and then as well it supports custom HTTP methods So one of the the hot things in Petrino is being able to generate an admin section So here's just a screenshot of what the admin section looks like out of the box as well it comes with a variety of different themes and Let me show you how to create this So here you create a model and you give it some stuff you migrate your model and then here's the Really important command you say admin page and then you give it the model that you want to generate on and you start Pedrino up this admin section is ORM agnostic so it supports all of our ORMs I didn't add the last two that we just released today So it actually supports all of them seriously and so for instance if you take a look at the scaffolding that it generates for active record For instance, it does the right thing a count dot find update attributes Here's the same example in sequel and it's doing the right thing for sequel as Well, too, it gives you an authentication layer for the admin section That's really easy to hook up and set up So you inside your application you just enable authentication enable star location and then you set a login page And then you can just define Which pages you want to have protected so in this case customers orders cart checkout these are protected pages For any role so you need to have some role to satisfy it But you can also use this for more complex so sort of ACL role-based authentication So say for instance, you have this example here and at the top you want to control For everything from route onwards But you want to allow them to go through slash sessions for any role at all And then for the admin section They are the only ones that can access slash admin and then editors can go to posts and categories and edit stuff that way, too Next feature. I'll show you is the mailer So here's a just a really nice thing to have you can just send an email in one line I only broke it to two lines because the slide it just wouldn't quite fit You know looks very simple form to sorry from to subject body As well, too, you can break out these mailers into reusable bits a little more complex like this And this example here you can just generate it again with a generator and it will create that mailer for you And then you just inside your views directory you get a mailer's and Inside there you can create all your email templates One of the nice things you get is a multi-part goodness. So take for example You have a text and you have a plain part You can just specify those two parts or you can even dry that up further and you can just say provides Plane and HTML and just render it to a path and it finds the correct one based on the content type And it just renders out the right one for the mailer and As well, too, you have an ad file you have a bunch of other things But if you really want to know everything you have go take a look at this gem the mail gem and Ruby it's excellent It's awesome, and it's what's underlying all the Padrino mailer functionality As well, too, you can set to defaults for the mailer So you can have an application wide default from or you can have it on a per mailer basis This makes your code again just more dry and easy to understand Something else Padrino gives you is a localization. So out of the box. Here's the languages we support we just added a Dutch and Polish today, I think and These are for the admin section. You have all these languages available to you and then as well, too It's pretty easy to just add your own So all you have to do is grab these four YAML files translate them just email them back to us or whatever We've had lots of people contribute languages, and it's pretty awesome in your application Then you just specify the locale that you want you have a YAML file inside your locale directory, and then you can just render it out Again, this is all provided by this excellent gem. So go take a look and see what it gives you For models We have a really nice little thing where if you create a model Then you can just run a rate task to get the translation of that and then that just provides you with a blank YAML That you then just fill in the blanks and start localizing away, and it separates every model into its own YAML file So nothing's going to trample over anything else And then when you get to your forms, this was a slide from previous You had label colon name and why the colon name because that then lets you do a localization look up on that So in this example here will look under en account name, and it'll fill in the correct thing there Another hot new feature that was added is templates and plugins So beyond what you have from Sinatra plugins, there's as well some Padrino plugins that are quite useful Let me show you templates first though. So if you have like a common Application style that you're creating over and over we give you a template DSL And this looks more or less like Thor's DSL with more stuff on top Here's just a sample of part of a template You can see, you know, we ask some questions of the developer We instantiate a project. We do some other things. We ask some more questions. We have some output So it just gives you a really nice way if you're doing the same application over and over And then as well too on the plugins side if you just go G plugins dash dash list Take a look. We have 26 plugins available right now, and here's the list of them I'm sure you can find something there that's useful and these plugins are incredibly simple to write So just as an example here, here's the Google analytics plugin This is literally what it looks like. It's five lines and all it does is just require the the google analytics rack Gem and then it just initializes it for you So that's all well and good But I think something that everyone loves is benchmarks And I love these too. I love things that go fast. So how does Padrino do on benchmarks? Well, we have some benchmarks from 910 And you can see here rendering a string inline or rendering a basic Herb file you do pretty well not quite as well as a merb If you get to a more complex application, we're on top And we beat Sinatra handily too. So that's a really nice thing to see So it's a very fast low weight application framework and as well too someone did a An awesome blog post on romaze versus padrino and this was the numbers that they came up with again This is a bit old. So I'd love to see what the numbers say now. I'm sure it's a bit faster And uh, you know again great results So take a look In terms of the team itself, uh, it was originally created. I believe by Nathan and Arthur And they're both in california and then David joined he runs like a 10 man Web shop off in italy And uh, they use padrino now for everything and then as well, there's me and florian and lori And we all contribute we're on the core team And then as well to do as well today. We just added a lusus and he's working on redis support inside padrino And as It's pretty awesome And then as well too, uh, come and get in touch with us. There's a screencast online As well we're on free note and because of the fact that the core members are all over the world Like we have the italian one and then us there's always someone in the channel And the channel is pretty busy. So it's a good place to be As well check us out on github and then as well We have a twitter account that you can follow find out the latest what's going on And thank you very much