Implementing a uml class diagram on rails - ruby-on-rails

I'm new on rails and I want to implement this diagram in Rails. The problem is with the heritage between classes, I don't know how to do that with models/controllers. Anyone have any idea?

Rails let your share features pretty easily among your models via 2 main pattern :
STI (single table inheritence) : basically just add a type string column and inherit your model class from its parent instead of inheriting from ActiveRecord::Base
Concern : Share some features grouped into a module you defined and include it into your target model or controller's classes
On your controller side you can directly build your own inheritence chain (making sure the first ancestor is your ApplicationController), use the concern pattern or other types of goodies such as Service Objects (and much much much more;-) )

Related

Inheritance services Ruby on Rails

So I'm learning RoR and I have to 3 services that calls an API with the same structure and i want to know if i can do it with a parent class and then work with the parent class to save code.
Thanks!
Yes. This may work if you can define a method with fewer arguments, which builds that structure for the API call.
Approaches are:
Put that common method in a base class which the other classes inherit from.
Put that common method in a module as a mix in.
Write a class to handle the call to the API, which builds the structure.
I don't think you have an "isa" relationship from the sound of it. So unless you do, 2 is preferred to 1. You can only inherit from one class, so mixins are more flexible.
Approach 3 is a good idea. You can have internal methods for the hostname and other constants for your API call.
This can be combined with the other approaches as you can use the Aggregation pattern to aggregate the API object in the other classes. That might or might not make sense. It might be just as well as the other classes have methods which instantiate the class in approach 3 and call it.

How to extend Rails models?

Say, I have a Rails model with a method:
class Order < ApplicationRecord
def process
do_some
do_some_more
do_even_more_here
end
end
Its purpose is solely Rails-model based, meaning its actions are performed on the object itself. What would be the best design pattern to adapt if I'd want to refactor this method and move it out somewhere else?
What I've found so far is that a Decorator shouldn't be the answer as they are more view related. ServiceObject is meant to only perform calculations without the ActiveRecord object and Concerns are mixins that divide responsibilities over several (ActiveRecord) objects.
What I've found so far is that a Decorator shouldn't be the answer as
they are more view related.
Yes, the decorator pattern is most often used in Rails apps to decorate models with view specific behavior. That does not necessarily mean that you can't use the pattern for other purposes.
Rather what you should consider is that models even with no code instead are already fat objects due to the insane amount of features that they get from ActiveModel and ActiveRecord:
validations
persistence
dirty tracking
type casting
querying
etc
A decorator may just add more fat even through the code is neatly tucked away in a separate class.
ServiceObject is meant to only perform calculations without the
ActiveRecord object
Stop listening to whoever told you this. The ServiceObject pattern is really just about creating single purpose objects that do one single job and do it well.
ActiveJob is an example of the ServiceObject pattern. And yes it even has an API for passing in ActiveRecord objects. What you are describing sound like a prime candidate for a ServiceObject or ActiveJob.
Concerns are mixins that divide responsibilities over several
(ActiveRecord) objects.
Mixins don't divide responsibilities. They share behaviors between classes. Its basically like copy-pasting the same methods between a set of classes.
Service object is the one you are looking for.
Mixins and concerns are just a bunches of code you want to mix and share, they can be anywhere.
Make a service object, design it’s public and private interfaces. If you are thinking it’s coming too fat just separate them, easy as that. It might worth reading about SOLID.

How to use two concerns with the same name?

I want to use concerns app/controllers/concerns/likeable.rb and app/models/concerns/likeable.rb.
The first one goes to controllers and the second one goes to models.
If I create two files, only the first one is loaded.
What's the best way to solve this problem?
I didn't find a way to use controller and model concerns with the same name without any namespaces.
So, my final solution is to use LikeableModel and LikeableController concerns.
I had the same problem but I was able to resolve it by namespacing the controller and model concerns. I moved all model concerns to app/models/concerns/models/ and all controller concerns to app/controllers/concerns/controllers/. With this, it's possible to have model and controller concerns with the same name.
app/models/concerns/models/likeable.rb
module Models::Likeable
end
app/controllers/concerns/controllers/likeable.rb
module Controllers::Likeable
end
The concerns can be included like this;
class Post < ActiveRecord::Base
include Models::Likeable
end
class PostsController < ApplicationController
include Controllers::Likeable
end
Older question, but I thought I'd throw in another option that I believe fits in with Rails conventions a little neater. The Likeable namespace is responsible for dealing with any items that are likeable, and there are controllers that need to deal with building responses for likeable resources, and models for those likeable resources. Both of these can implement aspects of the Likeable concept. What is needed is a breakdown within that namespace of the different responsibilities.
What I would do in this case is create a file in app/controllers/concerns/likeable/respondable.rb that implements the Likeable::Respondable functionality that a Controller provides. (You may find a better name than Respondable for your needs - for instance, if the controller concern only really handles some logic around params, you may call it Likeable::Paramable, etc.)
Similarly, if the model side of your Likeable scaffold has to do mainly with persistence logic, you might define a Likeable::Persistable module in app/models/concerns/likeable/persistable.rb.
In this way, you can still keep all your logic for likeables in a single namespace, and get more specific for your controller and model concerns.
What's nice about this approach is that you can easily add to the namespace later if you find a need for, say, a utility module or class that needs to live in the lib directory for dealing with special calculations or other shared functionality. In that case, you could easily define a Likeable::Utils module in lib/likeable/utils.rb, or something similar depending on the need, and everything will live under that one, consistent namespace.
You can disttingush two concerns with same name using namespace
Following is example
app/controllers/concerns/like/likeable.rb
module Like
class Likeable
# do some stuff here
end
end
app/models/concerns/likeable.rb
class Likeable
# do some stuff here
end

