How to use two concerns with the same name?
Asked Answered
M

4

7

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?

Midmost answered 27/2, 2017 at 7:52 Comment(0)
M
4

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.

Midmost answered 12/3, 2017 at 22:27 Comment(1)
Huh, going through the same issue, seems your solution is the only one I've found so far.Glint
B
3

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

Bridgettebridgewater answered 14/8, 2019 at 14:48 Comment(0)
T
1

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.

Takishatakken answered 23/9, 2020 at 13:3 Comment(0)
W
0

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
Whitver answered 27/2, 2017 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.