00:00:05.120
Our next speaker is Bryan Liles, otherwise known as Smarticus, at least on his blog, and Briannella on Twitter. Today, we've heard about how ActiveModel is an interesting new feature in Rails 3, and Bryan is going to talk about that in his ActiveModel Extravaganza.
00:00:35.520
As a bonus for my talk at the beginning, I want to give a little public service announcement: This is Randall Thomas, this is me. One more time: Randall Thomas, me. Problem solved. One for the talk. So, one of the most important parts of Rails 3 for me is ActiveModel. One of the things I really love that Yehuda, Carl, and everyone else on the team did was to eliminate a lot of the junk and make many things much better. So, let's delve into ActiveModel.
00:01:15.439
Who here remembers Rails 2.3? And who remembers ActiveRecord from Rails 2.3? It didn't really do much. It was like the spaghetti monster, or should I say, the meatball monster. When you dug into the code, you realized you really could not extract anything out of it. This talk is dedicated to Yehuda.
00:01:37.759
Let's start off with a little appreciation song. Come and listen to a story about a man named Cat, a faithful packer—it made me wonder if he has any cats, as he was hacking away. I never thought that we would see Rails 3; it’s now released. All of my words are a sandwich!
00:02:39.760
So, this brings us to the meat of our talk: What is ActiveModel? After reading the code, ActiveModel is actually two different things. This was highlighted two years ago at RubyConf in Florida, where Yehuda gave a talk about external APIs that struck a chord with me. What we found out is that ActiveModel is more than one thing. We want to use ActiveModel to make interactions simpler, especially for rendering forms in Rails without relying solely on ActiveRecord. For those of us who need to use SOAP and alternative databases, ActiveModel enables this flexibility without all the struggle of Rails 2.3.
00:04:06.239
The advantage of ActiveModel is that it provides an instance variable and some associations that work with it. This allows those of us who want to create our own non-relational mappings to have a good set of guidelines to follow. Essentially, what I'm looking for are guidelines that let me proceed with my work as I choose.
00:04:57.760
ActiveModel consists of an external API, which is a set of guidelines that, if followed, will make everything function correctly. It also provides useful helpers! While preparing for this talk, I went through the entire ActiveModel code. If you download Rails, go to the 'active model' directory, and read through everything, you’ll find a wealth of useful gems within.
00:05:37.279
Who here uses ActiveRecord in their Rails applications? Well, there you go—you’ve already used ActiveModel but probably didn’t realize it. Most of you are using the external part, but I want to show you the other features. I also love SQL—it's straightforward and has been working fine for a few years. I have some code that demonstrates SQL usage through creating a new Task model and inserting it into the database.
00:06:24.240
The challenge I faced was that this model, as it stands, cannot be used effectively within forms without additional work. Thankfully, the SQL guys took care of us!
00:06:58.720
So, how did they do it? When I say they, I’m talking about people I know who were instrumental in these changes. It's about extracting the good parts to allow extension and customization. To illustrate this process, imagine ActiveRecord prays to the gods, and what comes out is ActiveModel.
00:07:41.760
ActiveModel functions like a contract; it sets out that you will perform a few essential tasks. In essence, you need to manage errors and have a method in your class that allows the implementation of a model name. It is surprisingly simple. And the best part? You don't have to use ActiveModel directly to have support for it in your application; you simply need to ensure compliance with certain expectations.
00:08:00.480
Let’s discuss the helpers included in ActiveModel. For those needing something active model-compatible, the Rails team has provided invaluable resources, including modules to make the process easier. However, if you don’t use Test Unit, which I don’t, and prefer RSpec, resources are available to integrate ActiveModel within RSpec without hassle.
00:08:58.000
I also want to emphasize the importance of testing—test as much as possible! Testing is vital for a smooth development journey. Is anyone here a wrestling fan? You have to add some flair to your testing efforts!
00:09:45.839
ActiveModel also provides attribute magic—a feature that appeals to us Ruby enthusiasts. Let me show you some real code. If you create a class, say for Cows, you can define attribute methods easily. This allows you to create something that smacks of ActiveRecord; you wouldn’t even know it wasn't.
00:10:18.560
Now, describing my experience, especially with SOAP integrations, I found ActiveModel useful for developing an interface for SOAP APIs. This approach lets you expose functionality that resembles ActiveRecord while relying on older backend services.
00:11:06.639
Now let’s discuss callbacks—one of my favorite features! In Rails or Active Record, you can set this up easily using the built-in before_create callback; what’s convenient is that you can establish chains of callbacks within your ActiveModel definitions.
00:11:59.760
The naming convention for instances in Rails can often lead to confusion; however, now, you can extend ActiveRecord's naming features to simplify this process, allowing code to aggregate around the class naming.
00:12:43.279
Errors are a common occurrence in applications, but ActiveModel will help you implement error messages effectively through minimal code. You only require a few lines of setup to have validation working as intended. This facilitates easier management of validation errors while developing your models.
00:13:11.600
Let’s pivot to serialization. ActiveModel simplifies converting objects into JSON, which is often a dreaded task. With just an include directive for your attributes, you get a fully-fledged JSON representation through just a simple method call.
00:14:14.640
There's another interesting feature about validations. ActiveModel can easily be included with minimal setup code for validations. This allows you to enforce standards such as starting letters on attributes in your models.
00:14:50.720
ActiveModel provides introspection, observers, and even allows for dirty objects to enable status checks of changes. It’s amazing what these tools offer for modern web application development!
00:16:01.240
Now allowing me to talk about my personal project, I created a soundboard application using ActiveModel and Redis. The goal was straightforward: to drag and drop sounds into this application seamlessly. The integration with Redis allows the sound file to bypass traditional filesystem storage, enhancing the app's performance.
00:17:01.680
While discussing sound uploading to a Redis-backed application, I realized how Rails could benefit from better content handling for streaming. The core functionality relies on HTML5 and audio tags to deliver the functionalities, but some features are lacking and may require middleware for proper support.
00:18:31.760
Ultimately, I wanted to develop an intuitive sound board that could work across devices, leveraging the latest web technologies. In closing, my hope is that this presentation provides insight into how you can utilize ActiveModel in developing modern applications efficiently. Thank you all for your attention!