Grails: What is the difference between extending and embedding a domain class?

I'm very new to the Grails framework, so please bear with me.
Nonetheless, I am a bit confused on the functionality difference between extending a domain class and embedding objects.
From a database point of view, they both do the same thing. When embedding an object, all the properties of all the classes are stored in one table. Similarily, when extending a class (using table-per-hierarchy), all the properties of all the classes are stored in one table.
I'm sure there is a functionality difference between these two, and so I figured I ask this question.
When do you use either one?
The only technical difference is the ability to have multiple tables through the table per subclass property when extending a class. Otherwise, they are identical in use.
However, that said, by extending another class you are also modeling that within the class structure so you can make use of instanceof and polymorphic features of Java/Groovy.

Where should I put functions that are accessed by models? -- Rails 3.1

I've been told that the helpers are just for functions that are needed by the views.
Where should I put in functions that are used commonly by models? What about controllers?
What's the convention to place commonly used functions that will be used in:
1) models
2) views
3) controllers
Problem: Creating a module in lib to hold the functions and including the module in a class would create a boat-load of instance methods for the class.
Problem: What about functions that are common and needed in all three?
Problem: Creating a module in lib to hold the functions and including the module in a class would create a boat-load of instance methods for the class.
First organize, then optimize
Problem: What about functions that are common and needed in all three?
Do you really have methods that are needed in all the three and not exist yet ?
If yes, may be you can give an exemple
I think the question should be where to put logic in general.
You should first think what your method does before to think about where to put it.
But whatever you create, when it's getting big and or valuable, you should really think about exporting it as a gem/plugin.
Inner navigation logic (what to display and where to go after an action) : Controllers
App navigation logic; application_controller
Sub set of app logic; create a namespace with master controller, class API_controller < application_controller
Data logic (How to manipulate, process data) : Models
Data; Model class method (search, sorting, counting, macro process ...)
Datum; Model instance method (modification, micro process ...)
Data presentation logic (How to display data) : Helper, Partial and Decorators
Helper are not designed for that in my opinion.
Partial handle layouting of specific data.
application decorator; handle generic data presentation help
scope_decoration; you can use inheritance
Layout language logic (layout language help) : Helper
Specific to your app; application_helper
Specific to a model ...; model_helper, but you should consider decorator
Generic; export it in a gem (super form helper, templating system ...)
Layout logic (should i display this menu ?) : ?
Helper/decorator/model can should answer the question : #user.can_edit?(#article)
Layout handle the display <%= render :partial => allowed ? "something" : "somthing else" %>
I think if you are not in this configuration you are creating kind of backend system.
So it should go in lib, then in a gem later.
This organization is just an exemple. The most important thing is to organize your code and split different logic layers and don't hesitate to refactor/export code to make it generic after adding new features...
for Controllers - put common methods in application_controller.rb
for Views - put common methods in application_helper.rb
for Models - monkeypatch ActiveRecord::Base to include common methods OR write a module with common model methods and include it in the models that need it OR do it in OOP way by subclassing ActiveRecord::Base with your abstract class, then inheriting all your models from this class.
To use common methods in both Model and Controller, do one of the following:
Write a plain ruby class, put it in /lib or elsewhere, just make sure it's loaded, then require it when you need to use its methods.
Extract common functionality to a gem, install it, require it when you need it. Publish it to rubygems if it's something valuable.
... Usually, I put those kind of functions into common superclasses: For models, that could be (for example) Animal for subclasses Dog, Cat, etc. Within the Animal model, you would have to
self.abstract_class = true
so it doesn't expect a table for that class. For controllers, you could either use ApplicationController or you could make your controllers be derived by another common subclass.
In the Model you should store all the methods that have a relation to the model itself like manipulating attributes, scopes, associating,...
In the View you dont store any logic! The logic belongs to the model. In the view you only put code that helps you to display stuff.
The Controller is the "bridge" between both. You select data in the controller, call methods that are stored in the model,... A common failure is to store the logic in the controller which should be stored in the model.
When you store a method in your Modelyou can access it from the model, the view and the controller! If you have a method that doesn't have a relation to a specific model or its needed in several models you can use the Helper. An example for such a case might be a method that rewrites your url using a pattern. This might be needed in 20 models to prepare a string for to_param. That method would be stored in an Helper that could be included in the Models its needed.

Resources