Is there any way to put a mixin on another mixin like this? I want to share some common methods among a few mixins.
mixin CircleMixin with SquareMixin {}
No, it's currently not possible to compose mixins. See this issue that's tracking the request for this feature.
Related
Is there any way to find out if a module present on https://apidock.com/ is part of the public API? Some of them, like ActionDispatch::Http::URL, have no description but many of their methods do. Is there a rule or something?
Just now I started to using Concerns in rails, but i have doubt why we go for concerns, because we can achieve same thing on module & mixing concept. So please any one tell about shat is the use of concerns instead of using module.
It's well described here: http://api.rubyonrails.org/classes/ActiveSupport/Concern.html
In short:
Concerns allow you to use #included and #class_methods instead of self.included hook with additional module ClassMethods creation;
Concerns give you a better dependency resolution for modules included in each other;
ActiveSupport::Concern adds some convenient features (i.e class_methods) to your module. You can use "pure" ruby modules without extending it. Essentially you create a module which you mix-in to a class. Doesn't matter if this module extends AS::Concern, the mechanism is the same.
when you write in concern that mean you are making one module. My opinion is concern and module be similar together. Concern can appear somewhere as model, controller and at here you can write module for yourself. And with general module is write in lib folder. Both can be used by way include or extend into a class.
I am using RSpec/Capybara.
I am writing shared examples in the same spec file. I want to place the shared examples after the feature as the shares examples are really long.
But if I place the shared examples below the feature, RSpec is complaining that it is not able to find the shared example. Is there any way to make it happen?
This question is a year old, but wanted to mention that rspecs shared examples are an alternative to using a normal module. Basically you define module ('concern') that can be included in any spec file using it_behaves_like "my_concern"
See this question for a good example of this in action.
a common practice is to define the code for your shared examples in a module and include it via the configuration. that way, you have a clear code separation and reuse.
in spec/support/xyz.rb
module SomeSharedExamples
[...]
end
in your spec_helper.rb
config.include SomeSharedExamples
I am using Ruby on Rails 3.0.7 and I have multiple resources that almost have the same behavior. That is, those almost have same model, controller and view codes and same database table columns definition.
So I would like to find a way to DRY those resources. I already implemented modules and mixins for those in order to share part of the code (as validation methods, callbacks, view files but not controller files that, anyway, have very similar code).
Now, how can I do to handle this common behavior? Should I use something that Ruby on Rails developers named as acts_as_something? What do you advice about?
I think you already did that, just name a method in your modules act_as_your_module_name and make sure your module extends from your Base Class, e.g. ActiveRecord::Base.extend act_as_your_module_name
http://www.cowboycoded.com/tag/acts_as/
I am new to Ruby on Rails and my questions are about the application design, based on Rails 3. There are many data on the internet on the creation of standard websites (such as blogs) on Rails, but my application requires more than just "post and read" and I am not sure how to implement that.
The idea:
The model "Route" includes a number of airlines modules: "Ryanair", "easyJet", etc.
The "Route.Update" method calls the "UpdateRoutes" on each airline module (for example, "Ryanair.UpdateRoutes", "easyJet.UpdateRoutes")
It should work the same way with more models (such as "Flight.find") and more airlines ("Delta.FindFlights")
The questions:
Where should I store all the modules? I don't see any app/modules folder in Rails.
If my modules require gems, should I include them in the modules or in the models (where they are actually used)?
I want to make my application scalable. For example, I want to add a new working airline (module) without changing any code in "Route", "Flight" or any other model. I imagine something like the method "IncludeAirlines" which would go through modules/airlines/name.rb, include every module and call the needed method of it (such as name.UpdateRoutes). Is there any better way to implement that in Ruby on Rails?
As you might know, modules are generally used either as namespaces or as mixins.
Where you place a module depends on how tightly coupled a module is with the app directory . A few patterns in storing modules :
The /lib directory, if the module does not particularly 'interact' or concern the app/ and you treat the module as an internal plug-in.
The app/models directory, would be an appropriate place if your module is central to your business logic. A popular use case here, is where you use a module as a mixin to DRY your models/controllers.
37 Signals introduced a pattern of treating them as 'concerns' and storing them in app/concerns.
If your module uses a gem, you may need to require the gem in the module (sometimes a require is not at all necessary).
Your 3rd question is not clear. Sorry about that. Not quite sure what you're trying to do.