I'm currently trying to figure out how to extend the default ActiveRecord generator to use a different file path other than app/models and db/migrate. I've found the sources for the generators for models and for migrations, but I'm unsure how to extend or override them to create my own generators that can use almost all of the ActiveRecord ones' functionality.
Ideally, the generators would act exactly the same as the current ActiveRecord generators, with only the generated file path changed. And it would be nice if I didn't end up duplicating the code from the ActiveRecord source. Has anyone created a custom generator like this? Are there any examples I could look at? Thanks!
I'm sure you anticipated this response, but this seems like a very bad idea indeed! Secretly overwriting basic Rails functionality can cause all sorts of problems if someone else comes along and the standard Rails stuff doesn't work as they expected. If it's important that you are able to use this generator then you could write a new rake task, separate from the Rails one.
Related
I am building a rails app without a database.
In Disable ActiveRecord for Rails 4 I learned how to configure the app so that the absence of the database related gems does not interfere with running it. The problem is that I still want to create models using the commend rails generate mode MyModel.
Under this configuration, the above command does nothing at all.
I am assuming here I would need to require some modules (for example, activemodel, which seems to provide ActiveRecord-like capabilities without necessarily having a DB) in application.rb, but I can seem to find which.
Could someone help?
Thanks in advance
You can take a look at How to create custom generators for my rails app.
Basically you will have to change the behavior of your model generators. This way you can tell which file will be created, with which code template and etc.
This is my first on StackOverflow and I'm a new rails developer.
I'm using RoR to create an inventory application for Magic: The Gathering cards. I've found a Json API that I'd like to use to pull data on all of the cards, sets, etc into a local database.
My initial inclination is to create a helper class to manage all of this (which can also be called in seeds.rb during db:setup), but I have no idea where I should put this class in my project's directory structure. It's not really a model/controller/view, so I feel it should be kept separate from those parts of the app.
Further more, I'm having trouble testing any class I do make. I initially created a directory app/classes and put the class there. Then in my spec directory, I created spec/classes and created the spec file. Accessing my helper class from the spec did not work in the same way that accessing my models in their spec classes did.
I'm at a loss as to how to do this and quite a bit of googling and searching on here has just left me more confused. I'd love any help that can be offered. How would you do this?
You don't mention specifically what issues you're encountering, but for starters app/classes isn't in rails's autoload path - rails' require magic doesn't know to look in there to find these classes (as an aside, 'classes' sounds like a slightly meaningless name - models & controllers are classes too).
You can add to the paths rails searches (see config.autoload_paths in config/application.rb) but I would put this in lib (and the corresponding specs in spec/lib).
It would also work just fine with these classes in app/models, whether or not it does there is down to choice. There's nothing that says that files in there have to be active record subclasses - the decision of whether or not this functionality belongs in there boils down to whether it works/feels like a model to you.
I'm rewriting one old app - Rails 1.2.6 :)) - completely in Rails 4... so you can imagine the information overload.
It's going quite well so far but I'm currently struggling with one task that should be pretty obvious but it lacks proper documentation and there are just too many blogs with different solutions to this issue.
I have a custom class with custom text conversion functionality (using Redcloth, autolinker, Sanitize etc.), let's call it Textilize class. It's used in models as well as controllers so I guess the best solution would be to create a gem from it. I want to attack gem creation later though since it's just a simple one-file class.
So for now I just added textilize.rb file to /lib directory and added config.autoload_paths += %W(#{config.root}/lib).
It works fine and I can now use it in the app without requiring it in the models and controllers.
Is this a good practice in Rails 4? Is it thread-safe?
If not, is there a way to refactor it without creating a complete gem for now?
Thanks!
"Is this a good practice?" I think it is.
"Is it thread-safe?" I don't know
Any other way? I will use your solution if the lib is crossing Model and Controller and it is simple. If it get rather complex, I will create a plugin. If it is complex and can be extended to be useful on other apps, I will create a gem.
I'm new to Ruby and I come from a C# background so apologies if this is very basic. I'm navigating around a large Rails project and looking at the model (ActiveRecord) classes. I want to be able to easily see all of the fields of a class so that I can see whether a particular class has the fields that I need. I can't find any way of doing this in the RubyMine IDE. Is there any easy way or am I misunderstanding the way that dynamic languages work? The only way that I have found so far is by looking at the underlying database tables.
TIA
You can look at schema.rb under the db folder.
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/