 Our next speaker is Brian Lyles, otherwise known as Smarterness, at least on his blog, and Brian L on Twitter. And as we've mentioned before today, Sarah May talked about how Active Model is a really interesting new part of Rails 3, and Brian is going to talk about that in his Active Model extravaganza. So as a bonus for my talk at the beginning, and I just want to give out a little public service announcement. This is Randall Thomas. This is me. This is Randall Thomas. This is me. One more time. Randall Thomas, me. Problem solved. On to the talk. So one of the most important parts for Rails 3 for me is Active Model. One of the things I really love that Yehuda and Carl and everyone else on the team did is they went through and they got rid of a lot of the junk, and then they made a lot of things not more awesome. And one of the things I really love is Active Model. So let's talk about it. So who here remembers Rails 2.3? And who remembers Active Record from Rails 2.3? It doesn't really do anything with it. It's like it literally is the spaghetti monster, the spaghetti meatball monster. If you've ever done inside the code, you realize that you really could not extract anything out. So basically this talk right here, I'm going to dedicate it to Yehuda. So let's start off with a little appreciation song. So this brings us to the meat of our talk. What is Active Model? Actually after reading the code, Active Model is two different things. And what really brought us to light is two years ago at Ruby Comp down in, I guess, Florida, Yehuda gave this talk about external APIs. And it really, really struck a chord with me. And what we found out is that Active Model is more than one thing. So let's talk about really what we want. We don't really care about Active Model. All we want to do is this. We have a helper in Rails, or we have a partial in Rails, and we're trying to render a form of something in that sound instance variable. What the hell is it? Usually it's going to be Active Record. But for some of us who have to use SOAP and things like DeSandra and things like that, it might not always be. And before in Rails 2.3, we have been coming up with a whole bunch of crazy work arounds to make this kind of stuff work. Active Model gives us this. And to notice that Active Model gives you the instance variable sound, and it also gives you some of the things with the label and the textbook. And also for crazy people like me, if you want to create their own OHMs, object cache mappers, not relational, Active Model actually gives me a good set of guidelines of things that I should do to make it work with this case. So basically what we're trying to do is give me some guidelines to allow me to do my stuff like I want. So let's talk about Active Model. So Active Model, like I said before, is two things. It's an experimental API, which is just basically a set of guidelines to say you better do this, and if you do this, it's crap for work. And it's also a set of awesome helpers. Actually, in preparing for this talk, I actually read all the code for Active Model and if you go down with Rails, the Rails slash Active Model slash Lib slash Active Model, you read every single thing in there, there's a whole bunch of gems. I actually think if I'm ever going to teach people how to code and how to document your code, that's actually going to be something that you're going to show people. So let's talk about Active Model. You already used Active Model. Who here uses Active Record in the Rails apps? So there you go. You've already used Active Model, but you don't really know you're using Active Model. You're using mostly the external part. You're not using the part that I'm getting ready to talk about. But who here likes SQL? That's E-Q-U-P-L. So I love SQL. I like SQL because it's really no frills. And before A-Rail, it's actually working for a few years. So what I have here is some code showing SQL. And what I'm doing here is I have a task model, and what I want to do is create a new task model and insert it into the database. Because that's all you want to do is create data and put it into the database. So you know that I've done this, but the problem is the way that this is right now, I can't use it in my form cover without a lot of extra work. But you know what? The SQL guys, they took care of us. And I'm typing really slow. I'm getting kind of old. So there we go. So after I finish typing all this, what you're going to see is I've saved it, but it doesn't look Rails like... Oh, snap! So how did they do it? And when I say they, I'm actually going to speak to you, because I personally know this guy and I personally know that he had a big thing to do with it. So if they, someone else in this room, someone else watching this talk at no future time, thank you. So actually, let's think about this. How did they take after-record, take the good parts out to allow the people to extend it? And you know what? I got an idea about how this is happening. So we have active record over here. It's very scared in the corner. And what we had to do is we have Deuda sitting right here. And what he does is he prays to the God. Ba-dee-mah! And you guys seem to see pretty much what happens next year. So they reached into the guts of active record while it was screaming and crying and they pulled something out. And once they pull out, well, you know what it is. It's active model. So, that's how active model was born. Maybe that's not true. Maybe it is. I don't know. So, whenever I was talking to Josh about this talk, he says, I said, you know what? I'll put some code in it. Normally I don't like code. I like to talk about meta stuff. But I'm going to do some code today. So what is active model? Active model is a contract. It says that you will do these five things. And it might be a little bit more, but you know, these five things is what I got. All you need to do is say that you have some kind of errors and you can actually have the right messages on errors. You need to respond to a few other things. And then you need to have a method on your class that allows you to do modeling. That's it. It's actually very, very simple. And believe it or not, you don't actually have to use active model anything to have active models in court for your app. All you need to do is implement something that works with this or that. So, let's talk about the helpers, the stuff that's actually in that active model directory. So, earlier, there was talk in IRC, at least, about Lent. And everyone needs a little Lent in their life. So the Rails guys did us a big favor and they said, well, if you want something that needs to be active model compatible, well, why don't you just include this little module here. I don't think you need to do tests. I think you can just get away with Lent. I do know that. But you know what? I don't use test units. And I use RSpec. The first thing I found out is that there was no helpers for RSpec. And there is someone out there who did write it. And unfortunately, I found this code, but I was unable to find it again. So I put it up on this gist. So if you ever go to this gist, I will make sure that I actually do who wrote this. I did not write this code, but it works with RSpec, and it makes everybody happy. Oh, look at slide. Let's keep moving. So, one thing I want to say is, I like talking about testing. And unfortunately, this is not a talk about testing. But what I will say is that test all the effing time still lives because, look, easy testing. Easy testing. And you know what? Anybody here a wrestling fan? So you know who this guy is? Woo! Always got to give it a little bit... Ric Flair. So let's talk about how easy it is to test. There you go. That's a test all the effing time for you right there. So what else does Active Model give you? It gives you attribute magic. And us being Ruby people, we love magic. But this is like real magic. This is magic you can touch. So let's talk about some real code. So let's say I was writing an ORM. And I'm sorry to throw so much code at you, but it was so good I couldn't keep it to myself. So let's talk about some of the things that Active Model proper gives you. It gives you this attribute method stuff. So let's say you have a class of calves and you have some attributes on it and you have your calves have name and your calves have data. All you need to do is use the defined method attributes. It comes out of this Active Model attribute methods and then you can define a method called attributes and guess what? This bad boy looks just like Active Record. You would not even know what it was defined for. And really the reason I tell you the reason I got into this is because unfortunately the real world, at least the real world that I live in, people like to use Soap. So what I did is I actually did implement Active Model to interface in front of Soap. Actually Saban being more specific. And what I was able to do to expose to the developers was something that looked like Active Record but was coming from some crazy old Windows service on the backside. So let's talk about, next we'll talk about callbacks. I like pictures of phone boots. I don't know what it is about phone boots. Maybe they're red. Maybe they have lots of windows. But I like phone boots. So let's talk about callbacks. In Rails... So in Rails, in Active Record, you're able to do the fourth grade and you have a whole chain of callbacks that you can use. Well, Active Model Helpers gives you ability to create these callbacks in your code. All of this is a couple of extra lines. And notice all I had to do was extend Active Model callbacks. And then define which block callbacks that I wanted and then I was able to do the regular one before create, do some stuff. Because we all do some stuff in our apps. So the next slide here is about naming. And I may actually change this slide up a little bit because another IRC conversation. Naming. One thing that we always learn and I've written this a thousand times is you'll send an instance variable controller in your Rails app and you'll pass the form board and the first thing it'll say is you can't find model name. But now we don't have that problem anymore. All you need to do, all you need to do is extend Active Record naming. No, include Active Record naming and you'll actually be able to do code just like this. And actually the cool thing about it is that you don't have to do any extra work. All it does here is it just keeps it from the name of the class. No extra work. So what's next? We all have errors in our apps. At least you guys have errors in your apps. My apps are awesome. So let's look at some code. Oh there is that. There is that in Active Model naming. This is what I get for splitting slides based on IRC conversations. So the cool thing about Active Model is like I keep on saying it's basically a set of helpers that she probably should be using but she don't have to use. Here's a good example for something that I don't want to do. If you want easy, easy, easy errors on your models all you need to do is actually you don't need any more code than this. These eight or so lines of code including the spaces make sure you include the spaces will give you real errors and it'll give you an Active Record error object that you can work with inside of your forms and pages. And the cool thing is because you now you have errors you can do validations. So let's talk about serial boxes. When I was young I didn't need serial I don't like serial to this day but I like pictures of serial boxes. So let's talk about serialization. One of the cool things another cool thing that Active Model gives you is the ability to serialize to JSON. I hate taking some object and JSON or XML or anything else. Once again, Active Model gives you this and notice that all you need to do here is an include. And then say what your attributes are and look at this. You set it to use the .2JSON method and you automatically get JSON out of this. So next up will be validation. I will give someone $5 or you can tell me why I use pink van to talk about validation. But I don't remember right now so I have no idea why I use this. And actually it's like a pink short van too. I don't know what's going on with these pictures. It is a pink short bus. Is it IRC again? No this is not IRC. This might have been a little bit of home time. This might have been a little bit of time or something. I don't know what this was. So, validations. Once again, notice that the theme with Active Model is include, extend. They give it all to you. So let's say if you want to include Active Model, if you want to have validations, this actually is the least amount of code that you need to do validations. And the cool thing about this example is that I basically cut and paste it from the example in the source. And notice it works. But notice what this gives you. With pretty much only four lines or five lines of validation code you can actually get rid of people who have the last thing to start with Z. Because we don't like people with the last thing to start with Z. And because you have validations you can do a whole bunch of other Active Records-esque things with your models. So let's see what else we have here. So here's the example of giving a 30 minute talk. How long does it take to give a 30 minute talk? For me, if I was going to do Active Model it would take me an hour and a half. But because I wasn't sure how long it was going to take and my wife is not a good, she didn't want to sit down for 20 minutes for me to talk to. Actually the third time I actually did this talk for her she kind of worked with it. So what we're going to do here is we're going to gloss over everything else. But I'm going to tell you a little bit about the fashion. It gives you observers and it allows some dirty objects. So you can actually see if you have changes. One cool thing about the internet is that we have a great name called Flickr. And they allow me to download all these pictures for free. And with attribution of course, so I put my attribution here. And here is me. I'm Brian Lyles. I blog there. I Twitter up top. And that's it. With that portion of the talk. So yeah, actually that's it with slides. I actually don't like slides I rather just talk. So you're going to ask hey Brian, why did you want to use ActiveModel? Why are you pursuing this in your own time? And I'll tell you, it's because I wanted to create a sound board. You know the sound board. You go to the internet and you press it and it makes funny sounds. This is what I wanted to do. But not the one that I wanted to use sound board, I wanted to use Redis. So I said, I'm going to make a sound board in Redis. And not only do I want to make a sound board in Redis, I'm going to store the binary object in Redis. Why not? So let me show you why I created this talk. So notice we have HTML5 here mounted corners head shadow. This right here is a Web 2.0 app. We can start with that right now. Let's get some VC. We are in San Francisco. So what I all wanted to do was add new sounds to my app. So let me put this a little more spicy for you guys. And all I wanted to do was add new sounds and give them a name. Let me see what we call this one. We'll call this the chicken head sound. You know what chicken head sounds like? The side step over there? I know step knows what a chicken head sounds like. Would you cut it off? No, chicken head. You know what a chicken head is. So we're going to drag a chicken head. That's what a chicken head sounds like. So let's go to our desktop here. We'll drag over our chicken head MP3 and we'll create a sound. And all we're doing here is we're uploading it. Notice that I can tell you, I swear to God to you that behind the scenes here this is stored in red. It's a whole thing. There's no files with them. There's no MySQL. But let me get, since I have a couple more minutes here or a couple more seconds let me talk a little bit about Rails. So I was doing a sound app in Rails. And one thing you'll notice when you're doing a sound app in Rails you're going to want some content ranges because you're going to want to know because when you're going to have a slide you're going to want to ask for the beginning of the app or the end of the app. Rails can support that. Somebody please write a middleware that does. So what we have here is now is HTML5, an audio tag that's actually streaming live from our app, from Redis, not hitting the disk. But you know what, we can do better than that. How about for the people who like classics let's put in, let's say, Crazy Sound. Let's drag another song over there. Anybody familiar with this song? And that's it. So you'll notice what's happening here is Safari is loading this maybe. I've noticed it's hit or miss but we'll do the chicken pick one in a second. Actually let's just do one more since I have a little more time. Right, Josh? Okay. Yeah, I have a little bit more time. I'm going to find my mouse and let's drag for this one. Since I really do I really do like hip-hop. There's a theme song we'll just call this one. And I will give I'll buy lunch for someone tomorrow at lunchtime downstairs if you can actually identify that song as it comes up. So this is all I wanted. I wanted an internet soundboard and actually not only did I want an internet soundboard I wanted an internet soundboard that worked on my iPad and using these technologies, these Leve 2.0 HTML5, CSS3 things, I was actually able to get the tap working on my iPad and let's hear the fruits of my efforts and I'm really done this time. So thank you guys. Besides that, he doesn't count.