0:20
So before we jump into scaffolding, just want to do a quick recap of
the basic steps that we have been doing in all the other examples,
the [INAUDIBLE], the [INAUDIBLE] and all that.
So it's, we'll start off with our usual rails new movies.
Go into the movies directory, open up the gem file.
You'll have to add mongoid gem, right?
Bundle install.
Now, we've talked about this multiple times.
When you set up your, when you configure your application with mongoid by
doing rails g mongoid:config mongoid.yml,
you know that mongoid becomes your default or
it becomes your default model generator by doing this.
Now, you can always switch back to active record by opening up application RV and
uncomment, comment mangoid and uncomment that.
I've talked about this in the past ,so again just to kind of recap that
point here, mongoid rails g mongoid automatically
makes mongoid your default ORM which means all the bootstrapping and
all that will happen directly in application .RV file.
Finally rails s, to study rails server and then we'll go and
make some changes to the project by adding custom classes and
eventually the scaffolding command.
Let's take a quick look at custom classes and methods.
We have two custom classes in this application, Measurement and Point.
Now what are custom classes?
These are primarily they represent anonymous blocks of information
within the document.
So for example, measurement if you remember, it deals with amount in units.
The amount being the running time of the movie, so 60 minutes or
two hours and you have a method called initialize where
there is a normalized form independent of source format.
So for example, going back to measurement right.
So notice how we have amount and unit.
So this is completely, it all depends on what is the implementation you have in
your custom class.
So based on that you can take in different parameters.
Couple other helper methods like to_s is used in producing formatted outputs.
And you also have one what we call instance method.
Mongoid requires you to have one instance method called the mongoize,
which is basically creates a DB form of the instance.
Which is taking whatever the data is, whatever the data format is and
converting that into a database form.
Whatever is the format that the database understand.
Now, the next things is for custom classes,
mongoid provides three class methods, they are mongoize, demongoize and evolve.
So, the first one is demongoize, takes the instance of the class from the DB-form
of the data, so it's basically database format to the class format.
Mongoize is the other way around, class data into DB-friendly form.
And then you also have a method called self.evolve, or evolve basically,
it is used by criteria to convert object to a DB-friendly form.
So basically, these are the three methods that you will see in all the custom
classes like measurement and point.
3:43
All right, let's jump into Scaffolding.
Again, remember mongoid is the default model generator to be explicit at command
line you can always add the --orm to your command line.
So let's look at a place first start first class Place models of point,
and its descriptive address information.
So, rails g model Place and then a bunch of values.
Remember, string is the default, but you can always add a custom type or integer,
or other types by doing a colon point.
So, this will give us a place.
Should look something like this.
That's our place.
And then here is all the different fields.
A couple of other important information here embedded in locatable,
polymorphic:true.
So this will come, you'd probably seen this in other lectures in the past or
the relationship lectures.
Basically actor can have a place, writer will have a place,
director will have a place, so place will be embedded in multiple objects.
So that's basically the polymorphic behavior.
We also have a method, a useful methods called sanitize for mass assignment here.
And notice pretty much everything here is a string
except say geolocation which is a point.
So any time you want to handle any custom type or
additional forms that are not strings basically.
That's a good way of putting it.
So any time you want to take additional forms and
convert the representation to an instance form.
That's pretty much what this is doing using the demongoize method
which we've talked about in the previous slide.
So that's place a model class.
The next model is a director.
Now go to the next few models pretty quickly.
You don't have to go through in a lot more data's we've cover does doing relationship
lectured plus you can always read up on our get hub read me documentation.
So director is fairly straight forward and simple and the next one is a DirectorRef
which is an annotated reference to a director that gets embedded into a Movie.
Writer holds the detailed information about the writer of a movie, okay.
And Actor of course we've seen this before, contains the actor information.
Notice how we are using the custom type for height for example to measurement.
7:06
One of the next important step that you need to do is to go into your
config routes.rb and add the resources movies so
this is where all the magic happens right, with your get and post and put and delete.
This is your resources.
And before we bring up our index.html, let's look at the basic flow,
its a very simple just like zip.
A simple HTML page, we will display all of this title on this heading poster,
title, type, rated and so on.
And this is the information that we will pull from our movies model and
displayed on the screen on our page basically type, rated and
all the rest of the information, here.
Also notice that the first cell is actually a image.
So we can use the url_poster which is basically an image and
display that as part of our, just to make our application a little more
interesting and have some kind of a image on our HTML page.
Also you can notice here, link_to movie.title, so
we are combining the title and the url_imdb,
such that the title is the link text and
the url_imdb is the actual hyperlink or the URL of the link.
Now, from here on I mean it's really up to you,
you can do a lot of different things you can add additional data remove,
whatever you need to remove and of course you have your show.html and
the new.html which will support the new and show and of course delete options.
So at this point, if I just go to the localhost3000/movies,
I should get my movies page.
So again, I get a very basic page with all the information of the movie.
9:59
So here is a stalker movie or TV series with all the data coming up like that.
Okay.
So that wraps up our simple scaffolding and assembly instructions with a demo.
And again, as I said, the possibilities are, I mean, it's endless.
Now that you have this working, you can go and do a lot of different things.
Deploy to Heroku, and show it to all your friends,
as to what all you can do with this movie example.
And of course, you can enrich the data by adding more relationships and
again make it a nice little working application.