How to Use Concerns in Rails - Ruby on Rails Tutorial

preview_player
Показать описание
How to Use Concerns in Rails is a free tutorial by Jordan Hudgens from Ruby on Rails course
Link to this course(Special Discount):
This is the best Ruby on Rails Course
Course summary:
Build a professional Rails application.
Implement advanced JavaScript components, such as persistent drag and drop functionality and live page update via ActionCable into a Rails application.
Deploying a Rails application along with the ActionCable web socket feature to Heroku.
Build a Rails 5 application that utilizes multiple layouts.
Build jQuery and CoffeeScript components that can be utilized by the Rails application.
English [Auto]
Moving down our list of to do items our next task is to integrate concerns. Now if you have never heard of what a concern is. Don't worry that's we're going to go through right now and we're going to do it right now and I'm going to give what I think would be a good time to use concerns. Now there is a lot of debate on concerns and if they even should be included in Rails. But I think it's something that should definitely be included in a comprehensive guide to rails because there are many times where concerns can come in handy. And so if you go to your model's file you'll see right here that we have a nother directory inside of it. It's empty called concerns. Now what concerns are at their essence is when ever you have some type of functionality that doesn't fully belong inside of a model file or possibly it should it should be shared across a couple different models then that is when a concern might be a good fit. Now I say that with a caveat and that is concerns have definitely gotten a reputation for being abused and they're abused when individuals will build something and they'll implement some functionality and think that it's a helper methods such as you're uploading something or doing something that is not actually even really data related. It's just a helper module and they'll put it inside the concern because they think that that's where it should go. The problem with doing this is that it really isn't the way that software engineering should work from an architectural standpoint. So your concerns should always have to deal with data there in the model directory. So therefore anything here should be data related. If you have something that is a helper module that's not data related. In other courses I have built out a semester sending module and that would that I know a lot of developers who would put that in their concern directory. The problem with that is that is not where it should go. That didn't have any sending a text message from your application has nothing to do with data. So in those cases that is what your lib directory right down here is for and that is where those items should be placed and where later on in the course we're going to get into examples when that would be a good fit. So for right now I just wanted to give that caveat because I don't want you to go out into the big wide rails world and start building applications and any time you wanted to build a helper module just sticking it in the concerns because that's just kind of a bad practice. But with all that means said let's talk about something where a situation where this would be a good fit. And I think I have just the scenario right here we have our portfolio and we have our skill models. Now let's imagine and we're not just going to imagine we're going to update our skill so that it could contain an image so it could have a little thumbnail like little Rail's icon or a ruby logo or angular logo something like that. I think that's a really good fit and something we should have. So I'm going to open up the terminal and say rails G. Migration and still bad we'll call it a skill badge two skills. Actually you don't need to call it a skill bad you just call it a badge. And then from here we can say badge and this is going to be of type text and I can say rails D-B migrate and this is going to now give our skills the ability to have images associated with them. Now for the sake of development and not just development it could even be in a real production kind of environment. We may also want to have defaults there so that I think that would make sense where we want skills to by default have some type of image so we can copy this paces inside. We're not going to have two of them only have one. And this is just going to be called badge and inside of this. Now we can change this value to be let's say 250 by 250. Give it a nice little square and this will be perfectly fine. However I think this would be a good spot for concerns because what we can do is I hate having to have these big long image you are or else here. I don't think that that'
Рекомендации по теме
join shbcf.ru