Monday, September 24, 2018

Fighting Custom Actions in Rails Controllers




I have never observed a Rails venture which doesn't have custom activities in the Rails controllers. Furthermore, that makes me irritated. Here I will depict my considerations about custom activities, why individuals utilize them, why I abhor them, and how to manage them. Read More Info On Ruby On Rails Online Training

Suppose we need to manufacture a straightforward posting framework. We have just made a model Post and need to include a controller. Everyone thinks about REST and we clearly need to fabricate a RESTful application. So we make a controller: 

class PostsController < ApplicationController 

def record 

... 

end 

def appear 

... 

end 

... 

def wreck 

.. 

end 

end 

Additionally, include assets: presents on routes.RB 

We can make it much less demanding and quicker utilizing the Rails generators: rails g framework Post title and string description: text. 

Accordingly, we have a RESTful controller with seven basic activities. Presently clients can include another post, survey existing posts, and so forth. Later on, we need clients to have the capacity to like posts. We locate an extraordinary pearl called acts_as_votable, take after the directions, and add the jewel to the venture. Presently we figure: "alright, there is as of now a PostsController and the client can like a Post. It's simply one more activity that the client can perform on a Post so it bodes well to keep all activities identified with the Post inside PostsController," and include a custom activity. Learn More Info On Ruby On Rails Online Course

class PostsController < ApplicationController 

... 

def like 

@post = Post.find(params[:id]) 

@post.liked_by current_user 

end 

end 

What's more, include assets: posts do part do post: like ended to routes.RB 

Awesome, it's a decent, small activity and now the client can like posts. Cool? NO! 

We do like a post which appears as though it's playing out an activity on a post yet actually, we make another asset - like. It's not so evident at the present time but rather later on when we'll have to include different activities (e.g. despise, get all preferences, and so forth.) it will turn out to be significantly less demanding to see. What's more, we'll see that those activities are RESTful activities with the like asset. So the correct methodology makes a different RESTful controller - like controller. It makes, significantly more, sense to make it a settled asset of a post. Get In More Info On Ruby On Rails Online Course Bangalore

class LikesController < ApplicationController 

... 

def make 

@post = Post.find(params[:id]) 

@post.liked_by current_user 

end 

def file 

@likes = @post.get_likes 

end 

... 

private 

def find_post 

@post = Post.find(params[:post_id]) 

end 

end 

In routes.rb, include: assets :posts do assets :likesend 

Therefore, we have two assets, two RESTful controllers, and no custom activities. I found that this error is typically made by new RoR designers and this one is a long way from the main case. How about we investigate another precedent. 

Suppose we are building an API for an occasions administration framework. We have a controller that is particularly similar to a PostsController. 

class EventsController < ApplicationController 

def record 

... 

end 

def appear 

... 

end 

... 

def wreck 

... 

end 

end 

Additionally, include assets: occasions to routes.rb. What's more, we utilize ActiveModelSerializers to create JSON objects. 

The API chips away at the benefit of a client and now we have to create JSON for occasions that have a place just with a client. This time, we think: "Alright, last time we had a very surprising asset 'like,' yet now we are working with correct occasions, so it unquestionably should be in EventsController." 

class EventsController < ApplicationController 

... 

def my_events 

json = ActiveModel::ArraySerializer.new( current_user.events, 

each_serializer: MyEventSerializer, root: nil) 

render json: json, status: :alright 

end 

end 

class MyEventSerializer < ActiveModel::Serializer 

traits :id, :title, :descriptionend In routes.rb 

assets: occasions do 

gathering do 

get :my_events 

end 

end 

Cool? NO! 

On the off chance that "my occasions" JSON is only the same as "all occasions" JSON at that point it merits considering a type of sifting capacity to the events_controller#index activity. For our situation, we included a different serializer which implies that "my occasions" JSON is not quite the same as "all occasions" JSON. I can wager that later MyEventsSerializer will change so regularly that "my occasion" question will be altogether different from "occasion" protest, however, they are both put away in one DB table. Later we'll additionally need to erase possess occasions, and so forth. This frequently happens in light of the fact that a client has more consents with his own particular occasions. It's not so clear at first, but rather occasion and my_event are separate assets. Regardless of whether they both utilize a similar model we have no clue how they are utilized by portable applications or web applications. It's presumable that portable applications have distinctive classes to wrap our JSON, and it doesn't make a difference on the off chance that they utilize legacy since they are cases of various classes. So it bodes well to just make a different controller in our Rails API and concentrate all information identified with "my occasion" from that controller. Learn More Info On Ruby On Rails Online Training Course

class MyEventsController < ApplicationController 

def record 

json = ActiveModel::ArraySerializer.new( current_user.events, 

each_serializer: MyEventSerializer, root: nil) 

render json: json, status: :alright 

end 

def appear 

render json: MyEventSerializer.new(@event), status: :alright 

end 

... 

end 

Our routes.rb: 

assets :eventsresources :my_events, just: [:index, :obliterate, :show] 

There are numerous situations when designers add custom activities to Rails controllers, and as their controllers wind up bigger, their application develops. A ton of stuff starts to occur in a controller and you think "for what reason is it here? For what reason do we make books in the AuthenticationController?" I made a straightforward govern for myself: there shouldn't be custom activities in the RESTful application. Any custom activity in one controller is a RESTful activity in another. 

When you are adding the eighth activity to your controller ponder internally, "perhaps it bodes well just to extricate it to a different controller?" Don't be sluggish and make extra controllers! Try not to over-burden controllers! Each controller ought to oversee just its own particular asset! Read More Info On Ruby On Rails Online Training Bangalore

No comments:

Post a Comment